Skip to content

Commit

Permalink
更新了一些东西
Browse files Browse the repository at this point in the history
可以自己选择一个星期的第一天了
现在有两个示例了
版本号提升到了0.1.2
  • Loading branch information
mogmoug committed Jan 3, 2023
1 parent 3b518be commit 132d67c
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-calendar"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
description = "A calendar command-line tool written in rust"
readme = "README.md"
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# rust-calendar
# [rust-calendar](https://github.com/mogmoug/rust-calendar)
生锈的日历
一个使用rust编写的日历,可以打印某个月的日历
适合初学者学习此项目
```
A calendar command-line tool written in rust
Usage: rust-calendar [COMMAND]
Usage: rust-calendar [OPTIONS] [COMMAND]
Commands:
now Now get the calendar for this month.
Expand All @@ -15,8 +15,12 @@ Commands:
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help information
-V, --version Print version information
-t, --the-first-day-of-the-week <THE_FIRST_DAY_OF_THE_WEEK>
The first day of the week.Sunday(0) or Monday(1) [default: 0]
-h, --help
Print help information
-V, --version
Print version information
```
## 编译
```shell
Expand Down
4 changes: 4 additions & 0 deletions examples/now.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use rust_calendar::calendar::Calendar;
fn main(){
Calendar::get_print_calendar_now(0);
}
5 changes: 5 additions & 0 deletions examples/print_calendar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use rust_calendar::calendar::*;
fn main(){
let c = Calendar::from_year_month(2023, 2,0);
c.print_calendar();
}
51 changes: 41 additions & 10 deletions src/calendar.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
use chrono::prelude::*;
#[derive(Debug)]
pub(crate) struct Calendar {
pub struct Calendar {
first_day: u32,
fst_day_of_week: i8,
num_of_days: u32,
days: [[u32; 7]; 6],
}
#[allow(dead_code)]
/// 日历的impl,包含很多的有用的函数
impl Calendar {
pub fn new(first_day: u32, num_of_days: u32) -> Calendar {
pub fn new(first_day: u32, num_of_days: u32,fst_day_of_week: i8) -> Calendar {
Calendar {
first_day,
fst_day_of_week,
num_of_days,
days: [[0; 7]; 6],
}
}
pub fn init(&mut self) {
let mut curday: u32 = self.first_day;
let mut curday: u32 = {
if self.fst_day_of_week == 0{
self.first_day
}else if self.fst_day_of_week == 1 {
if self.first_day >= 6{
self.first_day
}else {
self.first_day+1
}
}else {
eprintln!("Error fstday");
self.first_day
}
};
let mut num_of_weeks: usize = 0;
for d in 1..=self.num_of_days {
if curday >= 7 {
Expand All @@ -29,8 +45,15 @@ impl Calendar {
}
}
}
/// 打印日历
pub fn print_calendar(&self) {
println!("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
if self.fst_day_of_week == 0 {
println!("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
}else if self.fst_day_of_week == 1 {
println!("Mon\tTue\tWed\tThu\tFri\tSat\tSun");
}else {
eprintln!("Error unknown fst_day_of_week");
}
for w in self.days {
for d in w {
if d != 0{
Expand Down Expand Up @@ -58,28 +81,36 @@ impl Calendar {
_ => 30,
};
}
pub fn get_print_calendar_now() -> Calendar {
pub fn get_print_calendar_now(fst_day_of_week: i8) -> Calendar {
let date = Local::now();
let first_day = date.clone().with_day(1);
let mut c: Calendar = Calendar::new(
first_day.unwrap().weekday().num_days_from_sunday(),
Self::num_day_of_month(date.year(),date.month()),
Self::num_day_of_month(date.year(),date.month()),fst_day_of_week
);
c.init();
c.print_calendar();
c
}
pub fn get_calendar_now() -> Calendar {
pub fn get_calendar_now(fst_day_of_week: i8) -> Calendar {
let date = Local::now();
let first_day = date.clone().with_day(1);
let mut c: Calendar = Calendar::new(
first_day.unwrap().weekday().num_days_from_sunday(),
Self::num_day_of_month(date.year(),date.month()),
Self::num_day_of_month(date.year(),date.month()),fst_day_of_week
);
c.init();
c
}
pub fn from_year_month(year: i32,month: i32) -> Calendar{
/// 根据参数year(年)和month(月)来返回一个日历
/// ```
/// use rust_calendar::calendar::*;
/// fn main(){
/// let c = Calendar::from_year_month(2023,10);
/// c.print_calendar();
/// }
/// ```
pub fn from_year_month(year: i32,month: i32,fst_day_of_week: i8) -> Calendar{
let mut date = Local::now();
date = match date.with_year(year){
Some(dt) => {
Expand All @@ -96,7 +127,7 @@ impl Calendar {
let first_day = date.clone().with_day(1);
let mut c: Calendar = Calendar::new(
first_day.unwrap().weekday().num_days_from_sunday(),
Self::num_day_of_month(date.year(),date.month()),
Self::num_day_of_month(date.year(),date.month()),fst_day_of_week
);
c.init();
c
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
#![crate_type = "lib"]

pub mod calendar;
pub use calendar::Calendar;
mod test;
mod options;
pub mod options;
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ fn main() {
let cil = options::Cil::parse();
match &cil.command{
Some(options::Commands::Date { year, month }) => {
Calendar::from_year_month((&year).as_ref().unwrap().parse().expect("Error year"),(&month).as_ref().unwrap().parse().expect("Error year"));
Calendar::from_year_month((&year).as_ref().unwrap().parse().expect("Error year"),(&month).as_ref().unwrap().parse().expect("Error year"),cil.the_first_day_of_the_week);
},
Some(options::Commands::Now)=>{
Calendar::get_print_calendar_now();
Calendar::get_print_calendar_now(cil.the_first_day_of_the_week);
},
Some(options::Commands::DebugInfo)=>{
println!("{:?}",Calendar::get_calendar_now());
println!("{:?}",Calendar::get_calendar_now(cil.the_first_day_of_the_week));
},
None => {
options::Cil::parse_from(vec!["rust-calendar","help"].iter());
Expand Down
3 changes: 3 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use clap::*;
pub struct Cil{
#[command(subcommand)]
pub command: Option<Commands>,
/// The first day of the week.Sunday(0) or Monday(1).
#[arg(short,long,default_value_t = 0)]
pub the_first_day_of_the_week: i8,
}

#[derive(Subcommand)]
Expand Down
6 changes: 3 additions & 3 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ mod tests{
#[test]
fn test_print_calendar(){
use crate::calendar::Calendar;
let c = Calendar::get_calendar_now();
let c = Calendar::get_calendar_now(0);
c.print_calendar();
}
#[test]
fn can_print(){
use crate::calendar::Calendar;
let mut c: Calendar = Calendar::new(6, 29);
let mut c: Calendar = Calendar::new(6, 29,0);
c.init();
c.print_calendar();
}
#[test]
fn print_calendar_now(){
use crate::calendar::Calendar;
Calendar::get_print_calendar_now();
Calendar::get_print_calendar_now(0);
}
}

0 comments on commit 132d67c

Please sign in to comment.