dnote-tui 0.5.10

TUI for dnote
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
use std::process::Command;

use color_eyre::eyre::Result;
use crossterm::style::{Color, Print, ResetColor, SetForegroundColor};
use crossterm::{event::KeyEvent, ExecutableCommand};
use ratatui::{
    layout::{Constraint, Direction, Layout},
    prelude::Rect,
};
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};

use crate::{
    action::Action,
    components::{
        books::BooksPane,
        content::ContentPane,
        footer::FooterPane,
        header::HeaderPane,
        pages::PagesPane,
        popup::{Popup, PopupType},
        Component,
    },
    config::Config,
    dnote::Dnote,
    state::{InputMode, Mode, State},
    tui,
};

pub struct App {
    pub config: Config,
    pub tui: tui::Tui,
    pub action_tx: UnboundedSender<Action>,
    pub action_rx: UnboundedReceiver<Action>,
    pub tick_rate: f64,
    pub frame_rate: f64,
    pub components: Vec<Box<dyn Component>>,
    pub header: Box<dyn Component>,
    pub footer: Box<dyn Component>,
    pub popup: Option<Box<dyn Component>>,
    pub should_quit: bool,
    pub should_suspend: bool,
    pub last_tick_key_events: Vec<KeyEvent>,
    pub dnote: Dnote,
    pub state: State,
}

impl App {
    pub fn new(tick_rate: f64, frame_rate: f64) -> Result<Self> {
        let tui = tui::Tui::new()?.tick_rate(tick_rate).frame_rate(frame_rate);
        let (action_tx, action_rx) = mpsc::unbounded_channel();
        let mut state = State::new();
        state.mode = Mode::Book;
        let dnote = Dnote::new();
        let header = HeaderPane::default();
        let footer = FooterPane::default();
        let books = BooksPane::default();
        let pages = PagesPane::default();
        let content = ContentPane::default();
        let config = Config::new()?;
        let app = Self {
            tui,
            action_tx,
            action_rx,
            tick_rate,
            frame_rate,
            components: vec![
                // Note: ordering is important as layout constraints draw components in order
                Box::new(books),
                Box::new(pages),
                Box::new(content),
            ],
            header: Box::new(header),
            footer: Box::new(footer),
            popup: None,
            should_quit: false,
            should_suspend: false,
            config,
            last_tick_key_events: Vec::new(),
            dnote,
            state,
        };
        Ok(app)
    }

    fn wait_for_enter_to_return(&self) -> Result<()> {
        std::io::stdout()
            .execute(SetForegroundColor(Color::Green))?
            .execute(Print("\nPress enter to return to dnote-tui"))?
            .execute(ResetColor)?;
        use std::io::{self, BufRead};
        let stdin = io::stdin();
        stdin.lock().lines().next();
        Ok(())
    }

    fn log_command(&self, command: &str, args: &[&str]) -> Result<()> {
        std::io::stdout()
            .execute(SetForegroundColor(Color::Blue))?
            .execute(Print(format!("\n=> {} {}\n\n", command, args.join(" "))))?
            .execute(ResetColor)?;
        Ok(())
    }

    fn spawn_process(&self, command: &str, args: &[&str]) -> Result<()> {
        self.log_command(command, args)?;
        let status = Command::new(command).args(args).status()?;
        if !status.success() {
            eprintln!("\nCommand failed with status: {}", status);
        }
        self.wait_for_enter_to_return()?;
        Ok(())
    }

    pub fn resume(&mut self) -> Result<()> {
        self.tui.enter()?;
        self.tui.clear()?;
        Ok(())
    }

    pub fn pause(&mut self) -> Result<()> {
        self.tui.exit()?;
        Ok(())
    }

    pub fn close_popup(&mut self) -> Result<()> {
        self.popup.take();
        Ok(())
    }

    pub async fn run(&mut self) -> Result<()> {
        // tui.mouse(true);
        self.tui.enter()?;

        for component in self.components.iter_mut() {
            component.register_action_handler(self.action_tx.clone())?;
        }

        for component in self.components.iter_mut() {
            component.register_config_handler(self.config.clone())?;
        }

        for component in self.components.iter_mut() {
            component.init(self.tui.size()?)?;
        }

        self.header
            .register_action_handler(self.action_tx.clone())?;
        self.footer
            .register_action_handler(self.action_tx.clone())?;

        self.header.register_config_handler(self.config.clone())?;
        self.footer.register_config_handler(self.config.clone())?;

        self.header.init(self.tui.size()?)?;
        self.footer.init(self.tui.size()?)?;

        if let Some(popup) = &mut self.popup {
            popup.register_action_handler(self.action_tx.clone())?;
            popup.register_config_handler(self.config.clone())?;
            popup.init(self.tui.size()?)?;
        }

        loop {
            if let Some(e) = self.tui.next_event().await {
                match e {
                    tui::Event::Quit if self.state.input_mode == InputMode::Normal => {
                        self.action_tx.send(Action::Quit)?
                    }
                    tui::Event::Tick => self.action_tx.send(Action::Tick)?,
                    tui::Event::Render => self.action_tx.send(Action::Render)?,
                    tui::Event::Resize(x, y) => self.action_tx.send(Action::Resize(x, y))?,
                    tui::Event::Key(key) => {
                        match self.state.input_mode {
                            InputMode::Normal => {
                                if let Some(keymap) = self.config.keybindings.get(&self.state.mode)
                                {
                                    if let Some(action) = keymap.get(&vec![key]) {
                                        log::info!("Got action: {action:?}");
                                        self.action_tx.send(action.clone())?;
                                    } else {
                                        // If the key was not handled as a single key action,
                                        // then consider it for multi-key combinations.
                                        self.last_tick_key_events.push(key);

                                        // Check for multi-key combinations
                                        if let Some(action) = keymap.get(&self.last_tick_key_events)
                                        {
                                            log::info!("Got action: {action:?}");
                                            self.action_tx.send(action.clone())?;
                                        }
                                    }
                                };
                            }
                            InputMode::Insert => {
                                log::debug!("Skipping keybinds from config in insert mode...");
                            }
                        }
                    }
                    _ => {}
                }
                for component in self.components.iter_mut() {
                    if let Some(action) =
                        component.handle_events(Some(e.clone()), &mut self.state)?
                    {
                        self.action_tx.send(action)?;
                    }
                }
                if let Some(popup) = &mut self.popup {
                    if let Some(action) = popup.handle_events(Some(e.clone()), &mut self.state)? {
                        self.action_tx.send(action)?
                    };
                }
                if let Some(action) = self
                    .header
                    .handle_events(Some(e.clone()), &mut self.state)?
                {
                    self.action_tx.send(action)?
                };
                if let Some(action) = self
                    .footer
                    .handle_events(Some(e.clone()), &mut self.state)?
                {
                    self.action_tx.send(action)?
                };
            }

            while let Ok(action) = self.action_rx.try_recv() {
                if action != Action::Tick && action != Action::Render {
                    log::info!("{action:?}");
                }
                match action {
                    Action::Tick => {
                        self.last_tick_key_events.drain(..);
                    }
                    Action::Quit if self.state.input_mode == InputMode::Normal => {
                        self.should_quit = true
                    }
                    Action::Suspend => self.should_suspend = true,
                    Action::Resume => self.should_suspend = false,
                    Action::Refresh => self.tui.terminal.clear()?,
                    Action::Resize(w, h) => {
                        self.tui.resize(Rect::new(0, 0, w, h))?;
                        self.draw()?;
                    }
                    Action::Render => {
                        self.draw()?;
                    }
                    Action::ExecuteCommand(ref command, ref args) => {
                        self.pause()?;
                        let cmd = command.to_string();
                        let cmd_args = args.iter().map(|s| s.as_str()).collect::<Vec<&str>>();
                        self.spawn_process(&cmd, &cmd_args)?;
                        self.resume()?;
                    }
                    Action::AddBook => {
                        let input_popup = Popup::new(
                            "Add New Book".into(),
                            "Name".into(),
                            "".into(),
                            Some("Note: Book names cannot contain spaces!".into()),
                            PopupType::NewBook,
                        );
                        self.popup = Some(Box::new(input_popup));
                        if let Some(popup) = &mut self.popup {
                            popup.register_action_handler(self.action_tx.clone())?;
                            popup.register_config_handler(self.config.clone())?;
                            popup.init(self.tui.size()?)?;
                        }
                        self.state.input_mode = InputMode::Insert;
                    }
                    Action::RenameActiveBook => {
                        if let Some(ref book) = self.state.get_active_book() {
                            let input_popup = Popup::new(
                                "Rename Book".into(),
                                "Name".into(),
                                book.name.clone(),
                                Some("Note: Book names cannot contain spaces!".into()),
                                PopupType::RenameBook,
                            );
                            self.popup = Some(Box::new(input_popup));
                            if let Some(popup) = &mut self.popup {
                                popup.register_action_handler(self.action_tx.clone())?;
                                popup.register_config_handler(self.config.clone())?;
                                popup.init(self.tui.size()?)?;
                            }
                            self.state.input_mode = InputMode::Insert;
                        } else {
                            log::error!("No active book to rename");
                        }
                    }
                    Action::SubmitPopup => {
                        self.popup.take(); // set popup to None
                        self.state.input_mode = InputMode::Normal;
                    }
                    Action::ClosePopup => {
                        self.popup.take(); // set popup to None
                        self.state.input_mode = InputMode::Normal;
                    }
                    _ => {}
                }
                for component in self.components.iter_mut() {
                    if let Some(action) = component.update(action.clone(), &mut self.state)? {
                        self.action_tx.send(action)?
                    };
                }
                if let Some(popup) = &mut self.popup {
                    if let Some(action) = popup.update(action.clone(), &mut self.state)? {
                        self.action_tx.send(action)?
                    };
                }
                if let Some(action) = self.header.update(action.clone(), &mut self.state)? {
                    self.action_tx.send(action)?
                };
                if let Some(action) = self.footer.update(action.clone(), &mut self.state)? {
                    self.action_tx.send(action)?
                };
            }
            if self.should_suspend {
                self.tui.suspend()?;
                self.action_tx.send(Action::Resume)?;
                self.action_tx.send(Action::Refresh)?;
                // tui.mouse(true);
                self.tui.enter()?;
                self.tui.clear()?;
            } else if self.should_quit {
                self.tui.stop()?;
                break;
            }
        }
        self.tui.exit()?;
        Ok(())
    }

    fn draw(&mut self) -> Result<()> {
        self.tui.draw(|f| {
            let vertical_layout = Layout::vertical(vec![
                Constraint::Max(3),
                Constraint::Fill(1),
                Constraint::Max(1),
            ])
            .horizontal_margin(1)
            .split(f.area());

            let header_chunk = vertical_layout[0];
            let main_chunk = vertical_layout[1];
            let footer_chunk = vertical_layout[2];

            let chunks = Layout::default()
                .direction(Direction::Horizontal)
                .horizontal_margin(1)
                .constraints(
                    [
                        Constraint::Percentage(15),
                        Constraint::Percentage(35),
                        Constraint::Percentage(50),
                    ]
                    .as_ref(),
                )
                .split(main_chunk);

            self.header
                .draw(f, header_chunk, &mut self.state)
                .unwrap_or_else(|err| {
                    self.action_tx
                        .send(Action::Error(format!("Failed to draw header: {:?}", err)))
                        .unwrap();
                });

            for (index, component) in self.components.iter_mut().enumerate() {
                component
                    .draw(f, chunks[index], &mut self.state)
                    .unwrap_or_else(|err| {
                        self.action_tx
                            .send(Action::Error(format!(
                                "Failed to draw component: {:?}",
                                err
                            )))
                            .unwrap();
                    });
            }

            if let Some(popup) = &mut self.popup {
                let popup_vertical_layout = Layout::vertical(vec![
                    Constraint::Min(1),
                    Constraint::Length(10),
                    Constraint::Min(1),
                ])
                .split(f.area());

                let popup_layout = Layout::horizontal(vec![
                    Constraint::Min(3),
                    Constraint::Length(50),
                    Constraint::Min(3),
                ])
                .split(popup_vertical_layout[1]);

                popup
                    .draw(f, popup_layout[1], &mut self.state)
                    .unwrap_or_else(|err| {
                        self.action_tx
                            .send(Action::Error(format!("Failed to draw popup: {:?}", err)))
                            .unwrap();
                    });
            }
            self.footer
                .draw(f, footer_chunk, &mut self.state)
                .unwrap_or_else(|err| {
                    self.action_tx
                        .send(Action::Error(format!("Failed to draw footer: {:?}", err)))
                        .unwrap();
                });
        })?;
        Ok(())
    }
}