Skip to content

Commit 7df4159

Browse files
authored
Added bookmark module (nushell#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
1 parent a7bde3a commit 7df4159

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

modules/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ An extensive example of a wrapper for docker operations, with nushell completion
6161

6262
## filesystem
6363

64-
- [cdpath](./filesystem/cdpath.nu) - ???
65-
- [up](./filesystem/up.nu) - Cd up `X` times
64+
- [bm](./filesystem/bm.nu) - A Simple bookmarking module. It uses `XGD_DATA_HOME` to save bookmarks.
6665

6766
## formats
6867
Examples of input/output formatters:

modules/filesystem/bm.nu

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# simple bookmark module
2+
3+
# Prints general information about bm.
4+
export def main [] {
5+
print -n (help bm)
6+
7+
print (
8+
[
9+
$"(ansi green)Environment(ansi reset):"
10+
$" (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)"
11+
] |
12+
str join "\n" |
13+
nu-highlight
14+
)
15+
}
16+
17+
# List all bookmarked paths
18+
export def list [] {
19+
let bm_path = (get_path)
20+
21+
if (not ($bm_path | path exists)) {
22+
[] | save $bm_path
23+
}
24+
open ($bm_path)
25+
}
26+
27+
def get_path [] {
28+
$env.BM_PATH? |
29+
default (
30+
$env.XDG_DATA_HOME? |
31+
default (
32+
$env.HOME | path join ".local" "share" |
33+
default (
34+
$env.USERPROFILE? | path join "bm"
35+
)
36+
)
37+
) |
38+
if (not ($in | path exists)) {
39+
mkdir $in
40+
$in
41+
} |
42+
path join "bookmarks.nuon"
43+
)
44+
}
45+
46+
def save_path [] {
47+
$in |
48+
update path { str replace $env.HOME '~' } |
49+
save -f (get_path)
50+
}
51+
52+
# Reset the bookmarks
53+
export def reset [] {
54+
list |
55+
where name == "prev" |
56+
save -f (get_path)
57+
}
58+
59+
# Add a new bookmark with an optional name
60+
export def add [
61+
pth: path # Path to bookmark to.
62+
name?: string # Optional name to give to it
63+
] {
64+
if (($pth | path type) == "dir") and ($pth | path exists) {
65+
list |
66+
append {name: $name, path: $pth} |
67+
save_path
68+
}
69+
}
70+
71+
# remove one or more bookmarks
72+
export def remove [] {
73+
let rm_these = (
74+
list |
75+
where name != "prev" |
76+
input list -m
77+
)
78+
79+
list | where {|it|
80+
not $it in $rm_these
81+
} |
82+
print
83+
84+
}
85+
86+
def marks [] {
87+
list | each {|it|
88+
{
89+
value: $it.path,
90+
description: $it.name
91+
}
92+
}
93+
}
94+
95+
# Goto your bookmark
96+
export def-env goto [
97+
pth: path@marks # Path to "go to"
98+
] {
99+
let prev = $env.PWD
100+
cd $pth
101+
change_prev $prev
102+
}
103+
104+
# Experimental use of `input` instead of completion
105+
export def-env goto_alternative [] {
106+
let prev = $env.PWD
107+
list | input list -f | cd $in.path
108+
change_prev $prev
109+
}
110+
111+
def change_prev [new_path: path] {
112+
( list |
113+
where name != "prev"
114+
) |
115+
append {name: prev, path: $new_path} |
116+
save_path
117+
}

0 commit comments

Comments
 (0)