|
| 1 | +use warp_completion_metadata::{ |
| 2 | + CommandBuilder, CommandSignatureGenerators, Generator, GeneratorResultsCollector, Suggestion, |
| 3 | +}; |
| 4 | + |
| 5 | +/// Returns the SDKMAN directory reference using the SDKMAN_DIR env var with a fallback. |
| 6 | +fn sdkman_dir() -> &'static str { |
| 7 | + "${SDKMAN_DIR:-$HOME/.sdkman}" |
| 8 | +} |
| 9 | + |
| 10 | +/// Extracts the candidate name from tokens. |
| 11 | +/// |
| 12 | +/// When `has_trailing_whitespace` is true, the candidate is the last token |
| 13 | +/// (the user just finished typing the candidate and pressed space). |
| 14 | +/// Otherwise, the candidate is the second-to-last token (the user is typing |
| 15 | +/// the version prefix). |
| 16 | +fn candidate_from_tokens<'a>(tokens: &'a [&str], has_trailing_whitespace: bool) -> Option<&'a str> { |
| 17 | + if has_trailing_whitespace { |
| 18 | + tokens.last().copied() |
| 19 | + } else { |
| 20 | + tokens.get(tokens.len().saturating_sub(2)).copied() |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +pub fn generator() -> CommandSignatureGenerators { |
| 25 | + CommandSignatureGenerators::new("sdk") |
| 26 | + .add_generator( |
| 27 | + "candidates", |
| 28 | + Generator::script( |
| 29 | + CommandBuilder::single_command(format!( |
| 30 | + "cat {}/var/candidates | tr ',' '\\n'", |
| 31 | + sdkman_dir() |
| 32 | + )), |
| 33 | + |output| { |
| 34 | + output |
| 35 | + .trim() |
| 36 | + .lines() |
| 37 | + .filter(|line| !line.is_empty()) |
| 38 | + .map(|line| Suggestion::with_description(line.trim(), "SDK candidate")) |
| 39 | + .collect_unordered_results() |
| 40 | + }, |
| 41 | + ), |
| 42 | + ) |
| 43 | + .add_generator( |
| 44 | + "installed_versions", |
| 45 | + Generator::command_from_tokens( |
| 46 | + |tokens, has_trailing_whitespace, _| { |
| 47 | + match candidate_from_tokens(tokens, has_trailing_whitespace) { |
| 48 | + Some(candidate) => CommandBuilder::pipe( |
| 49 | + CommandBuilder::single_command(format!( |
| 50 | + "ls {}/candidates/{}/", |
| 51 | + sdkman_dir(), |
| 52 | + candidate |
| 53 | + )), |
| 54 | + CommandBuilder::single_command("grep -v current"), |
| 55 | + ), |
| 56 | + None => CommandBuilder::single_command(""), |
| 57 | + } |
| 58 | + }, |
| 59 | + |output| { |
| 60 | + output |
| 61 | + .trim() |
| 62 | + .lines() |
| 63 | + .filter(|line| !line.is_empty()) |
| 64 | + .map(|line| { |
| 65 | + Suggestion::with_description(line.trim(), "Installed version") |
| 66 | + }) |
| 67 | + .collect_unordered_results() |
| 68 | + }, |
| 69 | + ), |
| 70 | + ) |
| 71 | + .add_generator( |
| 72 | + "available_versions", |
| 73 | + Generator::command_from_tokens( |
| 74 | + |tokens, has_trailing_whitespace, _| { |
| 75 | + match candidate_from_tokens(tokens, has_trailing_whitespace) { |
| 76 | + Some(candidate) => CommandBuilder::single_command(format!( |
| 77 | + "curl -s https://api.sdkman.io/2/candidates/{}/$(cat {}/var/platform)/versions/all | tr ',' '\\n'", |
| 78 | + candidate, |
| 79 | + sdkman_dir() |
| 80 | + )), |
| 81 | + None => CommandBuilder::single_command(""), |
| 82 | + } |
| 83 | + }, |
| 84 | + |output| { |
| 85 | + output |
| 86 | + .trim() |
| 87 | + .lines() |
| 88 | + .filter(|line| !line.is_empty()) |
| 89 | + .map(|line| { |
| 90 | + Suggestion::with_description(line.trim(), "Available version") |
| 91 | + }) |
| 92 | + .collect_unordered_results() |
| 93 | + }, |
| 94 | + ), |
| 95 | + ) |
| 96 | +} |
0 commit comments