Skip to content

Commit

Permalink
fix(windows): escape backslash in nu script & use proper csv (#2710)
Browse files Browse the repository at this point in the history
  • Loading branch information
finalchild authored Oct 6, 2024
1 parent b06878d commit 89c29f3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
9 changes: 5 additions & 4 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,10 @@ yum install -y mise

### Windows

Download the latest release from [GitHub](https://github.com/jdx/mise/releases). Add the binary
to your PATH and edit PATH to include the shims directory (by default:
`%LOCALAPPDATA%\mise\shims`).
Download the latest release from [GitHub](https://github.com/jdx/mise/releases) and add the binary
to your PATH.

If your shell does not support `mise activate`, you would want to edit PATH to include the shims directory (by default: `%LOCALAPPDATA%\mise\shims`).

Note that Windows support is very minimal for now.

Expand Down Expand Up @@ -374,7 +375,7 @@ Install Mise by appending `env.nu` and `config.nu`:
let mise_path = $nu.default-config-dir | path join mise.nu
^mise activate nu | save $mise_path --force
' | save $nu.env-path --append
"\nuse mise.nu" | save $nu.config-path --append
"\nuse ($nu.default-config-dir | path join mise.nu)" | save $nu.config-path --append
```

If you prefer to keep your dotfiles clean you can save it to a different directory then
Expand Down
21 changes: 15 additions & 6 deletions src/shell/nushell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ impl<'a> Display for EnvOp<'a> {
}
}

impl Nushell {
fn escape_csv_value(s: &str) -> String {
if s.contains(['\r', '\n', '"', ',']) {
format!("\"{}\"", s.replace('"', "\"\""))
} else {
s.to_owned()
}
}
}

impl Shell for Nushell {
fn activate(&self, exe: &Path, flags: String) -> String {
let exe = exe.display();
let exe = exe.to_string_lossy().replace('\\', r#"\\"#);

formatdoc! {r#"
export-env {{
Expand All @@ -45,7 +55,7 @@ impl Shell for Nushell {
}}
def "parse vars" [] {{
$in | lines | parse "{{op}},{{name}},{{value}}"
$in | from csv --noheaders --no-infer | rename 'op' 'name' 'value'
}}
export def --env --wrapped main [command?: string, --help, ...rest: string] {{
Expand Down Expand Up @@ -88,9 +98,8 @@ impl Shell for Nushell {
}

fn set_env(&self, k: &str, v: &str) -> String {
let k = shell_escape::unix::escape(k.into());
let v = shell_escape::unix::escape(v.into());
let v = v.replace('\'', "");
let k = Nushell::escape_csv_value(k);
let v = Nushell::escape_csv_value(v);

EnvOp::Set { key: &k, val: &v }.to_string()
}
Expand All @@ -100,7 +109,7 @@ impl Shell for Nushell {
}

fn unset_env(&self, k: &str) -> String {
let k = shell_escape::unix::escape(k.into());
let k = Nushell::escape_csv_value(k);
EnvOp::Hide { key: k.as_ref() }.to_string()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def --env add-hook [field: cell-path new_hook: any] {
}

def "parse vars" [] {
$in | lines | parse "{op},{name},{value}"
$in | from csv --noheaders --no-infer | rename 'op' 'name' 'value'
}

export def --env --wrapped main [command?: string, --help, ...rest: string] {
Expand Down

0 comments on commit 89c29f3

Please sign in to comment.