From f82c784faf9b33693cc451bab33e9fbdb47babd2 Mon Sep 17 00:00:00 2001 From: kazuminn Date: Sat, 20 Jul 2024 17:37:37 +0900 Subject: [PATCH] output to file --- src/main.rs | 4 ++-- src/options/auto_complete.rs | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index c058da47c..ba3839efa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -155,9 +155,9 @@ impl App { return; } - //ロゴと時間が表示さないように実行したい + //ロゴと時間が表示さないように実行したいので、この位置 if let Action::AutoComplete(_) = &stored_static.config.action.as_ref().unwrap() { - auto_complete(app); + auto_complete(app, stored_static.output_path.as_ref()); return; } diff --git a/src/options/auto_complete.rs b/src/options/auto_complete.rs index c5da47b2a..8a92979b7 100644 --- a/src/options/auto_complete.rs +++ b/src/options/auto_complete.rs @@ -1,13 +1,15 @@ -use clap_complete::{generate, Generator, Shell}; +use std::{env, path::PathBuf}; + +use clap_complete::{generate_to, Generator, Shell}; use dialoguer::Select; -pub fn auto_complete(app: &mut clap::Command) { +pub fn auto_complete(app: &mut clap::Command, output_path: Option<&PathBuf>) { let shell = select_shell(); - print_completer(shell, app); + print_completer(shell, app, output_path); } fn select_shell() -> Shell { - let items:Vec = vec![Shell::Bash, Shell::Elvish, Shell::Fish, Shell::PowerShell, Shell::Zsh]; + let items:Vec = vec![Shell::Bash, Shell::Elvish, Shell::Fish, Shell::PowerShell]; let selection = Select::new() .with_prompt("Which shell are you using?") @@ -17,8 +19,12 @@ fn select_shell() -> Shell { items[selection] } -fn print_completer(generator: G, app: &mut clap::Command) { - let name = app.get_name().to_owned(); +fn print_completer(generator: G, app: &mut clap::Command, output_path: Option<&PathBuf>) { + let mut name = "auto-complete".to_string(); + if output_path.is_some() { + name = output_path.unwrap().to_str().unwrap().to_string(); + } + let out_dir: PathBuf = env::current_dir().expect("can't get current directory"); - generate(generator, app, name, &mut std::io::stdout()); + let _ = generate_to(generator, app, name, out_dir); } \ No newline at end of file