mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-03 15:47:47 +00:00
refactor: ✨ (#418)
* refactor: ✨ move in one commit Eveything in modules should probably be changed to `exported` defs. The idea is to move everything first to keep proper history. * refactor: 📝 add modules readme (wip) * refactor: ✨ small move * refactor: 📝 changed nestring, updated modules readme * refactor: 📝 to document or not to document * fix: 🐛 themes replaced the template to use `main` and regenerated them from lemnos themes. * Revert "fix: 🐛 themes" This reverts commit 4918d3633c8d2d81950a0ed0cfd9eb84241bc886. * refactor: ✨ introduce sourced - Created a source `root` in which sourcable demos are stored. Some might get converted to modules later on. - Moved some files to bin too. * fix: 🐛 fehbg.nu * fix: 🐛 modules/after.nu * moved some other stuff around --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
parent
382696cd21
commit
c47ccd42b8
128 changed files with 185 additions and 12 deletions
68
modules/formats/to-number-format.nu
Normal file
68
modules/formats/to-number-format.nu
Normal file
|
@ -0,0 +1,68 @@
|
|||
export def number-format [
|
||||
num # Number to format
|
||||
--thousands_delim (-t) = ' ' # Thousands delimiter: number-format 1000 -t ': 1'000
|
||||
--whole_part_length (-w) = 0 # Length of padding whole-part digits: number-format 123 -w 6: 123
|
||||
--decimal_digits (-d) = 0 # Number of digits after decimal delimiter: number-format 1000.1234 -d 2: 1000.12
|
||||
--denom (-D) = "" # Denom `--denom "Wt": number-format 1000 --denom 'Wt': 1000Wt
|
||||
] {
|
||||
|
||||
let parts = (
|
||||
$num
|
||||
| into string
|
||||
| split row "."
|
||||
)
|
||||
|
||||
let whole_part = (
|
||||
$parts.0
|
||||
| split chars
|
||||
| reverse
|
||||
| reduce -n -f [] {
|
||||
|it, acc| if ((($it.index + 1) mod 3) == 0) {
|
||||
$acc.item
|
||||
| append $it.item
|
||||
| append $thousands_delim
|
||||
} else {
|
||||
$acc.item
|
||||
| append $it.item
|
||||
}
|
||||
}
|
||||
| reverse
|
||||
)
|
||||
|
||||
let whole_part2 = (
|
||||
if ($whole_part | first) == $thousands_delim {
|
||||
($whole_part | skip 1)
|
||||
} else {
|
||||
$whole_part
|
||||
}
|
||||
| str join ''
|
||||
)
|
||||
|
||||
let whole_part3 = (
|
||||
if $whole_part_length == 0 {
|
||||
$whole_part2
|
||||
} else {
|
||||
$whole_part2
|
||||
| fill -w $whole_part_length -c ' ' -a r
|
||||
}
|
||||
)
|
||||
|
||||
let dec_part = (
|
||||
if ($parts | length) == 1 {
|
||||
"0"
|
||||
} else {
|
||||
$parts.1
|
||||
}
|
||||
)
|
||||
|
||||
let dec_part2 = (
|
||||
if $decimal_digits == 0 {
|
||||
""
|
||||
} else {
|
||||
$".($dec_part)" | fill -w ($decimal_digits + 1) -c '0' -a l
|
||||
}
|
||||
)
|
||||
|
||||
let out = $"(ansi green)($whole_part3)($dec_part2)(ansi reset)(ansi green_bold)($denom)(ansi reset)"
|
||||
$out
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue