send2gokapi/display.go

65 lines
1.8 KiB
Go
Raw Permalink Normal View History

2025-01-04 03:21:10 +00:00
package main
import (
"flag"
"fmt"
"github.com/schollz/progressbar/v3"
"os"
"path/filepath"
"strings"
)
var (
ansiMoveUp = []byte{keyEscape, '[', '1', 'A'}
ansiClearLine = []byte{keyEscape, '[', '2', 'K'}
ansiLineFeed = []byte{keyEscape, '[', '1', 'G'}
)
func envVarUsage() {
fmt.Fprintf(flag.CommandLine.Output(), " %-20s Gokapi URL (e.g. https://gokapi.example.com/)\n", envVarURL)
fmt.Fprintf(flag.CommandLine.Output(), " %-20s Gokapi API key\n", envVarAPIKey)
}
func init() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [FLAGS] FILES...\n", os.Args[0])
flag.PrintDefaults()
fmt.Fprintf(flag.CommandLine.Output(), "Environment variables:\n")
envVarUsage()
}
}
func progressBarReport(filename string, maxFileLen int) func(cr ChunkReport) {
var pr *progressbar.ProgressBar
displayedFileName := formatFilename(filename, maxFileLen)
return func(cr ChunkReport) {
if cr.UploadedChunks == 0 && pr == nil {
pr = progressbar.DefaultBytes(cr.TotalSize, displayedFileName)
}
pr.Set(int(cr.UploadedBytes))
}
}
func displayCompletion(filename string, maxFileLen int, ur UploadResponse) {
os.Stdout.Write(ansiMoveUp)
os.Stdout.Write(ansiClearLine)
os.Stdout.Write(ansiLineFeed)
displayedFileName := formatFilename(filename, maxFileLen)
fmt.Printf("%v: done - %v\n", displayedFileName, ur.FileInfo.UrlHotlink)
}
func displayFailedCompletion(filename string, maxFileLen int, err error) {
os.Stdout.Write(ansiClearLine)
os.Stdout.Write(ansiLineFeed)
displayedFileName := formatFilename(filename, maxFileLen)
fmt.Printf("%v: error - %v\n", displayedFileName, err)
}
func formatFilename(filename string, maxFileLen int) string {
baseFileName := filepath.Base(filename)
return strings.Repeat(" ", maxFileLen-len(baseFileName)) + baseFileName
}