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

Added --monday-as-first flag (-m for short) #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ USAGE:

FLAGS:
-h, --help Prints help information
-m, --monday-as-first Use Monday as first day of the week
-V, --version Prints version information

OPTIONS:
Expand All @@ -55,4 +56,4 @@ ${color #888}27 ${color}28 29 30
Thus I put this in my `conky.config` file:
```
${execpi 600 conky-calendar --today-color dd3a21 --weekend-color 888}
```
```
30 changes: 26 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,48 @@ fn main() {
.value_name("COLOR")
.takes_value(true),
)
.arg(
Arg::with_name("monday-as-first")
.short("m")
.long("monday-as-first")
.help("Use Monday as first day of the week")
)
.get_matches();

let now = Local::now(); // current date & time via chrono

let labels = match matches.is_present("monday-as-first") {
true => String::from("Mo Tu We Th Fr Sa Su"),
false => String::from("Su Mo Tu We Th Fr Sa"),
};

// weekday labels
if matches.is_present("label-color") {
println!(
"${{color #{color}}}{:^20}\nSu Mo Tu We Th Fr Sa${{color}}",
"${{color #{color}}}{:^20}\n{}${{color}}",
now.format("%B %Y"),
labels,
color = matches.value_of("label-color").unwrap()
);
} else {
println!("{:^20}\nSu Mo Tu We Th Fr Sa", now.format("%B %Y"));
println!("{:^20}\n{}", now.format("%B %Y"), labels);
}

// calculate where the numbers go, add spaces to offset the first day
let first_day_of_month = NaiveDate::from_ymd(now.year(), now.month(), 1);

let initial_offset = match matches.is_present("monday-as-first") {
true => first_day_of_month.weekday().num_days_from_monday(),
false => first_day_of_month.weekday().num_days_from_sunday(),
};

let (saturday_day, sunday_day) = match matches.is_present("monday-as-first") {
true => (5, 6),
false => (6, 0),
};

let mut col = 0;
for _ in 0..first_day_of_month.weekday().num_days_from_sunday() {
for _ in 0..initial_offset {
print!("{:^3}", "");
col = col + 1;
}
Expand All @@ -127,7 +149,7 @@ fn main() {
day,
color = matches.value_of("today-color").unwrap()
);
} else if matches.is_present("weekend-color") && (col == 0 || col == 6) { // color weekends if needed
} else if matches.is_present("weekend-color") && (col == saturday_day || col == sunday_day) { // color weekends if needed
print!(
"${{color #{color}}}{:^3}${{color}}",
day,
Expand Down