parse name of positional args in parse cmd (#527)

* parse name of positional args in `parse cmd`

* Fix the problem that git stashes cannot be parsed correctly

* Fix parsing failure not in git directory

---------

Co-authored-by: agent <agent@nuc>
This commit is contained in:
fj0r 2023-06-09 20:58:24 +08:00 committed by GitHub
parent b901e0737e
commit 039930b4e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 28 deletions

View File

@ -36,6 +36,7 @@ def get-sign [cmd] {
let x = ($nu.scope.commands | where name == $cmd).signatures?.0?.any?
mut s = []
mut n = {}
mut p = []
for it in $x {
if $it.parameter_type in ['switch' 'named'] {
let name = $it.parameter_name
@ -48,9 +49,11 @@ def get-sign [cmd] {
$s = ($s | append $it.short_flag)
}
}
} else if $it.parameter_type == 'positional' {
$p = ($p | append $it.parameter_name)
}
}
{ switch: $s, name: $n }
{ switch: $s, name: $n, positional: $p }
}
def "parse cmd" [] {
@ -85,7 +88,8 @@ def "parse cmd" [] {
$sw = ''
}
}
$opt.args = $pos
$opt._args = $pos
$opt._pos = ( $pos | range 1.. | enumerate | reduce -f {} {|it, acc| $acc | upsert ($sign.positional | get $it.index) $it.item } )
$opt
}
@ -158,8 +162,8 @@ export def gb [
}
if $branch in (remote_braches | each {|x| $x.1}) and (agree -n 'delete remote branch?!') {
let remote = if ($remote|is-empty) { 'origin' } else { $remote }
git push $remote -d $branch
git branch -D -r $'($remote)/($branch)'
git push $remote -d $branch
}
} else if $branch in $bs {
git checkout $branch
@ -491,8 +495,11 @@ export alias gswc = git switch -c
export alias gts = git tag -s
export def _git_status [] {
# TODO: show-stash
let raw_status = (do -i { git --no-optional-locks status --porcelain=2 --branch | lines })
let stashes = (do -i { git stash list | lines | length })
mut status = {
idx_added_staged : 0
idx_modified_staged : 0
@ -508,7 +515,7 @@ export def _git_status [] {
conflicts : 0
ahead : 0
behind : 0
stashes : 0
stashes : $stashes
repo_name : no_repository
tag : no_tag
branch : no_branch
@ -659,7 +666,7 @@ export def remote_braches [] {
def "nu-complete git remote branches" [context: string, offset: int] {
let ctx = ($context | parse cmd)
let rb = (remote_braches)
if ($ctx.args | length) < 3 {
if ($ctx._args | length) < 3 {
$rb | each {|x| {value: $x.1, description: $x.0} }
} else {
$rb | filter {|x| $x.1 == $ctx.1 } | each {|x| $x.0}

View File

@ -2,6 +2,7 @@ def get-sign [cmd] {
let x = ($nu.scope.commands | where name == $cmd).signatures?.0?.any?
mut s = []
mut n = {}
mut p = []
for it in $x {
if $it.parameter_type in ['switch' 'named'] {
let name = $it.parameter_name
@ -14,9 +15,11 @@ def get-sign [cmd] {
$s = ($s | append $it.short_flag)
}
}
} else if $it.parameter_type == 'positional' {
$p = ($p | append $it.parameter_name)
}
}
{ switch: $s, name: $n }
{ switch: $s, name: $n, positional: $p }
}
def "parse cmd" [] {
@ -51,7 +54,8 @@ def "parse cmd" [] {
$sw = ''
}
}
$opt.args = $pos
$opt._args = $pos
$opt._pos = ( $pos | range 1.. | enumerate | reduce -f {} {|it, acc| $acc | upsert ($sign.positional | get $it.index) $it.item } )
$opt
}
@ -187,6 +191,11 @@ def "nu-complete helm list" [context: string, offset: int] {
def "nu-complete helm charts" [context: string, offset: int] {
let ctx = ($context | parse cmd)
let path = ($ctx | get _pos.chart)
let path = if ($path | is-empty) { '.' } else { $path }
let paths = (do -i { ls $"($path)*" | each {|x| if $x.type == dir { $"($x.name)/"} else { $x.name }} })
helm repo list | from ssv -a | rename value description
| append $paths
}
def record-to-set-json [value] {
@ -400,22 +409,22 @@ def "nu-complete kube kind" [] {
def "nu-complete kube res" [context: string, offset: int] {
let ctx = ($context | parse cmd)
let kind = ($ctx | get args.1)
let kind = ($ctx | get _args.1)
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
kubectl get $ns $kind | from ssv -a | get NAME
}
def "nu-complete kube res via name" [context: string, offset: int] {
let ctx = ($context | parse cmd)
let kind = ($env.KUBERNETES_RESOURCE_ABBR | get ($ctx | get args.0 | str substring (-1..)))
let kind = ($env.KUBERNETES_RESOURCE_ABBR | get ($ctx | get _args.0 | str substring (-1..)))
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
kubectl get $ns $kind | from ssv -a | get NAME
}
def "nu-complete kube jsonpath" [context: string] {
let ctx = ($context | parse cmd)
let kind = ($ctx | get args.1)
let res = ($ctx | get args.2)
let kind = ($ctx | get _args.1)
let res = ($ctx | get _args.2)
let path = $ctx.jsonpath?
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
mut r = []
@ -575,7 +584,7 @@ def "nu-complete kube ctns" [context: string, offset: int] {
let ns = (spr [-n $ns])
let ctn = (do -i { $ctx | get container })
let ctn = (spr [-c $ctn])
let pod = ($ctx | get args.1)
let pod = ($ctx | get _args.1)
kubectl get $ns pod $pod -o jsonpath={.spec.containers[*].name} | split row ' '
}
@ -680,9 +689,9 @@ def "nu-complete port forward type" [] {
def "nu-complete kube port" [context: string, offset: int] {
let ctx = ($context | parse cmd)
let kind = ($ctx | get args.1)
let kind = ($ctx | get _args.1)
let ns = if ($ctx.namespace? | is-empty) { [] } else { [-n $ctx.namespace] }
let res = ($ctx | get args.2)
let res = ($ctx | get _args.2)
if ($kind | str starts-with 's') {
kubectl get $ns svc $res --output=jsonpath="{.spec.ports}"
| from json
@ -709,7 +718,7 @@ export def kpf [
def "nu-complete kube cp" [cmd: string, offset: int] {
let ctx = ($cmd | str substring ..$offset | parse cmd)
let p = ($ctx.args | get (($ctx.args | length) - 1))
let p = ($ctx._args | get (($ctx._args | length) - 1))
let ns = (do -i { $ctx | get namespace })
let ns = (spr [-n $ns])
let c = (do -i { $ctx | get container })

View File

@ -1,7 +1,10 @@
### git
export def git_status [] {
def _git_status [] {
# TODO: show-stash
let raw_status = (do -i { git --no-optional-locks status --porcelain=2 --branch | lines })
let stashes = (do -i { git stash list | lines | length })
mut status = {
idx_added_staged : 0
idx_modified_staged : 0
@ -17,7 +20,7 @@ export def git_status [] {
conflicts : 0
ahead : 0
behind : 0
stashes : 0
stashes : $stashes
repo_name : no_repository
tag : no_tag
branch : no_branch
@ -91,18 +94,9 @@ export def git_status [] {
$status
}
export def _git_status [] {
let status = (do -i { gstat })
if not ($status | is-empty) {
$status
} else {
git_status_raw
}
}
export def git_stat [] {
{|bg|
let status = (git_status)
let status = (_git_status)
if $status.branch == 'no_branch' { return [$bg ''] }
@ -134,7 +128,6 @@ export-env {
[
[behind (char branch_behind) yellow]
[ahead (char branch_ahead) yellow]
[stashes = blue]
[conflicts ! red]
[ignored _ purple]
[idx_added_staged + green]
@ -147,6 +140,7 @@ export-env {
[wt_deleted - red]
[wt_renamed % red]
[wt_type_changed * red]
[stashes = blue]
]
)