mirror of
https://github.com/kdheepak/taskwarrior-tui.git
synced 2025-08-25 08:47:18 +02:00
feat: case insensitive completion ✨
This commit is contained in:
parent
19558ced3b
commit
73ff6cbe39
2 changed files with 54 additions and 37 deletions
49
src/app.rs
49
src/app.rs
|
@ -890,11 +890,8 @@ impl TaskwarriorTui {
|
|||
.iter()
|
||||
.map(|p| {
|
||||
let lines = vec![Spans::from(vec![
|
||||
Span::styled(
|
||||
p.display.replace(p.replacement.clone().as_str(), ""),
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
),
|
||||
Span::from(p.replacement.clone()),
|
||||
Span::styled(p.3.clone(), Style::default().add_modifier(Modifier::BOLD)),
|
||||
Span::from(p.4.clone()),
|
||||
])];
|
||||
ListItem::new(lines)
|
||||
})
|
||||
|
@ -2837,9 +2834,10 @@ impl TaskwarriorTui {
|
|||
Key::Char('\n') => {
|
||||
if self.show_completion_pane {
|
||||
self.show_completion_pane = false;
|
||||
if let Some(s) = self.completion_list.selected() {
|
||||
let s = format!("{}{}", self.modify.as_str(), &s);
|
||||
self.modify.update(&s, s.len());
|
||||
if let Some((i, (r, m, o, _, _))) = self.completion_list.selected() {
|
||||
let (before, after) = self.modify.as_str().split_at(self.modify.pos());
|
||||
let fs = format!("{}{}{}", before.trim_end_matches(&o), r, after);
|
||||
self.modify.update(&fs, self.modify.pos() + r.len() - o.len());
|
||||
}
|
||||
self.completion_list.unselect();
|
||||
} else if self.error.is_some() {
|
||||
|
@ -2958,10 +2956,10 @@ impl TaskwarriorTui {
|
|||
Key::Char('\n') => {
|
||||
if self.show_completion_pane {
|
||||
self.show_completion_pane = false;
|
||||
if let Some(s) = self.completion_list.selected() {
|
||||
if let Some((i, (r, m, o, _, _))) = self.completion_list.selected() {
|
||||
let (before, after) = self.command.as_str().split_at(self.command.pos());
|
||||
let fs = format!("{}{}{}", before, s, after);
|
||||
self.command.update(&fs, self.command.pos() + s.len());
|
||||
let fs = format!("{}{}{}", before.trim_end_matches(&o), r, after);
|
||||
self.command.update(&fs, self.command.pos() + r.len() - o.len());
|
||||
}
|
||||
self.completion_list.unselect();
|
||||
} else if self.error.is_some() {
|
||||
|
@ -3057,10 +3055,10 @@ impl TaskwarriorTui {
|
|||
Key::Char('\n') => {
|
||||
if self.show_completion_pane {
|
||||
self.show_completion_pane = false;
|
||||
if let Some(s) = self.completion_list.selected() {
|
||||
if let Some((i, (r, m, o, _, _))) = self.completion_list.selected() {
|
||||
let (before, after) = self.command.as_str().split_at(self.command.pos());
|
||||
let fs = format!("{}{}{}", before, s, after);
|
||||
self.command.update(&fs, self.command.pos() + s.len());
|
||||
let fs = format!("{}{}{}", before.trim_end_matches(&o), r, after);
|
||||
self.command.update(&fs, self.command.pos() + r.len() - o.len());
|
||||
}
|
||||
self.completion_list.unselect();
|
||||
} else if self.error.is_some() {
|
||||
|
@ -3181,10 +3179,10 @@ impl TaskwarriorTui {
|
|||
Key::Char('\n') => {
|
||||
if self.show_completion_pane {
|
||||
self.show_completion_pane = false;
|
||||
if let Some(s) = self.completion_list.selected() {
|
||||
if let Some((i, (r, m, o, _, _))) = self.completion_list.selected() {
|
||||
let (before, after) = self.command.as_str().split_at(self.command.pos());
|
||||
let fs = format!("{}{}{}", before, s, after);
|
||||
self.command.update(&fs, self.command.pos() + s.len());
|
||||
let fs = format!("{}{}{}", before.trim_end_matches(&o), r, after);
|
||||
self.command.update(&fs, self.command.pos() + r.len() - o.len());
|
||||
}
|
||||
self.completion_list.unselect();
|
||||
} else if self.error.is_some() {
|
||||
|
@ -3289,10 +3287,10 @@ impl TaskwarriorTui {
|
|||
Key::Char('\n') => {
|
||||
if self.show_completion_pane {
|
||||
self.show_completion_pane = false;
|
||||
if let Some(s) = self.completion_list.selected() {
|
||||
if let Some((i, (r, m, o, _, _))) = self.completion_list.selected() {
|
||||
let (before, after) = self.filter.as_str().split_at(self.filter.pos());
|
||||
let fs = format!("{}{}{}", before, s, after);
|
||||
self.filter.update(&fs, self.filter.pos() + s.len());
|
||||
let fs = format!("{}{}{}", before.trim_end_matches(&o), r, after);
|
||||
self.filter.update(&fs, self.filter.pos() + r.len() - o.len());
|
||||
}
|
||||
self.completion_list.unselect();
|
||||
self.dirty = true;
|
||||
|
@ -4824,15 +4822,24 @@ mod tests {
|
|||
// #[test]
|
||||
fn test_taskwarrior_tui_completion() {
|
||||
let mut app = TaskwarriorTui::new("next").unwrap();
|
||||
app.handle_input(Key::Char('z')).unwrap();
|
||||
app.mode = Mode::Tasks(Action::Add);
|
||||
app.update_completion_list();
|
||||
let input = "Wash car project:packing project:p";
|
||||
let input = "Wash car";
|
||||
for c in input.chars() {
|
||||
app.handle_input(Key::Char(c)).unwrap();
|
||||
}
|
||||
app.handle_input(Key::Ctrl('e')).unwrap();
|
||||
|
||||
let input = " project:CO";
|
||||
for c in input.chars() {
|
||||
app.handle_input(Key::Char(c)).unwrap();
|
||||
}
|
||||
|
||||
app.mode = Mode::Tasks(Action::Add);
|
||||
app.update_completion_list();
|
||||
app.handle_input(Key::Tab).unwrap();
|
||||
app.handle_input(Key::Char('\n')).unwrap();
|
||||
let backend = TestBackend::new(80, 50);
|
||||
let mut terminal = Terminal::new(backend).unwrap();
|
||||
terminal
|
||||
|
|
|
@ -8,7 +8,6 @@ use tui::{
|
|||
Terminal,
|
||||
};
|
||||
|
||||
use rustyline::completion::{Completer, FilenameCompleter, Pair};
|
||||
use rustyline::error::ReadlineError;
|
||||
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
|
||||
use rustyline::hint::Hinter;
|
||||
|
@ -38,19 +37,27 @@ pub struct TaskwarriorTuiCompletionHelper {
|
|||
pub input: String,
|
||||
}
|
||||
|
||||
impl Completer for TaskwarriorTuiCompletionHelper {
|
||||
type Candidate = Pair;
|
||||
type Completion = (String, String, String, String, String);
|
||||
|
||||
fn complete(&self, word: &str, pos: usize, _ctx: &Context) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
|
||||
let candidates: Vec<Pair> = self
|
||||
impl TaskwarriorTuiCompletionHelper {
|
||||
fn complete(&self, word: &str, pos: usize, _ctx: &Context) -> rustyline::Result<(usize, Vec<Completion>)> {
|
||||
let candidates: Vec<Completion> = self
|
||||
.candidates
|
||||
.iter()
|
||||
.filter_map(|(context, candidate)| {
|
||||
if context == &self.context && candidate.starts_with(&word[..pos]) && !self.input.contains(candidate) {
|
||||
Some(Pair {
|
||||
display: candidate.clone(),
|
||||
replacement: candidate[pos..].to_string(),
|
||||
})
|
||||
if context == &self.context
|
||||
&& (candidate.starts_with(&word[..pos])
|
||||
|| candidate.to_lowercase().starts_with(&word[..pos].to_lowercase()))
|
||||
&& (!self.input.contains(candidate)
|
||||
|| !self.input.to_lowercase().contains(&candidate.to_lowercase()))
|
||||
{
|
||||
Some((
|
||||
candidate.clone(), // display
|
||||
candidate.to_string(), // replacement
|
||||
word[..pos].to_string(), // original
|
||||
candidate[..pos].to_string(),
|
||||
candidate[pos..].to_string(),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -150,27 +157,30 @@ impl CompletionList {
|
|||
}
|
||||
|
||||
pub fn max_width(&self) -> Option<usize> {
|
||||
self.candidates().iter().map(|p| p.display.width() + 4).max()
|
||||
self.candidates().iter().map(|p| p.1.width() + 4).max()
|
||||
}
|
||||
|
||||
pub fn get(&self, i: usize) -> Option<String> {
|
||||
pub fn get(&self, i: usize) -> Option<Completion> {
|
||||
let candidates = self.candidates();
|
||||
if i < candidates.len() {
|
||||
Some(candidates[i].replacement.clone())
|
||||
Some(candidates[i].clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn selected(&self) -> Option<String> {
|
||||
self.state.selected().and_then(|i| self.get(i))
|
||||
pub fn selected(&self) -> Option<(usize, Completion)> {
|
||||
self.state
|
||||
.selected()
|
||||
.and_then(|i| self.get(i))
|
||||
.and_then(|s| Some((self.pos, s)))
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.candidates().is_empty()
|
||||
}
|
||||
|
||||
pub fn candidates(&self) -> Vec<Pair> {
|
||||
pub fn candidates(&self) -> Vec<Completion> {
|
||||
let hist = rustyline::history::History::new();
|
||||
let ctx = rustyline::Context::new(&hist);
|
||||
let (pos, candidates) = self.helper.complete(&self.current, self.pos, &ctx).unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue