mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-02 07:07:46 +00:00
Added bookmark module (#509)
* initial commit * updated (module) README to include `bm` and removed missing `cdpath` and `up` * added comments to commands * removed missing function * updated to use `XDG_DATA_HOME` * added remove command * updated def and def-env commands * updated change_prev to use list * updated README to have some info on use * updated `get_path` to search `BM_PATH` > `XDG_DATA_HOME` > `~/.local/share` * Updated to use relative paths, added `save_path` function * added `main` with general information. * Updated help text to give more information. * Added Win support * Added if dir doesn't exist, it creates it
This commit is contained in:
parent
a7bde3acf2
commit
7df415984f
2 changed files with 118 additions and 2 deletions
|
@ -61,8 +61,7 @@ An extensive example of a wrapper for docker operations, with nushell completion
|
||||||
|
|
||||||
## filesystem
|
## filesystem
|
||||||
|
|
||||||
- [cdpath](./filesystem/cdpath.nu) - ???
|
- [bm](./filesystem/bm.nu) - A Simple bookmarking module. It uses `XGD_DATA_HOME` to save bookmarks.
|
||||||
- [up](./filesystem/up.nu) - Cd up `X` times
|
|
||||||
|
|
||||||
## formats
|
## formats
|
||||||
Examples of input/output formatters:
|
Examples of input/output formatters:
|
||||||
|
|
117
modules/filesystem/bm.nu
Normal file
117
modules/filesystem/bm.nu
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
# simple bookmark module
|
||||||
|
|
||||||
|
# Prints general information about bm.
|
||||||
|
export def main [] {
|
||||||
|
print -n (help bm)
|
||||||
|
|
||||||
|
print (
|
||||||
|
[
|
||||||
|
$"(ansi green)Environment(ansi reset):"
|
||||||
|
$" (ansi cyan)BM_PATH(ansi reset) - path to save bookmarks to with ('add' | nu-highlight). Alternatively searches for (ansi cyan)XDG_DATA_HOME(ansi reset) or (ansi cyan)~/.local/share/(ansi reset)"
|
||||||
|
] |
|
||||||
|
str join "\n" |
|
||||||
|
nu-highlight
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
# List all bookmarked paths
|
||||||
|
export def list [] {
|
||||||
|
let bm_path = (get_path)
|
||||||
|
|
||||||
|
if (not ($bm_path | path exists)) {
|
||||||
|
[] | save $bm_path
|
||||||
|
}
|
||||||
|
open ($bm_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_path [] {
|
||||||
|
$env.BM_PATH? |
|
||||||
|
default (
|
||||||
|
$env.XDG_DATA_HOME? |
|
||||||
|
default (
|
||||||
|
$env.HOME | path join ".local" "share" |
|
||||||
|
default (
|
||||||
|
$env.USERPROFILE? | path join "bm"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
) |
|
||||||
|
if (not ($in | path exists)) {
|
||||||
|
mkdir $in
|
||||||
|
$in
|
||||||
|
} |
|
||||||
|
path join "bookmarks.nuon"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
def save_path [] {
|
||||||
|
$in |
|
||||||
|
update path { str replace $env.HOME '~' } |
|
||||||
|
save -f (get_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Reset the bookmarks
|
||||||
|
export def reset [] {
|
||||||
|
list |
|
||||||
|
where name == "prev" |
|
||||||
|
save -f (get_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add a new bookmark with an optional name
|
||||||
|
export def add [
|
||||||
|
pth: path # Path to bookmark to.
|
||||||
|
name?: string # Optional name to give to it
|
||||||
|
] {
|
||||||
|
if (($pth | path type) == "dir") and ($pth | path exists) {
|
||||||
|
list |
|
||||||
|
append {name: $name, path: $pth} |
|
||||||
|
save_path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# remove one or more bookmarks
|
||||||
|
export def remove [] {
|
||||||
|
let rm_these = (
|
||||||
|
list |
|
||||||
|
where name != "prev" |
|
||||||
|
input list -m
|
||||||
|
)
|
||||||
|
|
||||||
|
list | where {|it|
|
||||||
|
not $it in $rm_these
|
||||||
|
} |
|
||||||
|
print
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
def marks [] {
|
||||||
|
list | each {|it|
|
||||||
|
{
|
||||||
|
value: $it.path,
|
||||||
|
description: $it.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Goto your bookmark
|
||||||
|
export def-env goto [
|
||||||
|
pth: path@marks # Path to "go to"
|
||||||
|
] {
|
||||||
|
let prev = $env.PWD
|
||||||
|
cd $pth
|
||||||
|
change_prev $prev
|
||||||
|
}
|
||||||
|
|
||||||
|
# Experimental use of `input` instead of completion
|
||||||
|
export def-env goto_alternative [] {
|
||||||
|
let prev = $env.PWD
|
||||||
|
list | input list -f | cd $in.path
|
||||||
|
change_prev $prev
|
||||||
|
}
|
||||||
|
|
||||||
|
def change_prev [new_path: path] {
|
||||||
|
( list |
|
||||||
|
where name != "prev"
|
||||||
|
) |
|
||||||
|
append {name: prev, path: $new_path} |
|
||||||
|
save_path
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue