Skip to content

Commit

Permalink
Make footer gray
Browse files Browse the repository at this point in the history
Also, add some rendering snapshot tests
  • Loading branch information
mawkler committed Nov 22, 2024
1 parent b145e31 commit 61009ae
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 18 deletions.
16 changes: 9 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ assert_cmd = "2.0.16"
wiremock = "0.6.2"
predicates = "3.1.2"
insta = { version = "1.41.1", features = ["yaml", "json"] }
anstyle = "1.0.10"
anstream = "0.6.18"

# Improved insta runtime
[profile.dev.package]
Expand Down
94 changes: 83 additions & 11 deletions src/cli/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,20 @@ impl<'a> From<&'a Line> for Row<'a> {
}
}

fn gray_borders() -> BorderColor {
let gray = Color::parse(' '.fg_rgb::<85, 85, 85>().to_string()).clone();
fn gray() -> Color {
Color::parse(' '.fg_rgb::<85, 85, 85>().to_string()).clone()
}

fn gray_borders() -> BorderColor {
BorderColor::new()
.top(gray.clone())
.left(gray.clone())
.bottom(gray.clone())
.right(gray.clone())
.corner_bottom_right(gray.clone())
.corner_bottom_left(gray.clone())
.corner_top_left(gray.clone())
.corner_top_right(gray.clone())
.top(gray().clone())
.left(gray().clone())
.bottom(gray().clone())
.right(gray().clone())
.corner_bottom_right(gray().clone())
.corner_bottom_left(gray().clone())
.corner_top_left(gray().clone())
.corner_top_right(gray().clone())
}

impl Display for TimeSheet {
Expand All @@ -90,6 +92,7 @@ impl Display for TimeSheet {
Rows::first(),
))
.with(Panel::footer(format!("Week {}", self.week_number)))
.with(Colorization::exact([gray()], Rows::last()))
.with(gray_borders());

write!(f, "{table}")
Expand All @@ -99,9 +102,10 @@ impl Display for TimeSheet {
#[cfg(test)]
mod test {
use super::*;
use crate::domain::models::{hours::Hours, time_sheet::Week};

#[test]
fn display_hours_test() {
fn displays_hours() {
let hours = [1.5, 0.25, 12.75, 0.0, 23.999, 10.1];
let expected = ["1:30", "0:15", "12:45", "", "23:59", "10:06"];

Expand All @@ -110,4 +114,72 @@ mod test {
assert_eq!(result, expected[i], "Failed at index {}", i);
}
}

fn create_week(days: [u8; 7]) -> Week {
let days: Vec<f32> = days.into_iter().map(Into::into).collect();
Week {
monday: Hours(days[0]),
tuesday: Hours(days[1]),
wednesday: Hours(days[2]),
thursday: Hours(days[3]),
friday: Hours(days[4]),
saturday: Hours(days[5]),
sunday: Hours(days[6]),
}
}

#[test]
fn display_ansi_stripped_timesheet() {
let time_sheet = (TimeSheet {
lines: vec![
Line {
job: "Job number one".to_string(),
task: "Task number one".to_string(),
week: create_week([8, 8, 0, 0, 0, 0, 0]),
},
Line {
job: "job number two".to_string(),
task: "task number two".to_string(),
week: create_week([0, 0, 8, 8, 1, 1, 0]),
},
Line {
job: "job number three".to_string(),
task: "task number three".to_string(),
week: create_week([0, 0, 0, 0, 7, 7, 8]),
},
],
week_number: 47,
})
.to_string();

// Strip away ANSI colorsa for easier debugging
let ansi_stripped_time_sheet = anstream::adapter::strip_str(&time_sheet);
insta::assert_snapshot!(ansi_stripped_time_sheet.to_string());
}

#[test]
fn display_timesheet() {
let time_sheet = (TimeSheet {
lines: vec![
Line {
job: "Job number one".to_string(),
task: "Task number one".to_string(),
week: create_week([8, 8, 0, 0, 0, 0, 0]),
},
Line {
job: "job number two".to_string(),
task: "task number two".to_string(),
week: create_week([0, 0, 8, 8, 1, 1, 0]),
},
Line {
job: "job number three".to_string(),
task: "task number three".to_string(),
week: create_week([0, 0, 0, 0, 7, 7, 8]),
},
],
week_number: 47,
})
.to_string();
insta::assert_snapshot!(time_sheet.to_string());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: src/cli/rendering.rs
expression: ansi_stripped_time_sheet.to_string()
snapshot_kind: text
---
╭───────────────────────────────────────────────────────────────────────────────╮
Job name Task name Mon Tue Wed Thu Fri Sat Sun
├───────────────────────────────────────────────────────────────────────────────┤
Job number one Task number one 8:00 8:00
├───────────────────────────────────────────────────────────────────────────────┤
job number two task number two 8:00 8:00 1:00 1:00
├───────────────────────────────────────────────────────────────────────────────┤
job number three task number three 7:00 7:00 8:00
├───────────────────────────────────────────────────────────────────────────────┤
Week 47
╰───────────────────────────────────────────────────────────────────────────────╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
source: src/cli/rendering.rs
expression: time_sheet.to_string()
snapshot_kind: text
---
╭───────────────────────────────────────────────────────────────────────────────╮
│ Job name   Task name   Mon   Tue   Wed   Thu   Fri   Sat   Sun  │
├───────────────────────────────────────────────────────────────────────────────┤
│ Job number one Task number one 8:00 8:00 │
├───────────────────────────────────────────────────────────────────────────────┤
│ job number two task number two 8:00 8:00 1:00 1:00 │
├───────────────────────────────────────────────────────────────────────────────┤
│ job number three task number three 7:00 7:00 8:00 │
├───────────────────────────────────────────────────────────────────────────────┤
│ Week 47  │
╰───────────────────────────────────────────────────────────────────────────────╯

0 comments on commit 61009ae

Please sign in to comment.