1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 22:57:46 +00:00

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`
This commit is contained in:
Igor 2024-05-26 16:49:03 +04:00 committed by GitHub
parent 93e71fe6a8
commit adaae97990
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 54 additions and 61 deletions

View file

@ -2,9 +2,10 @@
# by @jturner 2/10/21 # by @jturner 2/10/21
# Example: sys | flatter 3 # Example: sys | flatter 3
def flatter [levels:int] { def flatter [levels:int] {
let input = $in
if $levels > 0 { if $levels > 0 {
flatten | flatter ($levels - 1) $input | columns | reduce -f $input {|it acc| $acc | flatten $it } | flatter ($levels - 1)
} { } else {
each { echo $it } $input
} }
} }

View file

@ -1,26 +1,23 @@
# Documentation for get-col # Documentation for get-col
# Written on 2021-03-28 06:58:50 by andras_io on discord # 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 [ def get-col [
col_index:int # A 0 indexed col_index col_index:int # A 0 indexed col_index
] { ] {
# meant to be used like `ls | get-col 1` # meant to be used like `ls | get-col 1`
# ls | select (ls | get | nth 2) let input = $in
let name = ($input | columns | select $col_index | get 0)
each { $input | select $name
echo $it | select (echo $it | get | nth $col_index)
}
} }
# Documentation for get-col2 # Documentation for get-col2
# Written on 2021-03-28 07:00:24 by johng on discord # 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 [ def get-column [
col_index:int # A 0 indexed col_index col_index:int # A 0 indexed col_index
] { ] {
# meant to be used like `ls | get-column 1` # 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

View file

@ -2,22 +2,23 @@
# requires nushell 0.36.0 or greater # requires nushell 0.36.0 or greater
def get-latest-linux [] { def get-latest-linux [] {
# fetch the information about the latest release # fetch the information about the latest release
let metadata = (fetch https://api.github.com/repos/nushell/nushell/releases/latest) let metadata = (http get https://api.github.com/repos/nushell/nushell/releases/latest)
let release_name = ($metadata | get name | split row ' ' | nth 0) let release_name = ($metadata | get name | split row ' ' | first)
# get the body that shows information about this release # get the body that shows information about this release
let body = ($metadata | get body) let body = ($metadata | get body)
# find the linux download # 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 # construct the url
let download_url = ($asset_info | get browser_download_url) let download_url = ($asset_info | get browser_download_url)
let file_name = ($asset_info | get name) let file_name = ($asset_info | get name)
# tell you what i'm doing # tell you what i'm doing
$"Release name is ($release_name)(char newline)(char newline)" print $"Release name is ($release_name)(char newline)(char newline)"
$"($body)(char newline)(char newline)Downloading and following redirects ..." print $"($body)(char newline)(char newline)Downloading and following redirects ..."
# fetch follows redirects now # fetch follows redirects now
fetch $download_url | save $file_name http get $download_url | save $file_name
# tell you what i'm doing # 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 # extract the tar file to the temp folder
tar -xf ($file_name) -C /tmp tar -xf ($file_name) -C /tmp
# parse the $file_name to get the folder # parse the $file_name to get the folder
@ -27,17 +28,17 @@ def get-latest-linux [] {
# there are two extensions .tar and .gz # there are two extensions .tar and .gz
let root_file_name = ($file_name | path parse | get stem | path parse | get stem) let root_file_name = ($file_name | path parse | get stem | path parse | get stem)
# update our progress # 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 # this is for testing so it doesn't overwrite my real nu. this should really
# be a parameter # be a parameter
mkdir release mkdir release
# construct the copy from and to paths # 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" let cp_to_path = "./release"
# actually run the cp command # actually run the cp command
# we may want to make this overwrite and not prompt # we may want to make this overwrite and not prompt
cp ($cp_from_path) ($cp_to_path) cp ($cp_from_path) ($cp_to_path)
# exec ~/.cargo/bin/nu # exec ~/.cargo/bin/nu
$"Starting nushell(char nl)" print $"Starting nushell(char nl)"
exec ./release/nu exec ./release/nu
} }

View file

@ -2,6 +2,5 @@
def get-row [ def get-row [
row_num:int # A 0 indexed row row_num:int # A 0 indexed row
] { ] {
$in | get $row_num | table --index $row_num
nth $row_num | table --index $row_num }
}

View file

@ -1,6 +1,6 @@
# This is a first attempt and some type of logging # This is a first attempt and some type of logging
def log [message:any] { 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)" let mess = $"($now)|DBG|($message)(char nl)"
echo $mess | autoview $mess
} }

View file

@ -59,7 +59,7 @@ def fg_from_rgb [
green:int # green component 0-255 green:int # green component 0-255
blue:int # blue 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' alias bg_black = ansi -e '40m'
@ -107,4 +107,4 @@ def bg_from_index [
idx:int # index value 0-255 idx:int # index value 0-255
] { ] {
$"(ansi -e '48;5;')($idx)m" $"(ansi -e '48;5;')($idx)m"
} }

View file

@ -1,22 +1,22 @@
# A print command that concatenates arguments together with an optional separator # A print command that concatenates arguments together with an optional separator
# By default there will be no newline # By default there will be no newline
def print [ def print1 [
--separator(-s):any # Optional separator (not yet flagged as optional?) --separator(-s):any # Optional separator (not yet flagged as optional?)
...rest # All of the parameters ...rest # All of the parameters
] { ] {
let is_empty = ($separator | empty?) let is_empty = ($separator | is-empty)
let num_of_rest = ($rest | length) let num_of_rest = ($rest | length)
$rest | each --numbered { |param| $rest | enumerate | each { |param|
if $is_empty { if $is_empty {
$param.item $param.item
} { } else {
if $num_of_rest > ($param.index + 1) { if $num_of_rest > ($param.index + 1) {
$"($param.item)($separator)" $"($param.item)($separator)"
} { } else {
$param.item $param.item
} }
} }
} | into string | str collect } | into string | str join
} }
# > print 1 2 3 "four" -s '--' # > print 1 2 3 "four" -s '--'
@ -32,17 +32,15 @@ def print2 [
--separator(-s):any # Optional separator (not yet flagged as optional?) --separator(-s):any # Optional separator (not yet flagged as optional?)
...rest # All of the parameters ...rest # All of the parameters
] { ] {
let is_empty = ($separator | empty?) let is_empty = ($separator | is-empty)
let num_of_rest = ($rest | length) let num_of_rest = ($rest | length)
if $is_empty { if $is_empty {
$rest | into string | str collect $rest | into string | str join
} { } else {
$rest | into string | str collect $separator $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. # 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 # 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 --flat(-f) # If tables are found, flatten them
...rest # All of the parameters ...rest # All of the parameters
] { ] {
let sep_empty = ($separator | empty?) let sep_empty = ($separator | is-empty)
let num_of_rest = ($rest | length) let num_of_rest = ($rest | length)
let flat = ($flat | empty?) let flat = ($flat | is-empty)
$rest | each --numbered { |param| $rest | enumerate | each { |param|
if $sep_empty { if $sep_empty {
#log 'sep is empty' if ((echo $param.item | str length) > 1) and $flat {
if (echo $param.item | length) > 1 and $flat { let flatter = ($param.item | flatten | into string | str join)
#log 'flatten please'
let flatter = ($param.item | flatten | into string | str collect)
$flatter $flatter
} { } else {
#log 'no flat'
$param.item $param.item
} }
} { } else {
if $num_of_rest > ($param.index + 1) { if $num_of_rest > ($param.index + 1) {
if ($param.item | length) > 1 and $flat { 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)" $"($flatter)($separator)"
} { } else {
$"($param.item)($separator)" $"($param.item)($separator)"
} }
} { } else {
if ($param.item | length) > 1 and $flat { 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 $flatter
} { } else {
$param.item $param.item
} }
} }
} }
} | str collect } | str join
} }