Skip to content

Commit

Permalink
clean printing
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed May 7, 2020
1 parent 4f43f5b commit 82ae2df
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func download(r *dbImage) error {

bar := progressbar.DefaultBytes(
resp.ContentLength,
fmt.Sprintf("Downloading: %s ", r.URL),
"Downloading",
)
if _, err = io.Copy(io.MultiWriter(tmpFile, bar), resp.Body); err != nil {
return err
Expand Down
21 changes: 14 additions & 7 deletions progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import (

type StopFunc func()

func Show(title string) StopFunc {
func Immediate(msg ...string) {
var msgs string
for _, m := range msg {
msgs += m + " "
}
fmt.Printf("\r✔ %s\n", msgs)
}

func Show(msg string) StopFunc {
quit := make(chan bool, 1)
i, appendln := 0, false
i, isWritten := 0, false

bar := progressbar.NewOptions(-1,
progressbar.OptionSetDescription(title),
progressbar.OptionSetDescription(msg),
progressbar.OptionSpinnerType(11),
)

Expand All @@ -27,7 +35,7 @@ func Show(title string) StopFunc {
if i == 0 {
time.Sleep(1 * time.Second)
} else {
appendln = true
isWritten = true
_ = bar.Add(1)
time.Sleep(100 * time.Millisecond)
}
Expand All @@ -38,9 +46,8 @@ func Show(title string) StopFunc {

return func() {
quit <- true
if appendln {
fmt.Printf("\r✔ %s", title)
fmt.Println()
if isWritten {
fmt.Printf("\r✔ %s\n", msg)
}
}
}
5 changes: 3 additions & 2 deletions vms/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/mhewedy/vermin/command/ssh"
"github.com/mhewedy/vermin/db"
"github.com/mhewedy/vermin/images"
"github.com/mhewedy/vermin/progress"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -65,7 +66,7 @@ func Create(imageName string, script string, cpus int, mem int) (string, error)
}

func setNetworkAdapter(vmName string) error {
fmt.Println("Setting bridged network adapter")
progress.Immediate("Setting bridged network adapter")
r, err := command.VBoxManage("list", "bridgedifs").Call()
if err != nil {
return err
Expand Down Expand Up @@ -110,7 +111,7 @@ func provision(vmName string, script string) error {
}

func start(vmName string) error {
fmt.Println("Starting", vmName)
progress.Immediate("Starting", vmName)
if _, err := command.VBoxManage("startvm", vmName, "--type", "headless").Call(); err != nil {
return err
}
Expand Down

0 comments on commit 82ae2df

Please sign in to comment.