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

refactor left_prompt decorator logic (#469)

- added instructions for measurements
    - change NU_POWER_MODE default to `power` (in fast mode, there is still a problem with hid
eable components on the left)
    - tweaked atuin_stat theme

Co-authored-by: nash <nash@iffy.me>
This commit is contained in:
fj0r 2023-04-29 01:05:06 +08:00 committed by GitHub
parent 17103049f5
commit cfdc7b725c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 23 deletions

View file

@ -7,6 +7,8 @@ use power.nu
power inject 1 2 {source: kube, color: '#504945'} {
context: purple
}
use power_utils.nu
power inject 0 1 {source: atuin, color: '#678067'}
power init
```
or
@ -36,8 +38,29 @@ power init
- `let-env NU_POWER_DECORATOR = '<power|plain>'` power mode and plain mode
- `let-env NU_POWER_FRAME = '<default|fill>'` two line prompt (experimental)
### benchmark
```
let-env NU_POWER_BENCHMARK = true
```
Then execute a few commands casually, such as pressing the Enter key continuously.
then execute
```
let-env NU_POWER_MODE = 'fast' # or 'power'
```
Go ahead and press enter,
Execute power timelog to analyze the results.
```
power timelog
| group-by message
| transpose k v
| each {|x| $x | upsert v ($x.v | get duration | math avg)}
```
## todo
- [x] source return `$nothing` for hiding
- [ ] in fast mode, there is still a problem with hideable components on the left
- [x] proxy stat invalid in plain mode
- '<<' not longer hide separator in `fast` mode
- [ ] implement `power eject`

View file

@ -97,7 +97,7 @@ def logtime [msg act] {
return $result
}
def-env wraptime [message action] {
def wraptime [message action] {
if $env.NU_POWER_BENCHMARK? == true {
{|| logtime $message $action }
} else {
@ -120,7 +120,7 @@ export def timelog [] {
def decorator [ ] {
match $env.NU_POWER_DECORATOR {
'plain' => {
{|s, direction?: string, color?: string = 'light_yellow', fg?: string|
{|s, direction?: string, color?: string = 'light_yellow', next_color?: string|
match $direction {
'>' => {
let r = $'(ansi light_yellow)|'
@ -137,17 +137,15 @@ def decorator [ ] {
}
}
'power' => {
{|s, direction?: string, color?: string = 'light_yellow', fg?: string|
{|s, direction?: string, color?: string = 'light_yellow', next_color?: string|
match $direction {
'>' => {
let l = (ansi -e {bg: $fg})
let r = $'(ansi -e {fg: $fg, bg: $color})(char nf_left_segment)'
$'($l)($s)($r)'
let r = $'(ansi -e {fg: $color, bg: $next_color})(char nf_left_segment)'
$'($s)($r)'
}
'>>' => {
let l = (ansi -e {bg: $fg})
let r = $'(ansi reset)(ansi -e {fg: $fg})(char nf_left_segment)'
$'($l)($s)($r)'
let r = $'(ansi reset)(ansi -e {fg: $color})(char nf_left_segment)'
$'($s)($r)'
}
'<'|'<<' => {
let l = $'(ansi -e {fg: $color})(char nf_right_segment)(ansi -e {bg: $color})'
@ -171,13 +169,14 @@ def left_prompt [segment] {
| filter {|x| $x.1 != $nothing }
)
let stop = ($segment | length) - 1
let cs = ($segment | each {|x| $x.0 })
let cs = ($cs | prepend $cs.1?)
let cs = ($segment | each {|x| $x.0 } | append $segment.0.0 | range 1..)
$segment
| zip $cs
| enumerate
| each {|x|
if $x.index == $stop {
if $x.index == 0 and $env.NU_POWER_DECORATOR == 'power' {
$'(ansi -e {bg: $segment.0.0})(do $decorator $x.item.0.1 '>' $x.item.0.0 $x.item.1)'
} else if $x.index == $stop {
do $decorator $x.item.0.1 '>>' $x.item.0.0 $x.item.1
} else {
do $decorator $x.item.0.1 '>' $x.item.0.0 $x.item.1
@ -212,7 +211,7 @@ def right_prompt [segment] {
def decorator_gen [
direction?: string
color?: string = 'light_yellow'
fg?: string
next_color?: string
] {
match $env.NU_POWER_DECORATOR {
'plain' => {
@ -233,14 +232,12 @@ def decorator_gen [
'power' => {
match $direction {
'>' => {
let l = (ansi -e {bg: $fg})
let r = $'(ansi -e {fg: $fg, bg: $color})(char nf_left_segment)'
{|s| $'($l)($s)($r)' }
let r = $'(ansi -e {fg: $color, bg: $next_color})(char nf_left_segment)'
{|s| $'($s)($r)' }
}
'>>' => {
let l = (ansi -e {bg: $fg})
let r = $'(ansi reset)(ansi -e {fg: $fg})(char nf_left_segment)'
{|s| $'($l)($s)($r)' }
let r = $'(ansi reset)(ansi -e {fg: $color})(char nf_left_segment)'
{|s| $'($s)($r)' }
}
'<'|'<<' => {
let l = $'(ansi -e {fg: $color})(char nf_right_segment)(ansi -e {bg: $color})'
@ -265,13 +262,15 @@ def squash [thunk] {
def left_prompt_gen [segment] {
let stop = ($segment | length) - 1
let vs = ($segment | each {|x| [$x.color ($env.NU_PROMPT_COMPONENTS | get $x.source)]})
let cs = ($vs | each {|x| $x.0})
let cs = ($cs | prepend $cs.1?)
let cs = ($segment | each {|x| $x.color } | append $segment.0.color | range 1..)
let thunk = ($vs
| zip $cs
| enumerate
| each {|x|
if $x.index == $stop {
if $x.index == 0 and $env.NU_POWER_DECORATOR == 'power' {
let o = (decorator_gen '>' $x.item.0.0 $x.item.1)
[$x.item.0.1 {|x| $'(ansi -e {bg: $segment.0.color})(do $o $x)' }]
} else if $x.index == $stop {
[$x.item.0.1 (decorator_gen '>>' $x.item.0.0 $x.item.1)]
} else {
[$x.item.0.1 (decorator_gen '>' $x.item.0.0 $x.item.1)]
@ -451,7 +450,7 @@ export-env {
let-env NU_POWER_MODE = (default_env
NU_POWER_MODE
'fast' # power | fast
'power' # power | fast
)
let-env NU_POWER_SCHEMA = (default_env