Created
May 29, 2019 17:18
-
-
Save parachvte/d9b126def8f91987212d79a879f0d3b9 to your computer and use it in GitHub Desktop.
Simple progress bar generation
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
| package main | |
| import ( | |
| "fmt" | |
| "math" | |
| "strings" | |
| ) | |
| const total_length int = 80 | |
| func min(a, b int) int { | |
| if a < b { | |
| return a | |
| } else { | |
| return b | |
| } | |
| } | |
| func get_progress_string(percentage int) string { | |
| progress_size := total_length - 2 | |
| progress_len := int(math.Round(float64(percentage) / 100.0 * float64(progress_size))) | |
| progress_len = min(progress_len, progress_size-5) | |
| progress := strings.Repeat("=", progress_len) + strings.Repeat(" ", progress_size-5-progress_len) | |
| return fmt.Sprintf("[%s %3d%%]", progress, percentage) | |
| } | |
| func main() { | |
| fmt.Println(get_progress_string(0)) | |
| fmt.Println(get_progress_string(5)) | |
| fmt.Println(get_progress_string(20)) | |
| fmt.Println(get_progress_string(100)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment