Skip to content

Commit 50b9121

Browse files
feat: add support for multiline input, for lines ending with a backslash (#831)
1 parent 58537aa commit 50b9121

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

crates/q_cli/src/cli/chat/input_source.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,32 @@ impl InputSource {
3535

3636
pub fn read_line(&mut self, prompt: Option<&str>) -> Result<Option<String>, ReadlineError> {
3737
match &mut self.0 {
38-
inner::Inner::Readline(rl) => loop {
39-
let line = rl.readline(prompt.unwrap_or(""));
40-
match line {
41-
Ok(line) => {
42-
if line.trim().is_empty() {
43-
continue;
44-
}
45-
let _ = rl.add_history_entry(line.as_str());
46-
return Ok(Some(line));
47-
},
48-
Err(ReadlineError::Interrupted | ReadlineError::Eof) => {
49-
return Ok(None);
50-
},
51-
Err(err) => {
52-
return Err(err);
53-
},
38+
inner::Inner::Readline(rl) => {
39+
let mut prompt = prompt.unwrap_or_default();
40+
let mut line = String::new();
41+
loop {
42+
let curr_line = rl.readline(prompt);
43+
match curr_line {
44+
Ok(l) => {
45+
if l.trim().is_empty() {
46+
continue;
47+
} else if l.ends_with("\\") {
48+
line.push_str(&l);
49+
prompt = ">> ";
50+
continue;
51+
} else {
52+
line.push_str(&l);
53+
let _ = rl.add_history_entry(line.as_str());
54+
return Ok(Some(line));
55+
}
56+
},
57+
Err(ReadlineError::Interrupted | ReadlineError::Eof) => {
58+
return Ok(None);
59+
},
60+
Err(err) => {
61+
return Err(err);
62+
},
63+
}
5464
}
5565
},
5666
inner::Inner::Mock { index, lines } => {

0 commit comments

Comments
 (0)