nix-configs/programs/jrnl/src/commands/list_entries.rs

33 lines
1 KiB
Rust
Raw Normal View History

use owo_colors::OwoColorize;
2025-04-25 17:09:58 +02:00
use std::{
fs, io,
path::{Path, PathBuf},
};
use crate::md::Doc;
2025-04-25 17:09:58 +02:00
pub fn list_entries(path: &Path) -> io::Result<()> {
2024-04-22 21:25:29 +02:00
let file = fs::read_to_string(path)?;
if let Some(doc) = Doc::new(&file) {
2024-10-11 08:49:54 +02:00
// TODO: testing, so this shit doesn't blow the fuck up in our face anymore
2024-04-22 21:25:29 +02:00
let termsize::Size { cols, .. } = termsize::get().unwrap();
2024-10-11 08:49:54 +02:00
assert!(cols > 0, "we don't have a terminal width.");
for (i, entry) in doc.entries.into_iter().enumerate() {
let n = format!("{:>2}", i + 1);
let r = format!(". {}", entry.title,);
let l = format!(" {} ", crate::utils::format_datetime(entry.timestamp));
2024-10-11 08:49:54 +02:00
let fuck_you_debugging = cols as usize - (n.len() + r.chars().count() + l.len());
let padding = " ".repeat(fuck_you_debugging);
println!("{}{r}{padding}{}", n.cyan(), l.white())
}
2024-04-22 21:25:29 +02:00
Ok(())
} else {
eprintln!("Parsing error...");
std::process::exit(1);
}
}