elelem-ratatui 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.agents/backlog/001-run-without-dev-tty.md +57 -0
- data/.agents/backlog/002-manual-scrollback.md +46 -0
- data/.agents/backlog/003-quit-from-any-thread.md +49 -0
- data/.agents/backlog/004-readme.md +41 -0
- data/.agents/backlog/005-gem-metadata-links.md +65 -0
- data/.github/workflows/release.yml +73 -0
- data/.release-please-manifest.json +3 -0
- data/.rspec +1 -0
- data/.rubocop.yml +62 -0
- data/CHANGELOG.md +22 -0
- data/LICENSE.txt +21 -0
- data/Rakefile +13 -0
- data/exe/elelem-ratatui +9 -0
- data/ext/elelem/ratatui/extconf.rb +36 -0
- data/lib/elelem/ratatui/input.rb +20 -0
- data/lib/elelem/ratatui/output.rb +47 -0
- data/lib/elelem/ratatui/session.rb +56 -0
- data/lib/elelem/ratatui/version.rb +7 -0
- data/lib/elelem/ratatui.rb +15 -0
- data/release-please-config.json +12 -0
- data/rust/Cargo.lock +662 -0
- data/rust/Cargo.toml +17 -0
- data/rust/src/app.rs +264 -0
- data/rust/src/main.rs +181 -0
- data/rust/src/protocol.rs +34 -0
- data/spec/elelem/ratatui/output_spec.rb +28 -0
- data/spec/elelem/ratatui/session_spec.rb +36 -0
- data/spec/spec_helper.rb +11 -0
- metadata +122 -0
data/rust/src/app.rs
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
use crate::protocol::Event;
|
|
2
|
+
use crossterm::event::{KeyCode, KeyModifiers};
|
|
3
|
+
use ratatui::style::{Color, Modifier, Style};
|
|
4
|
+
use ratatui::text::{Line as TextLine, Span};
|
|
5
|
+
|
|
6
|
+
pub enum Row {
|
|
7
|
+
Text(String),
|
|
8
|
+
Thinking(String),
|
|
9
|
+
Header(String),
|
|
10
|
+
Error(String),
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
pub struct App {
|
|
14
|
+
pub rows: Vec<Row>,
|
|
15
|
+
pub input: String,
|
|
16
|
+
pub waiting: bool,
|
|
17
|
+
pub pending_prompt: Option<String>,
|
|
18
|
+
pub should_quit: bool,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
impl App {
|
|
22
|
+
pub fn new() -> Self {
|
|
23
|
+
Self {
|
|
24
|
+
rows: Vec::new(),
|
|
25
|
+
input: String::new(),
|
|
26
|
+
waiting: false,
|
|
27
|
+
pending_prompt: None,
|
|
28
|
+
should_quit: false,
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
pub fn apply(&mut self, event: Event) {
|
|
33
|
+
if !matches!(event, Event::Waiting) {
|
|
34
|
+
self.waiting = false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
match event {
|
|
38
|
+
Event::Say { text, kind } => {
|
|
39
|
+
if !text.trim().is_empty() {
|
|
40
|
+
let error = kind.as_deref() == Some("error");
|
|
41
|
+
self.rows.push(if error {
|
|
42
|
+
Row::Error(text)
|
|
43
|
+
} else {
|
|
44
|
+
Row::Text(text)
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
Event::Print { text } => {
|
|
49
|
+
if !text.trim().is_empty() {
|
|
50
|
+
self.rows.push(Row::Text(text));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
Event::Thinking { text } => {
|
|
54
|
+
if !text.trim().is_empty() {
|
|
55
|
+
self.rows.push(Row::Thinking(text));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
Event::Doing { header } => self.rows.push(Row::Header(header)),
|
|
59
|
+
Event::Waiting => self.waiting = true,
|
|
60
|
+
Event::Oops { message } => self.rows.push(Row::Error(message)),
|
|
61
|
+
// The file's text, as the plain terminal shows it, or the
|
|
62
|
+
// fallback when it cannot be read as text.
|
|
63
|
+
Event::DisplayFile { path, fallback } => {
|
|
64
|
+
let text = preview(&path).filter(|t| !t.trim().is_empty());
|
|
65
|
+
self.rows
|
|
66
|
+
.push(Row::Text(text.unwrap_or_else(|| fallback.unwrap_or(path))));
|
|
67
|
+
}
|
|
68
|
+
Event::Ask { prompt } => self.pending_prompt = Some(prompt),
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// One display line per line of each row, so a multi-line reply keeps its
|
|
73
|
+
/// line breaks and the scroll offset can count them.
|
|
74
|
+
pub fn render_rows(&self) -> Vec<TextLine<'_>> {
|
|
75
|
+
self.rows
|
|
76
|
+
.iter()
|
|
77
|
+
.flat_map(|row| {
|
|
78
|
+
let (text, style, prefix) = match row {
|
|
79
|
+
Row::Text(t) => (t.as_str(), Style::default(), ""),
|
|
80
|
+
Row::Thinking(t) => (
|
|
81
|
+
t.as_str(),
|
|
82
|
+
Style::default()
|
|
83
|
+
.fg(Color::DarkGray)
|
|
84
|
+
.add_modifier(Modifier::ITALIC),
|
|
85
|
+
"",
|
|
86
|
+
),
|
|
87
|
+
Row::Header(t) => (t.as_str(), Style::default().fg(Color::Cyan), ""),
|
|
88
|
+
Row::Error(t) => (t.as_str(), Style::default().fg(Color::Red), "\u{2717} "),
|
|
89
|
+
};
|
|
90
|
+
text.trim_end_matches('\n')
|
|
91
|
+
.split('\n')
|
|
92
|
+
.enumerate()
|
|
93
|
+
.map(move |(i, line)| {
|
|
94
|
+
let shown = if i == 0 {
|
|
95
|
+
format!("{prefix}{line}")
|
|
96
|
+
} else {
|
|
97
|
+
line.to_string()
|
|
98
|
+
};
|
|
99
|
+
TextLine::from(Span::styled(shown, style))
|
|
100
|
+
})
|
|
101
|
+
.collect::<Vec<_>>()
|
|
102
|
+
})
|
|
103
|
+
.collect()
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const PREVIEW_BYTES: u64 = 256 * 1024;
|
|
108
|
+
const PREVIEW_LINES: usize = 500;
|
|
109
|
+
|
|
110
|
+
/// The start of a regular file, so a huge file, a FIFO or a slow mount
|
|
111
|
+
/// cannot stall or flood the UI.
|
|
112
|
+
fn preview(path: &str) -> Option<String> {
|
|
113
|
+
use std::io::Read;
|
|
114
|
+
|
|
115
|
+
if !std::fs::metadata(path).ok()?.is_file() {
|
|
116
|
+
return None;
|
|
117
|
+
}
|
|
118
|
+
let mut bytes = Vec::new();
|
|
119
|
+
std::fs::File::open(path)
|
|
120
|
+
.ok()?
|
|
121
|
+
.take(PREVIEW_BYTES)
|
|
122
|
+
.read_to_end(&mut bytes)
|
|
123
|
+
.ok()?;
|
|
124
|
+
let text = String::from_utf8_lossy(&bytes);
|
|
125
|
+
let lines: Vec<&str> = text.lines().collect();
|
|
126
|
+
let cut = lines.len() > PREVIEW_LINES || bytes.len() as u64 == PREVIEW_BYTES;
|
|
127
|
+
let shown = lines[..lines.len().min(PREVIEW_LINES)].join("\n");
|
|
128
|
+
Some(if cut {
|
|
129
|
+
format!("{shown}\n... (truncated)")
|
|
130
|
+
} else {
|
|
131
|
+
shown + "\n"
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/// How far to scroll so the newest line is at the bottom of a pane.
|
|
136
|
+
pub fn scroll_to_bottom(content_height: usize, pane_height: u16) -> u16 {
|
|
137
|
+
let hidden = content_height.saturating_sub(usize::from(pane_height));
|
|
138
|
+
u16::try_from(hidden).unwrap_or(u16::MAX)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
#[derive(Debug, PartialEq)]
|
|
142
|
+
pub enum PromptKey {
|
|
143
|
+
Submit,
|
|
144
|
+
Quit,
|
|
145
|
+
Push(char),
|
|
146
|
+
Pop,
|
|
147
|
+
Ignore,
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/// What a key does while a prompt waits. Ctrl+C and Ctrl+D end input, which
|
|
151
|
+
/// refuses a pending permission prompt; no other Ctrl+letter is typed as text.
|
|
152
|
+
pub fn prompt_key(code: KeyCode, modifiers: KeyModifiers) -> PromptKey {
|
|
153
|
+
let control = modifiers.contains(KeyModifiers::CONTROL);
|
|
154
|
+
match code {
|
|
155
|
+
KeyCode::Enter => PromptKey::Submit,
|
|
156
|
+
KeyCode::Char('c' | 'd') if control => PromptKey::Quit,
|
|
157
|
+
KeyCode::Char(_) if control => PromptKey::Ignore,
|
|
158
|
+
KeyCode::Char(c) => PromptKey::Push(c),
|
|
159
|
+
KeyCode::Backspace => PromptKey::Pop,
|
|
160
|
+
_ => PromptKey::Ignore,
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
#[cfg(test)]
|
|
165
|
+
mod tests {
|
|
166
|
+
use super::*;
|
|
167
|
+
|
|
168
|
+
fn app_with(rows: Vec<Row>) -> App {
|
|
169
|
+
let mut app = App::new();
|
|
170
|
+
app.rows = rows;
|
|
171
|
+
app
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
#[test]
|
|
175
|
+
fn keeps_line_breaks_in_a_reply() {
|
|
176
|
+
let app = app_with(vec![Row::Text("one\ntwo\n".into())]);
|
|
177
|
+
assert_eq!(app.render_rows().len(), 2);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
fn applied(json: &str) -> App {
|
|
181
|
+
let mut app = App::new();
|
|
182
|
+
app.apply(serde_json::from_str(json).unwrap());
|
|
183
|
+
app
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
#[test]
|
|
187
|
+
fn says_an_error_as_an_error() {
|
|
188
|
+
assert!(matches!(
|
|
189
|
+
applied(r#"{"type":"say","text":"bad","as":"error"}"#).rows[0],
|
|
190
|
+
Row::Error(_)
|
|
191
|
+
));
|
|
192
|
+
assert!(matches!(
|
|
193
|
+
applied(r#"{"type":"oops","message":"bad"}"#).rows[0],
|
|
194
|
+
Row::Error(_)
|
|
195
|
+
));
|
|
196
|
+
assert!(matches!(
|
|
197
|
+
applied(r#"{"type":"say","text":"ok"}"#).rows[0],
|
|
198
|
+
Row::Text(_)
|
|
199
|
+
));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
#[test]
|
|
203
|
+
fn displays_a_file_or_its_fallback() {
|
|
204
|
+
let path = std::env::temp_dir().join(format!("elelem-ratatui-{}.txt", std::process::id()));
|
|
205
|
+
std::fs::write(&path, "contents\n").unwrap();
|
|
206
|
+
let shown = applied(&format!(
|
|
207
|
+
r#"{{"type":"display_file","path":{:?}}}"#,
|
|
208
|
+
path.to_str().unwrap()
|
|
209
|
+
));
|
|
210
|
+
std::fs::remove_file(&path).unwrap();
|
|
211
|
+
assert!(matches!(&shown.rows[0], Row::Text(t) if t == "contents\n"));
|
|
212
|
+
|
|
213
|
+
let missing = applied(r#"{"type":"display_file","path":"/nope","fallback":" -> /nope"}"#);
|
|
214
|
+
assert!(matches!(&missing.rows[0], Row::Text(t) if t == " -> /nope"));
|
|
215
|
+
|
|
216
|
+
let dir = applied(r#"{"type":"display_file","path":"/","fallback":" -> /"}"#);
|
|
217
|
+
assert!(matches!(&dir.rows[0], Row::Text(t) if t == " -> /"));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
#[test]
|
|
221
|
+
fn shows_only_the_start_of_a_long_file() {
|
|
222
|
+
let path =
|
|
223
|
+
std::env::temp_dir().join(format!("elelem-ratatui-long-{}.txt", std::process::id()));
|
|
224
|
+
std::fs::write(&path, "x\n".repeat(2000)).unwrap();
|
|
225
|
+
let text = preview(path.to_str().unwrap()).unwrap();
|
|
226
|
+
std::fs::remove_file(&path).unwrap();
|
|
227
|
+
assert_eq!(text.lines().count(), PREVIEW_LINES + 1);
|
|
228
|
+
assert!(text.ends_with("... (truncated)"));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
#[test]
|
|
232
|
+
fn scrolls_so_the_last_line_shows() {
|
|
233
|
+
assert_eq!(scroll_to_bottom(30, 10), 20);
|
|
234
|
+
assert_eq!(scroll_to_bottom(5, 10), 0);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
#[test]
|
|
238
|
+
fn ctrl_c_and_ctrl_d_quit_at_a_prompt() {
|
|
239
|
+
assert_eq!(
|
|
240
|
+
prompt_key(KeyCode::Char('c'), KeyModifiers::CONTROL),
|
|
241
|
+
PromptKey::Quit
|
|
242
|
+
);
|
|
243
|
+
assert_eq!(
|
|
244
|
+
prompt_key(KeyCode::Char('d'), KeyModifiers::CONTROL),
|
|
245
|
+
PromptKey::Quit
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
#[test]
|
|
250
|
+
fn other_control_keys_are_not_typed() {
|
|
251
|
+
assert_eq!(
|
|
252
|
+
prompt_key(KeyCode::Char('x'), KeyModifiers::CONTROL),
|
|
253
|
+
PromptKey::Ignore
|
|
254
|
+
);
|
|
255
|
+
assert_eq!(
|
|
256
|
+
prompt_key(KeyCode::Char('x'), KeyModifiers::NONE),
|
|
257
|
+
PromptKey::Push('x')
|
|
258
|
+
);
|
|
259
|
+
assert_eq!(
|
|
260
|
+
prompt_key(KeyCode::Char('X'), KeyModifiers::SHIFT),
|
|
261
|
+
PromptKey::Push('X')
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
}
|
data/rust/src/main.rs
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
mod app;
|
|
2
|
+
mod protocol;
|
|
3
|
+
|
|
4
|
+
use app::{App, PromptKey};
|
|
5
|
+
use crossterm::event::{self, Event as TermEvent, KeyCode, KeyEventKind};
|
|
6
|
+
use crossterm::terminal::{
|
|
7
|
+
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
|
|
8
|
+
};
|
|
9
|
+
use crossterm::ExecutableCommand;
|
|
10
|
+
use protocol::{Event, Line};
|
|
11
|
+
use ratatui::layout::{Constraint, Layout};
|
|
12
|
+
use ratatui::widgets::{Block, Borders, Paragraph};
|
|
13
|
+
use ratatui::Terminal;
|
|
14
|
+
use std::fs::{File, OpenOptions};
|
|
15
|
+
use std::io::{self, BufRead, Write};
|
|
16
|
+
use std::sync::mpsc;
|
|
17
|
+
use std::sync::{Arc, Mutex};
|
|
18
|
+
use std::thread;
|
|
19
|
+
use std::time::Duration;
|
|
20
|
+
|
|
21
|
+
fn write_reply(stdout: &Arc<Mutex<io::Stdout>>, line: Option<String>) {
|
|
22
|
+
let payload = serde_json::to_string(&Line { line }).unwrap();
|
|
23
|
+
let mut out = stdout.lock().unwrap();
|
|
24
|
+
let _ = writeln!(out, "{payload}");
|
|
25
|
+
let _ = out.flush();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
fn open_tty() -> io::Result<File> {
|
|
29
|
+
OpenOptions::new().read(true).write(true).open("/dev/tty")
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fn main() -> io::Result<()> {
|
|
33
|
+
let stdout_handle = Arc::new(Mutex::new(io::stdout()));
|
|
34
|
+
|
|
35
|
+
let (tx, rx) = mpsc::channel::<Event>();
|
|
36
|
+
thread::spawn(move || {
|
|
37
|
+
let stdin = io::stdin();
|
|
38
|
+
for line in stdin.lock().lines() {
|
|
39
|
+
let Ok(line) = line else { break };
|
|
40
|
+
if line.trim().is_empty() {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
match serde_json::from_str::<Event>(&line) {
|
|
44
|
+
Ok(event) => {
|
|
45
|
+
if tx.send(event).is_err() {
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
Err(err) => eprintln!("elelem-ratatui: bad event: {err}"),
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
let mut tty = open_tty()?;
|
|
55
|
+
enable_raw_mode()?;
|
|
56
|
+
tty.execute(EnterAlternateScreen)?;
|
|
57
|
+
|
|
58
|
+
let backend_tty = tty.try_clone()?;
|
|
59
|
+
let terminal = Terminal::new(ratatui::backend::CrosstermBackend::new(backend_tty));
|
|
60
|
+
let result = match terminal {
|
|
61
|
+
Ok(mut terminal) => {
|
|
62
|
+
let mut app = App::new();
|
|
63
|
+
run(&mut terminal, &mut app, &rx, &stdout_handle)
|
|
64
|
+
}
|
|
65
|
+
Err(err) => Err(err),
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
disable_raw_mode()?;
|
|
69
|
+
tty.execute(LeaveAlternateScreen)?;
|
|
70
|
+
result
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
fn run(
|
|
74
|
+
terminal: &mut Terminal<ratatui::backend::CrosstermBackend<File>>,
|
|
75
|
+
app: &mut App,
|
|
76
|
+
rx: &mpsc::Receiver<Event>,
|
|
77
|
+
stdout_handle: &Arc<Mutex<io::Stdout>>,
|
|
78
|
+
) -> io::Result<()> {
|
|
79
|
+
loop {
|
|
80
|
+
while let Ok(event) = rx.try_recv() {
|
|
81
|
+
app.apply(event);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if app.should_quit {
|
|
85
|
+
return Ok(());
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
terminal.draw(|f| draw(f, app))?;
|
|
89
|
+
|
|
90
|
+
if event::poll(Duration::from_millis(50))? {
|
|
91
|
+
if let TermEvent::Key(key) = event::read()? {
|
|
92
|
+
if key.kind != KeyEventKind::Press {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if app.pending_prompt.is_some() {
|
|
97
|
+
match app::prompt_key(key.code, key.modifiers) {
|
|
98
|
+
PromptKey::Submit => {
|
|
99
|
+
let line = std::mem::take(&mut app.input);
|
|
100
|
+
app.pending_prompt = None;
|
|
101
|
+
write_reply(stdout_handle, Some(line));
|
|
102
|
+
}
|
|
103
|
+
PromptKey::Quit => {
|
|
104
|
+
app.pending_prompt = None;
|
|
105
|
+
app.should_quit = true;
|
|
106
|
+
write_reply(stdout_handle, None);
|
|
107
|
+
}
|
|
108
|
+
PromptKey::Push(c) => app.input.push(c),
|
|
109
|
+
PromptKey::Pop => {
|
|
110
|
+
app.input.pop();
|
|
111
|
+
}
|
|
112
|
+
PromptKey::Ignore => {}
|
|
113
|
+
}
|
|
114
|
+
} else if key.code == KeyCode::Char('c')
|
|
115
|
+
&& key
|
|
116
|
+
.modifiers
|
|
117
|
+
.contains(crossterm::event::KeyModifiers::CONTROL)
|
|
118
|
+
{
|
|
119
|
+
app.should_quit = true;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
fn draw(frame: &mut ratatui::Frame, app: &App) {
|
|
127
|
+
let area = frame.area();
|
|
128
|
+
let chunks = Layout::vertical([Constraint::Min(1), Constraint::Length(3)]).split(area);
|
|
129
|
+
|
|
130
|
+
let title = if app.waiting {
|
|
131
|
+
"elelem (waiting)"
|
|
132
|
+
} else {
|
|
133
|
+
"elelem"
|
|
134
|
+
};
|
|
135
|
+
let log = Paragraph::new(app.render_rows())
|
|
136
|
+
.block(Block::default().borders(Borders::ALL).title(title))
|
|
137
|
+
.wrap(ratatui::widgets::Wrap { trim: false });
|
|
138
|
+
// Follow the newest output. line_count includes the borders, so the pane's
|
|
139
|
+
// full height is what it has to fit in.
|
|
140
|
+
let offset = app::scroll_to_bottom(log.line_count(chunks[0].width), chunks[0].height);
|
|
141
|
+
let log = log.scroll((offset, 0));
|
|
142
|
+
frame.render_widget(log, chunks[0]);
|
|
143
|
+
|
|
144
|
+
let prompt_title = app
|
|
145
|
+
.pending_prompt
|
|
146
|
+
.as_deref()
|
|
147
|
+
.unwrap_or("(no prompt pending)");
|
|
148
|
+
let input = Paragraph::new(app.input.as_str())
|
|
149
|
+
.block(Block::default().borders(Borders::ALL).title(prompt_title));
|
|
150
|
+
frame.render_widget(input, chunks[1]);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
#[cfg(test)]
|
|
154
|
+
mod tests {
|
|
155
|
+
use super::*;
|
|
156
|
+
use app::Row;
|
|
157
|
+
use ratatui::backend::TestBackend;
|
|
158
|
+
|
|
159
|
+
#[test]
|
|
160
|
+
fn the_newest_line_is_on_screen_when_the_log_is_full() {
|
|
161
|
+
let mut app = App::new();
|
|
162
|
+
for i in 0..40 {
|
|
163
|
+
app.rows.push(Row::Text(format!(
|
|
164
|
+
"line {i} with some words that wrap in a narrow pane"
|
|
165
|
+
)));
|
|
166
|
+
}
|
|
167
|
+
app.rows.push(Row::Text("the newest line".into()));
|
|
168
|
+
|
|
169
|
+
let mut terminal = Terminal::new(TestBackend::new(30, 12)).unwrap();
|
|
170
|
+
terminal.draw(|f| draw(f, &app)).unwrap();
|
|
171
|
+
|
|
172
|
+
let screen: String = terminal
|
|
173
|
+
.backend()
|
|
174
|
+
.buffer()
|
|
175
|
+
.content()
|
|
176
|
+
.iter()
|
|
177
|
+
.map(|c| c.symbol())
|
|
178
|
+
.collect();
|
|
179
|
+
assert!(screen.contains("the newest line"), "{screen}");
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
use serde::{Deserialize, Serialize};
|
|
2
|
+
|
|
3
|
+
#[derive(Debug, Deserialize)]
|
|
4
|
+
#[serde(tag = "type")]
|
|
5
|
+
pub enum Event {
|
|
6
|
+
#[serde(rename = "say")]
|
|
7
|
+
Say {
|
|
8
|
+
text: String,
|
|
9
|
+
#[serde(rename = "as", default)]
|
|
10
|
+
kind: Option<String>,
|
|
11
|
+
},
|
|
12
|
+
#[serde(rename = "print")]
|
|
13
|
+
Print { text: String },
|
|
14
|
+
#[serde(rename = "thinking")]
|
|
15
|
+
Thinking { text: String },
|
|
16
|
+
#[serde(rename = "doing")]
|
|
17
|
+
Doing { header: String },
|
|
18
|
+
#[serde(rename = "waiting")]
|
|
19
|
+
Waiting,
|
|
20
|
+
#[serde(rename = "oops")]
|
|
21
|
+
Oops { message: String },
|
|
22
|
+
#[serde(rename = "display_file")]
|
|
23
|
+
DisplayFile {
|
|
24
|
+
path: String,
|
|
25
|
+
fallback: Option<String>,
|
|
26
|
+
},
|
|
27
|
+
#[serde(rename = "ask")]
|
|
28
|
+
Ask { prompt: String },
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
#[derive(Debug, Serialize)]
|
|
32
|
+
pub struct Line {
|
|
33
|
+
pub line: Option<String>,
|
|
34
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../../../lib/elelem/ratatui/output"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Elelem::Ratatui::Output do
|
|
6
|
+
subject(:output) { described_class.new(session: session) }
|
|
7
|
+
|
|
8
|
+
let(:sent) { [] }
|
|
9
|
+
let(:session) { double("session").tap { |s| allow(s).to receive(:emit) { |payload| sent << payload } } }
|
|
10
|
+
|
|
11
|
+
it "sends an error as oops, so the UI shows it as one" do
|
|
12
|
+
output.say("bad", as: :error)
|
|
13
|
+
|
|
14
|
+
expect(sent).to eq([{ type: "oops", message: "bad" }])
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "passes as through for other text" do
|
|
18
|
+
output.say("**hi**", as: :markdown)
|
|
19
|
+
|
|
20
|
+
expect(sent).to eq([{ type: "say", text: "**hi**", as: "markdown" }])
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "sends display_file with its path, for the UI to show the file" do
|
|
24
|
+
output.display_file("~/a.rb", fallback: " -> ~/a.rb")
|
|
25
|
+
|
|
26
|
+
expect(sent).to eq([{ type: "display_file", path: File.expand_path("~/a.rb"), fallback: " -> ~/a.rb" }])
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "open3"
|
|
5
|
+
require_relative "../../../lib/elelem/ratatui/session"
|
|
6
|
+
|
|
7
|
+
RSpec.describe Elelem::Ratatui::Session do
|
|
8
|
+
subject(:session) { described_class.new }
|
|
9
|
+
|
|
10
|
+
let(:to_ui) { IO.pipe }
|
|
11
|
+
let(:from_ui) { IO.pipe }
|
|
12
|
+
|
|
13
|
+
before do
|
|
14
|
+
allow(File).to receive(:executable?).and_return(true)
|
|
15
|
+
allow(Open3).to receive(:popen2).and_return([to_ui[1], from_ui[0], Thread.new {}])
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "returns the answer the UI sends" do
|
|
19
|
+
from_ui[1].puts(JSON.generate(line: "y"))
|
|
20
|
+
|
|
21
|
+
expect(session.ask("Allow?")).to eq("y")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "refuses with nil when the UI quits, then exits on the next output" do
|
|
25
|
+
from_ui[1].puts(JSON.generate(line: nil))
|
|
26
|
+
|
|
27
|
+
expect(session.ask("Allow?")).to be_nil
|
|
28
|
+
expect { session.emit(type: "say", text: "after") }.to raise_error(SystemExit)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "exits when the UI is gone" do
|
|
32
|
+
to_ui[0].close
|
|
33
|
+
|
|
34
|
+
expect { session.emit(type: "say", text: "x") }.to raise_error(SystemExit)
|
|
35
|
+
end
|
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: elelem-ratatui
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- mo khan
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: elelem
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.13'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '1'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '0.13'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '1'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: json
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - "~>"
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '3.0'
|
|
39
|
+
type: :runtime
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - "~>"
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '3.0'
|
|
46
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: open3
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - "~>"
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '0.2'
|
|
53
|
+
type: :runtime
|
|
54
|
+
prerelease: false
|
|
55
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - "~>"
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '0.2'
|
|
60
|
+
description: Overrides the default terminal with a ratatui terminal
|
|
61
|
+
email:
|
|
62
|
+
- mo@mokhan.ca
|
|
63
|
+
executables:
|
|
64
|
+
- elelem-ratatui
|
|
65
|
+
extensions:
|
|
66
|
+
- ext/elelem/ratatui/extconf.rb
|
|
67
|
+
extra_rdoc_files: []
|
|
68
|
+
files:
|
|
69
|
+
- ".agents/backlog/001-run-without-dev-tty.md"
|
|
70
|
+
- ".agents/backlog/002-manual-scrollback.md"
|
|
71
|
+
- ".agents/backlog/003-quit-from-any-thread.md"
|
|
72
|
+
- ".agents/backlog/004-readme.md"
|
|
73
|
+
- ".agents/backlog/005-gem-metadata-links.md"
|
|
74
|
+
- ".github/workflows/release.yml"
|
|
75
|
+
- ".release-please-manifest.json"
|
|
76
|
+
- ".rspec"
|
|
77
|
+
- ".rubocop.yml"
|
|
78
|
+
- CHANGELOG.md
|
|
79
|
+
- LICENSE.txt
|
|
80
|
+
- Rakefile
|
|
81
|
+
- exe/elelem-ratatui
|
|
82
|
+
- ext/elelem/ratatui/extconf.rb
|
|
83
|
+
- lib/elelem/ratatui.rb
|
|
84
|
+
- lib/elelem/ratatui/input.rb
|
|
85
|
+
- lib/elelem/ratatui/output.rb
|
|
86
|
+
- lib/elelem/ratatui/session.rb
|
|
87
|
+
- lib/elelem/ratatui/version.rb
|
|
88
|
+
- release-please-config.json
|
|
89
|
+
- rust/Cargo.lock
|
|
90
|
+
- rust/Cargo.toml
|
|
91
|
+
- rust/src/app.rs
|
|
92
|
+
- rust/src/main.rs
|
|
93
|
+
- rust/src/protocol.rs
|
|
94
|
+
- spec/elelem/ratatui/output_spec.rb
|
|
95
|
+
- spec/elelem/ratatui/session_spec.rb
|
|
96
|
+
- spec/spec_helper.rb
|
|
97
|
+
homepage: https://elelem.sh
|
|
98
|
+
licenses:
|
|
99
|
+
- MIT
|
|
100
|
+
metadata:
|
|
101
|
+
allowed_push_host: https://rubygems.org
|
|
102
|
+
homepage_uri: https://elelem.sh
|
|
103
|
+
source_code_uri: https://github.com/elelem-sh/ratatui
|
|
104
|
+
rubygems_mfa_required: 'true'
|
|
105
|
+
rdoc_options: []
|
|
106
|
+
require_paths:
|
|
107
|
+
- lib
|
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
|
+
requirements:
|
|
110
|
+
- - ">="
|
|
111
|
+
- !ruby/object:Gem::Version
|
|
112
|
+
version: 4.0.0
|
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - ">="
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
requirements: []
|
|
119
|
+
rubygems_version: 4.0.6
|
|
120
|
+
specification_version: 4
|
|
121
|
+
summary: A ratatui-based terminal UI for elelem.
|
|
122
|
+
test_files: []
|