Skip to content

Commit de20328

Browse files
authored
Merge pull request #67 from renatolond/implement_fish
Implement fish as a supported shell
2 parents 2c136b8 + 0e16251 commit de20328

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ eval "$(rv shell init zsh)"
2727
# bash
2828
echo 'eval "$(rv shell init bash)"' >> ~/.bashrc
2929
eval "$(rv shell init bash)"
30+
# fish
31+
echo 'rv shell init fish | source' >> ~/.config/fish/config.fish
32+
rv shell init fish | source
3033
```
3134

3235
## Usage

crates/rv/src/commands/shell.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ pub struct ShellArgs {
1414
pub enum ShellCommand {
1515
#[command(about = "Configure your shell to use rv")]
1616
Init {
17-
/// The shell to initialize (only zsh and bash so far)
17+
/// The shell to initialize (zsh, bash and fish so far)
1818
shell: Shell,
1919
},
2020
#[command(hide = true)]
2121
Env {
22-
/// The shell to configure (only zsh and bash so far)
22+
/// The shell to configure (zsh, bash and fish so far)
2323
shell: Shell,
2424
},
2525
}
@@ -29,4 +29,5 @@ pub enum Shell {
2929
#[default]
3030
Zsh,
3131
Bash,
32+
Fish,
3233
}

crates/rv/src/commands/shell/env.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,28 @@ pub fn env(config: &config::Config, shell: Shell) -> Result<()> {
3030
println!("hash -r");
3131
Ok(())
3232
}
33+
Shell::Fish => {
34+
if !unset.is_empty() {
35+
println!("set -ge {}", unset.join(" "))
36+
}
37+
for (var, val) in set {
38+
println!("set -gx {var} \"{}\"", backslack_escape(val))
39+
}
40+
Ok(())
41+
}
42+
}
43+
}
44+
45+
// From uv's crates/uv-shell/src/lib.rs
46+
// Assumes strings will be outputed as "str", so escapes any \ or " character
47+
fn backslack_escape(s: String) -> String {
48+
let mut escaped = String::with_capacity(s.len());
49+
for c in s.chars() {
50+
match c {
51+
'\\' | '"' => escaped.push('\\'),
52+
_ => {}
53+
}
54+
escaped.push(c)
3355
}
56+
escaped
3457
}

crates/rv/src/commands/shell/init.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,17 @@ pub fn init(config: &Config, shell: Shell) -> Result<()> {
4545
);
4646
Ok(())
4747
}
48+
Shell::Fish => {
49+
print!(
50+
concat!(
51+
"function _rv_autoload_hook --on-variable PWD --description 'Change Ruby version on directory change using rv'\n",
52+
" status --is-command-substitution; and return\n",
53+
" {} shell env fish | source\n",
54+
"end\n"
55+
),
56+
config.current_exe
57+
);
58+
Ok(())
59+
}
4860
}
4961
}

0 commit comments

Comments
 (0)