diff --git a/Cargo.lock b/Cargo.lock index a2210cb..5bbbb5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -138,10 +138,20 @@ dependencies = [ ] [[package]] -name = "todo-system" +name = "termcolor" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "todos" version = "0.1.0" dependencies = [ "clap", + "termcolor", ] [[package]] @@ -156,6 +166,37 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[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-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[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-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index d1430b0..55ae5e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "todo-system" +name = "todos" version = "0.1.0" edition = "2021" authors = ["Samuel Ć tancl "] @@ -9,3 +9,4 @@ description = "An intuitive system for organizing TODOs in code" [dependencies] clap = { version = "4.4.8", features = ["derive"] } +termcolor = "1.4.0" diff --git a/README.md b/README.md index 90316cc..4e6f23f 100644 --- a/README.md +++ b/README.md @@ -136,3 +136,16 @@ Notes: - `node_modules/` (for npm) and `vendor/` (for composer) are ignored by default - paths starting with `.` are **always** ignored - `--exclude`s are relative to the current working directory, not passed paths (including default excludes mentioned above). If you're running the script for another folder and want to exclude folders there, type out the path in `--exclude` + +To omit ANSI formatting and get raw markdown output, set `NO_COLOR=1` or `TERM=dumb`. + +### Installation + +There are no downloadable builds at the moment. To compile the tool manually: +1. Set up Rust locally https://www.rust-lang.org/tools/install +2. Clone the repo +3. `cargo build --release` +4. The binary (`todos`) will appear will `target/release`. Add it to your PATH or create a bash alias: + ```sh + alias todos="/path/to/todos" + ``` diff --git a/src/main.rs b/src/main.rs index 66b7fb3..3654cd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,10 @@ use std::collections::HashMap; -use std::{io, fs}; +use std::io::{self, Write}; +use std::fs; use std::path::{Path, PathBuf}; use clap::Parser; +use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; +use std::cmp::Ordering::{Less, Equal, Greater}; #[derive(Debug, PartialEq, Clone)] struct Location { @@ -25,14 +28,36 @@ enum EntryData { impl Entry { fn render(&self) { + let mut stdout = StandardStream::stdout(ColorChoice::Auto); + + write_ansi(&mut stdout, Color::Ansi256(243), "- [ ] ", false); + + let location = format!("{}:{}", self.location.file.to_string_lossy(), self.location.line); + if self.text.len() > 0 { - println!("- [ ] {} ({}:{})", self.text, self.location.file.to_string_lossy(), self.location.line); + write_ansi(&mut stdout, Color::Blue, self.text.as_str(), true); + + write_ansi(&mut stdout, Color::Ansi256(243), format!(" ({})", location).as_str(), false); } else { - println!("- [ ] {}:{}", self.location.file.to_string_lossy(), self.location.line); + write_ansi(&mut stdout, Color::Cyan, &location.as_str(), true); } + + write!(&mut stdout, "\n").unwrap(); } } +fn write_ansi(stdout: &mut StandardStream, color: Color, text: &str, bold: bool) { + stdout.set_color( + ColorSpec::new() + .set_fg(Some(color)) + .set_bold(bold) + ).unwrap(); + + write!(stdout, "{text}").unwrap(); + + stdout.reset().unwrap(); +} + struct Stats { visited_folders: usize, visited_files: usize, @@ -175,6 +200,8 @@ fn render(entries: Vec) { let mut category_entries: HashMap> = HashMap::new(); let mut generic_entries: Vec = Vec::new(); + let mut stdout = StandardStream::stdout(ColorChoice::Auto); + for entry in entries { match entry.data { EntryData::Priority(priority) => { @@ -199,14 +226,15 @@ fn render(entries: Vec) { } } - print!("# TODOs\n\n"); + write_ansi(&mut stdout, Color::Yellow, "# TODOs", true); + write!(stdout, "\n\n").unwrap(); let mut priority_keys = priority_entries.keys().collect::>(); priority_keys.sort_by(|a, b| a.partial_cmp(b).unwrap()); for priority in priority_keys { let priority_notation = match priority.cmp(&0) { - std::cmp::Ordering::Less => { + Less => { let mut str = "todo0".to_string(); // todo0 -> 0 @@ -216,11 +244,12 @@ fn render(entries: Vec) { str }, - std::cmp::Ordering::Equal => "todo0".to_string(), - std::cmp::Ordering::Greater => format!("todo{}", priority), + Equal => "todo0".to_string(), + Greater => format!("todo{}", priority), }; - println!("## {}", priority_notation); + write_ansi(&mut stdout, Color::Red, format!("## {}", &priority_notation).as_str(), true); + write!(stdout, "\n").unwrap(); for item in priority_entries.get(priority).unwrap() { item.render(); @@ -233,7 +262,8 @@ fn render(entries: Vec) { category_keys.sort_by(|a, b| a.partial_cmp(b).unwrap()); for category in category_keys { - println!("## {}", category); + write_ansi(&mut stdout, Color::Green, format!("## {}", &category).as_str(), true); + write!(stdout, "\n").unwrap(); for item in category_entries.get(category).unwrap() { item.render(); @@ -242,7 +272,8 @@ fn render(entries: Vec) { println!(""); } - println!("## Other"); + write_ansi(&mut stdout, Color::White, "## Other", true); + write!(stdout, "\n").unwrap(); generic_entries.sort_by(|a, b| a.text.partial_cmp(&b.text).unwrap()); diff --git a/todo.md b/todo.md index 2968d6e..8b01ef8 100644 --- a/todo.md +++ b/todo.md @@ -2,5 +2,3 @@ - README.md - todo.md -- Output format? -- termcolor + --no-ansi