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
use std::path::PathBuf;

use rscript::Hook;
use serde::{Deserialize, Serialize};

// Reexport crossterm event types
pub mod event {
    pub use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
}
pub mod color {
    pub use crossterm::style::Color;
}

macro_rules! hookit {
    (Hook => $hook: ident,
     Input => ($($input: ty $(,)?)*),
     Output => $output: ty) => (

    #[derive(Serialize, Deserialize)]
    pub struct $hook($(pub $input,)*);

    impl Hook for $hook {
        const NAME: &'static str = stringify!($hook);
        type Output = $output;
    }
)}

hookit!(
Hook => InputEvent,
Input => (GlobalVariables, event::Event),
Output => Option<Command>
);
hookit!(
Hook => OutputEvent,
Input => (GlobalVariables, String),
Output => Option<Command>
);
hookit!(
Hook => SetTitle,
Input => (),
Output => Option<String>
);
hookit!(
Hook => SetWelcomeMsg,
Input => (),
Output => Option<String>
);
hookit!(
Hook => Shutdown,
Input => (),
Output => Option<Command>
);
hookit!(
Hook => Startup,
Input => (),
Output => Option<Command>
);
hookit!(
Hook => SetInputPrompt,
Input => (GlobalVariables),
Output => String
);
hookit!(
Hook => SetOutputPrompt,
Input => (GlobalVariables),
Output => String
);
hookit!(
Hook => BeforeCompiling,
Input => (GlobalVariables),
Output => ()
);
hookit!(
Hook => AfterCompiling,
Input => (GlobalVariables),
Output => ()
);

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Command {
    AcceptSuggestion,
    Continue,
    DeleteNextWord,
    DeleteTillEnd,
    DeleteUntilChar(char, bool),
    MoveForwardTillChar(char),
    MoveBackwardTillChar(char),
    Parse(String),
    PrintInput,
    PrintOutput(String, color::Color),
    MacroRecordToggle,
    MacroPlay,
    Multiple(Vec<Command>),
    SetThinCursor,
    SetWideCursor,
    HandleCharacter(char),
    HandleEnter(bool),
    HandleAltEnter,
    HandleTab,
    HandleBackTab,
    HandleRight,
    HandleLeft,
    GoToLastRow,
    HandleBackSpace,
    HandleDelete,
    HandleCtrlC,
    HandleCtrlD,
    HandleCtrlE,
    HandleCtrlL,
    HandleCtrlR,
    HandleCtrlZ,
    HandleUp,
    HandleDown,
    HandleCtrlRight,
    HandleCtrlLeft,
    HandleHome,
    HandleEnd,
    Redo,
    RemoveRASugesstion,
    ResetPrompt,
    Undo,
    Exit,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlobalVariables {
    current_working_dir: PathBuf,
    previous_working_dir: PathBuf,
    last_loaded_code_path: Option<PathBuf>,
    /// last successful output
    last_output: Option<String>,
    pub operation_number: usize,

    pub prompt_position: (usize, usize), // (row, col)
    pub cursor_position: (usize, usize), // (row, col)
    pub prompt_len: usize,
    pub pid: u32,
    pub is_ra_suggestion_active: bool,
}

impl Default for GlobalVariables {
    fn default() -> Self {
        Self::new()
    }
}

impl GlobalVariables {
    pub fn new() -> Self {
        let cwd = std::env::current_dir().expect("Error getting current working directory");

        Self {
            current_working_dir: cwd.clone(),
            previous_working_dir: cwd,
            last_loaded_code_path: None,
            last_output: None,
            operation_number: 1,
            prompt_position: (0, 0), // (row, col)
            cursor_position: (0, 0), // (row, col)
            prompt_len: 0,
            pid: std::process::id(),
            is_ra_suggestion_active: false,
        }
    }

    pub fn update_cwd(&mut self, cwd: PathBuf) {
        self.previous_working_dir
            .clone_from(&self.current_working_dir);
        self.current_working_dir = cwd;
    }

    pub fn get_cwd(&self) -> PathBuf {
        self.current_working_dir.clone()
    }

    pub fn get_pwd(&self) -> PathBuf {
        self.previous_working_dir.clone()
    }

    pub fn set_last_loaded_coded_path(&mut self, path: PathBuf) {
        self.last_loaded_code_path = Some(path);
    }

    pub fn get_last_loaded_coded_path(&self) -> Option<PathBuf> {
        self.last_loaded_code_path.clone()
    }

    pub fn get_last_output(&self) -> Option<&String> {
        self.last_output.as_ref()
    }

    pub fn set_last_output(&mut self, out: String) {
        self.last_output = Some(out);
    }
}