Skip to content

Commit 2ac0155

Browse files
authored
feat: accept pipe (#17)
`cat data | aichat` works
1 parent d876818 commit 2ac0155

File tree

4 files changed

+69
-13
lines changed

4 files changed

+69
-13
lines changed

Cargo.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ crossbeam = "0.8.2"
3333
crossterm = "0.26.1"
3434
copypasta = "0.8.2"
3535
chrono = "0.4.23"
36+
atty = "0.2.14"
3637

3738
[dependencies.syntect]
3839
version = "5.0.0"

README.md

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Chat with ChatGPT-3.5 in the terminal.
77

8-
![demo](https://user-images.githubusercontent.com/4012553/222897111-dd5015a0-abc1-4c65-a0fb-d491aba3c427.gif)
8+
![demo](https://user-images.githubusercontent.com/4012553/222935716-33dc37df-f58d-4a66-8917-f741fd69682d.gif)
99

1010
## Install
1111

@@ -22,11 +22,9 @@ Download from [Github Releases](https://github.com/sigoden/aichat/releases), unz
2222
## Features
2323

2424
- Compared to the browser, the terminal starts faster and needs less resources.
25-
- Interactive chat and imperative query.
26-
- Support highlight.
27-
- Predefine prompts for role playing.
28-
- History query/completion.
29-
- Persist chat messages.
25+
- Highlight chat message and stream.
26+
- Define roles and let AI play them.
27+
- Save chat messages.
3028
- Support proxy.
3129
- Written in rust, single executable file, cross-platform.
3230

@@ -81,16 +79,45 @@ We can predefine a batch of roles in `roles.yaml`. For example, we define a emoj
8179
Let ChatGPT answer questions in the role of a emoji translator
8280

8381
```
84-
$ aichat --role emoji I am very angry
82+
〉.role emoji
83+
84+
〉I am very angry
8585
😠💢👿
8686
```
8787

88-
In interactive chat, we do this:
88+
## CLI
8989

9090
```
91-
〉.role emoji
91+
Chat with OpenAI GPT-3.5 in the terminal.
9292
93-
〉I am very angry
93+
Usage: aichat [OPTIONS] [TEXT]...
94+
95+
Arguments:
96+
[TEXT]... Input text, if no input text, enter interactive mode
97+
98+
Options:
99+
-L, --list-roles List all roles
100+
-r, --role <ROLE> Specify the role that the AI will play
101+
-h, --help Print help
102+
-V, --version Print version
103+
```
104+
105+
Interactive chat if no text input:
106+
```
107+
$ aichat
108+
Welcome to aichat x.x.x
109+
Type ".help" for more information.
110+
111+
```
112+
113+
Imperative request data:
114+
```
115+
aichat --role emoji I am very angry
116+
```
117+
118+
Accept pipe:
119+
```
120+
cat text | aichat
94121
```
95122

96123
## License

src/main.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod repl;
66
mod term;
77
mod utils;
88

9+
use std::io::{stdin, Read};
910
use std::sync::Arc;
1011
use std::{io::stdout, process::exit};
1112

@@ -43,9 +44,15 @@ fn start() -> Result<()> {
4344
None => None,
4445
};
4546
let client = ChatGptClient::init(config.clone())?;
46-
match text {
47-
Some(text) => start_directive(client, config, role, &text),
48-
None => start_interactive(client, config, role),
47+
if atty::isnt(atty::Stream::Stdin) {
48+
let mut text = String::new();
49+
stdin().read_to_string(&mut text)?;
50+
start_directive(client, config, role, &text)
51+
} else {
52+
match text {
53+
Some(text) => start_directive(client, config, role, &text),
54+
None => start_interactive(client, config, role),
55+
}
4956
}
5057
}
5158

0 commit comments

Comments
 (0)