Created
December 7, 2015 07:50
-
-
Save hkparker/6c609b712eb220b1c733 to your computer and use it in GitHub Desktop.
progress output for cli go applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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