Skip to content

Commit 298d6fd

Browse files
authored
feat: completions
feat: completions
2 parents dd3fe21 + 3f9b235 commit 298d6fd

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

Cargo.lock

Lines changed: 16 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ andromeda-runtime = { path = "runtime", features = ["canvas", "crypto"] }
1515
anyhow = "1.0.98"
1616
anymap = "0.12.1"
1717
base64-simd = "0.8.0"
18-
clap = { version = "4.5.39", features = ["derive"] }
18+
clap = { version = "4.5.40", features = ["derive"] }
19+
clap_complete = "4.5.54"
1920
console = "0.15.8"
2021
dprint-core = "0.67.4"
2122
dprint-plugin-typescript = "0.95.5"

cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ readme = "../README.md"
1010

1111
[dependencies]
1212
clap.workspace = true
13+
clap_complete.workspace = true
1314
console.workspace = true
1415
dprint-core.workspace = true
1516
dprint-plugin-typescript.workspace = true

cli/src/main.rs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

55
use andromeda_core::RuntimeFile;
6-
use clap::{Parser as ClapParser, Subcommand};
6+
use clap::{CommandFactory, Parser as ClapParser, Subcommand};
7+
use clap_complete::{Shell, generate};
78
use libsui::find_section;
9+
use std::io;
810
use std::path::PathBuf;
911

1012
mod compile;
@@ -82,6 +84,12 @@ enum Command {
8284
#[arg(required = false)]
8385
paths: Vec<PathBuf>,
8486
},
87+
/// Generate shell completion scripts
88+
Completions {
89+
/// The shell to generate completions for
90+
#[arg(value_enum)]
91+
shell: Option<Shell>,
92+
},
8593
}
8694

8795
fn main() {
@@ -169,9 +177,12 @@ fn run_main() -> Result<()> {
169177
}
170178
Ok(())
171179
}
180+
Command::Completions { shell } => {
181+
generate_completions(shell);
182+
Ok(())
183+
}
172184
}
173185
});
174-
175186
match rt.block_on(nova_thread) {
176187
Ok(result) => result,
177188
Err(e) => Err(AndromedaError::config_error(
@@ -181,3 +192,46 @@ fn run_main() -> Result<()> {
181192
)),
182193
}
183194
}
195+
196+
fn generate_completions(shell: Option<Shell>) {
197+
let mut cmd = Cli::command();
198+
let bin_name = "andromeda";
199+
200+
match shell {
201+
Some(shell) => {
202+
generate(shell, &mut cmd, bin_name, &mut io::stdout());
203+
}
204+
None => {
205+
// If no shell is specified, try to detect it from environment
206+
// This mimics Deno's behavior
207+
if let Some(detected_shell) = detect_shell() {
208+
generate(detected_shell, &mut cmd, bin_name, &mut io::stdout());
209+
} else {
210+
eprintln!(
211+
"Error: Could not detect shell. Please specify one of: bash, zsh, fish, powershell"
212+
);
213+
std::process::exit(1);
214+
}
215+
}
216+
}
217+
}
218+
219+
fn detect_shell() -> Option<Shell> {
220+
// Try to detect shell from environment variables
221+
if let Ok(shell) = std::env::var("SHELL") {
222+
if shell.contains("bash") {
223+
return Some(Shell::Bash);
224+
} else if shell.contains("zsh") {
225+
return Some(Shell::Zsh);
226+
} else if shell.contains("fish") {
227+
return Some(Shell::Fish);
228+
}
229+
}
230+
231+
// On Windows, check for PowerShell
232+
if cfg!(windows) && std::env::var("PSModulePath").is_ok() {
233+
return Some(Shell::PowerShell);
234+
}
235+
236+
None
237+
}

0 commit comments

Comments
 (0)