forked from nushell/nu_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new module comma and some updates (nushell#704)
update: - cwdhist: openeditor changed from alt+e to alt+o - docker: pull, push add nerdctl insecure-registry support - docker: registry show use curl instead http get (may cause dns resolution problems) - git: status `table -n` changed to `table -i` new module: comma Similar to `pwd-module`, but supports completion and description through custom data formats - `,` or `*` need to be exported in order to use `,` directly - Directly execute `,` to create a new template file or edit an existing file - Custom tasks are written in `$env.comma` and can be nested - Generate completions based on the structure of `$env.comma` - You can use closure to customize completion - In `$env.commax.act` of default closure, you can receive parameters after the current position - In `$env.commax.cmp` , you can receive the parameter before the current position --------- Co-authored-by: nash <[email protected]>
- Loading branch information
Showing
6 changed files
with
318 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
Working dir task runner, similar to `pwd-module`, but supports completion and description through custom data formats | ||
- `,` or `*` need to be exported in order to use `,` directly | ||
- Directly execute `,` to create a new template file or edit an existing file | ||
- Custom tasks are written in `$env.comma` and can be nested | ||
- Generate completions based on the structure of `$env.comma` | ||
- You can use closure to customize completion | ||
- In `$env.commax.act` of default closure, you can receive parameters after the current position | ||
- In `$env.commax.cmp` , you can receive the parameter before the current position | ||
|
||
example: | ||
``` | ||
$env.commav = { | ||
} | ||
$env.comma = { | ||
created: { '2023-12-20[3]16:02:56' } | ||
hello: { | ||
$env.commax.act: {|x| print $'hello ($x)' } | ||
$env.commax.dsc: 'hello (x)' | ||
$env.commax.cmp: {|args| $args} | ||
} | ||
open: { | ||
$env.commax.sub: { | ||
any: { | ||
$env.commax.act: {|x| open $x.0} | ||
$env.commax.cmp: {ls | get name} | ||
$env.commax.dsc: 'open a file' | ||
} | ||
json: { | ||
$env.commax.act: {|x| open $x.0} | ||
$env.commax.cmp: {ls *.json | get name} | ||
$env.commax.dsc: 'open a json file' | ||
} | ||
} | ||
$env.commax.dsc: 'open a file' | ||
} | ||
# Nest as you like | ||
a: { | ||
b: { | ||
c: { | ||
$env.commax.act: {|x| print $x } | ||
$env.commax.dsc: 'description' | ||
$env.commax.cmp: {|| ls | get name } | ||
} | ||
d: { pwd } | ||
} | ||
x: { | ||
$env.commax.sub: { | ||
y: { | ||
$env.commax.act: {|x| print y} | ||
$env.commax.cmp: {|| [y1 y2 y3]} | ||
$env.commax.dsc: 'description' | ||
} | ||
} | ||
$env.commax.dsc: 'xxx' | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
def unindent [] { | ||
let txt = $in | lines | range 1.. | ||
let indent = $txt.0 | parse --regex '^(?P<indent>\s*)' | get indent.0 | str length | ||
$txt | ||
| each {|s| $s | str substring $indent.. } | ||
| str join (char newline) | ||
} | ||
|
||
def 'path parents' [] { | ||
$in | ||
| path expand | ||
| path split | ||
| reduce -f [ '' ] {|x, acc| [( $acc.0 | path join $x ), ...$acc] } | ||
| range ..-2 | ||
} | ||
|
||
def find [] { | ||
$in | ||
| path parents | ||
| filter {|x| $x | path join ',.nu' | path exists } | ||
| get 0? | ||
} | ||
|
||
def comma_file [] { | ||
[ | ||
{ | ||
condition: {|_, after| not ($after | path join ',.nu' | path exists)} | ||
code: "$env.comma = null" | ||
} | ||
{ | ||
condition: {|_, after| $after | path join ',.nu' | path exists} | ||
code: " | ||
print $'(ansi default_underline)(ansi default_bold),(ansi reset).nu (ansi green_italic)detected(ansi reset)...' | ||
print $'(ansi yellow_italic)activating(ansi reset) (ansi default_underline)(ansi default_bold),(ansi reset) module with `(ansi default_dimmed)(ansi default_italic)source ,.nu(ansi reset)`' | ||
source ,.nu | ||
" | ||
} | ||
] | ||
} | ||
|
||
export-env { | ||
$env.config = ( $env.config | upsert hooks.env_change.PWD { |config| | ||
let o = ($config | get -i hooks.env_change.PWD) | ||
let val = (comma_file) | ||
if $o == null { | ||
$val | ||
} else { | ||
$o | append $val | ||
} | ||
}) | ||
let k = (gensym) | ||
$env.commax = { | ||
sub: $k.0 | ||
dsc: $k.1 | ||
act: $k.2 | ||
cmp: $k.3 | ||
} | ||
} | ||
|
||
def gensym [] { | ||
mut r = [] | ||
let rk = random chars | ||
for i in 1..4 { | ||
let b = ($i - 1) * 5 | ||
let e = $i * 5 | ||
$r ++= [($rk | str substring $b..$e)] | ||
} | ||
$r | ||
# TODO: debug | ||
# [sub dsc act cmp] | ||
} | ||
|
||
def log [tag? -c] { | ||
let o = $in | ||
if ($c) { | ||
echo $'---(char newline)' | save -f ~/.cache/comma.log | ||
} else { | ||
echo $'---($tag)---($o | describe)(char newline)($o | to yaml)' | save -a ~/.cache/comma.log | ||
} | ||
$o | ||
} | ||
|
||
def 'as act' [] { | ||
let o = $in | ||
let ix = $env.commax | ||
let t = ($o | describe -d).type | ||
if $t == 'closure' { | ||
{ $ix.act: $o } | ||
} else if ($ix.sub in $o) { | ||
null | ||
} else if ($ix.act in $o) { | ||
$o | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
def run [tbl] { | ||
let loc = $in | ||
let ix = $env.commax | ||
mut act = $tbl | ||
mut arg = [] | ||
for i in $loc { | ||
let a = $act | as act | ||
if ($a | is-empty) { | ||
if ($ix.sub in $act) and ($i in ($act | get $ix.sub)) { | ||
let n = $act | get $ix.sub | get $i | ||
$act = $n | ||
} else if $i in $act { | ||
let n = $act | get $i | ||
$act = $n | ||
} else { | ||
$act = {|| print $"not found `($i)`"} | ||
break | ||
} | ||
} else { | ||
$arg ++= [$i] | ||
} | ||
} | ||
let a = $act | as act | ||
if ($a | is-empty) { | ||
let c = if $ix.sub in $act { $act | get $ix.sub | columns } else { $act | columns } | ||
print $'require argument: ($c)' | ||
} else { | ||
do ($a | get $ix.act) $arg | ||
} | ||
} | ||
|
||
def complete [tbl] { | ||
let argv = $in | ||
let ix = $env.commax | ||
mut tbl = $env.comma | ||
for i in $argv { | ||
let c = if ($i | is-empty) { | ||
$tbl | ||
} else { | ||
let tp = ($tbl | describe -d).type | ||
if ($tp == 'record') and ($i in $tbl) { | ||
let j = $tbl | get $i | ||
if $ix.sub in $j { | ||
$j | get $ix.sub | ||
} else { | ||
$j | ||
} | ||
} else { | ||
$tbl | ||
} | ||
} | ||
let a = $c | as act | ||
if not ($a | is-empty) { | ||
let r = do ($a | get $ix.cmp) $argv | ||
$tbl = $r | ||
} else { | ||
$tbl = $c | ||
} | ||
} | ||
match ($tbl | describe -d).type { | ||
record => { | ||
$tbl | ||
| transpose k v | ||
| each {|x| | ||
if ($x.v | describe -d).type == 'closure' { | ||
$x.k | ||
} else if $ix.dsc in $x.v { | ||
{ value: $x.k, description: ($x.v | get $ix.dsc) } | ||
} else { | ||
$x.k | ||
} | ||
} | ||
} | ||
list => { $tbl } | ||
_ => { $tbl } | ||
} | ||
} | ||
|
||
def 'parse argv' [] { | ||
let context = $in | ||
$context.0 | ||
| str substring 0..$context.1 | ||
| split row -r '\s+' | ||
| range 1.. | ||
| where not ($it | str starts-with '-') | ||
} | ||
|
||
def compos [...context] { | ||
$context | ||
| parse argv | ||
| complete $env.comma | ||
} | ||
|
||
export def , [...args:string@compos] { | ||
if ($args | is-empty) { | ||
if ([$env.PWD, ',.nu'] | path join | path exists) { | ||
^$env.EDITOR ,.nu | ||
} else { | ||
let a = [yes no] | input list 'create ,.nu?' | ||
if $a == 'yes' { | ||
$" | ||
$env.commav = { | ||
} | ||
$env.comma = { | ||
created: { '(date now | format date '%Y-%m-%d[%w]%H:%M:%S')' } | ||
hello: { | ||
$env.commax.act: {|x| print $'hello \($x\)' } | ||
$env.commax.dsc: 'hello \(x\)' | ||
$env.commax.cmp: {|args| $args} | ||
} | ||
open: { | ||
$env.commax.sub: { | ||
any: { | ||
$env.commax.act: {|x| open $x.0} | ||
$env.commax.cmp: {ls | get name} | ||
$env.commax.dsc: 'open a file' | ||
} | ||
json: { | ||
$env.commax.act: {|x| open $x.0} | ||
$env.commax.cmp: {ls *.json | get name} | ||
$env.commax.dsc: 'open a json file' | ||
} | ||
} | ||
$env.commax.dsc: 'open a file' | ||
} | ||
} | ||
" | ||
| unindent | ||
| save $",.nu" | ||
#source ',.nu' | ||
} | ||
} | ||
} else { | ||
$args | run $env.comma | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.