Repository navigation
Expand file tree
/
Copy pathcompletion_setup.rs
More file actions
484 lines (431 loc) · 15.8 KB
/
Copy pathcompletion_setup.rs
File metadata and controls
484 lines (431 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! First-run shell completion setup module.
//!
//! This module handles prompting users to install shell completions on first run.
//! It detects the user's shell, offers to install completions, and tracks whether
//! the prompt has been shown to avoid repeated prompts.
use std::fs;
use std::io::{self, IsTerminal, Write};
use std::path::{Path, PathBuf};
use clap_complete::Shell;
/// Marker file name to track if completion setup has been offered.
const COMPLETION_OFFERED_MARKER: &str = ".completion_offered";
/// Check if this is an interactive terminal.
fn is_interactive_terminal() -> bool {
io::stdin().is_terminal() && io::stdout().is_terminal()
}
/// Get the cortex home directory.
fn get_cortex_home() -> Option<PathBuf> {
// Check CORTEX_HOME or CORTEX_CONFIG_DIR environment variables
if let Ok(val) = std::env::var("CORTEX_CONFIG_DIR")
&& !val.is_empty()
{
return Some(PathBuf::from(val));
}
if let Ok(val) = std::env::var("CORTEX_HOME")
&& !val.is_empty()
{
return Some(PathBuf::from(val));
}
// Default to ~/.cortex
dirs::home_dir().map(|h| h.join(".cortex"))
}
/// Check if completion setup has already been offered.
fn completion_already_offered(cortex_home: &Path) -> bool {
cortex_home.join(COMPLETION_OFFERED_MARKER).exists()
}
/// Mark that completion setup has been offered.
fn mark_completion_offered(cortex_home: &Path) -> io::Result<()> {
// Ensure the directory exists
fs::create_dir_all(cortex_home)?;
// Create the marker file
let marker_path = cortex_home.join(COMPLETION_OFFERED_MARKER);
fs::write(&marker_path, "completion setup offered\n")?;
Ok(())
}
/// Detect the user's shell from the SHELL environment variable.
fn detect_shell() -> Option<Shell> {
let shell_path = std::env::var("SHELL").ok()?;
let shell_name = std::path::Path::new(&shell_path)
.file_name()?
.to_str()?
.to_lowercase();
match shell_name.as_str() {
"bash" => Some(Shell::Bash),
"zsh" => Some(Shell::Zsh),
"fish" => Some(Shell::Fish),
"powershell" | "pwsh" => Some(Shell::PowerShell),
"elvish" => Some(Shell::Elvish),
_ => None,
}
}
/// Get the completion installation path for a given shell.
fn get_completion_install_path(shell: Shell) -> Option<PathBuf> {
let home = dirs::home_dir()?;
match shell {
Shell::Bash => {
// Try bash-completion directory first, then fall back to .bashrc
let bash_completion = home.join(".local/share/bash-completion/completions");
if bash_completion.parent().is_some_and(|p| p.exists()) {
Some(bash_completion.join("cortex"))
} else {
Some(home.join(".bashrc"))
}
}
Shell::Zsh => {
// Check for common zsh completion directories
let zsh_completions = home.join(".zsh/completions");
if zsh_completions.exists() {
Some(zsh_completions.join("_cortex"))
} else {
Some(home.join(".zshrc"))
}
}
Shell::Fish => Some(home.join(".config/fish/completions/cortex.fish")),
Shell::PowerShell => {
// PowerShell profile location varies by platform
#[cfg(windows)]
{
Some(home.join("Documents/PowerShell/Microsoft.PowerShell_profile.ps1"))
}
#[cfg(not(windows))]
{
Some(home.join(".config/powershell/Microsoft.PowerShell_profile.ps1"))
}
}
Shell::Elvish => Some(home.join(".elvish/lib/cortex.elv")),
_ => None,
}
}
/// Get the shell name in lowercase for command output.
fn shell_name(shell: Shell) -> &'static str {
match shell {
Shell::Bash => "bash",
Shell::Zsh => "zsh",
Shell::Fish => "fish",
Shell::PowerShell => "powershell",
Shell::Elvish => "elvish",
_ => "bash",
}
}
/// Install completions for the given shell.
///
/// For most shells, we add an eval command to the shell configuration file
/// that dynamically loads completions. This approach is more robust as it
/// doesn't require updating the completion script when the CLI changes.
fn install_completions(shell: Shell) -> io::Result<()> {
let install_path = get_completion_install_path(shell).ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
"Could not determine completion installation path",
)
})?;
// Create parent directory if needed
if let Some(parent) = install_path.parent() {
fs::create_dir_all(parent)?;
}
let shell_cmd = shell_name(shell);
match shell {
Shell::Bash => {
// Append eval command to .bashrc
let mut file = fs::OpenOptions::new()
.create(true)
.append(true)
.open(&install_path)?;
writeln!(file)?;
writeln!(file, "# Cortex CLI completions")?;
writeln!(file, "eval \"$(cortex completion {})\"\n", shell_cmd)?;
}
Shell::Zsh => {
// Append eval command to .zshrc
let mut file = fs::OpenOptions::new()
.create(true)
.append(true)
.open(&install_path)?;
writeln!(file)?;
writeln!(file, "# Cortex CLI completions")?;
writeln!(file, "eval \"$(cortex completion {})\"\n", shell_cmd)?;
}
Shell::Fish => {
// Fish needs the script written to the completions directory
// Generate it by running the command
let output = std::process::Command::new("cortex")
.args(["completion", shell_cmd])
.output()?;
if output.status.success() {
fs::write(&install_path, output.stdout)?;
} else {
return Err(io::Error::other("Failed to generate fish completions"));
}
}
Shell::PowerShell => {
// Append to PowerShell profile
let mut file = fs::OpenOptions::new()
.create(true)
.append(true)
.open(&install_path)?;
writeln!(file)?;
writeln!(file, "# Cortex CLI completions")?;
writeln!(
file,
"cortex completion powershell | Out-String | Invoke-Expression\n"
)?;
}
Shell::Elvish => {
// Generate script by running the command
let output = std::process::Command::new("cortex")
.args(["completion", shell_cmd])
.output()?;
if output.status.success() {
fs::write(&install_path, output.stdout)?;
} else {
return Err(io::Error::other("Failed to generate elvish completions"));
}
}
_ => {
return Err(io::Error::new(
io::ErrorKind::Unsupported,
"Unsupported shell for automatic installation",
));
}
}
Ok(())
}
/// Maximum number of completions to return for file/directory listings.
/// This prevents shell hangs when completing in directories with many files.
pub const MAX_COMPLETION_RESULTS: usize = 1000;
/// Prompt the user to install shell completions on first run.
///
/// This function checks if:
/// 1. We're running in an interactive terminal
/// 2. Completion setup hasn't been offered before
/// 3. We can detect the user's shell
///
/// If all conditions are met, it prompts the user and optionally installs completions.
///
/// Note: For large directories (>1000 files), completion may be slow.
/// Consider using more specific paths or limiting directory size.
pub fn maybe_prompt_completion_setup() {
// Only prompt in interactive terminals
if !is_interactive_terminal() {
return;
}
// Get cortex home directory
let cortex_home = match get_cortex_home() {
Some(home) => home,
None => return,
};
// Check if we've already offered completion setup
if completion_already_offered(&cortex_home) {
return;
}
// Detect the user's shell
let shell = match detect_shell() {
Some(s) => s,
None => {
// Mark as offered so we don't keep trying with unknown shells
let _ = mark_completion_offered(&cortex_home);
return;
}
};
let shell_str = shell_name(shell);
// Show the prompt
println!();
println!("Welcome to Cortex CLI!");
println!();
println!("Would you like to enable tab completion for your shell ({shell_str})?",);
println!("This will allow you to press TAB to complete commands and options.");
println!();
print!("Enable tab completion? [y/N] ");
let _ = io::stdout().flush();
// Read user input
let mut input = String::new();
if io::stdin().read_line(&mut input).is_err() {
let _ = mark_completion_offered(&cortex_home);
return;
}
let response = input.trim().to_lowercase();
if response == "y" || response == "yes" {
match install_completions(shell) {
Ok(()) => {
println!();
println!("Tab completion has been enabled!");
println!(
"Please restart your shell or run 'source ~/.{}rc' to activate.",
shell_str
);
println!();
}
Err(e) => {
println!();
println!("Could not automatically install completions: {e}");
println!();
println!("You can manually enable completions by running:");
println!(" cortex completion {shell_str}");
println!();
println!("Then add the output to your shell configuration file.");
println!();
}
}
} else {
println!();
println!("No problem! You can enable tab completion later by running:");
println!(" cortex completion {shell_str}");
println!();
}
// Mark as offered regardless of the user's choice
let _ = mark_completion_offered(&cortex_home);
}
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
#[test]
fn test_shell_name_bash() {
assert_eq!(shell_name(Shell::Bash), "bash");
}
#[test]
fn test_shell_name_zsh() {
assert_eq!(shell_name(Shell::Zsh), "zsh");
}
#[test]
fn test_shell_name_fish() {
assert_eq!(shell_name(Shell::Fish), "fish");
}
#[test]
fn test_shell_name_powershell() {
assert_eq!(shell_name(Shell::PowerShell), "powershell");
}
#[test]
fn test_shell_name_elvish() {
assert_eq!(shell_name(Shell::Elvish), "elvish");
}
#[test]
#[serial]
fn test_detect_shell_bash() {
// SAFETY: Tests run serially and we restore env vars immediately
unsafe { std::env::set_var("SHELL", "/bin/bash") };
let shell = detect_shell();
assert_eq!(shell, Some(Shell::Bash));
unsafe { std::env::remove_var("SHELL") };
}
#[test]
#[serial]
fn test_detect_shell_zsh() {
// SAFETY: Tests run serially and we restore env vars immediately
unsafe { std::env::set_var("SHELL", "/usr/local/bin/zsh") };
let shell = detect_shell();
assert_eq!(shell, Some(Shell::Zsh));
unsafe { std::env::remove_var("SHELL") };
}
#[test]
#[serial]
fn test_detect_shell_fish() {
// SAFETY: Tests run serially and we restore env vars immediately
unsafe { std::env::set_var("SHELL", "/usr/bin/fish") };
let shell = detect_shell();
assert_eq!(shell, Some(Shell::Fish));
unsafe { std::env::remove_var("SHELL") };
}
#[test]
#[serial]
fn test_detect_shell_powershell() {
// SAFETY: Tests run serially and we restore env vars immediately
unsafe { std::env::set_var("SHELL", "/usr/bin/pwsh") };
let shell = detect_shell();
assert_eq!(shell, Some(Shell::PowerShell));
unsafe { std::env::remove_var("SHELL") };
}
#[test]
#[serial]
fn test_detect_shell_case_insensitive() {
// SAFETY: Tests run serially and we restore env vars immediately
// Test that shell detection converts to lowercase (BASH -> bash -> Shell::Bash)
unsafe { std::env::set_var("SHELL", "/bin/BASH") };
let shell = detect_shell();
// The function lowercases the shell name, so BASH becomes bash
assert_eq!(shell, Some(Shell::Bash));
unsafe { std::env::remove_var("SHELL") };
}
#[test]
#[serial]
fn test_detect_shell_elvish() {
// SAFETY: Tests run serially and we restore env vars immediately
unsafe { std::env::set_var("SHELL", "/usr/local/bin/elvish") };
let shell = detect_shell();
assert_eq!(shell, Some(Shell::Elvish));
unsafe { std::env::remove_var("SHELL") };
}
#[test]
#[serial]
fn test_detect_shell_unknown() {
// SAFETY: Tests run serially and we restore env vars immediately
unsafe { std::env::set_var("SHELL", "/bin/unknown_shell") };
let shell = detect_shell();
assert_eq!(shell, None);
unsafe { std::env::remove_var("SHELL") };
}
#[test]
#[serial]
fn test_detect_shell_no_shell_env() {
// SAFETY: Tests run serially and we restore env vars immediately
unsafe { std::env::remove_var("SHELL") };
let shell = detect_shell();
assert_eq!(shell, None);
}
#[test]
fn test_get_completion_install_path_bash() {
let path = get_completion_install_path(Shell::Bash);
assert!(path.is_some());
let path = path.expect("bash completion path should be available");
let path_str = path.to_string_lossy();
assert!(
path_str.ends_with("bash-completion/completions/cortex")
|| path_str.ends_with(".bashrc"),
"bash path should end with bash-completion/completions/cortex or .bashrc"
);
}
#[test]
fn test_get_completion_install_path_zsh() {
let path = get_completion_install_path(Shell::Zsh);
assert!(path.is_some());
let path = path.expect("zsh completion path should be available");
let path_str = path.to_string_lossy();
assert!(
path_str.ends_with("_cortex") || path_str.ends_with(".zshrc"),
"zsh path should end with _cortex or .zshrc"
);
}
#[test]
fn test_get_completion_install_path_fish() {
let path = get_completion_install_path(Shell::Fish);
assert!(path.is_some());
let path = path.expect("fish completion path should be available");
let path_str = path.to_string_lossy();
assert!(
path_str.contains("fish/completions/cortex.fish"),
"fish path should contain fish/completions/cortex.fish"
);
}
#[test]
fn test_get_completion_install_path_powershell() {
let path = get_completion_install_path(Shell::PowerShell);
assert!(path.is_some());
let path = path.expect("powershell completion path should be available");
let path_str = path.to_string_lossy();
assert!(
path_str.contains("PowerShell") || path_str.contains("powershell"),
"powershell path should contain PowerShell or powershell"
);
}
#[test]
fn test_get_completion_install_path_elvish() {
let path = get_completion_install_path(Shell::Elvish);
assert!(path.is_some());
let path = path.expect("elvish completion path should be available");
let path_str = path.to_string_lossy();
assert!(
path_str.contains("elvish") && path_str.ends_with("cortex.elv"),
"elvish path should contain elvish and end with cortex.elv"
);
}
}