Skip to content

Commit

Permalink
powerline : NU_POWER_MODE and NU_POWER_BENCHMARK (nushell#464)
Browse files Browse the repository at this point in the history
* powerline : NU_POWER_MODE and NU_POWER_BENCHMARK

* zoxide-menu

* direnv

* dynamic-load

* logtime

---------

Co-authored-by: agent <agent@nuc>
  • Loading branch information
fj0r and agent committed Apr 26, 2023
1 parent 80df447 commit c5865e0
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 38 deletions.
51 changes: 51 additions & 0 deletions custom-menus/zoxide-menu.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
def __zoxide_menu [] {
{
name: zoxide_menu
only_buffer_difference: true
marker: "| "
type: {
layout: columnar
page_size: 20
}
style: {
text: green
selected_text: green_reverse
description_text: yellow
}
source: { |buffer, position|
zoxide query -ls $buffer
| parse -r '(?P<description>[0-9]+) (?P<value>.+)'
}
}
}

def __zoxide_keybinding [] {
{
name: zoxide_menu
modifier: control
keycode: char_o
mode: [emacs, vi_normal, vi_insert]
event: [
{ send: menu name: zoxide_menu }
]
}
}

def __edit_keybinding [] {
{
name: edit
modifier: alt
keycode: char_e
mode: [emacs, vi_normal, vi_insert]
event: [
{ send: OpenEditor }
]
}
}

export-env {
let-env config = ($env.config
| upsert menus ($env.config.menus | append (__zoxide_menu))
| upsert keybindings ($env.config.keybindings | append [(__zoxide_keybinding) (__edit_keybinding)])
)
}
32 changes: 32 additions & 0 deletions hooks/direnv/direnv.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def direnv [] {
[
{
condition: {|before, after| ($before != $after) and ($after | path join .env.yaml | path exists) }
code: "
open .env.yaml | load-env
"
}
{
condition: {|before, after| ($before != $after) and ($after | path join '.env' | path exists) }
code: "
open .env
| lines
| parse -r '(?P<k>.+?)=(?P<v>.+)'
| reduce -f {} {|x, acc| $acc | upsert $x.k $x.v}
| load-env
"
}
]
}

export-env {
let-env config = ( $env.config | upsert hooks.env_change.PWD { |config|
let o = ($config | get -i hooks.env_change.PWD)
let val = (direnv)
if $o == $nothing {
$val
} else {
$o | append $val
}
})
}
20 changes: 20 additions & 0 deletions hooks/dynamic-load/dynamic-load.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def dynamic_load [] {
[
{
condition: {|before, after| (not ('.nu' in (overlay list))) and ('~/.nu' | path exists) }
code: "overlay use ~/.nu as .nu"
}
]
}

export-env {
let-env config = ( $env.config | upsert hooks.env_change.PWD { |config|
let o = ($config | get -i hooks.env_change.PWD)
let val = (dynamic_load)
if $o == $nothing {
$val
} else {
$o | append $val
}
})
}
6 changes: 3 additions & 3 deletions modules/after/after.nu
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
def "nu-complete ps" [] {
ps | each {|x| { value: $x.pid, description: $x.name } }
ps -l | each {|x| { value: $"($x.pid)", description: $x.command } }
}

# after <pid> { do something ... }
export def main [pid: string@"nu-complete ps" action ] {
# after { do something ... } <pid>
export def main [action, pid: string@"nu-complete ps"] {
do -i { tail --pid $pid -f /dev/null }
do $action
}
7 changes: 2 additions & 5 deletions modules/git/git.nu
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,18 @@ export def _git_log [v num] {
}
}

# FIXME: number-like
def "nu-complete git log" [] {
git log -n 32 --pretty=%h»¦«%s
| lines
| split column "»¦«" value description
| each {|x| $x | update value $"`($x.value)`"}
| each {|x| $x | update value $"($x.value)"}
}

# FIXME: number-like
def "nu-complete git branches" [] {
git branch
| lines
| filter {|x| not ($x | str starts-with '*')}
| each {|x| $"'($x|str trim)'"}
| each {|x| $"($x|str trim)"}
}

export def gl [
Expand Down Expand Up @@ -181,7 +179,6 @@ export alias gapt = git apply --3way
export alias gb = git branch
export alias gba = git branch -a
export alias gbd = git branch -d
export alias gbda = 'git branch --no-color --merged | command grep -vE "^(\+|\*|\s*($(git_main_branch)|development|develop|devel|dev)\s*$)" | command xargs -n 1 git branch -d'
export alias gbl = git blame -b -w
export alias gbnm = git branch --no-merged
export alias gbr = git branch --remote
Expand Down
2 changes: 1 addition & 1 deletion modules/kubernetes/kubernetes.nu
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ export def "kclean evicted" [] {
| each { |x| kdel pod -n $x.NAMESPACE $x.NAME }
}

### fixme:
### FIXME:
export def "kclean stucked ns" [ns: string] {
kubectl get namespace $ns -o json \
| tr -d "\n"
Expand Down
24 changes: 24 additions & 0 deletions modules/log/log.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def logtime [msg act] {
let start = (date now)
let result = (do $act)
let period = ((date now) - $start
| into duration -c ns
| into string
| str replace ' ' '')

echo $'($start | date format '%Y-%m-%d_%H:%M:%S%z')(char tab)($period)(char tab)($msg)(char newline)'
| save -a ~/.cache/nushell/time.log

return $result
}

export def timelog [] {
open ~/.cache/nushell/time.log
| from tsv -n
| rename start duration message
| each {|x|
$x
| update start ($x.start | into datetime -f '%Y-%m-%d_%H:%M:%S%z')
| update duration ($x.duration | into duration)
}
}
5 changes: 2 additions & 3 deletions modules/prompt/powerline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ power init
`$env.NU_POWER_SCHEMA` support configuring dynamically

## mode
- `let-env NU_POWER_MODE = '<default|fast>'` fast mode and default mode (experimental)
- `let-env NU_POWER_DECORATOR = '<power|plain>'` power mode and plain mode
- `let-env NU_POWER_FRAME = '<default|fill>'` two line prompt (experimental)

## todo
- [x] source return `$nothing` for hiding
- FRAME can't dynamically, or can't optimize performance
- if can't predetermine '<' or '<<', it can't precalculate
- so '<<' not longer hide separator
- [x] proxy stat invalid in plain mode
- '<<' not longer hide separator in `fast` mode
- [ ] implement `power eject`
- [ ] `$env.config.menus[].maker` can be restored
- [x] support color theme
Expand Down
Loading

0 comments on commit c5865e0

Please sign in to comment.