mirror of
https://github.com/nushell/nu_scripts.git
synced 2024-09-11 21:57:33 +03:00
the function to display bars of given percentage from given width (#478)
* function to display bars of given percentage from given width * adding --progress, moving width to options thanks @amtoine * typos of ESL studnet * added examples and tests * feat(nano): add completions (#479) * feat(nano): add completions * feat(nano): add readme * removed escape symbols from tests, fixed fraction indexing * fixed tests Fixed tests, although they need escape sequences, as the bar function has the ability to change the colors of both the background and foreground and uses the default color by default. * Rename bar_fn.nu to bar.nu --------- Co-authored-by: Emily Grace Seville <EmilySeville7cfg@gmail.com>
This commit is contained in:
parent
58a05e91da
commit
b2fadc0ea8
10
custom-completions/nano/README.md
Normal file
10
custom-completions/nano/README.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# `nano` custom completions
|
||||||
|
|
||||||
|
This script provides custom completions for `nano`.
|
||||||
|
It can be used by importing its exported commands via:
|
||||||
|
|
||||||
|
```
|
||||||
|
use path/to/nano/nano-completions.nu *
|
||||||
|
```
|
||||||
|
|
||||||
|
With `path/to/` being either the relative path of the file to your current working directory or its absolute path.
|
61
sourced/progress_bar/bar.nu
Normal file
61
sourced/progress_bar/bar.nu
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
# construct bars based of a given percentage from a given width (5 is default)
|
||||||
|
# > bar 0.2
|
||||||
|
# █
|
||||||
|
# > bar 0.71 --width 10
|
||||||
|
# ███████
|
||||||
|
def 'bar' [
|
||||||
|
percentage: float
|
||||||
|
--background (-b): string = 'default'
|
||||||
|
--foreground (-f): string = 'default'
|
||||||
|
--progress (-p) # output the result using 'print -n' with '\r' at the end
|
||||||
|
--width (-w): int = 5
|
||||||
|
] {
|
||||||
|
let blocks = [null "▏" "▎" "▍" "▌" "▋" "▊" "▉" "█"]
|
||||||
|
let $whole_part = (($blocks | last) * ($percentage * $width // 1))
|
||||||
|
let $fraction = (
|
||||||
|
$blocks
|
||||||
|
| get (
|
||||||
|
($percentage * $width) mod 1
|
||||||
|
| $in * ($blocks | length)
|
||||||
|
| math floor
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
let $result = (
|
||||||
|
$"($whole_part)($fraction)"
|
||||||
|
| fill -c $' ' -w $width
|
||||||
|
| $"(ansi -e {fg: ($foreground), bg: ($background)})($in)(ansi reset)"
|
||||||
|
)
|
||||||
|
|
||||||
|
if $progress {
|
||||||
|
print -n $"($result)\r"
|
||||||
|
} else {
|
||||||
|
$result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def assert_eq [num: int, expected: string, input_1: float, input_2?] {
|
||||||
|
let actual = (
|
||||||
|
if ($input_2 == null) {bar $input_1} else {
|
||||||
|
bar $input_1 --width $input_2
|
||||||
|
}
|
||||||
|
)
|
||||||
|
let span = (metadata $expected).span;
|
||||||
|
if $actual != $expected {
|
||||||
|
error make {
|
||||||
|
msg: "Actual != Expected",
|
||||||
|
label: {
|
||||||
|
text: $"expected ($expected) but got ($actual)", start: $span.start, end: $span.end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
print $"Test ($num) (ansi green)passed(ansi reset) ✓"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def bar_tests [] {
|
||||||
|
assert_eq 1 "[49;39m▏ [0m" 0.03
|
||||||
|
assert_eq 2 "[49;39m▎ [0m" 0.03 10
|
||||||
|
assert_eq 3 "[49;39m▊[0m" 0.71 1
|
||||||
|
assert_eq 4 "[49;39m███████ [0m" 0.71 10
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user