jrnl: add entry listing
This commit is contained in:
parent
83c0fac427
commit
78bb79e258
8 changed files with 524 additions and 658 deletions
|
@ -1,15 +1,7 @@
|
|||
use clap::{Parser, Subcommand};
|
||||
use std::{fs, path::PathBuf};
|
||||
use time::format_description::well_known::{iso8601, Iso8601};
|
||||
|
||||
use crate::md::Doc;
|
||||
|
||||
const DATETIME_CONFIG: iso8601::EncodedConfig = iso8601::Config::DEFAULT
|
||||
.set_time_precision(iso8601::TimePrecision::Minute {
|
||||
decimal_digits: None,
|
||||
})
|
||||
.encode();
|
||||
const DT_FORMAT: Iso8601<DATETIME_CONFIG> = Iso8601::<DATETIME_CONFIG>;
|
||||
use crate::{commands::list_entries::list_entries, md::Doc};
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
struct Cli {
|
||||
|
@ -21,7 +13,8 @@ struct Cli {
|
|||
|
||||
#[derive(Debug, Subcommand)]
|
||||
enum Command {
|
||||
List,
|
||||
#[command(aliases = ["l", "ls", "list"])]
|
||||
ListEntries,
|
||||
Add,
|
||||
}
|
||||
|
||||
|
@ -30,23 +23,70 @@ fn main() {
|
|||
println!("Hello, world!");
|
||||
println!("cli: {cli:#?}");
|
||||
|
||||
// TODO: handle btter
|
||||
let file = fs::read_to_string(cli.s10e_jrnl_file_loc).unwrap();
|
||||
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);
|
||||
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};
|
||||
|
||||
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};
|
||||
use time::PrimitiveDateTime;
|
||||
|
||||
use crate::DT_FORMAT;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Doc {
|
||||
title: Vec<Span>,
|
||||
entries: Vec<Entry>,
|
||||
pub title: Vec<Span>,
|
||||
pub entries: Vec<Entry>,
|
||||
}
|
||||
|
||||
impl Doc {
|
||||
|
@ -70,7 +110,8 @@ mod md {
|
|||
};
|
||||
|
||||
let (ts, entry_title) = title.split_once(": ").unwrap();
|
||||
let ts = PrimitiveDateTime::parse(ts, &DT_FORMAT).unwrap();
|
||||
let ts = DateTime::parse_from_rfc3339(ts).unwrap();
|
||||
// let ts = PrimitiveDateTime::parse(ts, &DT_FORMAT).unwrap();
|
||||
|
||||
current = Some(Entry {
|
||||
timestamp: ts,
|
||||
|
@ -81,6 +122,9 @@ mod md {
|
|||
other => current.as_mut().unwrap().content.push(other),
|
||||
}
|
||||
}
|
||||
if let Some(cur) = current {
|
||||
entries.push(cur);
|
||||
}
|
||||
|
||||
Self {
|
||||
title: doc_title,
|
||||
|
@ -91,8 +135,8 @@ mod md {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Entry {
|
||||
timestamp: PrimitiveDateTime,
|
||||
title: String,
|
||||
content: Vec<Block>,
|
||||
pub timestamp: DateTime<FixedOffset>,
|
||||
pub title: String,
|
||||
pub content: Vec<Block>,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue