Skip to content

Commit

Permalink
Port before_v0.60/stdlib_candidate (#850)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
39555 committed May 26, 2024
1 parent 93e71fe commit adaae97
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
def get-row [
row_num:int # A 0 indexed row
] {

nth $row_num | table --index $row_num
}
$in | get $row_num | table --index $row_num
}
Original file line number Diff line number Diff line change
@@ -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
}
$mess
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -107,4 +107,4 @@ def bg_from_index [
idx:int # index value 0-255
] {
$"(ansi -e '48;5;')($idx)m"
}
}
Original file line number Diff line number Diff line change
@@ -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 '--'
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit adaae97

Please sign in to comment.