Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/fix bugs and add tests #28

Merged
merged 2 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 88 additions & 12 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,14 @@ impl Cli {
return Cli::match_subcommands(matches);
}

pub fn parse_gui<'a>(match_vec: Vec<&str>) -> Result<Cli, &'a str> {
pub fn parse_input_buffer<'a>(input_buffer: &String) -> Result<Cli, &'a str> {
let vec_matches: Vec<&str> = input_buffer.split_whitespace().collect();

let result_matches = Cli::base_command()
.try_get_matches_from(match_vec);
.try_get_matches_from(vec_matches)
.unwrap();

let matches = match result_matches {
Ok(matches) => {
return Cli::match_subcommands(matches);
},
Err(_) => {
return Err("Invalid command");
}
};
Cli::match_subcommands(result_matches)
}

pub fn base_command<'a>() -> Command {
Expand Down Expand Up @@ -428,16 +423,97 @@ mod tests {
}

#[test]
fn test_execute_done_command_with_id() {
fn test_execute_add_command_with_input_buffer() {
let (_tmp_dir, tmp_file_path) = create_tmp_file();
let mut task_list = load_task_list_from_file(&tmp_file_path);

// test command
let input_buffer = String::from("todo add Study");
let cli = Cli::parse_input_buffer(&input_buffer).unwrap();
let result = cli.execute(&mut task_list);

// test results
assert!(result.is_ok());
assert_eq!(task_list.get_tasks().len(), 1);
assert_eq!(task_list.get_tasks().get(0).unwrap().get_name(), "Study");
}


#[test]
fn test_execute_rm_command_with_input_buffer() {
let (_tmp_dir, tmp_file_path) = create_tmp_file();
let mut task_list = load_task_list_from_file(&tmp_file_path);
task_list.add_task("Buy groceries", "mid", "2024-04-20");
task_list.add_task("Study", "high", "2024-04-20");
task_list.add_task("Do laundry", "low", "2024-04-20");

// test command
let input_buffer = String::from("todo rm Study");
let cli = Cli::parse_input_buffer(&input_buffer).unwrap();
let result = cli.execute(&mut task_list);

// test results
assert!(result.is_ok());
for task in task_list.get_tasks() {
assert_ne!(task.get_name(), "Study");
}
}

#[test]
fn test_execute_rm_command_using_index_with_input_buffer() {
let (_tmp_dir, tmp_file_path) = create_tmp_file();
let mut task_list = load_task_list_from_file(&tmp_file_path);
task_list.add_task("Buy groceries", "mid", "2024-04-20");
task_list.add_task("Study", "high", "2024-04-20");
task_list.add_task("Do laundry", "low", "2024-04-20");

// test command
let input_buffer = String::from("todo rm -i 1");
let cli = Cli::parse_input_buffer(&input_buffer).unwrap();
let result = cli.execute(&mut task_list);

// test results
assert!(result.is_ok());
for task in task_list.get_tasks() {
assert_ne!(task.get_name(), "Study");
}
}

#[test]
fn test_execute_done_command_with_input_buffer() {
let (_tmp_dir, tmp_file_path) = create_tmp_file();
let mut task_list = load_task_list_from_file(&tmp_file_path);
task_list.add_task("Buy groceries", "mid", "2024-04-20");
task_list.add_task("Study", "high", "2024-04-20");
task_list.add_task("Do laundry", "low", "2024-04-20");

// test command
let input_buffer = String::from("todo done Study");
let cli = Cli::parse_input_buffer(&input_buffer).unwrap();
let result = cli.execute(&mut task_list);

// test results
assert!(result.is_ok());
for task in task_list.get_tasks() {
if task.get_name() == "Study" {
assert!(task.get_done());
} else {
assert!(!task.get_done());
}
}
}

#[test]
fn test_execute_done_command_using_index_with_input_buffer() {
let (_tmp_dir, tmp_file_path) = create_tmp_file();
let mut task_list = load_task_list_from_file(&tmp_file_path);
task_list.add_task("Buy groceries", "mid", "2024-04-20");
task_list.add_task("Study", "high", "2024-04-20");
task_list.add_task("Do laundry", "low", "2024-04-20");

// test command
let cli = create_cli("done", "", "", "", 1);
let input_buffer = String::from("todo done -i 1");
let cli = Cli::parse_input_buffer(&input_buffer).unwrap();
let result = cli.execute(&mut task_list);

// test results
Expand Down
7 changes: 2 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ fn main() {
let cli = Cli::parse()
.expect("Failed to parse command line arguments");

match cli.execute(&mut task_list) {
Ok(_) => {
task_list.save_tasks_to_csv();
},
Err(e) => (println!("{}", e)),
if cli.execute(&mut task_list).is_ok() {
task_list.save_tasks_to_csv();
}
}
24 changes: 11 additions & 13 deletions src/utils/gui/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,27 @@ impl Environment {
self.prev_terminal_size = terminal::size().unwrap();
}

pub fn is_file_modified(&self) -> bool {
pub fn is_file_modified(&mut self) -> bool {
let initial_metadata = fs::metadata(&self.file_path)
.expect("Failed to retrieve file metadata!");
let last_modified = initial_metadata.modified()
.expect("Failed to read the current modified time!");

self.prev_modified_time != last_modified
}

pub fn is_terminal_resized(&self) -> bool {
let current_size = terminal::size().unwrap();
if self.prev_modified_time != last_modified {
self.update_modified_time();
return true;
}

self.prev_terminal_size != current_size
false
}

pub fn should_update_view(&mut self) -> bool {
let result = self.is_file_modified() || self.is_terminal_resized();
pub fn is_terminal_resized(&mut self) -> bool {
let current_size = terminal::size().unwrap();

if result {
self.update_modified_time();
if self.prev_terminal_size != current_size {
self.update_terminal_size();
return true;
}

result
false
}
}
25 changes: 14 additions & 11 deletions src/utils/gui/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ pub fn handle_command_mode(task_list: &mut TaskList) -> Result<(), io::Error> {
}
}

if task_list.get_env_mut().should_update_view() {
if task_list.get_env_mut().is_file_modified() {
task_list.update_list_from_csv();
update_command_screen("task", &input_buffer, task_list)?;
}
if task_list.get_env_mut().is_terminal_resized() {
update_command_screen("task", &input_buffer, task_list)?;
}

Expand Down Expand Up @@ -87,11 +91,12 @@ fn update_and_handle_input(task_list: &mut TaskList, input_buffer: &mut String)
}
}

if task_list.get_env_mut().should_update_view() {
let file_path = task_list.get_env().get_file_path();
update_command_screen("task", input_buffer, task_list)?;
task_list.get_env_mut().update_modified_time();
task_list.get_env_mut().update_terminal_size();
if task_list.get_env_mut().is_file_modified() {
task_list.update_list_from_csv();
update_command_screen("task", &input_buffer, task_list)?;
}
if task_list.get_env_mut().is_terminal_resized() {
update_command_screen("task", &input_buffer, task_list)?;
}

std::thread::sleep(Duration::from_millis(10));
Expand All @@ -102,11 +107,9 @@ fn update_and_handle_input(task_list: &mut TaskList, input_buffer: &mut String)


fn handle_enter_key(input_buffer: &mut String, task_list: &mut TaskList) -> Result<(), io::Error> {
let matches: Vec<&str> = input_buffer.split_whitespace().collect();
let cli = Cli::parse_gui(matches.clone());
match cli {
Ok(cli) => {cli.execute(task_list);}
Err(e) => {}
let cli = Cli::parse_input_buffer(&input_buffer).unwrap();
if cli.execute(task_list).is_ok() {
task_list.save_tasks_to_csv();
}
input_buffer.clear();
clear_line()?;
Expand Down
4 changes: 4 additions & 0 deletions src/utils/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ fn gui_loop(task_list: &mut TaskList)-> Result<(), io::Error> {
}
}

if task_list.get_env_mut().is_file_modified() {
task_list.update_list_from_csv();
}

std::thread::sleep(Duration::from_millis(50));
}

Expand Down
5 changes: 5 additions & 0 deletions src/utils/task/task_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ impl TaskList {
}
}

pub fn update_list_from_csv(&mut self) {
let task_list = TaskList::load_tasks_from_csv(self.get_env().get_file_path()).unwrap();
self.tasks = task_list.tasks;
}

pub fn load_tasks_from_csv(file_path: &str) -> Result<TaskList, Box<dyn Error>> {
match read(file_path) {
Ok(task_list) => Ok(task_list),
Expand Down
Loading