Skip to content

Commit 56cc3dd

Browse files
committed
Add modified flag for completions
1 parent 9f96793 commit 56cc3dd

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/completion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/// Trait for completion handling.
22
pub trait Completion: Send {
3-
fn next(&mut self, input: &str) -> Option<String>;
3+
fn next(&mut self, input: &str, completion_modified: bool) -> Option<String>;
44
}

src/prompts/input.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ pub struct Input<'a, T> {
6565
history: Option<Arc<Mutex<&'a mut dyn History<T>>>>,
6666
#[cfg(feature = "completion")]
6767
completion: Option<Arc<Mutex<&'a mut dyn Completion>>>,
68+
#[cfg(feature = "completion")]
69+
completion_modified: bool,
6870
}
6971

7072
impl<T> Default for Input<'static, T> {
@@ -164,6 +166,8 @@ impl<'a, T> Input<'a, T> {
164166
history: None,
165167
#[cfg(feature = "completion")]
166168
completion: None,
169+
#[cfg(feature = "completion")]
170+
completion_modified: false,
167171
}
168172
}
169173

@@ -354,6 +358,10 @@ where
354358
}
355359

356360
term.flush()?;
361+
362+
if self.completion.is_some() {
363+
self.completion_modified = true;
364+
}
357365
}
358366
Key::Char(chr) if !chr.is_ascii_control() => {
359367
chars.insert(position, chr);
@@ -363,6 +371,10 @@ where
363371
term.write_str(&tail)?;
364372
term.move_cursor_left(tail.chars().count() - 1)?;
365373
term.flush()?;
374+
375+
if self.completion.is_some() {
376+
self.completion_modified = true;
377+
}
366378
}
367379
Key::ArrowLeft if position > 0 => {
368380
if (position + prompt_len) % term.size().1 as usize == 0 {
@@ -475,7 +487,11 @@ where
475487
Key::ArrowRight | Key::Tab => {
476488
if let Some(completion) = &mut self.completion {
477489
let input: String = chars.clone().into_iter().collect();
478-
if let Some(x) = completion.lock().unwrap().next(&input) {
490+
if let Some(x) = completion
491+
.lock()
492+
.unwrap()
493+
.next(&input, self.completion_modified)
494+
{
479495
term.clear_chars(chars.len())?;
480496
chars.clear();
481497
position = 0;
@@ -486,6 +502,8 @@ where
486502
term.write_str(&x)?;
487503
term.flush()?;
488504
}
505+
506+
self.completion_modified = false;
489507
}
490508
}
491509
#[cfg(feature = "history")]

0 commit comments

Comments
 (0)