summaryrefslogtreecommitdiffstats
path: root/archive/cal.rs
blob: 1dc2b0a0eb97fb142e6f2694e20696cb52b2a702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use chrono::Duration;
use chrono::prelude::*;
use yansi::{Style,Color};

use utils::misc;
use KhResult;

struct Cell {
    date: NaiveDate,
    content: (String,String)
}

pub fn printcal() -> KhResult<()> {
    let now = Local::today();
    let a = cal_month(now);
    let b = cal_month(now.with_month(now.month() + 1).unwrap());
    let c = cal_month(now.with_month(now.month() + 2).unwrap());

    let joined = misc::joinlines(&a, &b);
    let joined = misc::joinlines(&joined, &c);
    println!("{}", joined);

    Ok(())
}

pub fn dbg() -> KhResult<()> {
    let begin = Local::today().naive_local();
    let end = begin + Duration::days(5);
    let cells = get_cells(begin, end);
    let cells = expand_cells_to_week(cells);

    let render = render_cells(&cells);
    print!("{}", render);

    Ok(())
}

fn render_cells(cells: &[Cell]) -> String {
    let mut result = String::with_capacity(50);

    let now = cells[0].date;

    result.push_str(&format!("{:>28} {:<8}\n",
        now.format("%B").to_string(),
        now.format("%Y").to_string()
    ));
    let weekdays = &[ "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su" ].iter().map(|x| format!("{:8}", x)).collect::<String>();
    result.push_str(weekdays);
    result.push_str("\n");

    let flow = render_flow(7, 8, cells);
    result.push_str(&flow);

    result
}

fn render_flow(cells_per_line: usize, cell_width: usize, cells: &[Cell]) -> String {
    let mut result = String::with_capacity(50);

    let style = Style::new().bg(Color::Fixed(236));

    let it = cells.iter();
    let mut n = 0;
    while n < (cells.len() / cells_per_line) {
        let line = it.clone().skip(n * cells_per_line).take(cells_per_line);
        for cell in line.clone() {
            let cellstr = &format!("{:width$}", &cell.content.0, width = cell_width);
            let cellstr = &format!("{}", style.paint(cellstr));
            result.push_str(cellstr);
        }
        result.push_str("\n");
        for cell in line {
            let cellstr = &format!("{:width$}", &cell.content.1, width = cell_width);
            let cellstr = &format!("{}", style.paint(cellstr));
            result.push_str(cellstr);
        }
        let emptyline = &format!("{:width$}", "", width = cell_width * cells_per_line);
        result.push_str("\n");
        result.push_str(&format!("{}\n", style.paint(emptyline)));
        n += 1;
    }

    result
}

fn get_cells(date_begin: NaiveDate, date_end: NaiveDate) -> Vec<Cell> {
    let mut result = vec!();
    let mut date = date_begin;
    while date < date_end {
        let cell = cell_whatever(date);
        result.push(cell);

        date += Duration::days(1);
    }
    result
}

fn cell_whatever(date: NaiveDate) -> Cell {
    let fst = date.format("%d").to_string();
    let snd = String::from("");
    Cell{date, content: (fst, snd)}
}

fn cell_empty(date: NaiveDate) -> Cell {
    let fst = date.format("%d").to_string();
    let snd = String::from("");
    Cell{date, content: (fst, snd)}
}

fn expand_cells_to_week(cells: Vec<Cell>) -> Vec<Cell> {
    let mut result = vec!();

    let mut day = NaiveDate::from_isoywd(cells[0].date.year(), cells[0].date.iso_week().week(), Weekday::Mon);
    while day < cells[0].date {
        let cell = cell_empty(day);
        result.push(cell);

        day += Duration::days(1);
    }

    let mut day = cells[cells.len() - 1].date;

    for cell in cells {
        result.push(cell);
    }

    let last_date = NaiveDate::from_isoywd(day.year(), day.iso_week().week(), Weekday::Sun);
    day += Duration::days(1);
    while day <= last_date {
        let cell = cell_empty(day);
        result.push(cell);

        day += Duration::days(1);
    }

    result
}

pub fn cal_month(now: Date<Local>) -> String {
    let mut result = String::with_capacity(50);

    result.push_str(&format!("{:>11} {:<8}\n",
        now.format("%B").to_string(),
        now.format("%Y").to_string()
    ));
    result.push_str("Su Mo Tu We Th Fr Sa\n");

    let this_month = now.month();
    let mut current_day = Local.ymd(now.year(), now.month(), 1);

    let one_day = Duration::days(1);
    for _ in 0..current_day.weekday().num_days_from_sunday() {
        result.push_str("   ");
    }
    while current_day.month() == this_month {
        result.push_str(&format!("{:>2} ", current_day.day()));
        if current_day.weekday() == Weekday::Sat {
            result.push_str("\n");
        }
        current_day = current_day + one_day;
    }
    result.push_str("\n");

    result
}