Skip to content

Commit

Permalink
home: common: nushell: Resolved various issues in jump.nu.
Browse files Browse the repository at this point in the history
  - Typo of jump_bookmarks in one location
  - fuzzy picking jump mark or bookmark and hitting escape caused a cd to ~
  - add_bookmark/remove_bookmark would undo changes made in other shells
  - Output of fzf not properly being split after selection
  - Removed unused second parameter in add_bookmark
  - Inlined sanitize_location into the only place it was used
  • Loading branch information
aftix committed Oct 3, 2024
1 parent e67fafd commit 107ee7f
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 deletions home/common/_external.nushell/jump.nu
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export def jump_pick [] {
$to = ($jumps_explicit | append $jumps
| uniq
| to csv --noheaders --separator ' '
| fzf -i --border --header Jumps -n '1,3..'
| fzf -i --border --header Jumps
| split row --number 2 ' '
| get $.1
)
}
if $to == '.' { return }
Expand All @@ -98,7 +100,11 @@ export def jump_pick [] {

# Jumps destructively to a fuzzy picked jump point
export def --env jump_to_pick [] {
try { jump_pick | jump_to $in } | ignore
let to = jump_pick
if $to == null {
return
}
try { jump_to $to } | ignore
}

# Marks pwd explicity as a jump point
Expand All @@ -121,7 +127,7 @@ export def jump_unmark [] {
######### BOOKMARKS ########
# bookmarks - kept in $XDG_CONFIG_HOME/bookmarks, named list of directories managed by this module
# bookmarks can be added with add_bookmark <name> to add the cwd (will check for conflicts and prompt for overwriting previous values)
# bookmarks can be removed with remove_bookmark <name> (with tab completion)
# bookmarks can be removed with remove_bookmark (with fuzzy completion)
# Alt-x brings up a fuzzy finder to jump to a bookmark
# Shift-alt-x lets you insert a bookmark path at the cursor position in the shell buffer

Expand All @@ -132,16 +138,8 @@ def parse_bmarks [] {
)
}


def sanitize_location [] {
(str replace --all $env.XDG_CONFIG_HOME '$XDG_CONFIG_HOME'
| str replace --all $env.XDG_DATA_HOME '$XDG_DATA_HOME'
| str replace --all $env.XDG_CACHE_HOME '$XDG_CACHE_HOME'
| str replace --all $env.HOME '$HOME'
)
}

export def add_bookmark [name @rest] {
export def add_bookmark [name] {
sync_bmarks
let bmarks = (stor open | query db 'SELECT * FROM jump_bookmarks')
let conflicting_name = ($bmarks | each {
(str upcase $name) == (str upcase $in.name)
Expand All @@ -162,23 +160,31 @@ export def add_bookmark [name @rest] {
}

let bfile = $env.XDG_DATA_HOME ++ '/bookmarks'
stor open | query db 'SELECT * FROM jump_bookmarks' | to csv --noheaders --separator ' ' | sanitize_location | save --force $bfile
(stor open
| query db 'SELECT * FROM jump_bookmarks' | to csv --noheaders --separator ' '
| str replace --all $env.XDG_CONFIG_HOME '$XDG_CONFIG_HOME'
| str replace --all $env.XDG_DATA_HOME '$XDG_DATA_HOME'
| str replace --all $env.XDG_CACHE_HOME '$XDG_CACHE_HOME'
| str replace --all $env.HOME '$HOME'
| save --force $bfile
)
}

export def remove_bookmark [] {
sync_bmarks
let bmarks = (stor open | query db 'SELECT * FROM jump_bookmarks')

mut name = '.'
try {
$name = ($bmarks | to csv --noheaders --separator ' ' | fzf --border -i)
$name = ($bmarks | to csv --noheaders --separator ' ' | fzf --border -i --header Bookmarks | split row --number 2 ' ' | get $.0)
}
if $name == '.' {
return
}

let bfile = $env.XDG_DATA_HOME ++ '/bookmarks'
stor delete --table-name jump_bookmarks --where-clause $"name == '($name)'"
stor open | query db 'SELECT * from jump_bookmarkl' | to csv --noheaders --separator ' ' | save --force $bfile
stor delete --table-name jump_bookmarks --where-clause $"name = '($name)'"
stor open | query db 'SELECT * from jump_bookmarks' | to csv --noheaders --separator ' ' | save --force $bfile
}

export def get_bookmark [] {
Expand All @@ -189,7 +195,9 @@ export def get_bookmark [] {
| sort-by name
| uniq-by name
| to csv --noheaders --separator ' '
| fzf -i --border --header Bookmarks -n '1,3..'
| fzf -i --border --header Bookmarks
| split row --number 2 ' '
| get $.1
)
}
if $location == '.' { return }
Expand All @@ -203,17 +211,14 @@ export def get_bookmark [] {
}

export def --env goto_bookmark [] {
try {get_bookmark | jump_to $in} | ignore
let to = get_bookmark
if $to == null {
return
}
try {jump_to $to} | ignore
}

export def --env jump_init [] {
try { stor delete --table-name jumps } catch { } | ignore
stor create --table-name jumps --columns {idx: int location: str current: bool} | ignore
try { stor delete --table-name jumps_explicit } catch { } | ignore
stor create --table-name jumps_explicit --columns {location: str} | ignore

stor insert --table-name jumps --data-record {idx: 0 location: (pwd) current: true} | ignore

export def sync_bmarks [] {
try { stor delete --table-name jump_bookmarks } catch { } | ignore
stor create --table-name jump_bookmarks --columns {name: str location: str} | ignore

Expand All @@ -222,6 +227,17 @@ export def --env jump_init [] {
| each {stor insert --table-name jump_bookmarks}
| ignore
)
}

export def --env jump_init [] {
try { stor delete --table-name jumps } catch { } | ignore
stor create --table-name jumps --columns {idx: int location: str current: bool} | ignore
try { stor delete --table-name jumps_explicit } catch { } | ignore
stor create --table-name jumps_explicit --columns {location: str} | ignore

stor insert --table-name jumps --data-record {idx: 0 location: (pwd) current: true} | ignore

sync_bmarks

# Module keybindings
let jump_keybindings = [
Expand Down

0 comments on commit 107ee7f

Please sign in to comment.