Skip to content

Instantly share code, notes, and snippets.

@hkparker
Created December 7, 2015 07:50
Show Gist options
  • Save hkparker/6c609b712eb220b1c733 to your computer and use it in GitHub Desktop.
Save hkparker/6c609b712eb220b1c733 to your computer and use it in GitHub Desktop.
progress output for cli go applications
func PrintProgress(completed_files, statuses, finished chan string) {
last_status := ""
last_len := 0
for {
select {
case completed_file := <-completed_files:
fmt.Printf("\r")
line := "completed: " + completed_file
fmt.Print(line)
print_len := len(line)
trail_len := last_len - print_len
if trail_len > 0 {
for i := 0; i < trail_len; i++ {
fmt.Print(" ")
}
}
fmt.Print("\n" + last_status)
case status := <-statuses:
last_status = status
fmt.Printf("\r")
fmt.Print(status)
print_len := len(status)
trail_len := last_len - print_len
if trail_len > 0 {
for i := 0; i < trail_len; i++ {
fmt.Print(" ")
}
}
last_len = print_len
case elapsed := <-finished:
fmt.Println("\n" + elapsed)
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment