Skip to content
Open
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
115 changes: 114 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ chrono = { version = "0.4", features = ["serde"] }
csv = "1.1.6"
thiserror = "1.0.38"
anyhow = "1.0.68"
crossterm = "0.26.1"
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use colored::{ColoredString, Colorize};
use crossterm::{cursor, terminal, ExecutableCommand};

use std::io::{self, Write};
use std::process::Command;

use anyhow::Result;
use chrono::Utc;
Expand Down Expand Up @@ -76,7 +76,7 @@ pub fn sub_menu(todos: &mut Vec<Todo>) -> Result<bool> {
.to_string();

if id.contains('1') {
show_todos(todos);
show_todos(todos)?;
Ok(false)
} else if id.contains('2') {
add_todo(todos)?;
Expand Down Expand Up @@ -126,9 +126,11 @@ pub fn add_todo(td: &mut Vec<Todo>) -> Result<()> {
}

/// Show todos in a formatted table
pub fn show_todos(todos: &mut [Todo]) {
pub fn show_todos(todos: &mut [Todo]) -> Result<()> {
// Clear the screen
Command::new("clear").status().unwrap();
std::io::stdout()
.execute(terminal::Clear(terminal::ClearType::All))?
.execute(cursor::MoveTo(0, 0))?;

println!(
"{:^10} {:^40} {:^40} {:^10} {:^32}",
Expand Down Expand Up @@ -182,6 +184,8 @@ pub fn show_todos(todos: &mut [Todo]) {
);
}
}

Ok(())
}

/// Removes todo with the given ID from the list
Expand Down
17 changes: 11 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
//! add new features and help me learn Rust.

use colored::Colorize;
use crossterm::{cursor, terminal, ExecutableCommand};
use rtodo::commands::execute_commands;

use std::process::Command;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
Expand Down Expand Up @@ -54,7 +54,12 @@ fn main() {
// Handler loop
while running.load(Ordering::SeqCst) {
// Clear the screen
Command::new("clear").status().unwrap();
// Command::new("clear").status().unwrap();
std::io::stdout()
.execute(terminal::Clear(terminal::ClearType::All))
.unwrap()
.execute(cursor::MoveTo(0, 0))
.unwrap();

// Show main menu
main_menu();
Expand All @@ -74,15 +79,15 @@ fn main() {

// Open sub menu / show todos
if user.contains('1') {
show_todos(&mut todos);
show_todos(&mut todos).unwrap();

// Display the sub menu in a loop
while let Ok(e) = sub_menu(&mut todos) {
// Break if `sub_menu()` returned false
if !e {
break;
}
show_todos(&mut todos);
show_todos(&mut todos).unwrap();
}
// add todo
} else if user.contains('2') {
Expand All @@ -91,13 +96,13 @@ fn main() {
}
// update todo
} else if user.contains('3') {
show_todos(&mut todos);
show_todos(&mut todos).unwrap();
if let Err(e) = update_todo(&mut todos) {
eprintln!("{e}");
}
// remove todo
} else if user.contains('4') {
show_todos(&mut todos);
show_todos(&mut todos).unwrap();
if let Err(e) = remove_todo(&mut todos) {
eprintln!("{e}");
}
Expand Down