From 39a543e4d68e8602c6a2282e0483638f21d2f638 Mon Sep 17 00:00:00 2001 From: Rajveer Singh Saggu Date: Tue, 26 Mar 2024 15:08:21 +0530 Subject: [PATCH] Fixed minor bugs and added minor function --- Cargo.lock | 32 ++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 60 +++++++++++++++++++++++++++++------------------------ todo.td | 0 4 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 todo.td diff --git a/Cargo.lock b/Cargo.lock index 441d7a5..b14270f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ dependencies = [ "libc", ] +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -153,6 +162,7 @@ dependencies = [ name = "todo" version = "0.1.0" dependencies = [ + "ansi_term", "chrono", ] @@ -216,6 +226,28 @@ version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-core" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 266bdc0..9cc52b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +ansi_term = "0.12.1" chrono = { version = "0.4.23" } diff --git a/src/main.rs b/src/main.rs index 0a5709d..011b356 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,33 @@ use std::env; +use ansi_term::Style; use std::error::Error; +use std::process::exit; use std::fs::{write, File, OpenOptions}; use std::io::{BufRead, BufReader, BufWriter, Write}; fn main() -> Result<(), Box> { let args: Vec = std::env::args().collect(); + if args.len() < 2 { + println!("Usage: [command] *"); + exit(0x0101) + } + + let bold = Style::new().bold(); + println!("{}", bold.paint("TODO")); + let command = &args[1]; - let file_name = env::var("TODO_PATH").expect("not found!"); + let todo_path = env::var("TODO_PATH").expect("Environment variable 'TODO_PATH' not found!"); match command.as_str() { - "mark" => mark(args[2].parse::().unwrap(), &file_name), - "listC" => listC(&file_name), - "list" => listS(&file_name), - "reset" => reset(&file_name), - "remove" => remove(args[2].parse::().unwrap(), &file_name), + "mark" => mark_task(args[2].parse::().unwrap(), &todo_path), + "listC" => list_complex(&todo_path), + "list" => list_simple(&todo_path), + "reset" => reset_todo_file(&todo_path), + "remove" => remove_task(args[2].parse::().unwrap(), &todo_path), "add" => { let tasks: Vec = args[2..].to_vec(); - add(&tasks, &file_name)?; + add_tasks(&tasks, &todo_path)?; Ok(()) } _ => Err(Box::new(std::io::Error::new( @@ -27,13 +37,13 @@ fn main() -> Result<(), Box> { } } -fn add(tasks: &[String], file_name: &str) -> Result<(), Box> { +fn add_tasks(tasks: &[String], todo_path: &str) -> Result<(), Box> { let timestamp = chrono::offset::Local::now(); let formatted_time = timestamp.format("%d/%m/%Y %H:%M:%S"); let mut data_file = OpenOptions::new() .append(true) - .open(file_name) + .open(todo_path) .expect("Failed to open file for writing"); for task in tasks { @@ -44,10 +54,8 @@ fn add(tasks: &[String], file_name: &str) -> Result<(), Box> { Ok(()) } -fn listC(file_name: &str) -> Result<(), Box> { - println!("TODO"); - - let file = File::open(file_name)?; +fn list_complex(todo_path: &str) -> Result<(), Box> { + let file = File::open(todo_path)?; let reader = BufReader::new(file); let mut line_number = 1; @@ -61,31 +69,29 @@ fn listC(file_name: &str) -> Result<(), Box> { Ok(()) } -fn listS(file_name: &str) -> Result<(), Box> { - println!("TODO"); - - let file = File::open(file_name)?; +fn list_simple(todo_path: &str) -> Result<(), Box> { + let file = File::open(todo_path)?; let reader = BufReader::new(file); for line in reader.lines() { let line = line?; - println!("{}", chop(line)); + println!("{}", truncate(line)); } Ok(()) } -fn reset(file_name: &str) -> Result<(), Box> { - write(file_name, "")?; +fn reset_todo_file(todo_path: &str) -> Result<(), Box> { + write(todo_path, "")?; Ok(()) } -fn remove(line_number: usize, file_name: &str) -> Result<(), Box> { +fn remove_task(line_number: usize, todo_path: &str) -> Result<(), Box> { let mut lines = Vec::new(); let mut line_count = 0; { - let file = File::open(file_name)?; + let file = File::open(todo_path)?; let reader = BufReader::new(file); for line in reader.lines() { @@ -100,7 +106,7 @@ fn remove(line_number: usize, file_name: &str) -> Result<(), Box> { let file = OpenOptions::new() .write(true) .truncate(true) - .open(file_name)?; + .open(todo_path)?; let mut writer = BufWriter::new(file); @@ -111,11 +117,11 @@ fn remove(line_number: usize, file_name: &str) -> Result<(), Box> { Ok(()) } -fn mark(line_number: usize, file_name: &str) -> Result<(), Box> { +fn mark_task(line_number: usize, todo_path: &str) -> Result<(), Box> { let mut lines = Vec::new(); let mut line_count = 0; - let file = File::open(file_name)?; + let file = File::open(todo_path)?; let reader = BufReader::new(file); for line in reader.lines() { @@ -137,7 +143,7 @@ fn mark(line_number: usize, file_name: &str) -> Result<(), Box> { let file = OpenOptions::new() .write(true) .truncate(true) - .open(file_name)?; + .open(todo_path)?; let mut writer = BufWriter::new(file); @@ -148,7 +154,7 @@ fn mark(line_number: usize, file_name: &str) -> Result<(), Box> { Ok(()) } -fn chop(mut line: String) -> String { +fn truncate(mut line: String) -> String { if line.len() > 20 { line = line.chars().skip(20).collect(); } else { diff --git a/todo.td b/todo.td new file mode 100644 index 0000000..e69de29