Skip to content

Commit c271c2f

Browse files
committed
add: print completion to stdout
1 parent 3849beb commit c271c2f

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

CHANGELOG.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
99
### Added
1010

1111
- Support modification of task priorities via `pueue edit --priority/-o` and `pueue restart --edit-priority/-o` [#449](https://github.com/Nukesor/pueue/issues/449)
12-
13-
## [3.3.4] - unreleased
12+
- If no output directory is provided in `completions`, the generated file is printed to `stdout` [#489](https://github.com/Nukesor/pueue/issues/489).
1413

1514
### Fixed
1615

pueue/src/bin/pueue.rs

+42-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::path::PathBuf;
22

33
use anyhow::{bail, Context, Result};
44
use clap::{CommandFactory, Parser};
5-
use clap_complete::{generate_to, shells};
5+
use clap_complete::{generate, generate_to, shells};
66
use log::warn;
77
use simplelog::{Config, ConfigBuilder, LevelFilter, SimpleLogger};
88

@@ -96,17 +96,50 @@ async fn main() -> Result<()> {
9696
/// [clap] is capable of creating auto-generated shell completion files.
9797
/// This function creates such a file for one of the supported shells and puts it into the
9898
/// specified output directory.
99-
fn create_shell_completion_file(shell: &Shell, output_directory: &PathBuf) -> Result<()> {
99+
fn create_shell_completion_file(shell: &Shell, output_directory: &Option<PathBuf>) -> Result<()> {
100100
let mut app = CliArguments::command();
101101
app.set_bin_name("pueue");
102-
let completion_result = match shell {
103-
Shell::Bash => generate_to(shells::Bash, &mut app, "pueue", output_directory),
104-
Shell::Elvish => generate_to(shells::Elvish, &mut app, "pueue", output_directory),
105-
Shell::Fish => generate_to(shells::Fish, &mut app, "pueue", output_directory),
106-
Shell::PowerShell => generate_to(shells::PowerShell, &mut app, "pueue", output_directory),
107-
Shell::Zsh => generate_to(shells::Zsh, &mut app, "pueue", output_directory),
102+
103+
// Output a completion file to a directory, if one is provided
104+
if let Some(output_directory) = output_directory {
105+
let completion_result = match shell {
106+
Shell::Bash => generate_to(shells::Bash, &mut app, "pueue", output_directory),
107+
Shell::Elvish => generate_to(shells::Elvish, &mut app, "pueue", output_directory),
108+
Shell::Fish => generate_to(shells::Fish, &mut app, "pueue", output_directory),
109+
Shell::PowerShell => {
110+
generate_to(shells::PowerShell, &mut app, "pueue", output_directory)
111+
}
112+
Shell::Zsh => generate_to(shells::Zsh, &mut app, "pueue", output_directory),
113+
};
114+
completion_result.context(format!("Failed to generate completions for {shell:?}"))?;
115+
116+
return Ok(());
117+
}
118+
119+
if let Some(output_directory) = output_directory {
120+
let completion_result = match shell {
121+
Shell::Bash => generate_to(shells::Bash, &mut app, "pueue", output_directory),
122+
Shell::Elvish => generate_to(shells::Elvish, &mut app, "pueue", output_directory),
123+
Shell::Fish => generate_to(shells::Fish, &mut app, "pueue", output_directory),
124+
Shell::PowerShell => {
125+
generate_to(shells::PowerShell, &mut app, "pueue", output_directory)
126+
}
127+
Shell::Zsh => generate_to(shells::Zsh, &mut app, "pueue", output_directory),
128+
};
129+
completion_result.context(format!("Failed to generate completions for {shell:?}"))?;
130+
131+
return Ok(());
132+
}
133+
134+
// Print the completion file to stdout
135+
let mut stdout = std::io::stdout();
136+
match shell {
137+
Shell::Bash => generate(shells::Bash, &mut app, "pueue", &mut stdout),
138+
Shell::Elvish => generate(shells::Elvish, &mut app, "pueue", &mut stdout),
139+
Shell::Fish => generate(shells::Fish, &mut app, "pueue", &mut stdout),
140+
Shell::PowerShell => generate(shells::PowerShell, &mut app, "pueue", &mut stdout),
141+
Shell::Zsh => generate(shells::Zsh, &mut app, "pueue", &mut stdout),
108142
};
109-
completion_result.context(format!("Failed to generate completions for {shell:?}"))?;
110143

111144
Ok(())
112145
}

pueue/src/client/cli.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ https://github.com/Nukesor/pueue/issues/350#issue-1359083118"
493493
shell: Shell,
494494
/// The output directory to which the file should be written.
495495
#[arg(value_hint = ValueHint::DirPath)]
496-
output_directory: PathBuf,
496+
output_directory: Option<PathBuf>,
497497
},
498498
}
499499

0 commit comments

Comments
 (0)