mirror of
https://github.com/kdheepak/taskwarrior-tui.git
synced 2025-08-25 08:47:18 +02:00
Add history for add, log and modify
This commit is contained in:
parent
d4ac3561aa
commit
a1cd39a863
2 changed files with 108 additions and 28 deletions
132
src/app.rs
132
src/app.rs
|
@ -135,6 +135,7 @@ fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
|||
.split(popup_layout[1])[1]
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
pub enum AppMode {
|
||||
TaskReport,
|
||||
TaskFilter,
|
||||
|
@ -172,12 +173,14 @@ impl HistoryContext {
|
|||
}
|
||||
|
||||
pub fn history_search(&mut self, buf: &str, dir: HistoryDirection) -> Option<String> {
|
||||
if self.history_index == self.history.len() && dir == HistoryDirection::Forward
|
||||
|| self.history_index == 0 && dir == HistoryDirection::Reverse
|
||||
|| self.history.is_empty()
|
||||
{
|
||||
if self.history.is_empty() {
|
||||
return None;
|
||||
}
|
||||
if self.history_index == self.history.len().saturating_sub(1) && dir == HistoryDirection::Forward
|
||||
|| self.history_index == 0 && dir == HistoryDirection::Reverse
|
||||
{
|
||||
return Some(self.history.get(self.history_index).unwrap().clone());
|
||||
}
|
||||
let history_index = match dir {
|
||||
HistoryDirection::Reverse => self.history_index - 1,
|
||||
HistoryDirection::Forward => self.history_index + 1,
|
||||
|
@ -185,14 +188,22 @@ impl HistoryContext {
|
|||
if let Some(history_index) = self.history.starts_with(buf, history_index, dir) {
|
||||
self.history_index = history_index;
|
||||
Some(self.history.get(history_index).unwrap().clone())
|
||||
} else if buf.is_empty() {
|
||||
self.history_index = history_index;
|
||||
Some(self.history.get(history_index).unwrap().clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(&mut self, buf: &str) {
|
||||
self.history.add(buf);
|
||||
self.history_index = self.history.len() - 1;
|
||||
if self.history.add(buf) {
|
||||
self.history_index = self.history.len() - 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn last(&mut self) {
|
||||
self.history_index = self.history.len().saturating_sub(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -226,6 +237,7 @@ pub struct TaskwarriorTuiApp {
|
|||
pub terminal_width: u16,
|
||||
pub terminal_height: u16,
|
||||
pub filter_history_context: HistoryContext,
|
||||
pub command_history_context: HistoryContext,
|
||||
}
|
||||
|
||||
impl TaskwarriorTuiApp {
|
||||
|
@ -263,6 +275,7 @@ impl TaskwarriorTuiApp {
|
|||
terminal_width: w,
|
||||
terminal_height: h,
|
||||
filter_history_context: HistoryContext::new(),
|
||||
command_history_context: HistoryContext::new(),
|
||||
};
|
||||
for c in app.config.filter.chars() {
|
||||
app.filter.insert(c, 1);
|
||||
|
@ -318,9 +331,10 @@ impl TaskwarriorTuiApp {
|
|||
let area = centered_rect(50, 50, f.size());
|
||||
f.render_widget(Clear, area);
|
||||
let t = format!(
|
||||
"{} {:?}",
|
||||
self.filter.pos(),
|
||||
self.filter_history_context.history.iter().collect::<Vec<_>>()
|
||||
"{}\n{}\n{:?}",
|
||||
self.command.pos(),
|
||||
self.command_history_context.history_index,
|
||||
self.command_history_context.history.iter().collect::<Vec<_>>()
|
||||
);
|
||||
let p = Paragraph::new(Text::from(t))
|
||||
.block(Block::default().borders(Borders::ALL).border_type(BorderType::Rounded));
|
||||
|
@ -1287,10 +1301,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
let output = command.output();
|
||||
match output {
|
||||
Ok(_) => {
|
||||
self.command.update("", 0);
|
||||
Ok(())
|
||||
}
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(format!("Shell command `{}` exited with non-zero output", shell,)),
|
||||
}
|
||||
}
|
||||
|
@ -1316,10 +1327,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
let output = command.output();
|
||||
match output {
|
||||
Ok(_) => {
|
||||
self.command.update("", 0);
|
||||
Ok(())
|
||||
}
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(format!(
|
||||
"Cannot run `task log {}`. Check documentation for more information",
|
||||
shell
|
||||
|
@ -1407,7 +1415,6 @@ impl TaskwarriorTuiApp {
|
|||
match output {
|
||||
Ok(o) => {
|
||||
if o.status.success() {
|
||||
self.modify.update("", 0);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("Modify failed. {}", String::from_utf8_lossy(&o.stdout),))
|
||||
|
@ -1451,7 +1458,6 @@ impl TaskwarriorTuiApp {
|
|||
match output {
|
||||
Ok(o) => {
|
||||
if o.status.success() {
|
||||
self.command.update("", 0);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("Annotate failed. {}", String::from_utf8_lossy(&o.stdout),))
|
||||
|
@ -1485,10 +1491,7 @@ impl TaskwarriorTuiApp {
|
|||
}
|
||||
let output = command.output();
|
||||
match output {
|
||||
Ok(_) => {
|
||||
self.command.update("", 0);
|
||||
Ok(())
|
||||
}
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(format!(
|
||||
"Cannot run `task add {}`. Check documentation for more information",
|
||||
shell
|
||||
|
@ -2051,6 +2054,8 @@ impl TaskwarriorTuiApp {
|
|||
Key::Char('\n') => match self.task_modify() {
|
||||
Ok(_) => {
|
||||
self.mode = AppMode::TaskReport;
|
||||
self.command_history_context.add(self.modify.as_str());
|
||||
self.modify.update("", 0);
|
||||
self.update(true)?;
|
||||
}
|
||||
Err(e) => {
|
||||
|
@ -2058,16 +2063,40 @@ impl TaskwarriorTuiApp {
|
|||
self.error = e;
|
||||
}
|
||||
},
|
||||
Key::Up => {
|
||||
if let Some(s) = self
|
||||
.command_history_context
|
||||
.history_search(&self.modify.as_str()[..self.modify.pos()], HistoryDirection::Reverse)
|
||||
{
|
||||
let p = self.modify.pos();
|
||||
self.modify.update("", 0);
|
||||
self.modify.update(&s, p);
|
||||
}
|
||||
}
|
||||
Key::Down => {
|
||||
if let Some(s) = self
|
||||
.command_history_context
|
||||
.history_search(&self.modify.as_str()[..self.modify.pos()], HistoryDirection::Forward)
|
||||
{
|
||||
let p = self.modify.pos();
|
||||
self.modify.update("", 0);
|
||||
self.modify.update(&s, p);
|
||||
}
|
||||
}
|
||||
Key::Esc => {
|
||||
self.modify.update("", 0);
|
||||
self.mode = AppMode::TaskReport;
|
||||
}
|
||||
_ => handle_movement(&mut self.modify, input),
|
||||
_ => {
|
||||
self.command_history_context.last();
|
||||
handle_movement(&mut self.modify, input);
|
||||
}
|
||||
},
|
||||
AppMode::TaskSubprocess => match input {
|
||||
Key::Char('\n') => match self.task_subprocess() {
|
||||
Ok(_) => {
|
||||
self.mode = AppMode::TaskReport;
|
||||
self.command.update("", 0);
|
||||
self.update(true)?;
|
||||
}
|
||||
Err(e) => {
|
||||
|
@ -2085,6 +2114,8 @@ impl TaskwarriorTuiApp {
|
|||
Key::Char('\n') => match self.task_log() {
|
||||
Ok(_) => {
|
||||
self.mode = AppMode::TaskReport;
|
||||
self.command_history_context.add(self.command.as_str());
|
||||
self.command.update("", 0);
|
||||
self.update(true)?;
|
||||
}
|
||||
Err(e) => {
|
||||
|
@ -2092,16 +2123,40 @@ impl TaskwarriorTuiApp {
|
|||
self.error = e;
|
||||
}
|
||||
},
|
||||
Key::Up => {
|
||||
if let Some(s) = self
|
||||
.command_history_context
|
||||
.history_search(&self.command.as_str()[..self.command.pos()], HistoryDirection::Reverse)
|
||||
{
|
||||
let p = self.command.pos();
|
||||
self.command.update("", 0);
|
||||
self.command.update(&s, p);
|
||||
}
|
||||
}
|
||||
Key::Down => {
|
||||
if let Some(s) = self
|
||||
.command_history_context
|
||||
.history_search(&self.command.as_str()[..self.command.pos()], HistoryDirection::Forward)
|
||||
{
|
||||
let p = self.command.pos();
|
||||
self.command.update("", 0);
|
||||
self.command.update(&s, p);
|
||||
}
|
||||
}
|
||||
Key::Esc => {
|
||||
self.command.update("", 0);
|
||||
self.mode = AppMode::TaskReport;
|
||||
}
|
||||
_ => handle_movement(&mut self.command, input),
|
||||
_ => {
|
||||
self.command_history_context.last();
|
||||
handle_movement(&mut self.command, input);
|
||||
}
|
||||
},
|
||||
AppMode::TaskAnnotate => match input {
|
||||
Key::Char('\n') => match self.task_annotate() {
|
||||
Ok(_) => {
|
||||
self.mode = AppMode::TaskReport;
|
||||
self.command.update("", 0);
|
||||
self.update(true)?;
|
||||
}
|
||||
Err(e) => {
|
||||
|
@ -2119,6 +2174,8 @@ impl TaskwarriorTuiApp {
|
|||
Key::Char('\n') => match self.task_add() {
|
||||
Ok(_) => {
|
||||
self.mode = AppMode::TaskReport;
|
||||
self.command_history_context.add(self.command.as_str());
|
||||
self.command.update("", 0);
|
||||
self.update(true)?;
|
||||
}
|
||||
Err(e) => {
|
||||
|
@ -2126,11 +2183,34 @@ impl TaskwarriorTuiApp {
|
|||
self.error = e;
|
||||
}
|
||||
},
|
||||
Key::Up => {
|
||||
if let Some(s) = self
|
||||
.command_history_context
|
||||
.history_search(&self.command.as_str()[..self.command.pos()], HistoryDirection::Reverse)
|
||||
{
|
||||
let p = self.command.pos();
|
||||
self.command.update("", 0);
|
||||
self.command.update(&s, p);
|
||||
}
|
||||
}
|
||||
Key::Down => {
|
||||
if let Some(s) = self
|
||||
.command_history_context
|
||||
.history_search(&self.command.as_str()[..self.command.pos()], HistoryDirection::Forward)
|
||||
{
|
||||
let p = self.command.pos();
|
||||
self.command.update("", 0);
|
||||
self.command.update(&s, p);
|
||||
}
|
||||
}
|
||||
Key::Esc => {
|
||||
self.command.update("", 0);
|
||||
self.mode = AppMode::TaskReport;
|
||||
}
|
||||
_ => handle_movement(&mut self.command, input),
|
||||
_ => {
|
||||
self.command_history_context.last();
|
||||
handle_movement(&mut self.command, input);
|
||||
}
|
||||
},
|
||||
AppMode::TaskFilter => match input {
|
||||
Key::Char('\n') | Key::Esc => {
|
||||
|
|
|
@ -96,13 +96,13 @@ async fn tui_main(_config: &str) -> Result<()> {
|
|||
// Handle input
|
||||
match events.next().await? {
|
||||
Event::Input(input) => {
|
||||
if input == app.keyconfig.edit {
|
||||
if input == app.keyconfig.edit && app.mode == AppMode::TaskReport {
|
||||
events.leave_tui_mode(&mut terminal);
|
||||
}
|
||||
|
||||
let r = app.handle_input(input);
|
||||
|
||||
if input == app.keyconfig.edit {
|
||||
if input == app.keyconfig.edit && app.mode == AppMode::TaskReport {
|
||||
events.enter_tui_mode(&mut terminal);
|
||||
}
|
||||
if r.is_err() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue