1
0
mirror of https://github.com/byReqz/go-etcher.git synced 2024-11-25 08:14:54 +00:00

add confirmation dialog and made progressbar prettier

This commit is contained in:
Nils 2022-01-13 00:09:48 +01:00
parent 8390130252
commit 8454440711
Signed by: byreqz
GPG Key ID: 396A62D7D436749E

33
main.go
View File

@ -60,10 +60,19 @@ func GetDest() string {
} }
func WriteImage(image *os.File, target *os.File, size int64) (int64, error) { func WriteImage(image *os.File, target *os.File, size int64) (int64, error) {
bar := progressbar.DefaultBytes( bar := progressbar.NewOptions(int(size),
size, progressbar.OptionSetWriter(os.Stderr),
"Writing image", // progressbar.OptionEnableColorCodes(true),
) progressbar.OptionShowBytes(true),
progressbar.OptionSetWidth(50),
progressbar.OptionSetDescription("Writing image file..."),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "=",
SaucerHead: ">",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}))
writer := io.MultiWriter(target, bar) writer := io.MultiWriter(target, bar)
written, err := io.Copy(writer, image) written, err := io.Copy(writer, image)
if err != nil { if err != nil {
@ -147,14 +156,22 @@ func main() {
fmt.Println("\r[", color.GreenString("✓"), "] Opening files ") fmt.Println("\r[", color.GreenString("✓"), "] Opening files ")
} }
fmt.Println("[", color.BlueString("i"), "] Input device/file:", input)
fmt.Println("[", color.BlueString("i"), "] Output device/file:", device)
fmt.Print(color.HiWhiteString("Do you want to continue? [y/N]: "))
var yesno string
_, _ = fmt.Scanln(&yesno)
yesno = strings.TrimSpace(yesno)
if ! (yesno == "y" || yesno == "Y") {
log.Fatal("aborted")
}
written, err := WriteImage(image, target, stat.Size()) written, err := WriteImage(image, target, stat.Size())
if err != nil { if err != nil {
s.Stop() fmt.Println("\r[", color.RedString("✘"), "] Writing image,", written, "bytes written ")
fmt.Println("\r[", color.RedString("✘"), "] Writing image,", written, "bytes written")
log.Fatal(err) log.Fatal(err)
} else { } else {
s.Stop() fmt.Println("\r[", color.GreenString("✓"), "] Writing image,", written, "bytes written ")
fmt.Println("\r[", color.GreenString("✓"), "] Writing image,", written, "bytes written")
} }
s.Prefix = "[ " s.Prefix = "[ "