Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let first calendar day always start on Monday #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cursive::view::View;
use cursive::{Printer, Vec2};

use chrono::prelude::*;
use chrono::{Duration, Local, NaiveDate};
use chrono::{Duration, Local, NaiveDate, Weekday};

use crate::habit::{Bit, Count, Habit, TrackEvent, ViewMode};

Expand Down Expand Up @@ -111,6 +111,21 @@ where
};

let draw_day = |printer: &Printer| {
let offset = match NaiveDate::from_ymd_opt(year, month, 1) {
Copy link

@AdamKuziemski AdamKuziemski Jul 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably take into account that in some countries Sunday is the first day of the week, e.g. USA, Canada, Japan. Another thing could be making this offset optional.

Also, maybe calculating day offset deserves to be in a separate function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably take into account that in some countries Sunday is the first day of the week, e.g. USA, Canada, Japan. Another thing could be making this offset optional.

Yeah … pretty much what I wrote in the PR description.

Some(d) => {
match d.weekday() {
Weekday::Mon => 0,
Weekday::Tue => 1,
Weekday::Wed => 2,
Weekday::Thu => 3,
Weekday::Fri => 4,
Weekday::Sat => 5,
Weekday::Sun => 6,
}
}
None => 0
};

let mut i = 0;
while let Some(d) = NaiveDate::from_ymd_opt(year, month, i + 1) {
let day_style;
Expand All @@ -119,7 +134,8 @@ where
} else {
day_style = todo_style;
}
let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into();
let pos = i + offset;
let coords: Vec2 = ((pos % 7) * 3, pos / 7 + 2).into();
if let Some(c) = self.get_by_date(d) {
printer.with_style(day_style, |p| {
p.print(coords, &format!("{:^3}", c));
Expand Down