kodo 0.5.0

A CLI tool for analyzing Git commit statistics with TUI visualization
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Application state management

#![allow(clippy::cast_possible_wrap)]

use crate::error::Result;
use crate::stats::{ActivityStats, AnalysisResult};
use crate::tui::event::{Event, EventHandler};
use crate::tui::ui;
use crossterm::ExecutableCommand;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
use ratatui::prelude::*;
use std::io::stdout;

/// Data point for additions/deletions diverging bar chart
#[derive(Debug, Clone)]
pub struct AddDelDataPoint {
    pub label: String,
    pub additions: u64,
    pub deletions: u64,
}

/// Metric to display in charts
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Metric {
    #[default]
    Commits,
    AdditionsAndDeletions,
    FilesChanged,
}

impl Metric {
    /// Get the next metric in the cycle
    #[must_use]
    pub fn next(self) -> Self {
        match self {
            Self::Commits => Self::AdditionsAndDeletions,
            Self::AdditionsAndDeletions => Self::FilesChanged,
            Self::FilesChanged => Self::Commits,
        }
    }

    /// Get the previous metric in the cycle
    #[must_use]
    pub fn prev(self) -> Self {
        match self {
            Self::Commits => Self::FilesChanged,
            Self::AdditionsAndDeletions => Self::Commits,
            Self::FilesChanged => Self::AdditionsAndDeletions,
        }
    }

    /// Get display name
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::Commits => "Commits",
            Self::AdditionsAndDeletions => "Additions / Deletions",
            Self::FilesChanged => "Files Changed",
        }
    }
}

/// Chart type to display in single mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ChartType {
    #[default]
    Commits,
    FilesChanged,
    AddDel,
    Weekday,
    Hour,
}

impl ChartType {
    /// Get the next chart type in the cycle
    #[must_use]
    pub fn next(self) -> Self {
        match self {
            Self::Commits => Self::FilesChanged,
            Self::FilesChanged => Self::AddDel,
            Self::AddDel => Self::Weekday,
            Self::Weekday => Self::Hour,
            Self::Hour => Self::Commits,
        }
    }

    /// Get the previous chart type in the cycle
    #[must_use]
    pub fn prev(self) -> Self {
        match self {
            Self::Commits => Self::Hour,
            Self::FilesChanged => Self::Commits,
            Self::AddDel => Self::FilesChanged,
            Self::Weekday => Self::AddDel,
            Self::Hour => Self::Weekday,
        }
    }

    /// Get display name
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::Commits => "Commits",
            Self::FilesChanged => "Files Changed",
            Self::AddDel => "Add/Del",
            Self::Weekday => "Weekday",
            Self::Hour => "Hour",
        }
    }
}

/// Application state
pub struct App {
    /// Analysis result to display
    pub result: AnalysisResult,
    /// Activity statistics (commits by weekday and hour)
    pub activity_stats: ActivityStats,
    /// Currently selected chart type (for single mode)
    pub chart_type: ChartType,
    /// Whether the app should quit
    pub should_quit: bool,
    /// Show single metric instead of all metrics
    pub single_metric: bool,
    /// Scroll offset for diverging bar chart (0 = show latest)
    pub scroll_offset: usize,
}

impl App {
    /// Create a new App instance
    #[must_use]
    pub fn new(result: AnalysisResult, activity_stats: ActivityStats, single_metric: bool) -> Self {
        Self {
            result,
            activity_stats,
            chart_type: ChartType::default(),
            should_quit: false,
            single_metric,
            scroll_offset: 0,
        }
    }

    /// Run the TUI application
    ///
    /// # Errors
    ///
    /// Returns an error if terminal operations fail.
    pub fn run(&mut self) -> Result<()> {
        // Setup terminal
        terminal::enable_raw_mode()?;
        stdout().execute(EnterAlternateScreen)?;

        let backend = CrosstermBackend::new(stdout());
        let mut terminal = Terminal::new(backend)?;
        terminal.clear()?;

        // Create event handler
        let event_handler = EventHandler::new(250);

        // Main loop
        let result = self.main_loop(&mut terminal, &event_handler);

        // Restore terminal
        terminal::disable_raw_mode()?;
        stdout().execute(LeaveAlternateScreen)?;

        result
    }

    fn main_loop<B: Backend>(
        &mut self,
        terminal: &mut Terminal<B>,
        event_handler: &EventHandler,
    ) -> Result<()> {
        while !self.should_quit {
            // Draw UI
            terminal.draw(|frame| ui::render(frame, self))?;

            // Handle events
            match event_handler.next()? {
                Event::Key(key) => self.handle_key(key),
                Event::Tick | Event::Resize(_, _) => {}
            }
        }

        Ok(())
    }

    fn handle_key(&mut self, key: KeyEvent) {
        match key.code {
            // Quit
            KeyCode::Char('q') | KeyCode::Esc => {
                self.should_quit = true;
            }
            // Quit with Ctrl+C
            KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                self.should_quit = true;
            }
            // Next chart (only in single metric mode)
            KeyCode::Tab | KeyCode::Right | KeyCode::Char('l') => {
                if self.single_metric {
                    self.chart_type = self.chart_type.next();
                }
            }
            // Previous chart (only in single metric mode)
            KeyCode::BackTab | KeyCode::Left | KeyCode::Char('h') => {
                if self.single_metric {
                    self.chart_type = self.chart_type.prev();
                }
            }
            // Scroll up (past data)
            KeyCode::Up | KeyCode::Char('k') => {
                if self.can_scroll() {
                    self.scroll_up();
                }
            }
            // Scroll down (towards latest)
            KeyCode::Down | KeyCode::Char('j') => {
                if self.can_scroll() {
                    self.scroll_down();
                }
            }
            // Toggle single/split metric view
            KeyCode::Char('m') => {
                self.single_metric = !self.single_metric;
                // Reset scroll offset when toggling views
                self.scroll_offset = 0;
            }
            _ => {}
        }
    }

    /// Check if current view supports scrolling
    fn can_scroll(&self) -> bool {
        if self.single_metric {
            // Only AddDel chart supports scrolling in single mode
            matches!(self.chart_type, ChartType::AddDel)
        } else {
            // Split view supports scrolling
            true
        }
    }

    /// Scroll up to see older data
    fn scroll_up(&mut self) {
        let data_len = self.result.stats.len();
        if data_len > 0 {
            // Max offset: allows scrolling until the oldest item is visible
            let max_offset = data_len.saturating_sub(1);
            self.scroll_offset = (self.scroll_offset + 1).min(max_offset);
        }
    }

    /// Scroll down to see newer data
    fn scroll_down(&mut self) {
        self.scroll_offset = self.scroll_offset.saturating_sub(1);
    }

    /// Get values for a specific metric
    #[must_use]
    pub fn values_for_metric(&self, metric: Metric) -> Vec<(String, i64)> {
        self.result
            .stats
            .iter()
            .map(|s| {
                let value = match metric {
                    Metric::Commits => i64::from(s.commits),
                    Metric::AdditionsAndDeletions => s.net_lines,
                    Metric::FilesChanged => i64::from(s.files_changed),
                };
                (s.label.clone(), value)
            })
            .collect()
    }

    /// Get all metrics
    #[must_use]
    pub fn all_metrics() -> [Metric; 3] {
        [
            Metric::Commits,
            Metric::AdditionsAndDeletions,
            Metric::FilesChanged,
        ]
    }

    /// Get additions/deletions data for diverging bar chart
    #[must_use]
    pub fn additions_deletions_data(&self) -> Vec<AddDelDataPoint> {
        self.result
            .stats
            .iter()
            .map(|s| AddDelDataPoint {
                label: s.label.clone(),
                additions: s.additions,
                deletions: s.deletions,
            })
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::stats::{PeriodStats, TotalStats};
    use chrono::NaiveDate;

    fn make_result() -> AnalysisResult {
        AnalysisResult {
            repository: "test".to_string(),
            period: "daily".to_string(),
            from: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            to: NaiveDate::from_ymd_opt(2024, 1, 7).unwrap(),
            stats: vec![PeriodStats {
                label: "2024-01-01".to_string(),
                date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                commits: 5,
                additions: 100,
                deletions: 20,
                net_lines: 80,
                files_changed: 10,
            }],
            total: TotalStats::default(),
        }
    }

    #[test]
    fn test_metric_cycle() {
        let metric = Metric::Commits;
        assert_eq!(metric.next(), Metric::AdditionsAndDeletions);
        assert_eq!(metric.prev(), Metric::FilesChanged);
    }

    #[test]
    fn test_all_metrics() {
        let metrics = App::all_metrics();
        assert_eq!(metrics.len(), 3);
    }

    #[test]
    fn test_additions_deletions_data() {
        let result = make_result();
        let app = App::new(result, ActivityStats::default(), false);

        let data = app.additions_deletions_data();
        assert_eq!(data.len(), 1);
        assert_eq!(data[0].label, "2024-01-01");
        assert_eq!(data[0].additions, 100);
        assert_eq!(data[0].deletions, 20);
    }

    fn make_result_with_multiple_days() -> AnalysisResult {
        AnalysisResult {
            repository: "test".to_string(),
            period: "daily".to_string(),
            from: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            to: NaiveDate::from_ymd_opt(2024, 1, 5).unwrap(),
            stats: (1..=5)
                .map(|day| PeriodStats {
                    label: format!("2024-01-0{day}"),
                    date: NaiveDate::from_ymd_opt(2024, 1, day).unwrap(),
                    commits: day,
                    additions: u64::from(day) * 10,
                    deletions: u64::from(day) * 2,
                    net_lines: i64::from(day) * 8,
                    files_changed: day,
                })
                .collect(),
            total: TotalStats::default(),
        }
    }

    #[test]
    fn test_scroll_up_increases_offset() {
        let result = make_result_with_multiple_days();
        let mut app = App::new(result, ActivityStats::default(), false);

        assert_eq!(app.scroll_offset, 0);
        app.scroll_up();
        assert_eq!(app.scroll_offset, 1);
        app.scroll_up();
        assert_eq!(app.scroll_offset, 2);
    }

    #[test]
    fn test_scroll_down_decreases_offset() {
        let result = make_result_with_multiple_days();
        let mut app = App::new(result, ActivityStats::default(), false);

        app.scroll_offset = 3;
        app.scroll_down();
        assert_eq!(app.scroll_offset, 2);
        app.scroll_down();
        assert_eq!(app.scroll_offset, 1);
        app.scroll_down();
        assert_eq!(app.scroll_offset, 0);
    }

    #[test]
    fn test_scroll_down_does_not_go_negative() {
        let result = make_result_with_multiple_days();
        let mut app = App::new(result, ActivityStats::default(), false);

        assert_eq!(app.scroll_offset, 0);
        app.scroll_down();
        assert_eq!(app.scroll_offset, 0);
    }

    #[test]
    fn test_scroll_up_respects_max_offset() {
        let result = make_result_with_multiple_days();
        let mut app = App::new(result, ActivityStats::default(), false);

        // 5 items, max offset should be 4 (data_len - 1)
        for _ in 0..10 {
            app.scroll_up();
        }
        assert_eq!(app.scroll_offset, 4);
    }

    #[test]
    fn test_scroll_offset_resets_on_view_toggle() {
        let result = make_result_with_multiple_days();
        let mut app = App::new(result, ActivityStats::default(), false);

        app.scroll_offset = 3;
        // Simulate pressing 'm' to toggle view
        app.single_metric = !app.single_metric;
        app.scroll_offset = 0; // This happens in handle_key

        assert_eq!(app.scroll_offset, 0);
    }

    #[test]
    fn test_chart_type_cycle() {
        let chart = ChartType::Commits;
        assert_eq!(chart.next(), ChartType::FilesChanged);
        assert_eq!(chart.next().next(), ChartType::AddDel);
        assert_eq!(chart.next().next().next(), ChartType::Weekday);
        assert_eq!(chart.next().next().next().next(), ChartType::Hour);
        assert_eq!(chart.next().next().next().next().next(), ChartType::Commits);
    }

    #[test]
    fn test_chart_type_prev_cycle() {
        let chart = ChartType::Commits;
        assert_eq!(chart.prev(), ChartType::Hour);
        assert_eq!(chart.prev().prev(), ChartType::Weekday);
        assert_eq!(chart.prev().prev().prev(), ChartType::AddDel);
        assert_eq!(chart.prev().prev().prev().prev(), ChartType::FilesChanged);
        assert_eq!(chart.prev().prev().prev().prev().prev(), ChartType::Commits);
    }

    #[test]
    fn test_chart_type_name() {
        assert_eq!(ChartType::Commits.name(), "Commits");
        assert_eq!(ChartType::FilesChanged.name(), "Files Changed");
        assert_eq!(ChartType::AddDel.name(), "Add/Del");
        assert_eq!(ChartType::Weekday.name(), "Weekday");
        assert_eq!(ChartType::Hour.name(), "Hour");
    }

    #[test]
    fn test_can_scroll_in_split_mode() {
        let result = make_result();
        let app = App::new(result, ActivityStats::default(), false);

        // Split mode always supports scrolling
        assert!(app.can_scroll());
    }

    #[test]
    fn test_can_scroll_in_single_mode() {
        let result = make_result();
        let mut app = App::new(result, ActivityStats::default(), true);

        // Only AddDel chart supports scrolling in single mode
        app.chart_type = ChartType::AddDel;
        assert!(app.can_scroll());

        app.chart_type = ChartType::Commits;
        assert!(!app.can_scroll());

        app.chart_type = ChartType::FilesChanged;
        assert!(!app.can_scroll());

        app.chart_type = ChartType::Weekday;
        assert!(!app.can_scroll());

        app.chart_type = ChartType::Hour;
        assert!(!app.can_scroll());
    }

    #[test]
    fn test_chart_type_default() {
        assert_eq!(ChartType::default(), ChartType::Commits);
    }

    #[test]
    fn test_app_new_initializes_chart_type() {
        let result = make_result();
        let app = App::new(result, ActivityStats::default(), false);

        assert_eq!(app.chart_type, ChartType::default());
    }
}