diff --git a/src/main.rs b/src/main.rs index 0f4cb76..aa7d2a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,7 @@ pub struct Args { #[arg(short='p', long)] todo_path: Option, } + impl Args { pub fn is_cli(&self) -> bool { self.stdout | self.non_interactive | self.tree diff --git a/src/todo_list.rs b/src/todo_list.rs index f7f0883..e999f66 100644 --- a/src/todo_list.rs +++ b/src/todo_list.rs @@ -266,6 +266,21 @@ impl TodoList { self.undone.insert(0,todo); } + #[inline] + pub fn all_dependent_files(&mut self, path: &PathBuf, output: &mut Vec) -> Vec{ + let mut todos = [&mut self.undone.todos, &mut self.done.todos]; + + for todo in todos.iter_mut().flat_map(|v| v.iter_mut()) { + if let Some(todo_path) = todo.dependency_path(path) { + output.push(todo_path); + todo.dependencies.all_dependent_files(path, output); + } + } + + return output.clone(); + } + + #[inline] pub fn fix_undone(&mut self) { for index in 0..self.undone.todos.len() { @@ -346,6 +361,8 @@ impl TodoList { mod tests { use std::fs::{self, remove_file}; + use crate::fileio::todo_path; + use super::*; fn get_todo_list() -> TodoList { @@ -419,4 +436,12 @@ mod tests { assert_eq!(todo_list, sorted_list) } + + #[test] + fn test_something() { + let todo_path = todo_path().unwrap(); + let mut todo_list = TodoList::read(&todo_path, true, true); + let mut output = vec![]; + println!("{:?}", todo_list.all_dependent_files(&todo_path, &mut output)); + } } diff --git a/src/todo_list/todo.rs b/src/todo_list/todo.rs index befed96..ce521eb 100644 --- a/src/todo_list/todo.rs +++ b/src/todo_list/todo.rs @@ -212,7 +212,6 @@ impl Todo { DependencyType::None => {} } Ok(()) - // self.dependencies.write(&path.join(&self.dependency_name), false)?; } #[inline] @@ -220,14 +219,13 @@ impl Todo { if self.has_dependency() { return Err(TodoError::AlreadyExists) } - // let _ = self.remove_note(path); + if let Some(path) = self.dependency_path(path) { + let _ = self.remove_note(&path); + } self.dependency_name = Self::todolist_name(&self.hash()); - // if File::create(&path).is_err() { - // return Err(TodoError::DependencyCreationFailed) - // } self.dependency_type = DependencyType::TodoList; - self.dependencies = TodoList::read(&path, true, false); + self.dependencies = TodoList::new(); Ok(()) } diff --git a/src/tui/app.rs b/src/tui/app.rs index cfdeeac..4d5538b 100644 --- a/src/tui/app.rs +++ b/src/tui/app.rs @@ -635,7 +635,7 @@ impl<'a>App<'a>{ #[inline] pub fn update_editor(&mut self) -> io::Result { if self.module_enabled { - if event::poll(std::time::Duration::from_millis(500))? { + if event::poll(std::time::Duration::from_millis(self.module.update_time_ms()))? { self.enable_text_editor()? } } else { @@ -684,7 +684,7 @@ impl<'a>App<'a>{ #[inline] fn update_no_editor(&mut self) -> io::Result { if self.module_enabled { - if event::poll(std::time::Duration::from_millis(500))? { + if event::poll(std::time::Duration::from_millis(self.module.update_time_ms()))? { return self.read_keys(); } } else { @@ -741,6 +741,7 @@ impl<'a>App<'a>{ Char(' ') => self.module.on_space(), Char('s') => self.module.on_s(), Char('H') => self.module.on_capital_h(), + Char('c') => self.module.on_c(), Char('L') => self.module.on_capital_l(), Char('r') => self.module.on_r(), Char('+') | Char('=') => self.module.on_plus(), diff --git a/src/tui/modules.rs b/src/tui/modules.rs index 26aaaa8..1a0bbd3 100644 --- a/src/tui/modules.rs +++ b/src/tui/modules.rs @@ -2,6 +2,7 @@ pub mod potato; use ratatui::widgets::Paragraph; pub trait Module <'a>{ + fn update_time_ms(&self) -> u64; fn get_widget(&self) -> Paragraph<'a>; fn on_space(&mut self); fn on_s(&mut self); @@ -12,4 +13,5 @@ pub trait Module <'a>{ fn on_plus(&mut self); fn on_dot(&mut self); fn on_comma(&mut self); + fn on_c(&mut self); } diff --git a/src/tui/modules/potato.rs b/src/tui/modules/potato.rs index 7105510..45f408e 100644 --- a/src/tui/modules/potato.rs +++ b/src/tui/modules/potato.rs @@ -22,6 +22,16 @@ impl <'a> Module <'a> for Potato <'a> { Paragraph::new(time_str).block(default_block("Potato")) } + #[inline] + fn update_time_ms(&self) -> u64 { + 500 + } + + #[inline] + fn on_c(&mut self) { + self.quit() + } + #[inline] fn on_space(&mut self) { self.toggle_pause() @@ -155,4 +165,9 @@ impl<'a> Potato<'a> { self.index = self.len() - 1; } } + + #[inline] + pub fn quit(&mut self) { + self.run(vec![self.resolve_arg("q")]) + } }