Skip to content

Commit

Permalink
use fill -c instead of str repeat (nushell#740)
Browse files Browse the repository at this point in the history
Sorry, I forgot to use `fill -c` and implemented `str repeat` with
similar function. Now change it back
Added a `log` helper function to avoid the frequent need to adjust the
output format
The original `lg` with a similar name was changed to `show`, but it is
basically only used for internal debugging.
I'm not sure if these should be merged, feel free to do whatever you
want

Co-authored-by: nash <[email protected]>
  • Loading branch information
fj0r and nashvi committed Jan 17, 2024
1 parent 551b181 commit 32d074e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
39 changes: 19 additions & 20 deletions modules/comma/comma.nu
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export def lg [tag?] {
export def show [tag?] {
let o = $in
let t = [
$'(ansi xterm_grey)--------(ansi xterm_olive)($tag)(ansi xterm_grey)--------'
Expand Down Expand Up @@ -35,16 +35,6 @@ module utils {
}


export def 'str repeat' [n] {
let o = $in
if $n < 1 { return '' }
mut r = ''
for i in 0..($n - 1) {
$r += $o
}
$r
}

export def distro [] {
match $nu.os-info.name {
'linux' => {
Expand Down Expand Up @@ -235,8 +225,10 @@ module run {
clear
}
do $act $argv $scope
if not $cl {
do $env.comma_index.settings.theme.watch_separator
}
sleep $w.interval
print -e $env.comma_index.settings.theme.watch_separator
}
} else {
if $cl {
Expand All @@ -257,7 +249,7 @@ module run {
new_path: $path
})
if not $cl {
print -e $env.comma_index.settings.theme.watch_separator
do $env.comma_index.settings.theme.watch_separator
}
}
}
Expand Down Expand Up @@ -366,16 +358,15 @@ module run {
}

export def --wrapped dry [...x --prefix=' ' --strip] {
use utils 'str repeat'
let w = term size | get columns
mut lines = []
for a in (unnest (if $strip { $x.0 } else { $x })) {
mut nl = ($prefix | str repeat $a.lv)
mut nl = ('' | fill -c $prefix -w $a.lv)
for t in $a.it {
let line = if ($nl | str replace -a ' ' '' | is-empty) { $"($nl)($t)" } else { $"($nl) ($t)" }
if ($line | str length) > $w {
$lines ++= $nl
$nl = $"($prefix | str repeat $a.lv)($t)"
$nl = $"('' | fill -c $prefix -w $a.lv)($t)"
} else {
$nl = $line
}
Expand Down Expand Up @@ -726,14 +717,14 @@ export-env {
| gendict 5 {
settings: {
test_group: {|x|
let indent = ' ' | str repeat $x.indent
let indent = '' | fill -c ' ' -w $x.indent
let s = $"(ansi bg_dark_gray)GROUP(ansi reset)"
let t = $"(ansi yellow_bold)($x.title)(ansi reset)"
let d = $"(ansi light_gray)($x.desc)(ansi reset)"
print $"($indent)($s) ($t) ($d)"
}
test_message: {|x|
let indent = ' ' | str repeat $x.indent
let indent = '' | fill -c ' ' -w $x.indent
let status = if $x.status {
$"(ansi bg_green)SUCC(ansi reset)"
} else {
Expand All @@ -753,11 +744,13 @@ export-env {
theme: {
info: 'yellow_italic'
batch_hint: 'dark_gray'
watch_separator: $"(ansi dark_gray)------------------------------(ansi reset)"
watch_separator: {
let w = term size | get columns
print -e $"(ansi dark_gray)('' | fill -c '-' -w $w)(ansi reset)"
}
}
}
distro: (distro)
lg: {$in | lg}
batch: {|mod|
let o = $in
| lines
Expand All @@ -773,13 +766,19 @@ export-env {
let fmt = $env.comma_index.settings.test_message
test $fmt 0 $dsc $spec
}
show: {$in | show}
tips: {|...m|
if ($m | length) > 2 {
print -e $"(ansi light_gray_italic)Accepts no more than (ansi yellow_bold)2(ansi reset)(ansi light_gray_italic) parameters(ansi reset)"
} else {
print -e $"(ansi light_gray_italic)($m.0)(ansi reset) (ansi yellow_bold)($m.1?)(ansi reset)"
}
}
log: {|lv s|
let c = ['navy' 'teal' 'xpurplea' 'xgreen' 'olive' 'maroon']
let t = date now | format date '%Y-%m-%dT%H:%M:%S'
print -e $"(ansi ($c | get $lv))($t) (ansi light_gray)($s)(ansi reset)"
}
T: {|f| {|r,a,s| do $f $r $a $s; true } }
F: {|f| {|r,a,s| do $f $r $a $s; false } }
I: {|x| $x }
Expand Down
2 changes: 1 addition & 1 deletion modules/comma/comma_test.nu
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ $env.comma = {|_|{
$_.act: {, -c example a b c e }
$_.x: [
{|r,a,s| $r | where value == 'f' | not ($in | is-empty) }
(do $_.T {|r,a,s| $s | lg 'expect'})
(do $_.T {|r,a,s| $s | show 'expect'})
{|r,a| 'q1|q2|q3|q4| open a file' == ($r | get 1.description) }
]
}
Expand Down

0 comments on commit 32d074e

Please sign in to comment.