Skip to content

Commit a2b4b93

Browse files
Copilotyiwang
andcommitted
feat(gen): Replace raw stdin loop with rustyline for better interactive input
Co-authored-by: yiwang <142937+yiwang@users.noreply.github.com>
1 parent b4e7371 commit a2b4b93

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

Cargo.lock

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

crates/gen/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ gltf-json = { version = "1", features = ["names"] }
3535
# Path resolution
3636
shellexpand = { workspace = true }
3737

38+
# CLI line editor
39+
rustyline = "17.0.2"
40+
3841
# Procedural audio (environmental soundscapes)
3942
fundsp = "0.20"
4043
cpal = "0.15"

crates/gen/src/main.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ async fn run_agent_loop(
124124
use localgpt_core::agent::Agent;
125125
use localgpt_core::agent::tools::create_safe_tools;
126126
use localgpt_core::memory::MemoryManager;
127-
use std::io::{self, Write};
127+
use rustyline::DefaultEditor;
128+
use rustyline::error::ReadlineError;
128129
use std::sync::Arc;
129130

130131
// Set up memory
@@ -147,20 +148,33 @@ async fn run_agent_loop(
147148
}
148149

149150
// Interactive loop
150-
let stdin = io::stdin();
151+
let mut rl = DefaultEditor::new()?;
151152
loop {
152-
print!("> ");
153-
io::stdout().flush()?;
153+
let readline = rl.readline("> ");
154154

155-
let mut input = String::new();
156-
if stdin.read_line(&mut input)? == 0 {
157-
break; // EOF
158-
}
155+
let input = match readline {
156+
Ok(line) => line,
157+
Err(ReadlineError::Interrupted) => {
158+
println!("^C");
159+
continue;
160+
}
161+
Err(ReadlineError::Eof) => {
162+
break; // Ctrl+D
163+
}
164+
Err(err) => {
165+
eprintln!("Error: {:?}", err);
166+
break;
167+
}
168+
};
159169

160170
let input = input.trim();
161171
if input.is_empty() {
162172
continue;
163173
}
174+
175+
// Add to history
176+
let _ = rl.add_history_entry(input);
177+
164178
if input == "/quit" || input == "/exit" || input == "/q" {
165179
break;
166180
}

0 commit comments

Comments
 (0)