mirror of
https://github.com/archtechx/todo-system.git
synced 2025-12-12 00:54:03 +00:00
ansi
This commit is contained in:
parent
574cee8895
commit
5cda67a4cd
5 changed files with 98 additions and 14 deletions
51
src/main.rs
51
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<Entry>) {
|
|||
let mut category_entries: HashMap<String, Vec<Entry>> = HashMap::new();
|
||||
let mut generic_entries: Vec<Entry> = 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<Entry>) {
|
|||
}
|
||||
}
|
||||
|
||||
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::<Vec<&isize>>();
|
||||
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<Entry>) {
|
|||
|
||||
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<Entry>) {
|
|||
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<Entry>) {
|
|||
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());
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue