rnk 0.17.3

A React-like declarative terminal UI framework for Rust, inspired by Ink
Documentation
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Calendar component for date selection
//!
//! Displays a monthly calendar with navigation and date selection.

use crate::components::{Box as RnkBox, Text};
use crate::core::{AlignItems, Color, Element, FlexDirection, JustifyContent};

/// Calendar component
#[derive(Debug, Clone)]
pub struct Calendar {
    /// Currently displayed year
    year: i32,
    /// Currently displayed month (1-12)
    month: u32,
    /// Selected date (day of month)
    selected_day: Option<u32>,
    /// Highlighted dates
    highlighted: Vec<u32>,
    /// First day of week (0 = Sunday, 1 = Monday)
    first_day_of_week: u8,
    /// Show week numbers
    show_week_numbers: bool,
    /// Header color
    header_color: Color,
    /// Selected color
    selected_color: Color,
    /// Today color
    today_color: Color,
    /// Highlighted color
    highlighted_color: Color,
    /// Today's date (year, month, day)
    today: Option<(i32, u32, u32)>,
    /// Key for reconciliation
    key: Option<String>,
}

impl Calendar {
    /// Create a new calendar for the current month
    pub fn new(year: i32, month: u32) -> Self {
        Self {
            year,
            month: month.clamp(1, 12),
            selected_day: None,
            highlighted: Vec::new(),
            first_day_of_week: 0, // Sunday
            show_week_numbers: false,
            header_color: Color::Cyan,
            selected_color: Color::Green,
            today_color: Color::Yellow,
            highlighted_color: Color::Magenta,
            today: None,
            key: None,
        }
    }

    /// Set selected day
    pub fn selected(mut self, day: u32) -> Self {
        self.selected_day = Some(day);
        self
    }

    /// Set highlighted dates
    pub fn highlighted(mut self, days: Vec<u32>) -> Self {
        self.highlighted = days;
        self
    }

    /// Set first day of week (0 = Sunday, 1 = Monday)
    pub fn first_day_of_week(mut self, day: u8) -> Self {
        self.first_day_of_week = day % 7;
        self
    }

    /// Start week on Monday
    pub fn monday_first(mut self) -> Self {
        self.first_day_of_week = 1;
        self
    }

    /// Show week numbers
    pub fn show_week_numbers(mut self, show: bool) -> Self {
        self.show_week_numbers = show;
        self
    }

    /// Set header color
    pub fn header_color(mut self, color: Color) -> Self {
        self.header_color = color;
        self
    }

    /// Set selected color
    pub fn selected_color(mut self, color: Color) -> Self {
        self.selected_color = color;
        self
    }

    /// Set today color
    pub fn today_color(mut self, color: Color) -> Self {
        self.today_color = color;
        self
    }

    /// Set highlighted color
    pub fn highlighted_color(mut self, color: Color) -> Self {
        self.highlighted_color = color;
        self
    }

    /// Set today's date for highlighting
    pub fn today(mut self, year: i32, month: u32, day: u32) -> Self {
        self.today = Some((year, month, day));
        self
    }

    /// Set key
    pub fn key(mut self, key: impl Into<String>) -> Self {
        self.key = Some(key.into());
        self
    }

    /// Convert to element
    pub fn into_element(self) -> Element {
        let mut rows = Vec::new();

        // Month/Year header
        let month_name = month_name(self.month);
        let header = Text::new(format!("{} {}", month_name, self.year))
            .color(self.header_color)
            .bold();
        rows.push(
            RnkBox::new()
                .justify_content(JustifyContent::Center)
                .child(header.into_element())
                .into_element(),
        );

        // Day headers
        let day_headers = self.build_day_headers();
        rows.push(day_headers);

        // Calendar grid
        let weeks = self.build_weeks();
        for week in weeks {
            rows.push(week);
        }

        let mut container = RnkBox::new()
            .flex_direction(FlexDirection::Column)
            .gap(0.0)
            .children(rows);

        if let Some(key) = self.key {
            container = container.key(key);
        }

        container.into_element()
    }

    fn build_day_headers(&self) -> Element {
        let days = if self.first_day_of_week == 1 {
            ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"]
        } else {
            ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
        };

        let mut children = Vec::new();

        if self.show_week_numbers {
            children.push(
                RnkBox::new()
                    .width(3)
                    .child(Text::new("Wk").dim().into_element())
                    .into_element(),
            );
        }

        for day in days {
            children.push(
                RnkBox::new()
                    .width(3)
                    .justify_content(JustifyContent::Center)
                    .child(Text::new(day).dim().into_element())
                    .into_element(),
            );
        }

        RnkBox::new()
            .flex_direction(FlexDirection::Row)
            .children(children)
            .into_element()
    }

    fn build_weeks(&self) -> Vec<Element> {
        let days_in_month = days_in_month(self.year, self.month);
        let first_day = day_of_week(self.year, self.month, 1);

        // Adjust for first day of week setting
        let offset = (first_day + 7 - self.first_day_of_week as u32) % 7;

        let mut weeks = Vec::new();
        let mut current_day = 1u32;
        let mut week_num = week_number(self.year, self.month, 1);

        // First week (may have empty cells at start)
        let mut first_week = Vec::new();

        if self.show_week_numbers {
            first_week.push(
                RnkBox::new()
                    .width(3)
                    .child(Text::new(format!("{:2}", week_num)).dim().into_element())
                    .into_element(),
            );
        }

        for i in 0..7 {
            if i < offset {
                first_week.push(
                    RnkBox::new()
                        .width(3)
                        .child(Text::new("  ").into_element())
                        .into_element(),
                );
            } else {
                first_week.push(self.build_day_cell(current_day));
                current_day += 1;
            }
        }

        weeks.push(
            RnkBox::new()
                .flex_direction(FlexDirection::Row)
                .children(first_week)
                .into_element(),
        );

        // Remaining weeks
        while current_day <= days_in_month {
            week_num += 1;
            let mut week = Vec::new();

            if self.show_week_numbers {
                week.push(
                    RnkBox::new()
                        .width(3)
                        .child(
                            Text::new(format!("{:2}", week_num % 53))
                                .dim()
                                .into_element(),
                        )
                        .into_element(),
                );
            }

            for _ in 0..7 {
                if current_day <= days_in_month {
                    week.push(self.build_day_cell(current_day));
                    current_day += 1;
                } else {
                    week.push(
                        RnkBox::new()
                            .width(3)
                            .child(Text::new("  ").into_element())
                            .into_element(),
                    );
                }
            }

            weeks.push(
                RnkBox::new()
                    .flex_direction(FlexDirection::Row)
                    .children(week)
                    .into_element(),
            );
        }

        weeks
    }

    fn build_day_cell(&self, day: u32) -> Element {
        let is_selected = self.selected_day == Some(day);
        let is_today = self
            .today
            .map(|(y, m, d)| y == self.year && m == self.month && d == day)
            .unwrap_or(false);
        let is_highlighted = self.highlighted.contains(&day);

        let text = format!("{:2}", day);
        let mut text_elem = Text::new(text);

        if is_selected {
            text_elem = text_elem.color(self.selected_color).bold();
        } else if is_today {
            text_elem = text_elem.color(self.today_color);
        } else if is_highlighted {
            text_elem = text_elem.color(self.highlighted_color);
        }

        RnkBox::new()
            .width(3)
            .justify_content(JustifyContent::Center)
            .align_items(AlignItems::Center)
            .child(text_elem.into_element())
            .into_element()
    }
}

/// Get month name
fn month_name(month: u32) -> &'static str {
    match month {
        1 => "January",
        2 => "February",
        3 => "March",
        4 => "April",
        5 => "May",
        6 => "June",
        7 => "July",
        8 => "August",
        9 => "September",
        10 => "October",
        11 => "November",
        12 => "December",
        _ => "Unknown",
    }
}

/// Get number of days in a month
fn days_in_month(year: i32, month: u32) -> u32 {
    match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 => {
            if is_leap_year(year) {
                29
            } else {
                28
            }
        }
        _ => 30,
    }
}

/// Check if year is a leap year
fn is_leap_year(year: i32) -> bool {
    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}

/// Get day of week (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
/// Using Zeller's congruence
fn day_of_week(year: i32, month: u32, day: u32) -> u32 {
    let (y, m) = if month < 3 {
        (year - 1, month + 12)
    } else {
        (year, month)
    };

    let q = day as i32;
    let m = m as i32;
    let k = y % 100;
    let j = y / 100;

    let h = (q + (13 * (m + 1)) / 5 + k + k / 4 + j / 4 - 2 * j) % 7;
    ((h + 6) % 7) as u32 // Convert to 0 = Sunday
}

/// Get ISO week number
fn week_number(year: i32, month: u32, day: u32) -> u32 {
    // Simplified week number calculation
    let day_of_year = day_of_year(year, month, day);
    let first_day = day_of_week(year, 1, 1);
    let offset = (7 - first_day) % 7;

    if day_of_year <= offset {
        // Last week of previous year
        week_number(year - 1, 12, 31)
    } else {
        ((day_of_year - offset - 1) / 7 + 1).min(52)
    }
}

/// Get day of year (1-366)
fn day_of_year(year: i32, month: u32, day: u32) -> u32 {
    let mut total = day;
    for m in 1..month {
        total += days_in_month(year, m);
    }
    total
}

impl Default for Calendar {
    fn default() -> Self {
        Self::new(2024, 1)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_calendar_creation() {
        let cal = Calendar::new(2024, 6);
        assert_eq!(cal.year, 2024);
        assert_eq!(cal.month, 6);
    }

    #[test]
    fn test_days_in_month() {
        assert_eq!(days_in_month(2024, 1), 31);
        assert_eq!(days_in_month(2024, 2), 29); // Leap year
        assert_eq!(days_in_month(2023, 2), 28);
        assert_eq!(days_in_month(2024, 4), 30);
    }

    #[test]
    fn test_leap_year() {
        assert!(is_leap_year(2024));
        assert!(!is_leap_year(2023));
        assert!(is_leap_year(2000));
        assert!(!is_leap_year(1900));
    }

    #[test]
    fn test_day_of_week() {
        // January 1, 2024 was a Monday
        assert_eq!(day_of_week(2024, 1, 1), 1);
        // July 4, 2024 was a Thursday
        assert_eq!(day_of_week(2024, 7, 4), 4);
    }

    #[test]
    fn test_calendar_with_selection() {
        let cal = Calendar::new(2024, 6)
            .selected(15)
            .highlighted(vec![1, 10, 20])
            .today(2024, 6, 3);

        assert_eq!(cal.selected_day, Some(15));
        assert_eq!(cal.highlighted, vec![1, 10, 20]);
    }

    #[test]
    fn test_calendar_monday_first() {
        let cal = Calendar::new(2024, 6).monday_first();
        assert_eq!(cal.first_day_of_week, 1);
    }

    #[test]
    fn test_calendar_into_element() {
        let cal = Calendar::new(2024, 6).selected(15);
        let _ = cal.into_element();
    }
}