Skip to content

Commit

Permalink
feat: implement year progress feature
Browse files Browse the repository at this point in the history
Closes: #92
  • Loading branch information
b1rger committed Jan 10, 2024
1 parent 1f2a7c0 commit 0a434f0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub struct Action {
pub calendar: bool,
#[clap(short = 'a', long = "agenda", help = "show agenda")]
pub agenda: bool,
#[clap(long = "year-progress", help = "show year progress")]
pub yearprogress: bool,
}

impl Cli {
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use events::{Events, ReadFromIcsFile};
use output::agenda::Agenda;
use output::calendar::Calendar;
use output::date::Date;
use output::yearprogress::Yearprogress;
use utils::DateExtensions;

#[cfg(not(tarpaulin_include))]
Expand Down Expand Up @@ -84,6 +85,11 @@ fn main() {

if ctx.opts.action.agenda {
let agenda = Agenda { ctx: &ctx };
print!("{}", agenda)
print!("{}", agenda);
}

if ctx.opts.action.yearprogress {
let yp = Yearprogress { ctx: &ctx };
print!("{}", yp);
}
}
1 change: 1 addition & 0 deletions src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
pub mod agenda;
pub mod calendar;
pub mod date;
pub mod yearprogress;
30 changes: 30 additions & 0 deletions src/output/yearprogress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2021-2023 Birger Schacht <[email protected]>
//
// SPDX-License-Identifier: MIT

use crate::Context;

use chrono;
use chrono::Datelike;
use std::fmt;

pub struct Yearprogress<'a> {
pub ctx: &'a Context,
}

impl fmt::Display for Yearprogress<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let year = self.ctx.usersetdate.year();
let day = self.ctx.usersetdate.ordinal();
let days_in_year: u32 = if self.ctx.usersetdate.leap_year() {
366
} else {
365
};
let days_in_year_left: u32 = days_in_year - day;
let percentage: f32 = (day * 100) as f32 / days_in_year as f32;
let mut ret = format!("{percentage:.3}% of the year {year}\n");
ret += format!("Day number {day} of {year}, {days_in_year_left} left").as_str();
write!(f, "{}", ret)
}
}

0 comments on commit 0a434f0

Please sign in to comment.