From df13761fc8589d53145ca00d1f1f4396fda3b470 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Sat, 20 Apr 2024 20:11:04 +0200 Subject: [PATCH] jrnl: put modules in its own respective files --- programs/jrnl/src/commands.rs | 3 + programs/jrnl/src/commands/list_entries.rs | 20 ++++ programs/jrnl/src/main.rs | 109 +-------------------- programs/jrnl/src/md.rs | 59 +++++++++++ programs/jrnl/src/utils.rs | 15 +++ 5 files changed, 101 insertions(+), 105 deletions(-) create mode 100644 programs/jrnl/src/commands.rs create mode 100644 programs/jrnl/src/commands/list_entries.rs create mode 100644 programs/jrnl/src/md.rs create mode 100644 programs/jrnl/src/utils.rs diff --git a/programs/jrnl/src/commands.rs b/programs/jrnl/src/commands.rs new file mode 100644 index 0000000..7340468 --- /dev/null +++ b/programs/jrnl/src/commands.rs @@ -0,0 +1,3 @@ +pub mod list_entries; + +mod add_entry {} diff --git a/programs/jrnl/src/commands/list_entries.rs b/programs/jrnl/src/commands/list_entries.rs new file mode 100644 index 0000000..6d46388 --- /dev/null +++ b/programs/jrnl/src/commands/list_entries.rs @@ -0,0 +1,20 @@ +use owo_colors::OwoColorize; +use std::{fs, path::PathBuf}; + +use crate::md::Doc; + +pub fn list_entries(path: PathBuf) { + let file = fs::read_to_string(path).unwrap(); + let doc = Doc::new(&file); + + 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)); + let termsize::Size { cols, .. } = termsize::get().unwrap(); + + let padding = " ".repeat(cols as usize - (n.len() + r.len() + l.len())); + + println!("{}{r}{padding}{}", n.cyan(), l.white()) + } +} diff --git a/programs/jrnl/src/main.rs b/programs/jrnl/src/main.rs index 6d1b47e..294ade8 100644 --- a/programs/jrnl/src/main.rs +++ b/programs/jrnl/src/main.rs @@ -3,6 +3,10 @@ use std::{fs, path::PathBuf}; use crate::{commands::list_entries::list_entries, md::Doc}; +mod commands; +mod md; +mod utils; + #[derive(Debug, Parser)] struct Cli { #[arg(env)] @@ -35,108 +39,3 @@ fn main() { } } } - -mod utils { - use chrono::{DateTime, FixedOffset}; - pub fn format_datetime(ts: DateTime) -> String { - ts.format("%A, %-d. %B %Y %R").to_string() - } - pub fn format_datetime_padded(ts: DateTime) -> String { - format!( - "{:>9}{}{:<9}{}", - ts.format("%A, "), - ts.format("%d. "), - ts.format("%B"), - ts.format(" %Y %R"), - ) - } -} - -mod commands { - pub mod list_entries { - use owo_colors::OwoColorize; - use std::{fs, path::PathBuf}; - - use crate::md::Doc; - - pub fn list_entries(path: PathBuf) { - let file = fs::read_to_string(path).unwrap(); - let doc = Doc::new(&file); - - 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)); - let termsize::Size { cols, .. } = termsize::get().unwrap(); - - let padding = " ".repeat(cols as usize - (n.len() + r.len() + l.len())); - - println!("{}{r}{padding}{}", n.cyan(), l.white()) - } - } - } - - mod add_entry {} -} - -mod md { - use chrono::{DateTime, FixedOffset}; - use markdown::{Block, Span}; - - #[derive(Debug)] - pub struct Doc { - pub title: Vec, - pub entries: Vec, - } - - impl Doc { - pub fn new(f: &str) -> Self { - let mut entries = Vec::new(); - let mut doc_title = vec![Span::Text("Journal".to_owned())]; - let toks = markdown::tokenize(f); - let mut current = None; - - for tok in toks { - match tok { - Block::Header(title, 1) => doc_title = title, - Block::Header(entry_title, 2) => { - if let Some(cur) = current.take() { - entries.push(cur); - } - - let Some(Span::Text(title)) = entry_title.first() else { - eprintln!("Error: Titles should be text."); - std::process::exit(1); - }; - - let (ts, entry_title) = title.split_once(": ").unwrap(); - let ts = DateTime::parse_from_rfc3339(ts).unwrap(); - // let ts = PrimitiveDateTime::parse(ts, &DT_FORMAT).unwrap(); - - current = Some(Entry { - timestamp: ts, - title: entry_title.to_owned(), - content: Vec::new(), - }); - } - other => current.as_mut().unwrap().content.push(other), - } - } - if let Some(cur) = current { - entries.push(cur); - } - - Self { - title: doc_title, - entries, - } - } - } - - #[derive(Debug)] - pub struct Entry { - pub timestamp: DateTime, - pub title: String, - pub content: Vec, - } -} diff --git a/programs/jrnl/src/md.rs b/programs/jrnl/src/md.rs new file mode 100644 index 0000000..b04ef2e --- /dev/null +++ b/programs/jrnl/src/md.rs @@ -0,0 +1,59 @@ +use chrono::{DateTime, FixedOffset}; +use markdown::{Block, Span}; + +#[derive(Debug)] +pub struct Doc { + pub title: Vec, + pub entries: Vec, +} + +impl Doc { + pub fn new(f: &str) -> Self { + let mut entries = Vec::new(); + let mut doc_title = vec![Span::Text("Journal".to_owned())]; + let toks = markdown::tokenize(f); + let mut current = None; + + for tok in toks { + match tok { + Block::Header(title, 1) => doc_title = title, + Block::Header(entry_title, 2) => { + if let Some(cur) = current.take() { + entries.push(cur); + } + + let Some(Span::Text(title)) = entry_title.first() else { + eprintln!("Error: Titles should be text."); + std::process::exit(1); + }; + + let (ts, entry_title) = title.split_once(": ").unwrap(); + let ts = DateTime::parse_from_rfc3339(ts).unwrap(); + // let ts = PrimitiveDateTime::parse(ts, &DT_FORMAT).unwrap(); + + current = Some(Entry { + timestamp: ts, + title: entry_title.to_owned(), + content: Vec::new(), + }); + } + other => current.as_mut().unwrap().content.push(other), + } + } + if let Some(cur) = current { + entries.push(cur); + } + + Self { + title: doc_title, + entries, + } + } +} + +#[derive(Debug)] +pub struct Entry { + pub timestamp: DateTime, + pub title: String, + pub content: Vec, +} diff --git a/programs/jrnl/src/utils.rs b/programs/jrnl/src/utils.rs new file mode 100644 index 0000000..4814e42 --- /dev/null +++ b/programs/jrnl/src/utils.rs @@ -0,0 +1,15 @@ +use chrono::{DateTime, FixedOffset}; + +pub fn format_datetime(ts: DateTime) -> String { + ts.format("%A, %-d. %B %Y %R").to_string() +} + +pub fn format_datetime_padded(ts: DateTime) -> String { + format!( + "{:>9}{}{:<9}{}", + ts.format("%A, "), + ts.format("%d. "), + ts.format("%B"), + ts.format(" %Y %R"), + ) +}