diff options
| author | sigoden <sigoden@gmail.com> | 2023-03-05 11:11:15 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-05 11:11:15 +0800 |
| commit | 2ac0155fa357a656e11422cfe6ee07fcd357109a (patch) | |
| tree | 4feaac76437fb4ae8255fda30559e7708bc5ba16 /src | |
| parent | d876818df17ad7b25ebb38a678aa23c79b826e38 (diff) | |
| download | aichat-2ac0155fa357a656e11422cfe6ee07fcd357109a.tar.gz | |
feat: accept pipe (#17)
`cat data | aichat` works
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 13 |
1 files changed, 10 insertions, 3 deletions
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), + } } } |
