Skip to content

Commit

Permalink
fix: correct output of shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Apr 14, 2024
1 parent 5c49b52 commit 67ee0df
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
24 changes: 14 additions & 10 deletions src/completions.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
use clap::CommandFactory;
use clap_complete::{generate, Shell};
use clap_complete_nushell::Nushell;
use std::io::{self, Write};
use std::io::{Error, ErrorKind, Result, Write};
use std::str::FromStr;

use crate::args;

pub fn generate_shell_completions(shell_s: &str, buf: &mut dyn Write) {
pub fn generate_shell_completions(shell_s: &str, buf: &mut dyn Write) -> Result<()> {
if "nu" == shell_s || "nushell" == shell_s {
let shell = Nushell;
generate(shell, &mut args::Args::command(), "rip", buf);
} else {
let shell = Shell::from_str(shell_s).unwrap_or_else(|_| {
eprintln!(
"Invalid shell specification: {}. Available shells: bash, elvish, fish, powershell, zsh, nushell",
shell_s
);
std::process::exit(1);
});
generate(shell, &mut args::Args::command(), "rip", buf);
let tryshell = Shell::from_str(shell_s);
if tryshell.is_err() {
return Err(Error::new(
ErrorKind::InvalidInput,
format!(
"Invalid shell specification: {}. Available shells: bash, elvish, fish, powershell, zsh, nushell",
shell_s
)
));
}
generate(tryshell.unwrap(), &mut args::Args::command(), "rip", buf);
}
Ok(())
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::{env, fs};
use walkdir::WalkDir;

pub mod args;
pub mod completions;
pub mod record;
pub mod util;
pub mod completions;

use args::Args;
use record::{Record, RecordItem};
Expand Down
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ use clap::Parser;
use std::io;
use std::process::ExitCode;

use rip2::{args, util, completions};
use rip2::{args, completions, util};

fn main() -> ExitCode {
let cli = args::Args::parse();
let mut stream = io::stdout();
let mode = util::ProductionMode;

match &cli.command {
Some(args::Commands::Completions { shell }) => {
completions::generate_shell_completions(shell);
let result = completions::generate_shell_completions(shell, &mut io::stdout());
if result.is_err() {
eprintln!("{}", result.unwrap_err());
return ExitCode::FAILURE;
}
return ExitCode::SUCCESS;
}
None => {}
}

let mode = util::ProductionMode;
let mut stream = io::stdout();

if let Err(ref e) = rip2::run(cli, mode, &mut stream) {
println!("Exception: {}", e);
return ExitCode::FAILURE;
Expand Down

0 comments on commit 67ee0df

Please sign in to comment.