From 086feb0e0baeec21defd4c89280ce179b7fa6bc4 Mon Sep 17 00:00:00 2001 From: Abraham Toriz Date: Mon, 29 Aug 2022 19:45:45 -0400 Subject: [PATCH] allow to set a per-command default formatter --- src/commands/display.rs | 29 +++++++++++++++++++++++++++-- src/commands/month.rs | 30 ++++++++++++++++++++++++++++-- src/commands/today.rs | 29 +++++++++++++++++++++++++++-- src/commands/week.rs | 30 ++++++++++++++++++++++++++++-- src/commands/yesterday.rs | 30 ++++++++++++++++++++++++++++-- src/config.rs | 20 ++++++++++++++++++++ src/formatters/chart.rs | 7 ------- 7 files changed, 158 insertions(+), 17 deletions(-) diff --git a/src/commands/display.rs b/src/commands/display.rs index 4fe5d33..cee7e91 100644 --- a/src/commands/display.rs +++ b/src/commands/display.rs @@ -130,7 +130,7 @@ impl<'a> Command<'a> for DisplayCommand { args.end, args.sheet, streams, - args.format.unwrap_or_else(|| facts.config.default_formatter.clone()), + args.format.unwrap_or_else(|| facts.config.commands.display.default_formatter.as_ref().unwrap_or(&facts.config.default_formatter).clone()), args.ids, args.grep, facts @@ -144,7 +144,7 @@ mod tests { use pretty_assertions::{assert_eq, assert_str_eq}; use crate::database::SqliteDatabase; - use crate::config::Config; + use crate::config::{Config, CommandsSettings, BaseCommandSettings}; use super::*; @@ -340,4 +340,29 @@ Timesheet: sheet1 assert_eq!(&String::from_utf8_lossy(&streams.out), "1 2\n"); assert_eq!(String::from_utf8_lossy(&streams.err), ""); } + + #[test] + fn respect_command_default_formatter() { + std::env::set_var("TZ", "CST+6"); + + let args = Default::default(); + let mut streams = Streams::fake(b""); + let facts = Facts::new().with_config(Config { + commands: CommandsSettings { + display: BaseCommandSettings { + default_formatter: Some(Formatter::Ids), + }, + ..Default::default() + }, + ..Default::default() + }); + + streams.db.entry_insert(Utc.ymd(2021, 6, 30).and_hms(10, 0, 0), None, Some("hola".into()), "default").unwrap(); + streams.db.entry_insert(Utc.ymd(2021, 6, 30).and_hms(10, 10, 0), None, Some("hola".into()), "default").unwrap(); + + DisplayCommand::handle(args, &mut streams, &facts).unwrap(); + + assert_eq!(&String::from_utf8_lossy(&streams.out), "1 2\n"); + assert_eq!(String::from_utf8_lossy(&streams.err), ""); + } } diff --git a/src/commands/month.rs b/src/commands/month.rs index 17bdec2..88f42f7 100644 --- a/src/commands/month.rs +++ b/src/commands/month.rs @@ -136,7 +136,7 @@ impl<'a> Command<'a> for MonthCommand { Some(end), args.sheet, streams, - args.format.unwrap_or_else(|| facts.config.default_formatter.clone()), + args.format.unwrap_or_else(|| facts.config.commands.month.default_formatter.as_ref().unwrap_or(&facts.config.default_formatter).clone()), args.ids, args.grep, facts @@ -146,7 +146,7 @@ impl<'a> Command<'a> for MonthCommand { #[cfg(test)] mod tests { - use crate::config::Config; + use crate::config::{Config, CommandsSettings, BaseCommandSettings}; use super::*; @@ -170,4 +170,30 @@ mod tests { assert_eq!(&String::from_utf8_lossy(&streams.out), "1 2\n"); assert_eq!(String::from_utf8_lossy(&streams.err), ""); } + + #[test] + fn respect_command_default_formatter() { + std::env::set_var("TZ", "CST+6"); + + let args = Default::default(); + let mut streams = Streams::fake(b""); + let now = Utc.ymd(2021, 6, 30).and_hms(11, 0, 0); + let facts = Facts::new().with_config(Config { + commands: CommandsSettings { + month: BaseCommandSettings { + default_formatter: Some(Formatter::Ids), + }, + ..Default::default() + }, + ..Default::default() + }).with_now(now); + + streams.db.entry_insert(Utc.ymd(2021, 6, 30).and_hms(10, 0, 0), None, Some("hola".into()), "default").unwrap(); + streams.db.entry_insert(Utc.ymd(2021, 6, 30).and_hms(10, 10, 0), None, Some("hola".into()), "default").unwrap(); + + MonthCommand::handle(args, &mut streams, &facts).unwrap(); + + assert_eq!(&String::from_utf8_lossy(&streams.out), "1 2\n"); + assert_eq!(String::from_utf8_lossy(&streams.err), ""); + } } diff --git a/src/commands/today.rs b/src/commands/today.rs index c1169fe..65e2044 100644 --- a/src/commands/today.rs +++ b/src/commands/today.rs @@ -56,7 +56,7 @@ impl<'a> Command<'a> for TodayCommand { args.end, args.sheet, streams, - args.format.unwrap_or_else(|| facts.config.default_formatter.clone()), + args.format.unwrap_or_else(|| facts.config.commands.today.default_formatter.as_ref().unwrap_or(&facts.config.default_formatter).clone()), args.ids, args.grep, facts @@ -68,7 +68,7 @@ impl<'a> Command<'a> for TodayCommand { mod tests { use chrono::TimeZone; - use crate::config::Config; + use crate::config::{Config, CommandsSettings, BaseCommandSettings}; use super::*; @@ -91,4 +91,29 @@ mod tests { assert_eq!(&String::from_utf8_lossy(&streams.out), "1 2\n"); assert_eq!(String::from_utf8_lossy(&streams.err), ""); } + + #[test] + fn respect_command_default_formatter() { + std::env::set_var("TZ", "CST+6"); + + let args = Default::default(); + let mut streams = Streams::fake(b""); + let facts = Facts::new().with_config(Config { + commands: CommandsSettings { + today: BaseCommandSettings { + default_formatter: Some(Formatter::Ids), + }, + ..Default::default() + }, + ..Default::default() + }).with_now(Utc.ymd(2021, 6, 30).and_hms(11, 0, 0)); + + streams.db.entry_insert(Utc.ymd(2021, 6, 30).and_hms(10, 0, 0), None, Some("hola".into()), "default").unwrap(); + streams.db.entry_insert(Utc.ymd(2021, 6, 30).and_hms(10, 10, 0), None, Some("hola".into()), "default").unwrap(); + + TodayCommand::handle(args, &mut streams, &facts).unwrap(); + + assert_eq!(&String::from_utf8_lossy(&streams.out), "1 2\n"); + assert_eq!(String::from_utf8_lossy(&streams.err), ""); + } } diff --git a/src/commands/week.rs b/src/commands/week.rs index 8ce4579..bf550db 100644 --- a/src/commands/week.rs +++ b/src/commands/week.rs @@ -101,7 +101,7 @@ impl<'a> Command<'a> for WeekCommand { args.end, args.sheet, streams, - args.format.unwrap_or_else(|| facts.config.default_formatter.clone()), + args.format.unwrap_or_else(|| facts.config.commands.week.default_formatter.as_ref().unwrap_or(&facts.config.default_formatter).clone()), args.ids, args.grep, facts @@ -113,7 +113,7 @@ impl<'a> Command<'a> for WeekCommand { mod tests { use chrono::TimeZone; - use crate::config::Config; + use crate::config::{Config, CommandsSettings, BaseCommandSettings}; use super::*; @@ -151,4 +151,30 @@ mod tests { assert_eq!(&String::from_utf8_lossy(&streams.out), "1 2\n"); assert_eq!(String::from_utf8_lossy(&streams.err), ""); } + + #[test] + fn respect_command_default_formatter() { + std::env::set_var("TZ", "CST+6"); + + let args = Default::default(); + let mut streams = Streams::fake(b""); + let now = Utc.ymd(2021, 7, 1).and_hms(10, 0, 0); + let facts = Facts::new().with_config(Config { + commands: CommandsSettings { + week: BaseCommandSettings { + default_formatter: Some(Formatter::Ids), + }, + ..Default::default() + }, + ..Default::default() + }).with_now(now); + + streams.db.entry_insert(Utc.ymd(2021, 6, 30).and_hms(10, 0, 0), None, Some("hola".into()), "default").unwrap(); + streams.db.entry_insert(Utc.ymd(2021, 6, 30).and_hms(10, 10, 0), None, Some("hola".into()), "default").unwrap(); + + WeekCommand::handle(args, &mut streams, &facts).unwrap(); + + assert_eq!(&String::from_utf8_lossy(&streams.out), "1 2\n"); + assert_eq!(String::from_utf8_lossy(&streams.err), ""); + } } diff --git a/src/commands/yesterday.rs b/src/commands/yesterday.rs index 5ef5197..6c6dac8 100644 --- a/src/commands/yesterday.rs +++ b/src/commands/yesterday.rs @@ -55,7 +55,7 @@ impl<'a> Command<'a> for YesterdayCommand { end, args.sheet, streams, - args.format.unwrap_or_else(|| facts.config.default_formatter.clone()), + args.format.unwrap_or_else(|| facts.config.commands.yesterday.default_formatter.as_ref().unwrap_or(&facts.config.default_formatter).clone()), args.ids, args.grep, facts @@ -68,7 +68,7 @@ mod tests { use chrono::{Duration, TimeZone}; use pretty_assertions::assert_eq; - use crate::config::Config; + use crate::config::{Config, CommandsSettings, BaseCommandSettings}; use super::*; @@ -120,4 +120,30 @@ mod tests { assert_eq!(&String::from_utf8_lossy(&streams.out), "1 2\n"); assert_eq!(String::from_utf8_lossy(&streams.err), ""); } + + #[test] + fn respect_command_default_formatter() { + std::env::set_var("TZ", "CST+6"); + + let args = Default::default(); + let mut streams = Streams::fake(b""); + let now = Utc.ymd(2021, 7, 1).and_hms(10, 0, 0); + let facts = Facts::new().with_config(Config { + commands: CommandsSettings { + yesterday: BaseCommandSettings { + default_formatter: Some(Formatter::Ids), + }, + ..Default::default() + }, + ..Default::default() + }).with_now(now); + + streams.db.entry_insert(Utc.ymd(2021, 6, 30).and_hms(10, 0, 0), None, Some("hola".into()), "default").unwrap(); + streams.db.entry_insert(Utc.ymd(2021, 6, 30).and_hms(10, 10, 0), None, Some("hola".into()), "default").unwrap(); + + YesterdayCommand::handle(args, &mut streams, &facts).unwrap(); + + assert_eq!(&String::from_utf8_lossy(&streams.out), "1 2\n"); + assert_eq!(String::from_utf8_lossy(&streams.err), ""); + } } diff --git a/src/config.rs b/src/config.rs index 3d731e1..252710c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -86,6 +86,22 @@ pub struct FormattersSettings { pub chart: ChartFormatterSettings, } +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +#[serde(default)] +pub struct BaseCommandSettings { + pub default_formatter: Option, +} + +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +#[serde(default)] +pub struct CommandsSettings { + pub display: BaseCommandSettings, + pub month: BaseCommandSettings, + pub today: BaseCommandSettings, + pub week: BaseCommandSettings, + pub yesterday: BaseCommandSettings, +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(default)] pub struct Config { @@ -138,6 +154,9 @@ pub struct Config { /// Individual settings for each formatter pub formatters: FormattersSettings, + /// Settings for each command + pub commands: CommandsSettings, + #[serde(flatten)] pub extra: HashMap, } @@ -338,6 +357,7 @@ impl Default for Config { week_start: WeekDay::Monday, interactive_entries: 5, formatters: Default::default(), + commands: Default::default(), } } } diff --git a/src/formatters/chart.rs b/src/formatters/chart.rs index 9d9bb9f..b2ae80a 100644 --- a/src/formatters/chart.rs +++ b/src/formatters/chart.rs @@ -267,11 +267,6 @@ Aug 29 Mon \u{1b}[48;5;10m \u{1b}[0m 3.0 assert_str_eq!(String::from_utf8_lossy(&out), "No entries to display\n"); } - #[test] - fn allow_set_default_formatter_per_command() { - assert!(false, "just to remind me. This new formatter should be the default for week and month displays"); - } - #[test] fn days_without_hours_appear() { std::env::set_var("TZ", "CST+6"); @@ -288,8 +283,6 @@ Aug 29 Mon \u{1b}[48;5;10m \u{1b}[0m 3.0 print_formatted(entries, &mut out, &facts).unwrap(); - dbg!(String::from_utf8_lossy(&out)); - assert_str_eq!(String::from_utf8_lossy(&out), " Date Day Chart Hours Aug 15 Mon \u{1b}[48;5;10m \u{1b}[0m 5.0