Skip to content

Commit

Permalink
Split into smaller modules
Browse files Browse the repository at this point in the history
Remove fast mode.
  • Loading branch information
nashvi committed Sep 15, 2024
1 parent d04eea6 commit 7a8deaa
Show file tree
Hide file tree
Showing 7 changed files with 814 additions and 29 deletions.
66 changes: 37 additions & 29 deletions modules/prompt/powerline/README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,59 @@
### configuration
```
use power.nu
use power_git.nu
use power/power.nu
use power/plugin/git.nu *
power inject 0 1 {source: git, color: '#504945'}
use power_kube.nu
use power/plugin/kube.nu *
power inject 1 2 {source: kube, color: '#504945'} {
context: cyan
} {
reverse: true
separator: '@'
theme: {
context: cyan
}
config: {
reverse: true
separator: '@'
}
}
power set time {
config: { style: compact }
}
use power_utils.nu
power inject 0 1 {source: atuin, color: '#4C4B4A'}
power set time null { style: compact }
power init
```
or
```
use power
use power/plugin/git.nu *
use power/plugin/kube.nu *
$env.NU_POWER_SCHEMA = [
[
{source: pwd, color: '#353230'}
{source: git, color: '#504945'}
]
[source, color];
[pwd, xterm_grey23]
[git, xterm_grey30]
],
[
{source: proxy, color: 'dark_gray'}
{source: host, color: '#353230'}
{source: kube, color: '#504945'}
{source: time, color: '#666560'}
[source, color];
[proxy, xterm_grey39]
[host, xterm_grey30]
[kube, xterm_grey23]
[time, xterm_grey27]
]
]
use power.nu
use power_git.nu
use power_kube.nu
power set time {
config: { style: compact }
}
power set kube {
theme: {
context: cyan
}
config: {
reverse: true
separator: '@'
}
}
power init
```
`$env.NU_POWER_SCHEMA` support configuring dynamically

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

Expand All @@ -49,21 +64,14 @@ $env.NU_POWER_BENCHMARK = true
Then execute a few commands casually, such as pressing the Enter key continuously.
then execute

```
$env.NU_POWER_MODE = 'fast' # or 'power'
```

Go ahead and press enter,
Execute power timelog to analyze the results.
```
power analyze
```

## todo
- [x] source return `null` 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`
- [ ] `$env.config.menus[].maker` can be restored
- [x] support color theme
Expand Down
30 changes: 30 additions & 0 deletions modules/prompt/powerline/lib/profile.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export def logtime [msg act] {
let start = date now
let result = do $act
# HACK: serialization
let period = (date now) - $start | format duration ns | str replace ' ' ''

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

$result
}

export def timelog [] {
open ~/.cache/nushell/power_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)
}
}

export def analyze [] {
timelog
| group-by message
| transpose component metrics
| each {|x| $x | upsert metrics ($x.metrics | get duration | math avg)}
}

63 changes: 63 additions & 0 deletions modules/prompt/powerline/lib/pwd.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
def 'path parents' [] {
let p = $in | path expand | path split
mut r = []
for i in ($p | length | $in - 1)..0 {
$r ++= { a: ($p | range 0..$i), b: ($p | range ($i + 1)..) }
}
$r
}

def 'find vcs' [] {
for i in ($in | path parents) {
if ($i.a | append '.git' | path join | path exists) {
return $i
}
}
}

export def "pwd_abbr" [] {
{|bg|
let vcso = $env.PWD | find vcs
let no_vcs = $vcso | is-empty
let vcs = if $no_vcs {
[$env.PWD]
} else {
[($vcso.a | path join), ($vcso.b | path join)]
}
let relative = do -i { $vcs.0 | path relative-to $env.HOME }
let in_home = ($relative | describe -d).type == 'string'

let cwd = if $in_home {
if ($relative | is-empty) { '~' } else { $'~(char separator)($relative)' }
} else {
$vcs.0
}

mut dir_comp = ($cwd | path split)

if ($dir_comp | length) + ($vcso.b? | default 0 | length) > 5 {
let first = $dir_comp | first
let last = $dir_comp | last
let body = $dir_comp
|range 1..-2
|each {|x| $x | str substring ..<2 }
$dir_comp = ([$first $body $last] | flatten)
}
let dir_comp = $dir_comp | path join

let theme = $env.NU_POWER_THEME.pwd

let tm = if $in_home { $theme.default } else { $theme.out_home }
let fg = if $no_vcs {
$"($tm)($dir_comp)"
} else {
if ($vcs.1 | is-empty) {
$"($tm)($dir_comp)"
} else {
[$"($tm)($dir_comp)($theme.vcs)" $"($vcs.1)"]
| path join
}
}
[$bg $fg]
}
}
Loading

0 comments on commit 7a8deaa

Please sign in to comment.