go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/cli/components/hbar.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package components 5 6 import ( 7 "math" 8 "strings" 9 ) 10 11 // Hbar creates a horizontal bar up to len characters with a 12 // length proportional to percent, in range of [0, 100] 13 func Hbar(len uint32, percent float32) string { 14 if percent > 100 { 15 percent = 100 16 } 17 if percent < 0 { 18 percent = 0 19 } 20 segments := math.Round(float64(len) * (float64(percent) / 100)) 21 22 if segments == 0 { 23 return "" 24 } 25 return strings.Repeat("█", int(segments)) 26 }