mirror of
https://github.com/kdheepak/taskwarrior-tui.git
synced 2025-08-24 23:46:41 +02:00
Refactor completion
This commit is contained in:
parent
08c8710642
commit
92f9be8038
4 changed files with 150 additions and 63 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"
|
||||
|
|
94
src/app.rs
94
src/app.rs
|
@ -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;
|
||||
|
||||
|
@ -639,7 +640,6 @@ impl TaskwarriorTuiApp {
|
|||
// Iterate through all elements in the `items` app and append some debug text to it.
|
||||
let items: Vec<ListItem> = self
|
||||
.completion_list
|
||||
.items
|
||||
.iter()
|
||||
.map(|i| {
|
||||
let lines = vec![Spans::from(i.clone())];
|
||||
|
@ -657,14 +657,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 +2141,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 +2162,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 +2170,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 +2187,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
|
||||
|
@ -2241,11 +2235,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 +2256,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 +2264,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 +2281,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
|
||||
|
@ -2355,11 +2347,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 +2368,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 +2376,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 +2393,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
|
||||
|
@ -2433,11 +2423,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 +2436,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 +2449,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 +2462,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,7 +2470,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
}
|
||||
Key::BackTab => {
|
||||
if !self.completion_list.items.is_empty() {
|
||||
if !self.completion_list.is_empty() {
|
||||
self.completion_list.previous();
|
||||
}
|
||||
}
|
||||
|
@ -2525,8 +2513,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 +2522,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 +2544,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,9 +2559,7 @@ 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);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
@ -7,30 +7,93 @@ 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;
|
||||
|
||||
// find the beginning of the word in line which is currently under the cursor,
|
||||
// whose position is cursor_pos.
|
||||
//
|
||||
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 {
|
||||
hints: Vec<String>,
|
||||
}
|
||||
|
||||
impl Completer for TaskwarriorTuiCompletionHelper {
|
||||
type Candidate = String;
|
||||
|
||||
fn complete(&self, line: &str, pos: usize, _ctx: &Context) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
|
||||
let mut candidates: Vec<String> = self
|
||||
.hints
|
||||
.iter()
|
||||
.filter_map(|hint| {
|
||||
if pos > 0 && hint.starts_with(&line[..pos]) && !hint[pos..].contains(" ") {
|
||||
Some(hint[pos..].to_owned())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
candidates.sort();
|
||||
Ok((pos, candidates))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CompletionList {
|
||||
pub state: ListState,
|
||||
pub items: Vec<String>,
|
||||
pub helper: TaskwarriorTuiCompletionHelper,
|
||||
}
|
||||
|
||||
impl CompletionList {
|
||||
pub fn new() -> CompletionList {
|
||||
CompletionList {
|
||||
state: ListState::default(),
|
||||
items: Vec::new(),
|
||||
helper: TaskwarriorTuiCompletionHelper { hints: vec![] },
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_items(items: Vec<String>) -> CompletionList {
|
||||
let mut hints = vec![];
|
||||
for i in items {
|
||||
if !hints.contains(&i) {
|
||||
hints.push(i);
|
||||
}
|
||||
}
|
||||
CompletionList {
|
||||
state: ListState::default(),
|
||||
items,
|
||||
helper: TaskwarriorTuiCompletionHelper { hints },
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, item: String) {
|
||||
if !self.helper.hints.contains(&item) {
|
||||
self.helper.hints.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next(&mut self) {
|
||||
let i = match self.state.selected() {
|
||||
Some(i) => {
|
||||
if i >= self.items.len() - 1 {
|
||||
if i >= self.helper.hints.len() - 1 {
|
||||
0
|
||||
} else {
|
||||
i + 1
|
||||
|
@ -45,7 +108,7 @@ impl CompletionList {
|
|||
let i = match self.state.selected() {
|
||||
Some(i) => {
|
||||
if i == 0 {
|
||||
self.items.len() - 1
|
||||
self.helper.hints.len() - 1
|
||||
} else {
|
||||
i - 1
|
||||
}
|
||||
|
@ -60,7 +123,39 @@ impl CompletionList {
|
|||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.items.clear();
|
||||
self.helper.hints.clear();
|
||||
self.state.select(None);
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.helper.hints.iter().count()
|
||||
}
|
||||
|
||||
pub fn max_width(&self) -> Option<usize> {
|
||||
self.helper.hints.iter().map(|s| s.graphemes(true).count() + 4).max()
|
||||
}
|
||||
|
||||
pub fn get(&self, i: usize) -> Option<String> {
|
||||
if i < self.helper.hints.len() {
|
||||
Some(self.helper.hints[i].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.hints.is_empty()
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> std::slice::Iter<String> {
|
||||
self.helper.hints.iter()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue