2024-04-16 22:06:12 +02:00
|
|
|
use clap::{Parser, Subcommand};
|
2024-04-18 18:47:06 +02:00
|
|
|
use std::{fs, path::PathBuf};
|
|
|
|
|
2024-04-20 20:07:07 +02:00
|
|
|
use crate::{commands::list_entries::list_entries, md::Doc};
|
2024-04-16 22:06:12 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Parser)]
|
|
|
|
struct Cli {
|
|
|
|
#[arg(env)]
|
|
|
|
s10e_jrnl_file_loc: PathBuf,
|
|
|
|
#[command(subcommand)]
|
|
|
|
command: Option<Command>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Subcommand)]
|
|
|
|
enum Command {
|
2024-04-20 20:07:07 +02:00
|
|
|
#[command(aliases = ["l", "ls", "list"])]
|
|
|
|
ListEntries,
|
2024-04-16 22:06:12 +02:00
|
|
|
Add,
|
|
|
|
}
|
|
|
|
|
2024-04-16 21:37:01 +02:00
|
|
|
fn main() {
|
2024-04-16 22:06:12 +02:00
|
|
|
let cli = Cli::parse();
|
2024-04-16 21:37:01 +02:00
|
|
|
println!("Hello, world!");
|
2024-04-18 18:47:06 +02:00
|
|
|
println!("cli: {cli:#?}");
|
|
|
|
|
2024-04-20 20:07:07 +02:00
|
|
|
match cli.command {
|
|
|
|
Some(Command::ListEntries) => list_entries(cli.s10e_jrnl_file_loc.clone()),
|
|
|
|
Some(Command::Add) => todo!(),
|
|
|
|
None => {
|
|
|
|
// TODO: handle btter
|
|
|
|
let file = fs::read_to_string(cli.s10e_jrnl_file_loc).unwrap();
|
|
|
|
|
|
|
|
let doc = Doc::new(&file);
|
|
|
|
dbg!(doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod utils {
|
|
|
|
use chrono::{DateTime, FixedOffset};
|
|
|
|
pub fn format_datetime(ts: DateTime<FixedOffset>) -> String {
|
|
|
|
ts.format("%A, %-d. %B %Y %R").to_string()
|
|
|
|
}
|
|
|
|
pub fn format_datetime_padded(ts: DateTime<FixedOffset>) -> 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};
|
2024-04-18 18:47:06 +02:00
|
|
|
|
2024-04-20 20:07:07 +02:00
|
|
|
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 {}
|
2024-04-18 18:47:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mod md {
|
2024-04-20 20:07:07 +02:00
|
|
|
use chrono::{DateTime, FixedOffset};
|
2024-04-18 18:47:06 +02:00
|
|
|
use markdown::{Block, Span};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Doc {
|
2024-04-20 20:07:07 +02:00
|
|
|
pub title: Vec<Span>,
|
|
|
|
pub entries: Vec<Entry>,
|
2024-04-18 18:47:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2024-04-20 20:07:07 +02:00
|
|
|
let ts = DateTime::parse_from_rfc3339(ts).unwrap();
|
|
|
|
// let ts = PrimitiveDateTime::parse(ts, &DT_FORMAT).unwrap();
|
2024-04-18 18:47:06 +02:00
|
|
|
|
|
|
|
current = Some(Entry {
|
|
|
|
timestamp: ts,
|
|
|
|
title: entry_title.to_owned(),
|
|
|
|
content: Vec::new(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
other => current.as_mut().unwrap().content.push(other),
|
|
|
|
}
|
|
|
|
}
|
2024-04-20 20:07:07 +02:00
|
|
|
if let Some(cur) = current {
|
|
|
|
entries.push(cur);
|
|
|
|
}
|
2024-04-18 18:47:06 +02:00
|
|
|
|
|
|
|
Self {
|
|
|
|
title: doc_title,
|
|
|
|
entries,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Entry {
|
2024-04-20 20:07:07 +02:00
|
|
|
pub timestamp: DateTime<FixedOffset>,
|
|
|
|
pub title: String,
|
|
|
|
pub content: Vec<Block>,
|
2024-04-18 18:47:06 +02:00
|
|
|
}
|
2024-04-16 21:37:01 +02:00
|
|
|
}
|