Skip to content

Commit 79e2853

Browse files
feat(shell): add a proper standard shell autocomplete generation subcommand
1 parent c5bb97c commit 79e2853

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎television/cli/args.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,12 @@ pub enum Command {
673673
#[arg(value_enum)]
674674
shell: Shell,
675675
},
676+
/// Generates standard shell tab-completion scripts for tv's various subcommands.
677+
#[clap(name = "completions")]
678+
Completions {
679+
#[arg(value_enum)]
680+
shell: Shell,
681+
},
676682
/// Downloads the latest collection of channel prototypes from github
677683
/// and saves them to the local configuration directory.
678684
UpdateChannels {

‎television/main.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use television::{
2323
utils::{
2424
shell::{
2525
Shell, completion_script, render_autocomplete_script_template,
26+
render_clap_autocomplete,
2627
},
2728
stdin::is_readable_stdin,
2829
},
@@ -146,6 +147,18 @@ pub fn handle_subcommand(
146147
let _ = writeln!(stdout().lock(), "{script}");
147148
exit(0);
148149
}
150+
Command::Completions { shell } => {
151+
let target_shell = Shell::from(shell);
152+
if let Some(script) = render_clap_autocomplete(target_shell) {
153+
let _ = writeln!(stdout().lock(), "{script}");
154+
} else {
155+
eprintln!(
156+
"Shell completions are not supported for {target_shell}"
157+
);
158+
exit(1);
159+
}
160+
exit(0);
161+
}
149162
Command::UpdateChannels { force } => {
150163
update_local_channels(force)?;
151164
exit(0);

‎television/utils/shell.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ pub fn render_autocomplete_script_template(
238238
Ok(clap_autocomplete + &script)
239239
}
240240

241-
fn render_clap_autocomplete(shell: Shell) -> Option<String> {
241+
pub fn render_clap_autocomplete(shell: Shell) -> Option<String> {
242242
// Clap autocomplete
243243
let mut clap_autocomplete = vec![];
244244
let mut cmd = Cli::command();

0 commit comments

Comments
 (0)