Skip to content

Commit

Permalink
Fixed note saving problems
Browse files Browse the repository at this point in the history
Added on c for tui module
Added update time for tui module
  • Loading branch information
nimaaskarian committed Jan 26, 2024
1 parent 76a87ce commit d30f37a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct Args {
#[arg(short='p', long)]
todo_path: Option<PathBuf>,
}

impl Args {
pub fn is_cli(&self) -> bool {
self.stdout | self.non_interactive | self.tree
Expand Down
25 changes: 25 additions & 0 deletions src/todo_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,21 @@ impl TodoList {
self.undone.insert(0,todo);
}

#[inline]
pub fn all_dependent_files(&mut self, path: &PathBuf, output: &mut Vec<PathBuf>) -> Vec<PathBuf>{
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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}
}
10 changes: 4 additions & 6 deletions src/todo_list/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,20 @@ impl Todo {
DependencyType::None => {}
}
Ok(())
// self.dependencies.write(&path.join(&self.dependency_name), false)?;
}

#[inline]
pub fn add_dependency(&mut self, path: &PathBuf) -> Result<(), TodoError>{
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(())
}
Expand Down
5 changes: 3 additions & 2 deletions src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ impl<'a>App<'a>{
#[inline]
pub fn update_editor(&mut self) -> io::Result<Operation> {
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 {
Expand Down Expand Up @@ -684,7 +684,7 @@ impl<'a>App<'a>{
#[inline]
fn update_no_editor(&mut self) -> io::Result<Operation> {
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 {
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions src/tui/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
15 changes: 15 additions & 0 deletions src/tui/modules/potato.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")])
}
}

0 comments on commit d30f37a

Please sign in to comment.