2023-05-12 17:11:20 +03:00
|
|
|
# construct bars based of a given percentage from a given width (5 is default)
|
|
|
|
# > bar 0.2
|
|
|
|
# █
|
2023-09-01 15:18:21 +03:00
|
|
|
# > bar 0.71
|
|
|
|
# ███▌
|
2023-05-12 17:11:20 +03:00
|
|
|
def 'bar' [
|
|
|
|
percentage: float
|
|
|
|
--background (-b): string = 'default'
|
|
|
|
--foreground (-f): string = 'default'
|
2023-09-01 15:18:21 +03:00
|
|
|
--progress (-p) # output the result using 'print -n'
|
2023-05-12 17:11:20 +03:00
|
|
|
--width (-w): int = 5
|
|
|
|
] {
|
|
|
|
let blocks = [null "▏" "▎" "▍" "▌" "▋" "▊" "▉" "█"]
|
|
|
|
let $whole_part = (($blocks | last) * ($percentage * $width // 1))
|
|
|
|
let $fraction = (
|
2023-09-01 15:18:21 +03:00
|
|
|
$blocks
|
2023-05-12 17:11:20 +03:00
|
|
|
| get (
|
2023-09-01 15:18:21 +03:00
|
|
|
($percentage * $width) mod 1
|
|
|
|
| $in * ($blocks | length | $in - 1)
|
|
|
|
| math round
|
2023-05-12 17:11:20 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-09-01 15:18:21 +03:00
|
|
|
let result = (
|
|
|
|
$"($whole_part)($fraction)"
|
|
|
|
| fill -c $' ' -w $width
|
|
|
|
| if ($foreground == 'default') and ($background == 'default') {} else {
|
|
|
|
$"(ansi -e {fg: ($foreground), bg: ($background)})($in)(ansi reset)"
|
|
|
|
}
|
2023-05-12 17:11:20 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
if $progress {
|
|
|
|
print -n $"($result)\r"
|
|
|
|
} else {
|
|
|
|
$result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 15:18:21 +03:00
|
|
|
use std assert equal
|
2023-05-12 17:11:20 +03:00
|
|
|
|
2023-09-01 15:18:21 +03:00
|
|
|
#[test]
|
2023-05-12 17:11:20 +03:00
|
|
|
def bar_tests [] {
|
2023-09-01 15:18:21 +03:00
|
|
|
equal "█▌ " (bar 0.3)
|
|
|
|
equal "███ " (bar 0.3 --width 10)
|
|
|
|
equal "▊" (bar 0.71 --width 1)
|
|
|
|
equal "███████▏ " (bar 0.71 --width 10)
|
|
|
|
}
|