From adaae97990b3fb819da66e8acf3979a2f395d239 Mon Sep 17 00:00:00 2001 From: Igor Date: Sun, 26 May 2024 16:49:03 +0400 Subject: [PATCH] Port `before_v0.60/stdlib_candidate` (#850) This PR is part of porting all old scripts #221 and includes the `stdlib_candidate` scripts ## 7 changed files - `flatter.nu` - `get-column.nu` - `get-row.nu` - `get-latest-release-linux.nu` - `logging.nu` - `nu_style.nu` - `print.nu` --- .../std-rfc}/flatter.nu | 9 ++-- .../std-rfc}/get-column.nu | 15 +++--- .../std-rfc}/get-latest-release-linux.nu | 23 ++++---- .../std-rfc}/get-row.nu | 5 +- .../std-rfc}/logging.nu | 6 +-- .../std-rfc}/nu_style.nu | 4 +- .../std-rfc}/print.nu | 53 +++++++++---------- 7 files changed, 54 insertions(+), 61 deletions(-) rename {before_v0.60/stdlib_candidate => stdlib-candidate/std-rfc}/flatter.nu (52%) rename {before_v0.60/stdlib_candidate => stdlib-candidate/std-rfc}/get-column.nu (52%) rename {before_v0.60/stdlib_candidate => stdlib-candidate/std-rfc}/get-latest-release-linux.nu (65%) rename {before_v0.60/stdlib_candidate => stdlib-candidate/std-rfc}/get-row.nu (64%) rename {before_v0.60/stdlib_candidate => stdlib-candidate/std-rfc}/logging.nu (59%) rename {before_v0.60/stdlib_candidate => stdlib-candidate/std-rfc}/nu_style.nu (99%) rename {before_v0.60/stdlib_candidate => stdlib-candidate/std-rfc}/print.nu (72%) diff --git a/before_v0.60/stdlib_candidate/flatter.nu b/stdlib-candidate/std-rfc/flatter.nu similarity index 52% rename from before_v0.60/stdlib_candidate/flatter.nu rename to stdlib-candidate/std-rfc/flatter.nu index 2cf7fec..0695ed5 100644 --- a/before_v0.60/stdlib_candidate/flatter.nu +++ b/stdlib-candidate/std-rfc/flatter.nu @@ -2,9 +2,10 @@ # by @jturner 2/10/21 # Example: sys | flatter 3 def flatter [levels:int] { + let input = $in if $levels > 0 { - flatten | flatter ($levels - 1) - } { - each { echo $it } + $input | columns | reduce -f $input {|it acc| $acc | flatten $it } | flatter ($levels - 1) + } else { + $input } -} \ No newline at end of file +} diff --git a/before_v0.60/stdlib_candidate/get-column.nu b/stdlib-candidate/std-rfc/get-column.nu similarity index 52% rename from before_v0.60/stdlib_candidate/get-column.nu rename to stdlib-candidate/std-rfc/get-column.nu index d0eac0c..f5e8592 100644 --- a/before_v0.60/stdlib_candidate/get-column.nu +++ b/stdlib-candidate/std-rfc/get-column.nu @@ -1,26 +1,23 @@ # Documentation for get-col # Written on 2021-03-28 06:58:50 by andras_io on discord +# Written for the new Nushell version on 03/31/2022 10:10 PM by denkspuren on discord def get-col [ col_index:int # A 0 indexed col_index ] { # meant to be used like `ls | get-col 1` - # ls | select (ls | get | nth 2) - - each { - echo $it | select (echo $it | get | nth $col_index) - } + let input = $in + let name = ($input | columns | select $col_index | get 0) + $input | select $name } # Documentation for get-col2 # Written on 2021-03-28 07:00:24 by johng on discord +# Written for the new Nushell version on 03/31/2022 10:10 PM by denkspuren on discord def get-column [ col_index:int # A 0 indexed col_index ] { # meant to be used like `ls | get-column 1` - pivot | nth $col_index | pivot | get Column1 | skip + $in | transpose | select $col_index | transpose | select column1 | headers } - -# another working example is -# ls | keep 2 | drop column 3 \ No newline at end of file diff --git a/before_v0.60/stdlib_candidate/get-latest-release-linux.nu b/stdlib-candidate/std-rfc/get-latest-release-linux.nu similarity index 65% rename from before_v0.60/stdlib_candidate/get-latest-release-linux.nu rename to stdlib-candidate/std-rfc/get-latest-release-linux.nu index 39972bf..eac32ba 100644 --- a/before_v0.60/stdlib_candidate/get-latest-release-linux.nu +++ b/stdlib-candidate/std-rfc/get-latest-release-linux.nu @@ -2,22 +2,23 @@ # requires nushell 0.36.0 or greater def get-latest-linux [] { # fetch the information about the latest release - let metadata = (fetch https://api.github.com/repos/nushell/nushell/releases/latest) - let release_name = ($metadata | get name | split row ' ' | nth 0) + let metadata = (http get https://api.github.com/repos/nushell/nushell/releases/latest) + let release_name = ($metadata | get name | split row ' ' | first) # get the body that shows information about this release let body = ($metadata | get body) # find the linux download - let asset_info = ($metadata | get assets | where name =~ 'linux.tar.gz') + let asset_info = ($metadata | get assets | where name =~ 'x86_64-linux-gnu-full.tar.gz' | first) # construct the url let download_url = ($asset_info | get browser_download_url) + let file_name = ($asset_info | get name) # tell you what i'm doing - $"Release name is ($release_name)(char newline)(char newline)" - $"($body)(char newline)(char newline)Downloading and following redirects ..." + print $"Release name is ($release_name)(char newline)(char newline)" + print $"($body)(char newline)(char newline)Downloading and following redirects ..." # fetch follows redirects now - fetch $download_url | save $file_name + http get $download_url | save $file_name # tell you what i'm doing - $"(char newline)Extracting ($file_name) to /tmp(char newline)" + print $"(char newline)Extracting ($file_name) to /tmp(char newline)" # extract the tar file to the temp folder tar -xf ($file_name) -C /tmp # parse the $file_name to get the folder @@ -27,17 +28,17 @@ def get-latest-linux [] { # there are two extensions .tar and .gz let root_file_name = ($file_name | path parse | get stem | path parse | get stem) # update our progress - $"Copying files from /tmp/($root_file_name)/*/* to ~/.cargo/bin(char newline)" + print $"Copying files from /tmp/($root_file_name)/*/* to ~/.cargo/bin(char newline)" # this is for testing so it doesn't overwrite my real nu. this should really # be a parameter mkdir release # construct the copy from and to paths - let cp_from_path = $"/tmp/($root_file_name)/*/*" + let cp_from_path = $"/tmp/($root_file_name)/**/*" let cp_to_path = "./release" # actually run the cp command # we may want to make this overwrite and not prompt cp ($cp_from_path) ($cp_to_path) # exec ~/.cargo/bin/nu - $"Starting nushell(char nl)" + print $"Starting nushell(char nl)" exec ./release/nu -} \ No newline at end of file +} diff --git a/before_v0.60/stdlib_candidate/get-row.nu b/stdlib-candidate/std-rfc/get-row.nu similarity index 64% rename from before_v0.60/stdlib_candidate/get-row.nu rename to stdlib-candidate/std-rfc/get-row.nu index 687a870..401ef91 100644 --- a/before_v0.60/stdlib_candidate/get-row.nu +++ b/stdlib-candidate/std-rfc/get-row.nu @@ -2,6 +2,5 @@ def get-row [ row_num:int # A 0 indexed row ] { - - nth $row_num | table --index $row_num -} \ No newline at end of file + $in | get $row_num | table --index $row_num +} diff --git a/before_v0.60/stdlib_candidate/logging.nu b/stdlib-candidate/std-rfc/logging.nu similarity index 59% rename from before_v0.60/stdlib_candidate/logging.nu rename to stdlib-candidate/std-rfc/logging.nu index 7d07979..ee3f8cc 100644 --- a/before_v0.60/stdlib_candidate/logging.nu +++ b/stdlib-candidate/std-rfc/logging.nu @@ -1,6 +1,6 @@ # 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 now = (date now | format date '%Y%m%d_%H%M%S.%f') let mess = $"($now)|DBG|($message)(char nl)" - echo $mess | autoview -} \ No newline at end of file + $mess +} diff --git a/before_v0.60/stdlib_candidate/nu_style.nu b/stdlib-candidate/std-rfc/nu_style.nu similarity index 99% rename from before_v0.60/stdlib_candidate/nu_style.nu rename to stdlib-candidate/std-rfc/nu_style.nu index cfaf4b8..c13e0c2 100644 --- a/before_v0.60/stdlib_candidate/nu_style.nu +++ b/stdlib-candidate/std-rfc/nu_style.nu @@ -59,7 +59,7 @@ def fg_from_rgb [ green:int # green component 0-255 blue:int # blue component 0-255 ] { - echo [(ansi -e '38;2;') $red ';' $green ';' $blue 'm'] | str collect + echo [(ansi -e '38;2;') $red ';' $green ';' $blue 'm'] | str join } alias bg_black = ansi -e '40m' @@ -107,4 +107,4 @@ def bg_from_index [ idx:int # index value 0-255 ] { $"(ansi -e '48;5;')($idx)m" -} \ No newline at end of file +} diff --git a/before_v0.60/stdlib_candidate/print.nu b/stdlib-candidate/std-rfc/print.nu similarity index 72% rename from before_v0.60/stdlib_candidate/print.nu rename to stdlib-candidate/std-rfc/print.nu index 6e1ac07..6319c0d 100644 --- a/before_v0.60/stdlib_candidate/print.nu +++ b/stdlib-candidate/std-rfc/print.nu @@ -1,22 +1,22 @@ # A print command that concatenates arguments together with an optional separator # By default there will be no newline -def print [ +def print1 [ --separator(-s):any # Optional separator (not yet flagged as optional?) ...rest # All of the parameters ] { - let is_empty = ($separator | empty?) + let is_empty = ($separator | is-empty) let num_of_rest = ($rest | length) - $rest | each --numbered { |param| + $rest | enumerate | each { |param| if $is_empty { $param.item - } { + } else { if $num_of_rest > ($param.index + 1) { $"($param.item)($separator)" - } { + } else { $param.item } } - } | into string | str collect + } | into string | str join } # > print 1 2 3 "four" -s '--' @@ -32,17 +32,15 @@ def print2 [ --separator(-s):any # Optional separator (not yet flagged as optional?) ...rest # All of the parameters ] { - let is_empty = ($separator | empty?) + let is_empty = ($separator | is-empty) let num_of_rest = ($rest | length) if $is_empty { - $rest | into string | str collect - } { - $rest | into string | str collect $separator + $rest | into string | str join + } else { + $rest | into string | str join $separator + } } -} -# Bring in the logging command -#source logging.nu # A print command that concatenates arguments together with an optional separator. # This print command will also concatenate tables like [1 2 3] as well as most other primitives @@ -52,36 +50,33 @@ def print3 [ --flat(-f) # If tables are found, flatten them ...rest # All of the parameters ] { - let sep_empty = ($separator | empty?) + let sep_empty = ($separator | is-empty) let num_of_rest = ($rest | length) - let flat = ($flat | empty?) - $rest | each --numbered { |param| + let flat = ($flat | is-empty) + $rest | enumerate | each { |param| if $sep_empty { - #log 'sep is empty' - if (echo $param.item | length) > 1 and $flat { - #log 'flatten please' - let flatter = ($param.item | flatten | into string | str collect) + if ((echo $param.item | str length) > 1) and $flat { + let flatter = ($param.item | flatten | into string | str join) $flatter - } { - #log 'no flat' + } else { $param.item } - } { + } else { if $num_of_rest > ($param.index + 1) { if ($param.item | length) > 1 and $flat { - let flatter = ($param.item | flatten | into string | str collect $separator) + let flatter = ($param.item | flatten | into string | str join $separator) $"($flatter)($separator)" - } { + } else { $"($param.item)($separator)" } - } { + } else { if ($param.item | length) > 1 and $flat { - let flatter = ($param.item | flatten | into string | str collect $separator) + let flatter = ($param.item | flatten | into string | str join $separator) $flatter - } { + } else { $param.item } } } - } | str collect + } | str join }