mirror of
https://github.com/kdheepak/taskwarrior-tui.git
synced 2025-08-25 08:47:18 +02:00
Merge pull request #179 from kdheepak/refactor-completion
This commit is contained in:
commit
2d7d3dba47
4 changed files with 228 additions and 67 deletions
11
Cargo.lock
generated
11
Cargo.lock
generated
|
@ -1209,6 +1209,16 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustyline-derive"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "db9dfbf470021de34cfaf6983067f460ea19164934a7c2d4b92eec0968eb95f1"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.5"
|
||||
|
@ -1388,6 +1398,7 @@ dependencies = [
|
|||
"rand",
|
||||
"regex",
|
||||
"rustyline",
|
||||
"rustyline-derive",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"shellexpand",
|
||||
|
|
|
@ -33,6 +33,7 @@ lazy_static = "1.4.0"
|
|||
rand = "0.7"
|
||||
regex = "1"
|
||||
rustyline = "8"
|
||||
rustyline-derive = "0.4"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
shellexpand = "2.1"
|
||||
|
|
131
src/app.rs
131
src/app.rs
|
@ -1,5 +1,5 @@
|
|||
use crate::calendar::Calendar;
|
||||
use crate::completion::CompletionList;
|
||||
use crate::completion::{get_start_word_under_cursor, CompletionList};
|
||||
use crate::config;
|
||||
use crate::config::Config;
|
||||
use crate::context::Context;
|
||||
|
@ -58,6 +58,7 @@ use rustyline::line_buffer::LineBuffer;
|
|||
use rustyline::At;
|
||||
use rustyline::Editor;
|
||||
use rustyline::Word;
|
||||
use rustyline_derive::Helper;
|
||||
|
||||
use crate::history::HistoryContext;
|
||||
|
||||
|
@ -636,13 +637,17 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
|
||||
fn draw_completion_pop_up(&mut self, f: &mut Frame<impl Backend>, rect: Rect, cursor_position: usize) {
|
||||
if self.completion_list.candidates().is_empty() {
|
||||
self.show_completion_pane = false;
|
||||
return;
|
||||
}
|
||||
// Iterate through all elements in the `items` app and append some debug text to it.
|
||||
let items: Vec<ListItem> = self
|
||||
.completion_list
|
||||
.items
|
||||
.candidates()
|
||||
.iter()
|
||||
.map(|i| {
|
||||
let lines = vec![Spans::from(i.clone())];
|
||||
.map(|p| {
|
||||
let lines = vec![Spans::from(p.display.clone())];
|
||||
ListItem::new(lines).style(Style::default().fg(Color::Black))
|
||||
})
|
||||
.collect();
|
||||
|
@ -657,14 +662,11 @@ impl TaskwarriorTuiApp {
|
|||
let area = f.size();
|
||||
|
||||
let mut rect = rect;
|
||||
rect.height = std::cmp::min(area.height / 2, self.completion_list.items.len() as u16 + 2);
|
||||
rect.height = std::cmp::min(area.height / 2, self.completion_list.len() as u16 + 2);
|
||||
rect.width = std::cmp::min(
|
||||
area.width / 2,
|
||||
self.completion_list
|
||||
.items
|
||||
.iter()
|
||||
.map(|s| s.graphemes(true).count() + 4)
|
||||
.max()
|
||||
.max_width()
|
||||
.unwrap_or(40)
|
||||
.try_into()
|
||||
.unwrap_or(area.width / 2),
|
||||
|
@ -2144,12 +2146,9 @@ impl TaskwarriorTuiApp {
|
|||
Key::Char('\n') => {
|
||||
if self.show_completion_pane {
|
||||
self.show_completion_pane = false;
|
||||
if let Some(i) = self.completion_list.state.selected() {
|
||||
if i < self.completion_list.items.len() {
|
||||
let m = self.modify.as_str();
|
||||
let s = format!("{}{}", m, &self.completion_list.items[i]);
|
||||
self.modify.update(&s, s.graphemes(true).count());
|
||||
}
|
||||
if let Some(s) = self.completion_list.selected() {
|
||||
let s = format!("{}{}", self.modify.as_str(), &s);
|
||||
self.modify.update(&s, s.graphemes(true).count());
|
||||
}
|
||||
self.completion_list.unselect();
|
||||
} else {
|
||||
|
@ -2168,7 +2167,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::Tab => {
|
||||
if !self.completion_list.items.is_empty() {
|
||||
if !self.completion_list.is_empty() {
|
||||
if !self.show_completion_pane {
|
||||
self.show_completion_pane = true;
|
||||
}
|
||||
|
@ -2176,12 +2175,12 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::BackTab => {
|
||||
if !self.completion_list.items.is_empty() {
|
||||
if !self.completion_list.is_empty() {
|
||||
self.completion_list.previous();
|
||||
}
|
||||
}
|
||||
Key::Up => {
|
||||
if self.show_completion_pane && !self.completion_list.items.is_empty() {
|
||||
if self.show_completion_pane && !self.completion_list.is_empty() {
|
||||
self.completion_list.previous();
|
||||
} else if let Some(s) = self
|
||||
.command_history_context
|
||||
|
@ -2193,7 +2192,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::Down => {
|
||||
if self.show_completion_pane && !self.completion_list.items.is_empty() {
|
||||
if self.show_completion_pane && !self.completion_list.is_empty() {
|
||||
self.completion_list.next();
|
||||
} else if let Some(s) = self
|
||||
.command_history_context
|
||||
|
@ -2207,7 +2206,7 @@ impl TaskwarriorTuiApp {
|
|||
_ => {
|
||||
self.command_history_context.last();
|
||||
handle_movement(&mut self.modify, input);
|
||||
self.update_completion_list();
|
||||
self.update_input_for_completion();
|
||||
}
|
||||
},
|
||||
AppMode::TaskSubprocess => match input {
|
||||
|
@ -2241,11 +2240,9 @@ impl TaskwarriorTuiApp {
|
|||
Key::Char('\n') => {
|
||||
if self.show_completion_pane {
|
||||
self.show_completion_pane = false;
|
||||
if let Some(i) = self.completion_list.state.selected() {
|
||||
if i < self.completion_list.items.len() {
|
||||
let s = format!("{}{}", self.command.as_str(), &self.completion_list.items[i]);
|
||||
self.command.update(&s, s.graphemes(true).count());
|
||||
}
|
||||
if let Some(s) = self.completion_list.selected() {
|
||||
let s = format!("{}{}", self.command.as_str(), s);
|
||||
self.command.update(&s, s.graphemes(true).count());
|
||||
}
|
||||
self.completion_list.unselect();
|
||||
} else {
|
||||
|
@ -2264,7 +2261,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::Tab => {
|
||||
if !self.completion_list.items.is_empty() {
|
||||
if !self.completion_list.is_empty() {
|
||||
if !self.show_completion_pane {
|
||||
self.show_completion_pane = true;
|
||||
}
|
||||
|
@ -2272,12 +2269,12 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::BackTab => {
|
||||
if !self.completion_list.items.is_empty() {
|
||||
if !self.completion_list.is_empty() {
|
||||
self.completion_list.previous();
|
||||
}
|
||||
}
|
||||
Key::Up => {
|
||||
if self.show_completion_pane && !self.completion_list.items.is_empty() {
|
||||
if self.show_completion_pane && !self.completion_list.is_empty() {
|
||||
self.completion_list.previous();
|
||||
} else if let Some(s) = self
|
||||
.command_history_context
|
||||
|
@ -2289,7 +2286,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::Down => {
|
||||
if self.show_completion_pane && !self.completion_list.items.is_empty() {
|
||||
if self.show_completion_pane && !self.completion_list.is_empty() {
|
||||
self.completion_list.next();
|
||||
} else if let Some(s) = self
|
||||
.command_history_context
|
||||
|
@ -2303,6 +2300,7 @@ impl TaskwarriorTuiApp {
|
|||
_ => {
|
||||
self.command_history_context.last();
|
||||
handle_movement(&mut self.command, input);
|
||||
self.update_input_for_completion();
|
||||
}
|
||||
},
|
||||
AppMode::TaskAnnotate => match input {
|
||||
|
@ -2355,11 +2353,9 @@ impl TaskwarriorTuiApp {
|
|||
Key::Char('\n') => {
|
||||
if self.show_completion_pane {
|
||||
self.show_completion_pane = false;
|
||||
if let Some(i) = self.completion_list.state.selected() {
|
||||
if i < self.completion_list.items.len() {
|
||||
let s = format!("{}{}", self.command.as_str(), &self.completion_list.items[i]);
|
||||
self.command.update(&s, s.graphemes(true).count());
|
||||
}
|
||||
if let Some(s) = self.completion_list.selected() {
|
||||
let s = format!("{}{}", self.command.as_str(), s);
|
||||
self.command.update(&s, s.graphemes(true).count());
|
||||
}
|
||||
self.completion_list.unselect();
|
||||
} else {
|
||||
|
@ -2378,7 +2374,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::Tab => {
|
||||
if !self.completion_list.items.is_empty() {
|
||||
if !self.completion_list.is_empty() {
|
||||
if !self.show_completion_pane {
|
||||
self.show_completion_pane = true;
|
||||
}
|
||||
|
@ -2386,12 +2382,12 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::BackTab => {
|
||||
if !self.completion_list.items.is_empty() {
|
||||
if !self.completion_list.is_empty() {
|
||||
self.completion_list.previous();
|
||||
}
|
||||
}
|
||||
Key::Up => {
|
||||
if self.show_completion_pane && !self.completion_list.items.is_empty() {
|
||||
if self.show_completion_pane && !self.completion_list.is_empty() {
|
||||
self.completion_list.previous();
|
||||
} else if let Some(s) = self
|
||||
.command_history_context
|
||||
|
@ -2403,7 +2399,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::Down => {
|
||||
if self.show_completion_pane && !self.completion_list.items.is_empty() {
|
||||
if self.show_completion_pane && !self.completion_list.is_empty() {
|
||||
self.completion_list.next();
|
||||
} else if let Some(s) = self
|
||||
.command_history_context
|
||||
|
@ -2417,6 +2413,7 @@ impl TaskwarriorTuiApp {
|
|||
_ => {
|
||||
self.command_history_context.last();
|
||||
handle_movement(&mut self.command, input);
|
||||
self.update_input_for_completion();
|
||||
}
|
||||
},
|
||||
AppMode::TaskFilter => match input {
|
||||
|
@ -2433,11 +2430,9 @@ impl TaskwarriorTuiApp {
|
|||
Key::Char('\n') => {
|
||||
if self.show_completion_pane {
|
||||
self.show_completion_pane = false;
|
||||
if let Some(i) = self.completion_list.state.selected() {
|
||||
if i < self.completion_list.items.len() {
|
||||
let s = format!("{}{}", self.filter.as_str(), &self.completion_list.items[i]);
|
||||
self.filter.update(&s, s.graphemes(true).count());
|
||||
}
|
||||
if let Some(s) = self.completion_list.selected() {
|
||||
let s = format!("{}{}", self.filter.as_str(), s);
|
||||
self.filter.update(&s, s.graphemes(true).count());
|
||||
}
|
||||
self.completion_list.unselect();
|
||||
self.dirty = true;
|
||||
|
@ -2448,7 +2443,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::Up => {
|
||||
if self.show_completion_pane && !self.completion_list.items.is_empty() {
|
||||
if self.show_completion_pane && !self.completion_list.is_empty() {
|
||||
self.completion_list.previous();
|
||||
} else if let Some(s) = self
|
||||
.filter_history_context
|
||||
|
@ -2461,7 +2456,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::Down => {
|
||||
if self.show_completion_pane && !self.completion_list.items.is_empty() {
|
||||
if self.show_completion_pane && !self.completion_list.is_empty() {
|
||||
self.completion_list.previous();
|
||||
} else if let Some(s) = self
|
||||
.filter_history_context
|
||||
|
@ -2474,7 +2469,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::Tab => {
|
||||
if !self.completion_list.items.is_empty() {
|
||||
if !self.completion_list.is_empty() {
|
||||
if !self.show_completion_pane {
|
||||
self.show_completion_pane = true;
|
||||
}
|
||||
|
@ -2482,12 +2477,13 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::BackTab => {
|
||||
if !self.completion_list.items.is_empty() {
|
||||
if !self.completion_list.is_empty() {
|
||||
self.completion_list.previous();
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
handle_movement(&mut self.filter, input);
|
||||
self.update_input_for_completion();
|
||||
self.dirty = true;
|
||||
}
|
||||
},
|
||||
|
@ -2525,8 +2521,8 @@ impl TaskwarriorTuiApp {
|
|||
if let Some(tags) = task.tags() {
|
||||
for tag in tags {
|
||||
let t = format!("+{}", &tag);
|
||||
if !virtual_tags.contains(tag) && !self.completion_list.items.contains(&t) {
|
||||
self.completion_list.items.push(t);
|
||||
if !virtual_tags.contains(tag) {
|
||||
self.completion_list.insert(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2534,17 +2530,13 @@ impl TaskwarriorTuiApp {
|
|||
for task in self.tasks.iter() {
|
||||
if let Some(project) = task.project() {
|
||||
let p = format!("project:{}", &project);
|
||||
if !self.completion_list.items.contains(&p) {
|
||||
self.completion_list.items.push(p);
|
||||
}
|
||||
self.completion_list.insert(p);
|
||||
}
|
||||
}
|
||||
for task in self.tasks.iter() {
|
||||
if let Some(priority) = task.priority() {
|
||||
let p = format!("priority:{}", &priority);
|
||||
if !self.completion_list.items.contains(&p) {
|
||||
self.completion_list.items.push(p);
|
||||
}
|
||||
self.completion_list.insert(p);
|
||||
}
|
||||
}
|
||||
for task in self.tasks.iter() {
|
||||
|
@ -2560,9 +2552,7 @@ impl TaskwarriorTuiApp {
|
|||
date.minute(),
|
||||
date.second(),
|
||||
);
|
||||
if !self.completion_list.items.contains(&s) {
|
||||
self.completion_list.items.push(s);
|
||||
}
|
||||
self.completion_list.insert(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2577,14 +2567,33 @@ impl TaskwarriorTuiApp {
|
|||
"priority:".to_string(),
|
||||
"due:".to_string(),
|
||||
] {
|
||||
if !self.completion_list.items.contains(&s) {
|
||||
self.completion_list.items.push(s);
|
||||
}
|
||||
self.completion_list.insert(s);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_input_for_completion(&mut self) {
|
||||
match self.mode {
|
||||
AppMode::TaskAdd | AppMode::TaskLog => {
|
||||
let i = get_start_word_under_cursor(self.command.as_str(), self.command.pos());
|
||||
let input = self.command.as_str()[i..self.command.pos()].to_string();
|
||||
self.completion_list.input(input);
|
||||
}
|
||||
AppMode::TaskModify => {
|
||||
let i = get_start_word_under_cursor(self.modify.as_str(), self.modify.pos());
|
||||
let input = self.modify.as_str()[i..self.modify.pos()].to_string();
|
||||
self.completion_list.input(input);
|
||||
}
|
||||
AppMode::TaskFilter => {
|
||||
let i = get_start_word_under_cursor(self.filter.as_str(), self.filter.pos());
|
||||
let input = self.filter.as_str()[i..self.filter.pos()].to_string();
|
||||
self.completion_list.input(input);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_movement(linebuffer: &mut LineBuffer, input: Key) {
|
||||
|
|
|
@ -7,30 +7,104 @@ use tui::{
|
|||
Terminal,
|
||||
};
|
||||
|
||||
use rustyline::completion::{Completer, FilenameCompleter, Pair};
|
||||
use rustyline::error::ReadlineError;
|
||||
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
|
||||
use rustyline::hint::Hinter;
|
||||
use rustyline::line_buffer::LineBuffer;
|
||||
use rustyline::Context;
|
||||
use rustyline_derive::Helper;
|
||||
|
||||
use unicode_segmentation::Graphemes;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
pub fn get_start_word_under_cursor(line: &str, cursor_pos: usize) -> usize {
|
||||
let mut chars = line[..cursor_pos].chars();
|
||||
let mut res = cursor_pos;
|
||||
while let Some(c) = chars.next_back() {
|
||||
if c == ' ' || c == '(' || c == ')' {
|
||||
break;
|
||||
}
|
||||
res -= c.len_utf8();
|
||||
}
|
||||
// if iter == None, res == 0.
|
||||
res
|
||||
}
|
||||
|
||||
pub struct TaskwarriorTuiCompletionHelper {
|
||||
candidates: Vec<String>,
|
||||
completer: rustyline::completion::FilenameCompleter,
|
||||
}
|
||||
|
||||
impl Completer for TaskwarriorTuiCompletionHelper {
|
||||
type Candidate = Pair;
|
||||
|
||||
fn complete(&self, word: &str, pos: usize, _ctx: &Context) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
|
||||
let candidates: Vec<Pair> = self
|
||||
.candidates
|
||||
.iter()
|
||||
.filter_map(|candidate| {
|
||||
if candidate.starts_with(&word[..pos]) {
|
||||
Some(Pair {
|
||||
display: candidate.clone(),
|
||||
replacement: candidate[pos..].to_string(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Ok((pos, candidates))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CompletionList {
|
||||
pub state: ListState,
|
||||
pub items: Vec<String>,
|
||||
pub input: String,
|
||||
pub pos: usize,
|
||||
pub helper: TaskwarriorTuiCompletionHelper,
|
||||
}
|
||||
|
||||
impl CompletionList {
|
||||
pub fn new() -> CompletionList {
|
||||
let completer = FilenameCompleter::new();
|
||||
CompletionList {
|
||||
state: ListState::default(),
|
||||
items: Vec::new(),
|
||||
input: String::new(),
|
||||
pos: 0,
|
||||
helper: TaskwarriorTuiCompletionHelper {
|
||||
candidates: vec![],
|
||||
completer,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_items(items: Vec<String>) -> CompletionList {
|
||||
let completer = FilenameCompleter::new();
|
||||
let mut candidates = vec![];
|
||||
for i in items {
|
||||
if !candidates.contains(&i) {
|
||||
candidates.push(i);
|
||||
}
|
||||
}
|
||||
CompletionList {
|
||||
state: ListState::default(),
|
||||
items,
|
||||
input: String::new(),
|
||||
pos: 0,
|
||||
helper: TaskwarriorTuiCompletionHelper { candidates, completer },
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, item: String) {
|
||||
if !self.helper.candidates.contains(&item) {
|
||||
self.helper.candidates.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next(&mut self) {
|
||||
let i = match self.state.selected() {
|
||||
Some(i) => {
|
||||
if i >= self.items.len() - 1 {
|
||||
if i >= self.candidates().len() - 1 {
|
||||
0
|
||||
} else {
|
||||
i + 1
|
||||
|
@ -45,7 +119,7 @@ impl CompletionList {
|
|||
let i = match self.state.selected() {
|
||||
Some(i) => {
|
||||
if i == 0 {
|
||||
self.items.len() - 1
|
||||
self.candidates().len() - 1
|
||||
} else {
|
||||
i - 1
|
||||
}
|
||||
|
@ -60,7 +134,73 @@ impl CompletionList {
|
|||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.items.clear();
|
||||
self.helper.candidates.clear();
|
||||
self.state.select(None);
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.candidates().iter().count()
|
||||
}
|
||||
|
||||
pub fn max_width(&self) -> Option<usize> {
|
||||
self.candidates()
|
||||
.iter()
|
||||
.map(|p| p.display.graphemes(true).count() + 4)
|
||||
.max()
|
||||
}
|
||||
|
||||
pub fn get(&self, i: usize) -> Option<String> {
|
||||
let candidates = self.candidates();
|
||||
if i < candidates.len() {
|
||||
Some(candidates[i].replacement.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn selected(&self) -> Option<String> {
|
||||
if let Some(i) = self.state.selected() {
|
||||
self.get(i)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.helper.candidates.is_empty()
|
||||
}
|
||||
|
||||
pub fn candidates(&self) -> Vec<Pair> {
|
||||
let hist = rustyline::history::History::new();
|
||||
let ctx = rustyline::Context::new(&hist);
|
||||
let (pos, candidates) = self.helper.complete(&self.input, self.pos, &ctx).unwrap();
|
||||
candidates
|
||||
}
|
||||
|
||||
pub fn input(&mut self, input: String) {
|
||||
self.input = input;
|
||||
self.pos = self.input.len();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_completion() {
|
||||
let mut completion_list = CompletionList::new();
|
||||
|
||||
completion_list.insert("+test".to_string());
|
||||
completion_list.insert("+shortcut".to_string());
|
||||
completion_list.insert("project:color".to_string());
|
||||
completion_list.insert("due:'2021-04-07T00:00:00'".to_string());
|
||||
|
||||
completion_list.input("due:".to_string());
|
||||
|
||||
for p in completion_list.candidates().iter() {
|
||||
dbg!(format!("{:?}", p.display));
|
||||
dbg!(format!("{:?}", p.replacement));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue