-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogress_bar.nu
More file actions
46 lines (41 loc) · 1.56 KB
/
progress_bar.nu
File metadata and controls
46 lines (41 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/env nu
# progress_bar.nu
use std repeat
export def print-bar [on, off, max, count, show_value, max_disp_value, min_disp_value, percent] {
let $append_value = if $show_value {
let max_value = if ($max_disp_value | is-not-empty) { $max_disp_value } else { $max }
let min_value = if ($min_disp_value | is-not-empty) { $min_disp_value } else { 0 }
if $percent {
$" \((($count / $max | math round -p 2) * 100)%)"
} else {
$" \((($max_value - $min_value) * ($count / ($max - $min_value)) + $min_value | math round -p 3))"
}
} else {
""
}
print -n $"[($on | repeat $count | append ($off | repeat ($max - $count)) | str join)]($append_value)\r"
}
# Progress bar
export def main [
--start (-s): int = 0 # Starting value
--max (-m): int = 10 # Maximum width in chars
--inc (-i): int = 1 # Increment
--show-value (-S) # Show value to side of bar
--max-disp-value (-M): int # Max value to display
--min-disp-value (-m): int # Min value to display
--percent (-p) # Show value as percent
] {
mut count = $start
let min = 0
let on = '*'
let off = '-'
print-bar $on $off $max $count $show_value $max_disp_value $min_disp_value $percent
mut inp = (input listen --types [key])
while $inp.code not-in ['esc', 'enter'] {
if $inp.code == 'right' { $count = ([($count + $inc), $max] | math min) }
if $inp.code == 'left' { $count = ([($count - $inc), $min] | math max) }
print-bar $on $off $max $count $show_value $max_disp_value $min_disp_value $percent
$inp = (input listen --types [key])
}
($count / $max)
}