diff --git a/Cargo.lock b/Cargo.lock index 630a99a..f9c81f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,6 +115,17 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.5.0" @@ -443,8 +454,9 @@ dependencies = [ [[package]] name = "ghost_git_writer" -version = "0.16.0" +version = "0.17.0" dependencies = [ + "atty", "chrono", "clap", "derive-getters", @@ -512,6 +524,15 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "home" version = "0.5.11" @@ -1982,6 +2003,28 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-core" version = "0.62.0" diff --git a/Cargo.toml b/Cargo.toml index 61541ce..effda1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ghost_git_writer" description = "write a git commit message, README or Diff Summary by LLM services." -version = "0.16.0" +version = "0.17.0" repository = "https://github.com/Uliboooo/ghost_git_writer" edition = "2024" license = "MIT OR Apache-2.0" @@ -32,3 +32,4 @@ easy_storage = "0.4.*" indicatif = "0.18.*" unicode-width = "0.2.1" url = "2.5.7" +atty = "0.2.14" diff --git a/src/cli.rs b/src/cli.rs index 873cfe3..912ee3e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -80,6 +80,9 @@ pub struct RootOptions { #[arg(long = "oneline", help = "show only llm's return for cli pipes")] oneline: bool, + + #[arg(long = "stdin", help = "use stdin as diff content")] + stdin: bool, } pub trait DiffOption { diff --git a/src/helper.rs b/src/helper.rs index e054802..c46ec40 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -1,5 +1,6 @@ use std::{ - fs, io, path::{Path, PathBuf} + fs, io, + path::{Path, PathBuf}, }; // #[derive(Debug)] diff --git a/src/llm.rs b/src/llm.rs index ef2a6f7..0a299ed 100644 --- a/src/llm.rs +++ b/src/llm.rs @@ -1,4 +1,5 @@ -use std::fmt::Display; +use crate::cli_helper::Spinner; +use crate::config; use derive_getters::Getters; use llm_api_rs::{ self, LlmProvider, @@ -6,8 +7,7 @@ use llm_api_rs::{ providers::{Anthropic, DeepSeek, Gemini, OpenAI}, }; use ollama_rs::{Ollama, generation::completion::request::GenerationRequest}; -use crate::cli_helper::Spinner; -use crate::config; +use std::fmt::Display; #[derive(Debug)] pub enum Error { @@ -70,7 +70,7 @@ impl TryFrom<&str> for Provider { "ollama" => Ok(Self::Ollama), "openai" => Ok(Self::OpenAI), "gemini" => Ok(Self::Gemini), - "authropic" => Ok(Self::Anthropic), + "anthropic" => Ok(Self::Anthropic), "deepseek" => Ok(Self::DeepSeek), _ => Err(Error::NotSuppoeredProvider), } diff --git a/src/main.rs b/src/main.rs index 3963e13..2596298 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,8 +22,8 @@ use easy_storage::Storeable; use std::{ env, fmt::Display, - fs::OpenOptions, - io::Write, + fs::{File, OpenOptions}, + io::{BufRead, BufReader, Read, Write, stdout}, path::{Path, PathBuf}, }; @@ -251,15 +251,38 @@ async fn main() -> Result<(), Error> { let extra = root_options.extra().as_ref(); let git_status = get_git_status(&work_path)?; + // + // let diff = if root_options.stdin() { + // todo!() + // } else { + // } match &cli.subcommand { cli::Commands::Commit(commit) => { - let diff = { + // 👇 is from pipe. + let diff = if !atty::is(atty::Stream::Stdin) && *root_options.stdin() { + let mut input = String::new(); + std::io::stdin().lock().read_to_string(&mut input)?; + stdout().flush()?; + input + } else { let diff_opt = commit.resolve_diff_commit(); - git::get_diff(diff_opt, &work_path) - }?; + git::get_diff(diff_opt, &work_path)? + }; let msg = commit_gen::gen_commit_msg(diff, git_status, model_info, lang, extra).await?; + let conti = || { + let mut tty = BufReader::new(File::open("/dev/tty").unwrap()); + stdout().flush().unwrap(); + // let mut ans = String::new(); + // tty.read_line(&mut ans).unwrap(); + print!("continue?(y/n)>"); + std::io::stdout().flush().unwrap(); + let mut ans = String::new(); + tty.read_line(&mut ans).unwrap(); + + matches!(ans.trim(), "y" | "Y" | "Yes" | "yes") + }; if *commit.get_root_options().oneline() { println!("{msg}"); Ok(()) @@ -267,7 +290,10 @@ async fn main() -> Result<(), Error> { let fd_msg = cli_helper::Printer::from(&msg); println!("Generated msg:\n{fd_msg}"); - if *commit.auto_commit() || yes_no("commit?(y/n)") { + if *commit.auto_commit() || + //yes_no("commit?(y/n)") + conti() + { git::git_commit(&work_path, &msg, git_user.0, git_user.1)?; Ok(()) } else { @@ -312,15 +338,20 @@ async fn main() -> Result<(), Error> { Ok(f.write_all(readme_content.as_bytes())?) } } - cli::Commands::SumDiff(_diff_sum) => { - let diff = { - let diff_s = _diff_sum.resolve_diff_commit(); - git::get_diff(diff_s, &work_path) - }?; + cli::Commands::SumDiff(diff_sum) => { + let diff = if *root_options.stdin() { + let mut input = String::new(); + std::io::stdin().read_to_string(&mut input)?; + input + } else { + let diff_s = diff_sum.resolve_diff_commit(); + git::get_diff(diff_s, &work_path)? + }; + let res = diff_sum_gen::sum_diff(diff, git_status, model_info, lang.cloned(), extra.cloned()) .await?; - if *_diff_sum.get_root_options().oneline() { + if *diff_sum.get_root_options().oneline() { println!("{res}"); Ok(()) } else { @@ -329,10 +360,14 @@ async fn main() -> Result<(), Error> { } } cli::Commands::WhichSem(which) => { - let diff = { + let diff = if *root_options.stdin() { + let mut input = String::new(); + std::io::stdin().read_to_string(&mut input)?; + input + } else { let diff_s = which.resolve_diff_commit(); - git::get_diff(diff_s, &work_path) - }?; + git::get_diff(diff_s, &work_path)? + }; let res = which_sem::whichi_sem(diff, git_status, model_info, lang.cloned(), extra.cloned()) .await?;