cli_prompts/engine/mod.rs
1//! This module helps to abstract away the rendering of the prompts to the terminal.
2//!
3//! It consists of the multiple traits:
4//! - `Engine` trait, that represents the backend which draws content on the
5//! screen and handles the input;
6//! - `CommandBuffer` trait that represents the set of rendering commands to display
7//! the given prompt.
8//! - `Clear` trait that is complemetary to the `CommandBuffer` and allows to clear its contents
9//!
10//! Submodules are meant to implement the above traits using terminal manipulation libraries
11mod crossterm;
12
13pub use self::crossterm::CrosstermEngine;
14
15use crate::{input::Key, style::Formatting};
16use std::io::Result;
17
18/// Represents the backend to draw prompts on the screen and handle input
19pub trait Engine {
20
21 /// Type of the corresponding command buffer
22 type Buffer: CommandBuffer + Clear;
23
24 /// Creates a new instanse of the `CommandBuffer` implementation
25 fn get_command_buffer(&self) -> Self::Buffer;
26
27 /// Renders content to the terminal using the specified rendering commands
28 fn render(&mut self, render_commands: &Self::Buffer) -> Result<()>;
29
30 /// This is called when a prompt is submitted and needs to be rendered in its final state.
31 fn finish_rendering(&mut self) -> Result<()>;
32
33 /// Reads a key that was pressed. This is a blocking call
34 fn read_key(&self) -> Result<Key>;
35}
36
37/// Suplementary trait to the `CommandBuffer`
38pub trait Clear {
39
40 /// Clear the contents of the buffer
41 fn clear(&mut self);
42}
43
44/// Represents the set of rendering commands
45pub trait CommandBuffer {
46
47 /// Move the cursor to the new line
48 fn new_line(&mut self);
49
50 /// Print the text to the screen at the current cursor position
51 fn print(&mut self, text: &str);
52
53 /// Set the given formatting to all text before the next `reset_formatting` call
54 fn set_formatting(&mut self, formatting: &Formatting);
55
56 /// Resets the previously set formatting to default
57 fn reset_formatting(&mut self);
58}