| 1 | use std::ffi::OsString; |
| 2 | use std::str::FromStr; |
| 3 | |
| 4 | use super::EnvCompleter; |
| 5 | |
| 6 | /// Bash completion adapter |
| 7 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 8 | pub struct Bash; |
| 9 | |
| 10 | impl EnvCompleter for Bash { |
| 11 | fn name(&self) -> &'static str { |
| 12 | "bash" |
| 13 | } |
| 14 | fn is(&self, name: &str) -> bool { |
| 15 | name == "bash" |
| 16 | } |
| 17 | fn write_registration( |
| 18 | &self, |
| 19 | var: &str, |
| 20 | name: &str, |
| 21 | bin: &str, |
| 22 | completer: &str, |
| 23 | buf: &mut dyn std::io::Write, |
| 24 | ) -> Result<(), std::io::Error> { |
| 25 | let escaped_name = name.replace('-', "_"); |
| 26 | |
| 27 | let completer = |
| 28 | shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer)); |
| 29 | |
| 30 | let script = r#" |
| 31 | _clap_complete_NAME() { |
| 32 | local IFS=$'\013' |
| 33 | local _CLAP_COMPLETE_INDEX=${COMP_CWORD} |
| 34 | local _CLAP_COMPLETE_COMP_TYPE=${COMP_TYPE} |
| 35 | if compopt +o nospace 2> /dev/null; then |
| 36 | local _CLAP_COMPLETE_SPACE=false |
| 37 | else |
| 38 | local _CLAP_COMPLETE_SPACE=true |
| 39 | fi |
| 40 | local words=("${COMP_WORDS[@]}") |
| 41 | if [[ "${BASH_VERSINFO[0]}" -ge 4 ]]; then |
| 42 | words[COMP_CWORD]="$2" |
| 43 | fi |
| 44 | COMPREPLY=( $( \ |
| 45 | _CLAP_IFS="$IFS" \ |
| 46 | _CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \ |
| 47 | _CLAP_COMPLETE_COMP_TYPE="$_CLAP_COMPLETE_COMP_TYPE" \ |
| 48 | _CLAP_COMPLETE_SPACE="$_CLAP_COMPLETE_SPACE" \ |
| 49 | VAR="bash" \ |
| 50 | "COMPLETER" -- "${words[@]}" \ |
| 51 | ) ) |
| 52 | if [[ $? != 0 ]]; then |
| 53 | unset COMPREPLY |
| 54 | elif [[ $_CLAP_COMPLETE_SPACE == false ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then |
| 55 | compopt -o nospace |
| 56 | fi |
| 57 | } |
| 58 | if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then |
| 59 | complete -o nospace -o bashdefault -o nosort -F _clap_complete_NAME BIN |
| 60 | else |
| 61 | complete -o nospace -o bashdefault -F _clap_complete_NAME BIN |
| 62 | fi |
| 63 | "# |
| 64 | .replace("NAME", &escaped_name) |
| 65 | .replace("BIN", bin) |
| 66 | .replace("COMPLETER", &completer) |
| 67 | .replace("VAR", var); |
| 68 | |
| 69 | writeln!(buf, "{script}")?; |
| 70 | Ok(()) |
| 71 | } |
| 72 | fn write_complete( |
| 73 | &self, |
| 74 | cmd: &mut clap::Command, |
| 75 | args: Vec<OsString>, |
| 76 | current_dir: Option<&std::path::Path>, |
| 77 | buf: &mut dyn std::io::Write, |
| 78 | ) -> Result<(), std::io::Error> { |
| 79 | let index: usize = std::env::var("_CLAP_COMPLETE_INDEX") |
| 80 | .ok() |
| 81 | .and_then(|i| i.parse().ok()) |
| 82 | .unwrap_or_default(); |
| 83 | let _comp_type: CompType = std::env::var("_CLAP_COMPLETE_COMP_TYPE") |
| 84 | .ok() |
| 85 | .and_then(|i| i.parse().ok()) |
| 86 | .unwrap_or_default(); |
| 87 | let _space: Option<bool> = std::env::var("_CLAP_COMPLETE_SPACE") |
| 88 | .ok() |
| 89 | .and_then(|i| i.parse().ok()); |
| 90 | let ifs: Option<String> = std::env::var("_CLAP_IFS").ok().and_then(|i| i.parse().ok()); |
| 91 | let completions = crate::engine::complete(cmd, args, index, current_dir)?; |
| 92 | |
| 93 | for (i, candidate) in completions.iter().enumerate() { |
| 94 | if i != 0 { |
| 95 | write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; |
| 96 | } |
| 97 | write!(buf, "{}", candidate.get_value().to_string_lossy())?; |
| 98 | } |
| 99 | Ok(()) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// Type of completion attempted that caused a completion function to be called |
| 104 | #[derive(Default, Copy, Clone, Debug, PartialEq, Eq)] |
| 105 | #[non_exhaustive] |
| 106 | enum CompType { |
| 107 | /// Normal completion |
| 108 | #[default] |
| 109 | Normal, |
| 110 | /// List completions after successive tabs |
| 111 | Successive, |
| 112 | /// List alternatives on partial word completion |
| 113 | Alternatives, |
| 114 | /// List completions if the word is not unmodified |
| 115 | Unmodified, |
| 116 | /// Menu completion |
| 117 | Menu, |
| 118 | } |
| 119 | |
| 120 | impl FromStr for CompType { |
| 121 | type Err = String; |
| 122 | |
| 123 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 124 | match s { |
| 125 | "9" => Ok(Self::Normal), |
| 126 | "63" => Ok(Self::Successive), |
| 127 | "33" => Ok(Self::Alternatives), |
| 128 | "64" => Ok(Self::Unmodified), |
| 129 | "37" => Ok(Self::Menu), |
| 130 | _ => Err(format!("unsupported COMP_TYPE `{s}`")), |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /// Elvish completion adapter |
| 136 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 137 | pub struct Elvish; |
| 138 | |
| 139 | impl EnvCompleter for Elvish { |
| 140 | fn name(&self) -> &'static str { |
| 141 | "elvish" |
| 142 | } |
| 143 | fn is(&self, name: &str) -> bool { |
| 144 | name == "elvish" |
| 145 | } |
| 146 | fn write_registration( |
| 147 | &self, |
| 148 | var: &str, |
| 149 | _name: &str, |
| 150 | bin: &str, |
| 151 | completer: &str, |
| 152 | buf: &mut dyn std::io::Write, |
| 153 | ) -> Result<(), std::io::Error> { |
| 154 | let bin = shlex::try_quote(bin).unwrap_or(std::borrow::Cow::Borrowed(bin)); |
| 155 | let completer = |
| 156 | shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer)); |
| 157 | |
| 158 | let script = r#" |
| 159 | set edit:completion:arg-completer[BIN] = { |@words| |
| 160 | var index = (count $words) |
| 161 | set index = (- $index 1) |
| 162 | |
| 163 | put (env _CLAP_IFS="\n" _CLAP_COMPLETE_INDEX=(to-string $index) VAR="elvish" COMPLETER -- $@words) | to-lines |
| 164 | } |
| 165 | "# |
| 166 | .replace("COMPLETER", &completer) |
| 167 | .replace("BIN", &bin) |
| 168 | .replace("VAR", var); |
| 169 | |
| 170 | writeln!(buf, "{script}")?; |
| 171 | Ok(()) |
| 172 | } |
| 173 | fn write_complete( |
| 174 | &self, |
| 175 | cmd: &mut clap::Command, |
| 176 | args: Vec<OsString>, |
| 177 | current_dir: Option<&std::path::Path>, |
| 178 | buf: &mut dyn std::io::Write, |
| 179 | ) -> Result<(), std::io::Error> { |
| 180 | let index: usize = std::env::var("_CLAP_COMPLETE_INDEX") |
| 181 | .ok() |
| 182 | .and_then(|i| i.parse().ok()) |
| 183 | .unwrap_or_default(); |
| 184 | let ifs: Option<String> = std::env::var("_CLAP_IFS").ok().and_then(|i| i.parse().ok()); |
| 185 | let completions = crate::engine::complete(cmd, args, index, current_dir)?; |
| 186 | |
| 187 | for (i, candidate) in completions.iter().enumerate() { |
| 188 | if i != 0 { |
| 189 | write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; |
| 190 | } |
| 191 | write!(buf, "{}", candidate.get_value().to_string_lossy())?; |
| 192 | } |
| 193 | Ok(()) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /// Fish completion adapter |
| 198 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 199 | pub struct Fish; |
| 200 | |
| 201 | impl EnvCompleter for Fish { |
| 202 | fn name(&self) -> &'static str { |
| 203 | "fish" |
| 204 | } |
| 205 | fn is(&self, name: &str) -> bool { |
| 206 | name == "fish" |
| 207 | } |
| 208 | fn write_registration( |
| 209 | &self, |
| 210 | var: &str, |
| 211 | _name: &str, |
| 212 | bin: &str, |
| 213 | completer: &str, |
| 214 | buf: &mut dyn std::io::Write, |
| 215 | ) -> Result<(), std::io::Error> { |
| 216 | let bin = fish_quote(bin); |
| 217 | let completer = fish_quote_for_eval(completer); |
| 218 | |
| 219 | writeln!( |
| 220 | buf, |
| 221 | r#"complete --keep-order --exclusive --command {bin} --arguments "({var}=fish {completer} -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))""# |
| 222 | ) |
| 223 | } |
| 224 | fn write_complete( |
| 225 | &self, |
| 226 | cmd: &mut clap::Command, |
| 227 | args: Vec<OsString>, |
| 228 | current_dir: Option<&std::path::Path>, |
| 229 | buf: &mut dyn std::io::Write, |
| 230 | ) -> Result<(), std::io::Error> { |
| 231 | let index = args.len() - 1; |
| 232 | let completions = crate::engine::complete(cmd, args, index, current_dir)?; |
| 233 | |
| 234 | for candidate in completions { |
| 235 | write!(buf, "{}", candidate.get_value().to_string_lossy())?; |
| 236 | if let Some(help) = candidate.get_help() { |
| 237 | write!( |
| 238 | buf, |
| 239 | "\t{}", |
| 240 | help.to_string().lines().next().unwrap_or_default() |
| 241 | )?; |
| 242 | } |
| 243 | writeln!(buf)?; |
| 244 | } |
| 245 | Ok(()) |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /// Quote `s` for embedding inside fish's `--arguments` value. |
| 250 | /// |
| 251 | /// Fish parses `--arguments` twice: once when sourcing the registration (the |
| 252 | /// outer double-quoted string) and again when evaluating the embedded command |
| 253 | /// substitution at completion time. Each special character must therefore |
| 254 | /// survive two rounds of unescaping, which is what this helper does that |
| 255 | /// [`fish_quote`] does not. |
| 256 | fn fish_quote_for_eval(s: &str) -> std::borrow::Cow<'_, str> { |
| 257 | if !fish_needs_quoting(s) { |
| 258 | return std::borrow::Cow::Borrowed(s); |
| 259 | } |
| 260 | let mut out = String::with_capacity(s.len() + 2); |
| 261 | out.push('\''); |
| 262 | for c in s.chars() { |
| 263 | match c { |
| 264 | '\\' => out.push_str(r"\\\\"), |
| 265 | '\'' => out.push_str(r"\\'"), |
| 266 | '"' => out.push_str(r#"\""#), |
| 267 | '$' => out.push_str(r"\$"), |
| 268 | _ => out.push(c), |
| 269 | } |
| 270 | } |
| 271 | out.push('\''); |
| 272 | std::borrow::Cow::Owned(out) |
| 273 | } |
| 274 | |
| 275 | /// Quote `s` for fish's first-pass parser. |
| 276 | /// |
| 277 | /// Used for the `--command` value, where fish reads the token once when |
| 278 | /// sourcing the registration and never re-evaluates it. Single-quoting plus |
| 279 | /// escaping `\` and `'` is sufficient. |
| 280 | fn fish_quote(s: &str) -> std::borrow::Cow<'_, str> { |
| 281 | if !fish_needs_quoting(s) { |
| 282 | return std::borrow::Cow::Borrowed(s); |
| 283 | } |
| 284 | let mut out = String::with_capacity(s.len() + 2); |
| 285 | out.push('\''); |
| 286 | for c in s.chars() { |
| 287 | match c { |
| 288 | '\\' => out.push_str(r"\\"), |
| 289 | '\'' => out.push_str(r"\'"), |
| 290 | _ => out.push(c), |
| 291 | } |
| 292 | } |
| 293 | out.push('\''); |
| 294 | std::borrow::Cow::Owned(out) |
| 295 | } |
| 296 | |
| 297 | fn fish_needs_quoting(s: &str) -> bool { |
| 298 | s.is_empty() |
| 299 | || s.chars().any(|c| { |
| 300 | !(c.is_ascii_alphanumeric() |
| 301 | || matches!(c, '/' | '_' | '-' | '.' | ',' | '+' | '=' | ':' | '@')) |
| 302 | }) |
| 303 | } |
| 304 | |
| 305 | /// Powershell completion adapter |
| 306 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 307 | pub struct Powershell; |
| 308 | |
| 309 | impl EnvCompleter for Powershell { |
| 310 | fn name(&self) -> &'static str { |
| 311 | "powershell" |
| 312 | } |
| 313 | fn is(&self, name: &str) -> bool { |
| 314 | // pwsh is the cross-platform executable. |
| 315 | // The other two are Windows-only. |
| 316 | name == "pwsh" || name == "powershell" || name == "powershell_ise" |
| 317 | } |
| 318 | fn write_registration( |
| 319 | &self, |
| 320 | var: &str, |
| 321 | _name: &str, |
| 322 | bin: &str, |
| 323 | completer: &str, |
| 324 | buf: &mut dyn std::io::Write, |
| 325 | ) -> Result<(), std::io::Error> { |
| 326 | let bin = shlex::try_quote(bin).unwrap_or(std::borrow::Cow::Borrowed(bin)); |
| 327 | let completer = |
| 328 | shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer)); |
| 329 | |
| 330 | // `completer` may or may not be surrounded by double quotes, enclosing |
| 331 | // the expression in a here-string ensures the whole thing is |
| 332 | // interpreted as the first argument to the call operator |
| 333 | writeln!( |
| 334 | buf, |
| 335 | r#" |
| 336 | Register-ArgumentCompleter -Native -CommandName {bin} -ScriptBlock {{ |
| 337 | param($wordToComplete, $commandAst, $cursorPosition) |
| 338 | |
| 339 | $prev = $env:{var}; |
| 340 | $env:{var} = "powershell"; |
| 341 | |
| 342 | $args = $commandAst.Extent.Text |
| 343 | $args = $args.Substring(0, [math]::Min($cursorPosition, $args.Length)); |
| 344 | if ($wordToComplete -eq "") {{ |
| 345 | $args += " ''"; |
| 346 | }} |
| 347 | |
| 348 | $results = Invoke-Expression @" |
| 349 | & {completer} -- $args |
| 350 | "@; |
| 351 | if ($null -eq $prev) {{ |
| 352 | Remove-Item Env:\{var}; |
| 353 | }} else {{ |
| 354 | $env:{var} = $prev; |
| 355 | }} |
| 356 | $results | ForEach-Object {{ |
| 357 | $split = $_.Split("`t"); |
| 358 | $cmd = $split[0]; |
| 359 | |
| 360 | if ($split.Length -eq 2) {{ |
| 361 | $help = $split[1]; |
| 362 | }} |
| 363 | else {{ |
| 364 | $help = $split[0]; |
| 365 | }} |
| 366 | |
| 367 | [System.Management.Automation.CompletionResult]::new($cmd, $cmd, 'ParameterValue', $help) |
| 368 | }} |
| 369 | }}; |
| 370 | "# |
| 371 | ) |
| 372 | } |
| 373 | |
| 374 | fn write_complete( |
| 375 | &self, |
| 376 | cmd: &mut clap::Command, |
| 377 | args: Vec<OsString>, |
| 378 | current_dir: Option<&std::path::Path>, |
| 379 | buf: &mut dyn std::io::Write, |
| 380 | ) -> Result<(), std::io::Error> { |
| 381 | let index = args.len() - 1; |
| 382 | let completions = crate::engine::complete(cmd, args, index, current_dir)?; |
| 383 | |
| 384 | for candidate in completions { |
| 385 | write!(buf, "{}", candidate.get_value().to_string_lossy())?; |
| 386 | if let Some(help) = candidate.get_help() { |
| 387 | write!( |
| 388 | buf, |
| 389 | "\t{}", |
| 390 | help.to_string().lines().next().unwrap_or_default() |
| 391 | )?; |
| 392 | } |
| 393 | writeln!(buf)?; |
| 394 | } |
| 395 | Ok(()) |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /// Zsh completion adapter |
| 400 | #[derive(Copy, Clone, PartialEq, Eq, Debug)] |
| 401 | pub struct Zsh; |
| 402 | |
| 403 | impl EnvCompleter for Zsh { |
| 404 | fn name(&self) -> &'static str { |
| 405 | "zsh" |
| 406 | } |
| 407 | fn is(&self, name: &str) -> bool { |
| 408 | name == "zsh" |
| 409 | } |
| 410 | fn write_registration( |
| 411 | &self, |
| 412 | var: &str, |
| 413 | name: &str, |
| 414 | bin: &str, |
| 415 | completer: &str, |
| 416 | buf: &mut dyn std::io::Write, |
| 417 | ) -> Result<(), std::io::Error> { |
| 418 | let escaped_name = name.replace('-', "_"); |
| 419 | let bin = shlex::try_quote(bin).unwrap_or(std::borrow::Cow::Borrowed(bin)); |
| 420 | let completer = |
| 421 | shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer)); |
| 422 | |
| 423 | let script = r#"#compdef BIN |
| 424 | function _clap_dynamic_completer_NAME() { |
| 425 | local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1) |
| 426 | local _CLAP_IFS=$'\n' |
| 427 | |
| 428 | local completions=("${(@f)$( \ |
| 429 | _CLAP_IFS="$_CLAP_IFS" \ |
| 430 | _CLAP_COMPLETE_INDEX="$_CLAP_COMPLETE_INDEX" \ |
| 431 | VAR="zsh" \ |
| 432 | COMPLETER -- "${words[@]}" 2>/dev/null \ |
| 433 | )}") |
| 434 | |
| 435 | if [[ -n $completions ]]; then |
| 436 | local -a dirs=() |
| 437 | local -a other=() |
| 438 | local completion |
| 439 | for completion in $completions; do |
| 440 | local value="${completion%%:*}" |
| 441 | if [[ "$value" == */ ]]; then |
| 442 | local dir_no_slash="${value%/}" |
| 443 | if [[ "$completion" == *:* ]]; then |
| 444 | local desc="${completion#*:}" |
| 445 | dirs+=("$dir_no_slash:$desc") |
| 446 | else |
| 447 | dirs+=("$dir_no_slash") |
| 448 | fi |
| 449 | else |
| 450 | other+=("$completion") |
| 451 | fi |
| 452 | done |
| 453 | [[ -n $dirs ]] && _describe -V 'values' dirs -S '/' -r '/' |
| 454 | [[ -n $other ]] && _describe -V 'values' other |
| 455 | fi |
| 456 | } |
| 457 | |
| 458 | compdef _clap_dynamic_completer_NAME BIN"# |
| 459 | .replace("NAME", &escaped_name) |
| 460 | .replace("COMPLETER", &completer) |
| 461 | .replace("BIN", &bin) |
| 462 | .replace("VAR", var); |
| 463 | |
| 464 | writeln!(buf, "{script}")?; |
| 465 | Ok(()) |
| 466 | } |
| 467 | fn write_complete( |
| 468 | &self, |
| 469 | cmd: &mut clap::Command, |
| 470 | args: Vec<OsString>, |
| 471 | current_dir: Option<&std::path::Path>, |
| 472 | buf: &mut dyn std::io::Write, |
| 473 | ) -> Result<(), std::io::Error> { |
| 474 | let index: usize = std::env::var("_CLAP_COMPLETE_INDEX") |
| 475 | .ok() |
| 476 | .and_then(|i| i.parse().ok()) |
| 477 | .unwrap_or_default(); |
| 478 | let ifs: Option<String> = std::env::var("_CLAP_IFS").ok().and_then(|i| i.parse().ok()); |
| 479 | |
| 480 | // If the current word is empty, add an empty string to the args |
| 481 | let mut args = args.clone(); |
| 482 | if args.len() == index { |
| 483 | args.push("".into()); |
| 484 | } |
| 485 | let completions = crate::engine::complete(cmd, args, index, current_dir)?; |
| 486 | |
| 487 | for (i, candidate) in completions.iter().enumerate() { |
| 488 | if i != 0 { |
| 489 | write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?; |
| 490 | } |
| 491 | write!( |
| 492 | buf, |
| 493 | "{}", |
| 494 | Self::escape_value(&candidate.get_value().to_string_lossy()) |
| 495 | )?; |
| 496 | if let Some(help) = candidate.get_help() { |
| 497 | write!( |
| 498 | buf, |
| 499 | ":{}", |
| 500 | Self::escape_help(help.to_string().lines().next().unwrap_or_default()) |
| 501 | )?; |
| 502 | } |
| 503 | } |
| 504 | Ok(()) |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | impl Zsh { |
| 509 | /// Escape value string |
| 510 | fn escape_value(string: &str) -> String { |
| 511 | string.replace('\\', "\\\\").replace(':', "\\:") |
| 512 | } |
| 513 | |
| 514 | /// Escape help string |
| 515 | fn escape_help(string: &str) -> String { |
| 516 | string.replace('\\', "\\\\") |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | #[cfg(test)] |
| 521 | mod tests { |
| 522 | use super::*; |
| 523 | use snapbox::IntoData as _; |
| 524 | use snapbox::assert_data_eq; |
| 525 | |
| 526 | // This test verifies that fish shell path quoting works with or without spaces in the path. |
| 527 | #[test] |
| 528 | #[cfg(all(unix, feature = "unstable-dynamic"))] |
| 529 | #[cfg(feature = "unstable-shell-tests")] |
| 530 | fn fish_env_completer_path_quoting_works() { |
| 531 | // Returns the dynamic registration line for the fish shell, for example: |
| 532 | // complete --keep-order --exclusive --command my-bin --arguments "(COMPLETE=fish /path/to/my-bin ... )" |
| 533 | let get_fish_registration = |completer_bin: &str| { |
| 534 | let mut buf = Vec::new(); |
| 535 | let fish = Fish; |
| 536 | fish.write_registration( |
| 537 | "IGNORED_VAR", |
| 538 | "ignored-name", |
| 539 | "/ignored/bin", |
| 540 | completer_bin, |
| 541 | &mut buf, |
| 542 | ) |
| 543 | .expect("write_registration failed"); |
| 544 | String::from_utf8(buf).expect("Invalid UTF-8") |
| 545 | }; |
| 546 | |
| 547 | let script = get_fish_registration("completer"); |
| 548 | assert_data_eq!( |
| 549 | script.trim(), |
| 550 | snapbox::str![r#"complete [..] "([..] completer [..])""#] |
| 551 | ); |
| 552 | |
| 553 | let script = get_fish_registration("/path/completer"); |
| 554 | assert_data_eq!( |
| 555 | script.trim(), |
| 556 | snapbox::str![r#"complete [..] "([..] /path/completer [..])""#] |
| 557 | ); |
| 558 | |
| 559 | let script = get_fish_registration("/path with a space/completer"); |
| 560 | assert_data_eq!( |
| 561 | script.trim(), |
| 562 | snapbox::str![r#"complete [..] "([..] '/path with a space/completer' [..])""#] |
| 563 | ); |
| 564 | } |
| 565 | |
| 566 | #[test] |
| 567 | #[cfg(all(unix, feature = "unstable-dynamic"))] |
| 568 | fn fish_env_completer_path_with_backslash() { |
| 569 | let mut buf = Vec::new(); |
| 570 | Fish.write_registration("V", "n", "/ignored/bin", "/p/dyn\\amic/foo", &mut buf) |
| 571 | .expect("write_registration failed"); |
| 572 | let script = String::from_utf8(buf).expect("Invalid UTF-8"); |
| 573 | assert_data_eq!( |
| 574 | script, |
| 575 | snapbox::str![[r#" |
| 576 | complete --keep-order --exclusive --command /ignored/bin --arguments "(V=fish '/p/dyn\\\\amic/foo' -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))" |
| 577 | |
| 578 | "#]] |
| 579 | .raw() |
| 580 | ); |
| 581 | } |
| 582 | |
| 583 | #[test] |
| 584 | #[cfg(all(unix, feature = "unstable-dynamic"))] |
| 585 | fn fish_env_command_name_with_backslash() { |
| 586 | let mut buf = Vec::new(); |
| 587 | Fish.write_registration("V", "n", "dyn\\amic", "/p/completer", &mut buf) |
| 588 | .expect("write_registration failed"); |
| 589 | let script = String::from_utf8(buf).expect("Invalid UTF-8"); |
| 590 | assert_data_eq!( |
| 591 | script, |
| 592 | snapbox::str![[r#" |
| 593 | complete --keep-order --exclusive --command 'dyn\\amic' --arguments "(V=fish /p/completer -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))" |
| 594 | |
| 595 | "#]] |
| 596 | .raw() |
| 597 | ); |
| 598 | } |
| 599 | |
| 600 | #[test] |
| 601 | #[cfg(all(unix, feature = "unstable-dynamic"))] |
| 602 | fn fish_env_completer_path_with_dollar() { |
| 603 | let mut buf = Vec::new(); |
| 604 | Fish.write_registration("V", "n", "/ignored/bin", "/p/$var/c", &mut buf) |
| 605 | .expect("write_registration failed"); |
| 606 | let script = String::from_utf8(buf).expect("Invalid UTF-8"); |
| 607 | assert_data_eq!( |
| 608 | script, |
| 609 | snapbox::str![[r#" |
| 610 | complete --keep-order --exclusive --command /ignored/bin --arguments "(V=fish '/p/\$var/c' -- (commandline --current-process --tokenize --cut-at-cursor) (commandline --current-token))" |
| 611 | |
| 612 | "#]] |
| 613 | .raw() |
| 614 | ); |
| 615 | } |
| 616 | } |