From e8df70a406f1d5d8f0614d58b8b99741412010cb Mon Sep 17 00:00:00 2001 From: Igor Date: Sun, 26 May 2024 21:35:59 +0400 Subject: [PATCH] Port `before_v0.60/fuzzy`, `before_v0.60/ls-mods` and `before_v0.60/nu_101` (#845) This PR is part of porting all old scripts #221 and includes a set of modules: - fuzzy -> `modules/fuzzy/fuzzy_command_search.nu` - ls-mods -> `modules/ls-mods`: `ls-less.nu`, `ls-wide.nu` and `ls-wide-with-color.nu` - nu_101 -> `modules/nu_101`: `nothing.nu` and `inner_outer_loop.nu` Edit: `fuzzy` and `nu_101` have been moved to `sourced` --- before_v0.60/fuzzy/fuzzy_command_search.nu | 38 -------- before_v0.60/fuzzy/fuzzy_history_search.nu | 1 - before_v0.60/ls_mods/ls-hidden.nu | 6 -- before_v0.60/ls_mods/ls-wide-with-color.nu | 95 ------------------- before_v0.60/ls_mods/ls-wide.nu | 54 ----------- before_v0.60/nu_101/README.md | 5 - before_v0.60/nu_101/demo.nu | 5 - {before_v0.60 => modules}/fuzzy/README.md | 12 +-- modules/fuzzy/fuzzy_command_search.nu | 25 +++++ modules/fuzzy/fuzzy_history_search.nu | 1 + modules/ls_mods/ls-hidden.nu | 7 ++ {before_v0.60 => modules}/ls_mods/ls-less.nu | 8 +- modules/ls_mods/ls-wide-with-color.nu | 75 +++++++++++++++ modules/ls_mods/ls-wide.nu | 47 +++++++++ .../nu_101/inner_outer_loop.nu | 6 +- {before_v0.60 => sourced}/nu_101/nothing.nu | 22 ++--- 16 files changed, 177 insertions(+), 230 deletions(-) delete mode 100644 before_v0.60/fuzzy/fuzzy_command_search.nu delete mode 100644 before_v0.60/fuzzy/fuzzy_history_search.nu delete mode 100644 before_v0.60/ls_mods/ls-hidden.nu delete mode 100644 before_v0.60/ls_mods/ls-wide-with-color.nu delete mode 100644 before_v0.60/ls_mods/ls-wide.nu delete mode 100644 before_v0.60/nu_101/README.md delete mode 100644 before_v0.60/nu_101/demo.nu rename {before_v0.60 => modules}/fuzzy/README.md (78%) create mode 100644 modules/fuzzy/fuzzy_command_search.nu create mode 100644 modules/fuzzy/fuzzy_history_search.nu create mode 100644 modules/ls_mods/ls-hidden.nu rename {before_v0.60 => modules}/ls_mods/ls-less.nu (75%) create mode 100644 modules/ls_mods/ls-wide-with-color.nu create mode 100644 modules/ls_mods/ls-wide.nu rename {before_v0.60 => sourced}/nu_101/inner_outer_loop.nu (78%) rename {before_v0.60 => sourced}/nu_101/nothing.nu (52%) diff --git a/before_v0.60/fuzzy/fuzzy_command_search.nu b/before_v0.60/fuzzy/fuzzy_command_search.nu deleted file mode 100644 index 32a8da85..00000000 --- a/before_v0.60/fuzzy/fuzzy_command_search.nu +++ /dev/null @@ -1,38 +0,0 @@ -# calculate required tabs/spaces to get a nicely aligned table -let tablen = 8 -let max-len = (help commands | get subcommands | each { $it.name | str length } | math max) -let max-indent = ($max-len / $tablen | into int) - -def pad-tabs [input-name] { - let input-length = ($input-name| str length) - let required-tabs = $max-indent - ($"($input-length / $tablen | into int)" | str to-int) - echo $"( seq $required-tabs | reduce -f "" {$acc + (char tab)})" -} - -# fuzzy search a) commands b) subcommands -# on selection, will display `help` for the commands -# and paste command into clipboard for you to paste right away - - -def fuzzy-command-search [] { - help (echo (help commands | get subcommands | each { - let name = ($it.name | str trim | ansi strip) - $"( - $name - )( pad-tabs $name - )( - $it.description - )" - }) ( - help commands | reject subcommands | each { - let name = ($it.name | str trim | ansi strip) - $"( - $name - )( - pad-tabs $name - )( - $it.description - )" - }) | str collect (char nl) | fzf | split column (char tab)| get Column1 | clip; paste ) -} - diff --git a/before_v0.60/fuzzy/fuzzy_history_search.nu b/before_v0.60/fuzzy/fuzzy_history_search.nu deleted file mode 100644 index ad2a1ca9..00000000 --- a/before_v0.60/fuzzy/fuzzy_history_search.nu +++ /dev/null @@ -1 +0,0 @@ -def fuzzy-history-search [] { cat $nu.history-path | fzf | clip } diff --git a/before_v0.60/ls_mods/ls-hidden.nu b/before_v0.60/ls_mods/ls-hidden.nu deleted file mode 100644 index 94eee720..00000000 --- a/before_v0.60/ls_mods/ls-hidden.nu +++ /dev/null @@ -1,6 +0,0 @@ - -def ls-hidden [ - --dir(-d):any # The directory you want to list -] { - ls -a $dir | where { ($it.name | into string | str starts-with '.') } -} diff --git a/before_v0.60/ls_mods/ls-wide-with-color.nu b/before_v0.60/ls_mods/ls-wide-with-color.nu deleted file mode 100644 index 32f4ae34..00000000 --- a/before_v0.60/ls_mods/ls-wide-with-color.nu +++ /dev/null @@ -1,95 +0,0 @@ -alias relet = ansi -e '0m' # really reset but there are external commands for reset already - -alias fg_black = ansi -e '30m' -alias fg_red = ansi -e '31m' -alias fg_green = ansi -e '32m' -alias fg_yellow = ansi -e '33m' -alias fg_blue = ansi -e '34m' -alias fg_magenta = ansi -e '35m' -alias fg_purple = ansi -e '35m' -alias fg_cyan = ansi -e '36m' -alias fg_white = ansi -e '37m' - -alias fg_dark_gray = ansi -e '90m' -alias fg_light_black = ansi -e '90m' -alias fg_light_red = ansi -e '91m' -alias fg_light_green = ansi -e '92m' -alias fg_light_yellow = ansi -e '93m' -alias fg_light_blue = ansi -e '94m' -alias fg_light_magenta = ansi -e '95m' -alias fg_light_purple = ansi -e '95m' -alias fg_light_cyan = ansi -e '96m' -alias fg_light_gray = ansi -e '97m' -alias fg_light_white = ansi -e '97m' - -# A ls command that approximates the ls -sh command in bash -def ls-wide2 [ - --dir(-d):any # The directory you want to list - --columns(-c):int # The number of columns in your output - ] { - let is_dir_empty = ($dir | empty?) - let is_columns_empty = ($columns | empty?) - let ls_data = (if $is_dir_empty { ls } { ls $dir }) - let ansi_size = 9 # \x1b[36m + string + \x1b[0m, ~9 characters are added to each string for coloring - let max_fname_size = ((echo $ls_data | get name | into string | str length | math max) + $ansi_size) - let max_fsize_size = (echo $ls_data | get size | into string | str length | math max) - # log (build-string 'max_fname_size=' $max_fname_size ' max_fsize_size=' $max_fsize_size) - ($ls_data) | each -n { |file| - let clr_file = (colorize $file.item.name) - # log (build-string $clr_file ' ' $max_fname_size) - let clr_size = (echo $file.item.size | into string) - # log (build-string $clr_size ' ' $max_fsize_size) - $"($clr_file | str rpad -c ' ' -l $max_fname_size) ($clr_size | str lpad -c ' ' -l $max_fsize_size) " | autoview - if $is_columns_empty { - if ($file.index + 1) mod 3 == 0 { - echo (char newline) | autoview - } {} - } { - if ($file.index + 1) mod $columns == 0 { - echo (char newline) | autoview - } {} - } - } | str collect -} - -def colorize [thing:any] { - let thing_as_string = (echo $thing | into string) - let ext = (echo $thing_as_string | path parse | get extension) - let is_empty = ($ext | empty?) - - if $is_empty { - # build-string (ansi -e '36m') $thing (ansi -e '0m') - $"(fg_cyan)($thing)(relet)" - # build-string 'e[36m' $thing 'e[0m' - } { - if $ext == "nu" { - # build-string (ansi -e '95m') $thing (ansi -e '0m') - $"(fg_light_magenta)($thing)(relet)" - # build-string 'e[95m' $thing 'e[0m' - } { - # build-string (ansi -e '92m') $thing (ansi -e '0m') - $"(fg_light_green)($thing)(relet)" - # build-string 'e[92m' $thing 'e[0m' - } - } -} - -def colorit [] { - # log "123" - colorize 123 - # log "123.456" - colorize " 123.456" - # log "file.nu" - colorize " file.nu" - - # These all work - $"(fg_light_green)abc(relet)" - $"(fg_cyan)def(relet)" -} - -# This is a first attempt and some type of logging -def log [message:any] { - let now = (date now | date format '%Y%m%d_%H%M%S.%f') - let mess = $"($now)|DBG|($message)(char nl)" - echo $mess | autoview -} \ No newline at end of file diff --git a/before_v0.60/ls_mods/ls-wide.nu b/before_v0.60/ls_mods/ls-wide.nu deleted file mode 100644 index 613c58bd..00000000 --- a/before_v0.60/ls_mods/ls-wide.nu +++ /dev/null @@ -1,54 +0,0 @@ -# A ls command that approximates the ls -sh command in bash -def ls-wide [ - --path(-p):string # The path you want to list - --columns(-c):int # The number of columns in your output - ] { - let is_columns_empty = ($columns | empty?) - let is_path_empty = ($path | empty?) - let columns_default = 3 - - if $is_path_empty { - if $is_columns_empty { - run_ls "." $columns_default - } { - run_ls "." $columns - } - } { - if $is_columns_empty { - run_ls $path $columns_default - } { - run_ls $path $columns - } - } -} - -def run_ls [ - path:string - columns:int - ] { - let max_fname_size = (ls $path | get name | into string | str length | math max) - let max_fsize_size = (ls $path | get size | into string | str length | math max) - - ls $path | each -n { |file| - let the_file = ($file.item.name | into string | str rpad -c ' ' -l $max_fname_size) - let the_size = ($file.item.size | into string | str lpad -c ' ' -l $max_fsize_size) - $"($the_file) ($the_size) " | autoview - - if $is_columns_empty { - if ($file.index + 1) mod 3 == 0 { - echo (char newline) | autoview - } {} - } { - if ($file.index + 1) mod $columns == 0 { - echo (char newline) | autoview - } {} - } - } | str collect -} - -# This is a first attempt and some type of logging -def log [message:any] { - let now = (date now | date format '%Y%m%d_%H%M%S.%f') - let mess = $"($now)|DBG|($message)(char nl)" - echo $mess | autoview -} \ No newline at end of file diff --git a/before_v0.60/nu_101/README.md b/before_v0.60/nu_101/README.md deleted file mode 100644 index cb27bcaf..00000000 --- a/before_v0.60/nu_101/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Nu_101 Scripts - -### Definition - -These scripts should be used to demonstrate to the beginner how nushell scripting works. Perhaps how it's different from Windows `batch` files and `bash` shell scripts. Also, to show off how nushell pipes commands in and out of one another. diff --git a/before_v0.60/nu_101/demo.nu b/before_v0.60/nu_101/demo.nu deleted file mode 100644 index 257ccb69..00000000 --- a/before_v0.60/nu_101/demo.nu +++ /dev/null @@ -1,5 +0,0 @@ -def my-ls [x] { - ls $x | where size > 10kb -} - -my-ls . diff --git a/before_v0.60/fuzzy/README.md b/modules/fuzzy/README.md similarity index 78% rename from before_v0.60/fuzzy/README.md rename to modules/fuzzy/README.md index 9a97b94a..69011817 100644 --- a/before_v0.60/fuzzy/README.md +++ b/modules/fuzzy/README.md @@ -12,15 +12,13 @@ want to search commands/your history interactively, which is where [fzf](https:/ `./fuzzy_command_search.nu` searches both commands and subcommands for both a) names and b) their description, and, after pressing `enter`, copies the selected command into the clipboard To use them in your day-to-day workflow, add + ``` -[ - "source /fuzzy/fuzzy_history_search.nu", - "source /fuzzy/fuzzy_command_search.nu" -] +source /fuzzy/fuzzy_history_search.nu +source /fuzzy/fuzzy_command_search.nu ``` - -to your `startup` array in you config `.toml`. +to your `config.nu` It's likely a good idea to also add some short and sweet aliases, e.g. @@ -28,5 +26,3 @@ It's likely a good idea to also add some short and sweet aliases, e.g. alias hi = fuzzy-history-search alias hf = fuzzy-command-search ``` - -To your config diff --git a/modules/fuzzy/fuzzy_command_search.nu b/modules/fuzzy/fuzzy_command_search.nu new file mode 100644 index 00000000..ddfdeaef --- /dev/null +++ b/modules/fuzzy/fuzzy_command_search.nu @@ -0,0 +1,25 @@ +const tablen = 8 + +# calculate required tabs/spaces to get a nicely aligned table +def pad-tabs [input_name max_indent] { + let input_length = ($input_name | str length) + let required_tabs = $max_indent - ($input_length / $tablen | into int) + seq 0 $required_tabs | reduce -f "" {|it, acc| $acc + (char tab)} +} + +# fuzzy search a) commands b) subcommands +# on selection, will display `help` for the commands +# and paste command into clipboard for you to paste right away + + +export def fuzzy-command-search [] { + let max_len = (help commands | each { $in.name | str length } | math max) + let max_indent = ($max_len / $tablen | into int) + let command = ((help commands | each {|it| + let name = ($it.name | str trim | ansi strip) + $"($name)(pad-tabs $name $max_indent)($it.usage)" + }) | str join (char nl) | fzf | split column (char tab) | get column1.0) + if ($command | is-not-empty) { + help $command + } +} diff --git a/modules/fuzzy/fuzzy_history_search.nu b/modules/fuzzy/fuzzy_history_search.nu new file mode 100644 index 00000000..93ba2e94 --- /dev/null +++ b/modules/fuzzy/fuzzy_history_search.nu @@ -0,0 +1 @@ +export def fuzzy-history-search [] { cat $nu.history-path | fzf | clip } diff --git a/modules/ls_mods/ls-hidden.nu b/modules/ls_mods/ls-hidden.nu new file mode 100644 index 00000000..52b01855 --- /dev/null +++ b/modules/ls_mods/ls-hidden.nu @@ -0,0 +1,7 @@ + +export def ls-hidden [ + --dir(-d):any # The directory you want to list +] { + let dir = if ($dir | is-empty) { "." } else { $dir } + ls -a $dir | filter { ($in.name | into string | str starts-with '.') } +} diff --git a/before_v0.60/ls_mods/ls-less.nu b/modules/ls_mods/ls-less.nu similarity index 75% rename from before_v0.60/ls_mods/ls-less.nu rename to modules/ls_mods/ls-less.nu index 67d3d645..9b4cea15 100644 --- a/before_v0.60/ls_mods/ls-less.nu +++ b/modules/ls_mods/ls-less.nu @@ -1,12 +1,12 @@ # An attempt at trying to put ls into a paging mode -def ls-less [ +export def ls-less [ --dir(-d):any # The directory you want to list ] { - let is_empty = ($dir | empty?) + let is_empty = ($dir | is-empty) if $is_empty { nu -c 'ls' | less -r - } { + } else { let command = $"ls ($dir)" nu -c $command | less -r } -} \ No newline at end of file +} diff --git a/modules/ls_mods/ls-wide-with-color.nu b/modules/ls_mods/ls-wide-with-color.nu new file mode 100644 index 00000000..4d6027a2 --- /dev/null +++ b/modules/ls_mods/ls-wide-with-color.nu @@ -0,0 +1,75 @@ +alias relet = ansi -e '0m' # really reset but there are external commands for reset already + +alias fg_black = ansi -e '30m' +alias fg_red = ansi -e '31m' +alias fg_green = ansi -e '32m' +alias fg_yellow = ansi -e '33m' +alias fg_blue = ansi -e '34m' +alias fg_magenta = ansi -e '35m' +alias fg_purple = ansi -e '35m' +alias fg_cyan = ansi -e '36m' +alias fg_white = ansi -e '37m' + +alias fg_dark_gray = ansi -e '90m' +alias fg_light_black = ansi -e '90m' +alias fg_light_red = ansi -e '91m' +alias fg_light_green = ansi -e '92m' +alias fg_light_yellow = ansi -e '93m' +alias fg_light_blue = ansi -e '94m' +alias fg_light_magenta = ansi -e '95m' +alias fg_light_purple = ansi -e '95m' +alias fg_light_cyan = ansi -e '96m' +alias fg_light_gray = ansi -e '97m' +alias fg_light_white = ansi -e '97m' + +# A ls command that approximates the ls -sh command in bash +export def ls-wide2 [ + --dir(-d):any # The directory you want to list + --columns(-c):int # The number of columns in your output + ] { + let is_dir_empty = ($dir | is-empty) + let is_columns_empty = ($columns | is-empty) + let ls_data = (if $is_dir_empty { ls } else { ls $dir }) + let max_fname_size = ($ls_data | get name | into string | str length | math max) + let max_fsize_size = ($ls_data | get size | into string | str length | math max) + ($ls_data) | enumerate | each { |file| + let clr_file = (colorize $file.item.name) + let clr_size = (echo $file.item.size | into string) + let new_line = (if $is_columns_empty { + if (($file.index + 1) mod 3) == 0 { + char newline + } + } else { + if (($file.index + 1) mod $columns) == 0 { + char newline + } + }) + $"($clr_file | fill -a l -c ' ' -w $max_fname_size) ($clr_size | fill -a r -c ' ' -w $max_fsize_size) ($new_line)" + } | str join +} + +def colorize [thing:any] { + let thing_as_string = (echo $thing | into string) + let ext = (echo $thing_as_string | path parse | get extension) + let is_empty = ($ext | is-empty) + + if $is_empty { + $"(fg_cyan)($thing)(relet)" + } else { + if $ext == "nu" { + $"(fg_light_magenta)($thing)(relet)" + } else { + $"(fg_light_green)($thing)(relet)" + } + } +} + +def colorit [] { + print (colorize 123) + print (colorize " 123.456") + print (colorize " file.nu") + + # These all work + print $"(fg_light_green)abc(relet)" + print $"(fg_cyan)def(relet)" +} diff --git a/modules/ls_mods/ls-wide.nu b/modules/ls_mods/ls-wide.nu new file mode 100644 index 00000000..45b157f0 --- /dev/null +++ b/modules/ls_mods/ls-wide.nu @@ -0,0 +1,47 @@ +# A ls command that approximates the ls -sh command in bash +export def ls-wide [ + --path(-p):string # The path you want to list + --columns(-c):int # The number of columns in your output + ] { + let is_columns_empty = ($columns | is-empty) + let is_path_empty = ($path | is-empty) + let columns_default = 3 + + if $is_path_empty { + if $is_columns_empty { + run_ls "." $columns_default + } else { + run_ls "." $columns + } + } else { + if $is_columns_empty { + run_ls $path $columns_default + } else { + run_ls $path $columns + } + } +} + +def run_ls [ + path:string + columns:int + ] { + let max_fname_size = (ls $path | get name | into string | str length | math max) + let max_fsize_size = (ls $path | get size | into string | str length | math max) + let is_columns_empty = ($columns | is-empty) + + ls $path | enumerate | each { |file| + let the_file = ($file.item.name | into string | fill -a l -c ' ' -w $max_fname_size) + let the_size = ($file.item.size | into string | fill -a r -c ' ' -w $max_fsize_size) + let new_line = (if $is_columns_empty { + if ($file.index + 1) mod 3 == 0 { + char newline + } + } else { + if ($file.index + 1) mod $columns == 0 { + char newline + } + }) + $"($the_file) ($the_size) ($new_line)" + } | str join +} diff --git a/before_v0.60/nu_101/inner_outer_loop.nu b/sourced/nu_101/inner_outer_loop.nu similarity index 78% rename from before_v0.60/nu_101/inner_outer_loop.nu rename to sourced/nu_101/inner_outer_loop.nu index be972281..6b0f5ad7 100644 --- a/before_v0.60/nu_101/inner_outer_loop.nu +++ b/sourced/nu_101/inner_outer_loop.nu @@ -6,6 +6,6 @@ seq 30 39 | each { |outer| let row = $"($outer) " let data = (seq 40 49 | each { |inner| $"($inner) " - } | str collect) - $"($row)($data)(char newline)" -} | str collect \ No newline at end of file + } | str join) + $"($row)($data)" +} | str join (char newline) diff --git a/before_v0.60/nu_101/nothing.nu b/sourced/nu_101/nothing.nu similarity index 52% rename from before_v0.60/nu_101/nothing.nu rename to sourced/nu_101/nothing.nu index 21b35360..793c86ef 100644 --- a/before_v0.60/nu_101/nothing.nu +++ b/sourced/nu_101/nothing.nu @@ -1,24 +1,24 @@ # This checks the -f switch to see if it was supplied # and tests the new $nothing variable def nada [ - --flat(-f) + flat? ] { - if $flat == $nothing { - echo $true - } { - echo $false + if $flat == null { + true + } else { + false } } # This shows an alternate way to check for nothing def nada2 [ - --flat(-f) + flat? ] { - let flat = ($flat | empty?) + let flat = ($flat | is-empty) if $flat { - echo $true - } { - echo $false + true + } else { + false } -} \ No newline at end of file +}