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
pub mod actions;
pub mod state;

use ratatui::widgets::{Row, TableState};

use crate::{git::Diff, inputs::key::Key};

use self::{
    actions::{Action, Actions},
    state::AppState,
};

#[derive(Debug, PartialEq, Eq)]
pub enum AppReturn {
    Exit,
    Continue,
}

/// The main application, containing the state
#[derive(Clone)]
pub struct App {
    /// State
    state: AppState,
    /// Contextual Actions
    actions: Actions,
}

impl App {
    pub fn new(diff: Diff) -> Self {
        let state = AppState::new(diff);
        let actions = vec![Action::Quit, Action::Enter, Action::Up, Action::Down].into();

        Self { state, actions }
    }

    pub fn set_diff(&mut self, diff: Diff) {
        self.state = AppState::new(diff)
    }

    /// Handle a user action
    pub fn do_action(
        &mut self,
        key: Key,
        state_one: &mut TableState,
        state_two: &mut TableState,
        diff_one_rows: &Vec<Row>,
        diff_two_rows: &Vec<Row>,
    ) -> AppReturn {
        if let Some(action) = self.actions.find(key) {
            self.state
                .send_to_console(format!("Run action: {}", action));
            match action {
                Action::Quit => AppReturn::Exit,
                Action::Enter => AppReturn::Continue,
                Action::Up => {
                    previous_row(state_one, diff_one_rows);
                    previous_row(state_two, diff_two_rows);
                    AppReturn::Continue
                }
                Action::Down => {
                    next_row(state_one, diff_one_rows);
                    next_row(state_two, diff_two_rows);
                    AppReturn::Continue
                }
            }
        } else {
            self.state
                .send_to_console(format!("No action associated to {}", key));
            AppReturn::Continue
        }
    }

    /// Update the app or dispatch events on tick
    pub fn update_on_tick(&mut self) -> AppReturn {
        // Increments counter
        self.state.incr_tick();
        AppReturn::Continue
    }

    pub fn actions(&self) -> &Actions {
        &self.actions
    }

    pub fn state(&self) -> &AppState {
        &self.state
    }
}

fn next_row(state: &mut TableState, rows: &Vec<Row>) {
    let i = match state.selected() {
        Some(i) => {
            if i >= rows.len() - 1 {
                0
            } else {
                i + 1
            }
        }
        None => 0,
    };

    state.select(Some(i));
}

fn previous_row(state: &mut TableState, rows: &Vec<Row>) {
    let i = match state.selected() {
        Some(i) => {
            if i == 0 {
                rows.len() - 1
            } else {
                i - 1
            }
        }
        None => 0,
    };

    state.select(Some(i));
}