allow to set a per-command default formatter

This commit is contained in:
Abraham Toriz 2022-08-29 19:45:45 -04:00
parent 8ec245038f
commit 086feb0e0b
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
7 changed files with 158 additions and 17 deletions

View File

@ -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), "");
}
}

View File

@ -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), "");
}
}

View File

@ -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), "");
}
}

View File

@ -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), "");
}
}

View File

@ -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), "");
}
}

View File

@ -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<Formatter>,
}
#[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<String, serde_json::Value>,
}
@ -338,6 +357,7 @@ impl Default for Config {
week_start: WeekDay::Monday,
interactive_entries: 5,
formatters: Default::default(),
commands: Default::default(),
}
}
}

View File

@ -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