Skip to content

Commit 9967d24

Browse files
committed
Generated zsh, bash and nushell completions
1 parent 86197e1 commit 9967d24

File tree

4 files changed

+140
-18
lines changed

4 files changed

+140
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pathmarks"
3-
version = "0.1.4"
3+
version = "0.1.5"
44
edition = "2024"
55
description = "Simple path bookmarks for your shell"
66
license = "MIT"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ pathmarks init nushell | save ~/.nu-pathmarks.nu
2121
source ~/.nu-pathmarks.nu
2222
```
2323

24-
This will add commands `t` and `ts` to your shell.
24+
This will add commands `t`, `ts` and `ti` to your shell.
2525
`t` list saved bookmarks, picking one changed directory.
2626
`ts` saves given bookmark.
27+
`ti` interactively prompts the picker.
2728
`t <ARGUMENT>` tries to guess where you want to go. First checks case insensitive for directories. Then fussy finds in saved bookmarks.
2829

2930
You can provide a `--cmd` to specify the command.

src/init.rs

Lines changed: 136 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
use clap::ValueEnum;
22

3-
#[derive(Copy, Clone, ValueEnum)]
3+
#[derive(Copy, Clone, Debug, ValueEnum)]
44
pub enum Shell {
55
Fish,
6-
// Zsh,
7-
// Bash,
8-
// Nushell,
6+
Zsh,
7+
Bash,
8+
Nushell,
99
}
1010

1111
pub fn init(shell: Shell, command: Option<String>) -> String {
12+
let command = command.unwrap_or_else(|| "t".to_string());
1213
match shell {
13-
Shell::Fish => {
14-
let command = command.unwrap_or("t".to_string());
14+
Shell::Fish => fish_init(&command),
15+
Shell::Zsh => zsh_init(&command),
16+
Shell::Bash => bash_init(&command),
17+
Shell::Nushell => nushell_init(&command),
18+
}
19+
}
1520

16-
format!(
17-
r#"function {command}
21+
fn fish_init(command: &str) -> String {
22+
format!(
23+
r#"function {command}
1824
if test (count $argv) -gt 0
1925
cd (pathmarks guess $argv[1])
2026
return
@@ -24,7 +30,6 @@ pub fn init(shell: Shell, command: Option<String>) -> String {
2430
test -n "$p"; and cd "$p"
2531
end
2632
27-
2833
function {command}i
2934
while true
3035
set -l dest (pathmarks pick)
@@ -42,11 +47,127 @@ function {command}i
4247
end
4348
end
4449
45-
4650
alias {command}s "pathmarks save"
47-
complete --no-files --keep-order -c {command} -a "(pathmarks list)""#,
48-
command = command
49-
)
50-
}
51-
}
51+
complete --no-files --keep-order -c {command} -a "(pathmarks list)"
52+
"#
53+
)
54+
}
55+
56+
fn zsh_init(command: &str) -> String {
57+
format!(
58+
r#"{command}() {{
59+
if [[ $# -gt 0 ]]; then
60+
cd "$(pathmarks guess "$1")"
61+
return
62+
fi
63+
local p
64+
p="$(pathmarks pick)"
65+
[[ -n "$p" ]] && cd "$p"
66+
}}
67+
68+
{command}i() {{
69+
while true; do
70+
local dest
71+
dest="$(pathmarks pick)"
72+
local code=$?
73+
if [[ $code -ne 0 || -z "$dest" ]]; then
74+
break
75+
fi
76+
if [[ -d "$dest" ]]; then
77+
cd "$dest"
78+
else
79+
break
80+
fi
81+
done
82+
}}
83+
84+
alias {command}s='pathmarks save'
85+
86+
# Completion: compdef + helper that feeds candidates from `pathmarks list`
87+
_{command}() {{
88+
local -a candidates
89+
candidates=($(pathmarks list 2>/dev/null))
90+
compadd -a candidates
91+
}}
92+
compdef _{command} {command}
93+
"#
94+
)
95+
}
96+
97+
fn bash_init(command: &str) -> String {
98+
format!(
99+
r#"{command}() {{
100+
if [[ $# -gt 0 ]]; then
101+
cd "$(pathmarks guess "$1")"
102+
return
103+
fi
104+
local p
105+
p="$(pathmarks pick)"
106+
[[ -n "$p" ]] && cd "$p"
107+
}}
108+
109+
{command}i() {{
110+
while true; do
111+
local dest
112+
dest="$(pathmarks pick)"
113+
local code=$?
114+
if [[ $code -ne 0 || -z "$dest" ]]; then
115+
break
116+
fi
117+
if [[ -d "$dest" ]]; then
118+
cd "$dest"
119+
else
120+
break
121+
fi
122+
done
123+
}}
124+
125+
alias {command}s='pathmarks save'
126+
127+
_{command}_complete() {{
128+
local cur
129+
cur="${{COMP_WORDS[COMP_CWORD]}}"
130+
# Collect candidates from `pathmarks list`
131+
COMPREPLY=($(compgen -W "$(pathmarks list 2>/dev/null)" -- "$cur"))
132+
}}
133+
complete -o filenames -F _{command}_complete {command}
134+
"#
135+
)
136+
}
137+
138+
fn nushell_init(command: &str) -> String {
139+
format!(
140+
r#"export def "nu-complete pathmarks" [] {{
141+
pathmarks list | lines
142+
}}
143+
144+
export def --env {command} [name?: string@"nu-complete pathmarks"] {{
145+
if $name != null {{
146+
cd (pathmarks guess $name)
147+
}} else {{
148+
let p = (pathmarks pick)
149+
if $p != "" {{
150+
cd $p
151+
}}
152+
}}
153+
}}
154+
155+
export def --env {command}i [] {{
156+
loop {{
157+
let dest = (pathmarks pick)
158+
let code = $env.LAST_EXIT_CODE
159+
if $code != 0 or ($dest | is-empty) {{
160+
break
161+
}}
162+
if ($dest | path exists) and (($dest | path type) == "dir") {{
163+
cd $dest
164+
}} else {{
165+
break
166+
}}
167+
}}
168+
}}
169+
170+
export alias {command}s = pathmarks save
171+
"#
172+
)
52173
}

0 commit comments

Comments
 (0)