Skip to content

Commit

Permalink
cwdhist: alternative to zoxide (nushell#656)
Browse files Browse the repository at this point in the history
- cwdhist: alternative to zoxide
   - just-completions: remove `--unstable`
   - kubernetes: fix desc of kdh

---------

Co-authored-by: nash <[email protected]>
  • Loading branch information
fj0r and nashvi committed Nov 5, 2023
1 parent c2bb125 commit e4ae994
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 3 deletions.
4 changes: 2 additions & 2 deletions custom-completions/just/just-completions.nu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def "nu-complete just recipes" [] {
^just --unstable --unsorted --dump --dump-format json
^just --unsorted --dump --dump-format json
| from json
| get recipes
| transpose k v
Expand All @@ -22,7 +22,7 @@ def "nu-complete just recipes" [] {

def "nu-complete just args" [context: string, offset: int] {
let r = ($context | split row ' ')
^just --unstable -u --dump --dump-format json
^just -u --dump --dump-format json
| from json
| get recipes
| get ($r.1)
Expand Down
107 changes: 107 additions & 0 deletions modules/cwdhist/cwdhist.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
def __cwdhist_menu [] {
{
name: cwdhist_menu
only_buffer_difference: false
marker: "| "
type: {
layout: columnar
page_size: 20
}
style: {
text: green
selected_text: green_reverse
description_text: yellow
}
source: { |buffer, position|
#$"[($position)]($buffer);(char newline)" | save -a ~/.cache/cwdhist.log
let cwd = if ($buffer | is-empty) {
""
} else {
$buffer
}
if 'cwd_history_full' in $env {
open $nu.history-path | query db $"
select cwd as value, count\(*) as description
from history
where cwd like '%($cwd)%'
group by cwd
order by description desc
limit 20
;"
} else {
open $env.cwd_history_file | query db $"
select cwd as value, count as description
from cwd_history
where cwd like '%($cwd)%'
order by count desc
limit 20
;"
}
| append { value: '~' }
}
}
}

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

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


export-env {
$env.cwd_history_file = '~/.cache/nu_cwd_history.sqlite'

if not ($env.cwd_history_file | path exists) {
"create table if not exists cwd_history (
cwd text primary key,
count int default 1,
recent datetime default (datetime('now', 'localtime'))
);"
| sqlite3 ~/.cache/nu_cwd_history.sqlite
}

let __cwdhist_hook = {|_, dir|
if $dir == $nu.home-path { return }
let suffix = (do --ignore-errors { $dir | path relative-to $nu.home-path })
let path = if ($suffix | is-empty) {
$dir
} else {
['~', $suffix] | path join
}
open $env.cwd_history_file
| query db $"
insert into cwd_history\(cwd)
values \('($path)')
on conflict\(cwd)
do update set
count = count + 1,
recent = datetime\('now', 'localtime');"
}

$env.config = ($env.config
| update hooks.env_change.PWD ($env.config.hooks.env_change.PWD | append $__cwdhist_hook ))

$env.config = ($env.config
| upsert menus ($env.config.menus | append (__cwdhist_menu))
| upsert keybindings ($env.config.keybindings | append [(__cwdhist_keybinding) (__edit_keybinding)])
)
}
2 changes: 1 addition & 1 deletion modules/kubernetes/kubernetes.nu
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export def kah [
helm $act $name $chart -f $valuefile $values (spr [-n $namespace])
}

# helm install or upgrade via values file
# helm diff
export def kdh [
name: string@"nu-complete helm list"
chart: string@"nu-complete helm charts"
Expand Down

0 comments on commit e4ae994

Please sign in to comment.