-
Notifications
You must be signed in to change notification settings - Fork 237
/
bm.nu
127 lines (111 loc) · 2.28 KB
/
bm.nu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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 os_home [] {
if ($nu.os-info.name == "windows" ) {
($env.USERPROFILE)
} else {
($env.HOME)
}
}
def get_path [] {
$env.BM_PATH? |
default (
$env.XDG_DATA_HOME? |
default (
if $nu.os-info.name == windows {
($env.USERPROFILE? | path join "bm")
} else {
($env.HOME? | path join ".local" "share")
}
)
) |
if (not ($in | path exists)) {
mkdir $in
$in
} else {
$in
}|
path join "bookmarks.nuon"
}
def save_path [] {
$in |
update path { str replace (os_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
}