Skip to main content

intelli_shell/
tui.rs

1use std::{
2    cmp,
3    io::{self, Stdout, stdout},
4    ops::{Deref, DerefMut},
5    thread,
6    time::Duration,
7};
8
9use color_eyre::Result;
10use crossterm::{
11    cursor,
12    event::{
13        self, Event as CrosstermEvent, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers,
14        KeyboardEnhancementFlags, MouseEvent,
15    },
16    style,
17    terminal::{self, ClearType, supports_keyboard_enhancement},
18};
19use futures_util::{FutureExt, StreamExt};
20use ratatui::{CompletedFrame, Frame, Terminal, backend::CrosstermBackend as Backend, layout::Rect};
21use serde::{Deserialize, Serialize};
22use tokio::{
23    sync::mpsc::{self, UnboundedReceiver, UnboundedSender},
24    task::JoinHandle,
25    time::interval,
26};
27use tokio_util::sync::CancellationToken;
28use tracing::instrument;
29
30/// Events that can occur within the TUI application
31#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
32pub enum Event {
33    /// A periodic tick event, useful for time-based updates or animations
34    Tick,
35    /// A periodic render event, suggesting the UI should be redrawn
36    Render,
37    /// The terminal window gained focus
38    FocusGained,
39    /// The terminal window lost focus
40    FocusLost,
41    /// Text was pasted into the terminal (requires paste mode)
42    Paste(String),
43    /// A key was pressed
44    Key(KeyEvent),
45    /// A mouse event occurred (requires mouse capture)
46    Mouse(MouseEvent),
47    /// The terminal window was resized (columns and rows)
48    Resize(u16, u16),
49}
50
51/// Manages the terminal User Interface (TUI) lifecycle, event handling, and rendering
52pub struct Tui {
53    stdout: Stdout,
54    terminal: Terminal<Backend<Stdout>>,
55    task: JoinHandle<()>,
56    loop_cancellation_token: CancellationToken,
57    global_cancellation_token: CancellationToken,
58    event_rx: UnboundedReceiver<Event>,
59    event_tx: UnboundedSender<Event>,
60    frame_rate: f64,
61    tick_rate: f64,
62    mouse: bool,
63    paste: bool,
64    keyboard_enhancement: bool,
65    state: Option<State>,
66}
67
68#[derive(Clone, Copy)]
69enum State {
70    FullScreen(bool),
71    Inline(bool, InlineTuiContext),
72}
73
74#[derive(Clone, Copy)]
75struct InlineTuiContext {
76    min_height: u16,
77    x: u16,
78    y: u16,
79    restore_cursor_x: u16,
80    restore_cursor_y: u16,
81}
82
83#[allow(dead_code, reason = "provide a useful interface, even if not required yet")]
84impl Tui {
85    /// Constructs a new terminal ui with default settings
86    pub fn new(cancellation_token: CancellationToken) -> Result<Self> {
87        let (event_tx, event_rx) = mpsc::unbounded_channel();
88        Ok(Self {
89            stdout: stdout(),
90            terminal: Terminal::new(Backend::new(stdout()))?,
91            task: tokio::spawn(async {}),
92            loop_cancellation_token: CancellationToken::new(),
93            global_cancellation_token: cancellation_token,
94            event_rx,
95            event_tx,
96            frame_rate: 60.0,
97            tick_rate: 10.0,
98            mouse: false,
99            paste: false,
100            keyboard_enhancement: false,
101            state: None,
102        })
103    }
104
105    /// Sets the tick rate for the TUI.
106    ///
107    /// The tick rate determines how frequently `Event::Tick` is generated.
108    pub fn tick_rate(mut self, tick_rate: f64) -> Self {
109        self.state.is_some().then(|| panic!("Can't updated an entered TUI"));
110        self.tick_rate = tick_rate;
111        self
112    }
113
114    /// Sets the frame rate for the TUI.
115    ///
116    /// The frame rate determines how often `Event::Render` is emitted.
117    pub fn frame_rate(mut self, frame_rate: f64) -> Self {
118        self.state.is_some().then(|| panic!("Can't updated an entered TUI"));
119        self.frame_rate = frame_rate;
120        self
121    }
122
123    /// Enables or disables mouse event capture.
124    ///
125    /// If true, `Event::Mouse` events will be emitted.
126    pub fn mouse(mut self, mouse: bool) -> Self {
127        self.state.is_some().then(|| panic!("Can't updated an entered TUI"));
128        self.mouse = mouse;
129        self
130    }
131
132    /// Enables or disables bracketed paste mode.
133    ///
134    /// If true, `Event::Paste` events will be emitted.
135    pub fn paste(mut self, paste: bool) -> Self {
136        self.state.is_some().then(|| panic!("Can't updated an entered TUI"));
137        self.paste = paste;
138        self
139    }
140
141    /// Enables or disables keyboard enhancement flags.
142    pub fn keyboard_enhancement(mut self, keyboard_enhancement: bool) -> Self {
143        self.state.is_some().then(|| panic!("Can't updated an entered TUI"));
144        self.keyboard_enhancement = keyboard_enhancement;
145        self
146    }
147
148    /// Asynchronously retrieves the next event from the event queue.
149    ///
150    /// Returns `None` if the event channel has been closed (e.g., the event loop has stopped).
151    pub async fn next_event(&mut self) -> Option<Event> {
152        self.event_rx.recv().await
153    }
154
155    /// Prepares the terminal for full-screen TUI interaction and starts the event loop
156    pub fn enter(&mut self) -> Result<()> {
157        self.state.is_some().then(|| panic!("Can't re-enter on a TUI"));
158
159        tracing::trace!(mouse = self.mouse, paste = self.paste, "Entering a full-screen TUI");
160
161        // Enter raw mode and set up the terminal
162        let keyboard_enhancement_enabled = self.enter_raw_mode(true)?;
163
164        // Store the state and start the event loop
165        self.state = Some(State::FullScreen(keyboard_enhancement_enabled));
166        self.start();
167
168        Ok(())
169    }
170
171    /// Prepares the terminal for inline TUI interaction and starts the event loop
172    pub fn enter_inline(&mut self, extra_line: bool, min_height: u16) -> Result<()> {
173        self.state.is_some().then(|| panic!("Can't re-enter on a TUI"));
174        let extra_line = extra_line as u16;
175
176        tracing::trace!(
177            mouse = self.mouse,
178            paste = self.paste,
179            extra_line,
180            min_height,
181            "Entering an inline TUI"
182        );
183
184        // Save the original cursor position
185        let (orig_cursor_x, orig_cursor_y) = cursor::position()?;
186        tracing::trace!("Initial cursor position: ({orig_cursor_x},{orig_cursor_y})");
187        // Prepare the area for the inline content
188        crossterm::execute!(
189            self.stdout,
190            // Fill in the minimum height (plus the extra line), the cursor will end up at the end
191            style::Print("\n".repeat((min_height + extra_line) as usize)),
192            // Move the cursor back the min height (without the extra lines)
193            cursor::MoveToPreviousLine(min_height),
194            // And clear the lines below
195            terminal::Clear(ClearType::FromCursorDown)
196        )?;
197        // Retrieve the new cursor position, which defines the starting coords for the area to render in
198        let (cursor_x, cursor_y) = cursor::position()?;
199        // Calculate where the cursor should be restored to
200        let restore_cursor_x = orig_cursor_x;
201        let restore_cursor_y = cmp::min(orig_cursor_y, cmp::max(cursor_y, extra_line) - extra_line);
202        tracing::trace!("Cursor shall be restored at: ({restore_cursor_x},{restore_cursor_y})");
203
204        // Enter raw mode and set up the terminal
205        let keyboard_enhancement_enabled = self.enter_raw_mode(false)?;
206
207        // Store the state and start the event loop
208        self.state = Some(State::Inline(
209            keyboard_enhancement_enabled,
210            InlineTuiContext {
211                min_height,
212                x: cursor_x,
213                y: cursor_y,
214                restore_cursor_x,
215                restore_cursor_y,
216            },
217        ));
218        self.start();
219
220        Ok(())
221    }
222
223    /// Renders the TUI using the provided callback function.
224    ///
225    /// The callback receives a mutable reference to the `Frame` and the area to render in, which might not be the same
226    /// as the frame area for inline TUIs.
227    pub fn render<F>(&mut self, render_callback: F) -> io::Result<CompletedFrame<'_>>
228    where
229        F: FnOnce(&mut Frame, Rect),
230    {
231        let Some(state) = self.state else {
232            return Err(io::Error::other("Cannot render on a non-entered TUI"));
233        };
234
235        self.terminal.draw(|frame| {
236            let area = match state {
237                State::FullScreen(_) => frame.area(),
238                State::Inline(_, inline) => {
239                    let frame = frame.area();
240                    let min_height = cmp::min(frame.height, inline.min_height);
241                    let available_height = frame.height - inline.y;
242                    let height = cmp::max(min_height, available_height);
243                    let width = frame.width - inline.x;
244                    Rect::new(inline.x, inline.y, width, height)
245                }
246            };
247
248            render_callback(frame, area);
249        })
250    }
251
252    /// Restores the terminal to its original state and stops the event loop
253    pub fn exit(mut self) -> Result<()> {
254        self.state.is_none().then(|| panic!("Cannot exit a non-entered TUI"));
255        self.stop();
256        self.restore_terminal()
257    }
258
259    fn restore_terminal(&mut self) -> Result<()> {
260        match self.state.take() {
261            None => (),
262            Some(State::FullScreen(keyboard_enhancement_enabled)) => {
263                tracing::trace!("Leaving the full-screen TUI");
264                self.flush()?;
265                self.exit_raw_mode(true, keyboard_enhancement_enabled)?;
266            }
267            Some(State::Inline(keyboard_enhancement_enabled, ctx)) => {
268                tracing::trace!("Leaving the inline TUI");
269                self.flush()?;
270                self.exit_raw_mode(false, keyboard_enhancement_enabled)?;
271                crossterm::execute!(
272                    self.stdout,
273                    cursor::MoveTo(ctx.restore_cursor_x, ctx.restore_cursor_y),
274                    terminal::Clear(ClearType::FromCursorDown)
275                )?;
276            }
277        }
278
279        Ok(())
280    }
281
282    fn enter_raw_mode(&mut self, alt_screen: bool) -> Result<bool> {
283        terminal::enable_raw_mode()?;
284        crossterm::execute!(self.stdout, cursor::Hide)?;
285        if alt_screen {
286            crossterm::execute!(self.stdout, terminal::EnterAlternateScreen)?;
287        }
288        if self.mouse {
289            crossterm::execute!(self.stdout, event::EnableMouseCapture)?;
290        }
291        if self.paste {
292            crossterm::execute!(self.stdout, event::EnableBracketedPaste)?;
293        }
294
295        let mut ke_enabled = false;
296        if self.keyboard_enhancement {
297            tracing::trace!("Checking keyboard enhancement support");
298            let keyboard_enhancement_supported = supports_keyboard_enhancement()
299                .inspect_err(|err| tracing::error!("{err}"))
300                .unwrap_or(false);
301
302            if keyboard_enhancement_supported {
303                tracing::trace!("Keyboard enhancement flags enabled");
304                crossterm::execute!(
305                    self.stdout,
306                    event::PushKeyboardEnhancementFlags(
307                        KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
308                            | KeyboardEnhancementFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES
309                            | KeyboardEnhancementFlags::REPORT_ALTERNATE_KEYS
310                    ),
311                )?;
312                ke_enabled = true;
313            } else {
314                tracing::debug!("Keyboard enhancement not supported");
315            }
316        } else {
317            tracing::trace!("Keyboard enhancement flags not enabled");
318        }
319
320        Ok(ke_enabled)
321    }
322
323    fn exit_raw_mode(&mut self, alt_screen: bool, keyboard_enhancement_enabled: bool) -> Result<()> {
324        if keyboard_enhancement_enabled {
325            crossterm::execute!(self.stdout, event::PopKeyboardEnhancementFlags)?;
326        }
327
328        if self.paste {
329            crossterm::execute!(self.stdout, event::DisableBracketedPaste)?;
330        }
331        if self.mouse {
332            crossterm::execute!(self.stdout, event::DisableMouseCapture)?;
333        }
334        if alt_screen {
335            crossterm::execute!(self.stdout, terminal::LeaveAlternateScreen)?;
336        }
337        crossterm::execute!(self.stdout, cursor::Show)?;
338        terminal::disable_raw_mode()?;
339
340        Ok(())
341    }
342
343    fn start(&mut self) {
344        self.cancel();
345        self.loop_cancellation_token = CancellationToken::new();
346
347        tracing::trace!(
348            tick_rate = self.tick_rate,
349            frame_rate = self.frame_rate,
350            "Starting the event loop"
351        );
352
353        self.task = tokio::spawn(Self::event_loop(
354            self.event_tx.clone(),
355            self.loop_cancellation_token.clone(),
356            self.global_cancellation_token.clone(),
357            self.tick_rate,
358            self.frame_rate,
359        ));
360    }
361
362    #[instrument(skip_all)]
363    async fn event_loop(
364        event_tx: UnboundedSender<Event>,
365        loop_cancellation_token: CancellationToken,
366        global_cancellation_token: CancellationToken,
367        tick_rate: f64,
368        frame_rate: f64,
369    ) {
370        let mut event_stream = EventStream::new();
371        let mut tick_interval = interval(Duration::from_secs_f64(1.0 / tick_rate));
372        let mut render_interval = interval(Duration::from_secs_f64(1.0 / frame_rate));
373
374        loop {
375            let event = tokio::select! {
376                // Ensure signals are checked in order (cancellation first)
377                biased;
378
379                // Exit the loop if any cancellation is requested
380                _ = loop_cancellation_token.cancelled() => {
381                    break;
382                }
383                _ = global_cancellation_token.cancelled() => {
384                    break;
385                }
386
387                // Crossterm events
388                crossterm_event = event_stream.next().fuse() => match crossterm_event {
389                    Some(Ok(event)) => match event {
390                        // On raw mode, SIGINT is no longer received and we should handle it manually
391                        CrosstermEvent::Key(KeyEvent {
392                            code: KeyCode::Char('c'),
393                            modifiers: KeyModifiers::CONTROL,
394                            ..
395                        }) => {
396                            tracing::debug!("Ctrl+C key event received in TUI, cancelling token");
397                            global_cancellation_token.cancel();
398                            continue;
399                        }
400                        // Process only key press events to avoid duplicate events for release/repeat
401                        CrosstermEvent::Key(key) if key.kind == KeyEventKind::Press => Event::Key(key),
402                        CrosstermEvent::Mouse(mouse) => Event::Mouse(mouse),
403                        CrosstermEvent::Resize(cols, rows) => Event::Resize(cols, rows),
404                        CrosstermEvent::FocusLost => Event::FocusLost,
405                        CrosstermEvent::FocusGained => Event::FocusGained,
406                        CrosstermEvent::Paste(s) => Event::Paste(s),
407                        _ => continue, // Ignore other crossterm event types
408                    }
409                    Some(Err(err)) =>  {
410                        tracing::error!("Error retrieving next crossterm event: {err}");
411                        break;
412                    },
413                    None => break, // Event stream ended, exit the loop
414                },
415
416                // Intervals
417                _ = tick_interval.tick() => Event::Tick,
418                _ = render_interval.tick() => Event::Render,
419            };
420
421            // Try to send the processed event
422            if event_tx.send(event).is_err() {
423                // If sending fails, the receiver is likely dropped. Exit the loop
424                break;
425            }
426        }
427
428        // Ensure the token is cancelled if the loop exits for reasons other than direct cancellation
429        // (e.g. event_stream ending or send error).
430        loop_cancellation_token.cancel();
431    }
432
433    fn stop(&self) {
434        if !self.task.is_finished() {
435            tracing::trace!("Stopping the event loop");
436            self.cancel();
437            let mut counter = 0;
438            while !self.task.is_finished() {
439                thread::sleep(Duration::from_millis(1));
440                counter += 1;
441                // Attempt to abort the task if it hasn't finished in a short period
442                if counter > 50 {
443                    tracing::debug!("Task hasn't finished in 50 milliseconds, attempting to abort");
444                    self.task.abort();
445                }
446                // Log an error and give up waiting if the task hasn't finished after the abort
447                if counter > 100 {
448                    tracing::error!("Failed to abort task in 100 milliseconds for unknown reason");
449                    break;
450                }
451            }
452        }
453    }
454
455    fn cancel(&self) {
456        self.loop_cancellation_token.cancel();
457    }
458}
459
460impl Deref for Tui {
461    type Target = Terminal<Backend<Stdout>>;
462
463    fn deref(&self) -> &Self::Target {
464        &self.terminal
465    }
466}
467
468impl DerefMut for Tui {
469    fn deref_mut(&mut self) -> &mut Self::Target {
470        &mut self.terminal
471    }
472}
473
474impl Drop for Tui {
475    fn drop(&mut self) {
476        self.stop();
477        if let Err(err) = self.restore_terminal() {
478            tracing::error!("Failed to restore terminal state: {err:?}");
479        }
480    }
481}