From 30dae5c12b81a6f92be138966d8930fa15612cb0 Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 17 Jan 2025 20:06:28 +0800 Subject: feat: support macro (#1087) --- src/main.rs | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 056133c..48eb2b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,8 +17,8 @@ use crate::client::{ call_chat_completions, call_chat_completions_streaming, list_models, ModelType, }; use crate::config::{ - ensure_parent_exists, list_agents, load_env_file, Config, GlobalConfig, Input, WorkingMode, - CODE_ROLE, EXPLAIN_SHELL_ROLE, SHELL_ROLE, TEMP_SESSION_NAME, + ensure_parent_exists, list_agents, load_env_file, macro_execute, Config, GlobalConfig, Input, + WorkingMode, CODE_ROLE, EXPLAIN_SHELL_ROLE, SHELL_ROLE, TEMP_SESSION_NAME, }; use crate::render::render_error; use crate::repl::Repl; @@ -31,19 +31,13 @@ use inquire::Text; use is_terminal::IsTerminal; use parking_lot::RwLock; use simplelog::{format_description, ConfigBuilder, LevelFilter, SimpleLogger, WriteLogger}; -use std::{ - env, - io::{stdin, Read}, - process, - sync::Arc, -}; +use std::{env, io::stdin, process, sync::Arc}; #[tokio::main] async fn main() -> Result<()> { load_env_file()?; let cli = Cli::parse(); - let text = cli.text(); - let text = aggregate_text(text)?; + let text = cli.text()?; let working_mode = if cli.serve.is_some() { WorkingMode::Serve } else if text.is_none() && cli.file.is_empty() { @@ -67,7 +61,7 @@ async fn run(config: GlobalConfig, cli: Cli, text: Option) -> Result<()> return serve::run(config, addr).await; } if cli.info { - config.write().cli_info_flag = true; + config.write().info_flag = true; } if cli.list_models { @@ -91,6 +85,12 @@ async fn run(config: GlobalConfig, cli: Cli, text: Option) -> Result<()> println!("{rags}"); return Ok(()); } + if cli.list_macros { + let macros = Config::list_macros().join("\n"); + println!("{macros}"); + return Ok(()); + } + if cli.dry_run { config.write().dry_run = true; } @@ -158,6 +158,10 @@ async fn run(config: GlobalConfig, cli: Cli, text: Option) -> Result<()> return Ok(()); } } + if let Some(name) = &cli.macro_name { + macro_execute(&config, name, text.as_deref(), abort_signal.clone()).await?; + return Ok(()); + } if cli.execute && !is_repl { if cfg!(target_os = "macos") && !stdin().is_terminal() { bail!("Unable to read the pipe for shell execution on MacOS") @@ -318,21 +322,6 @@ async fn shell_execute( Ok(()) } -fn aggregate_text(text: Option) -> Result> { - let text = if stdin().is_terminal() { - text - } else { - let mut stdin_text = String::new(); - stdin().read_to_string(&mut stdin_text)?; - if let Some(text) = text { - Some(format!("{text}\n{stdin_text}")) - } else { - Some(stdin_text) - } - }; - Ok(text) -} - async fn create_input( config: &GlobalConfig, text: Option, -- cgit v1.2.3