From 2ac0155fa357a656e11422cfe6ee07fcd357109a Mon Sep 17 00:00:00 2001 From: sigoden Date: Sun, 5 Mar 2023 11:11:15 +0800 Subject: feat: accept pipe (#17) `cat data | aichat` works --- src/main.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index dc54b01..8dfa2fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod repl; mod term; mod utils; +use std::io::{stdin, Read}; use std::sync::Arc; use std::{io::stdout, process::exit}; @@ -43,9 +44,15 @@ fn start() -> Result<()> { None => None, }; let client = ChatGptClient::init(config.clone())?; - match text { - Some(text) => start_directive(client, config, role, &text), - None => start_interactive(client, config, role), + if atty::isnt(atty::Stream::Stdin) { + let mut text = String::new(); + stdin().read_to_string(&mut text)?; + start_directive(client, config, role, &text) + } else { + match text { + Some(text) => start_directive(client, config, role, &text), + None => start_interactive(client, config, role), + } } } -- cgit v1.2.3