rustyline/tty/
mod.rs

1//! This module implements and describes common TTY methods & traits
2
3/// Unsupported Terminals that don't support RAW mode
4const UNSUPPORTED_TERM: [&str; 3] = ["dumb", "cons25", "emacs"];
5
6use crate::config::Config;
7use crate::highlight::Highlighter;
8use crate::keys::KeyEvent;
9use crate::layout::{GraphemeClusterMode, Layout, Position, Unit};
10use crate::line_buffer::LineBuffer;
11use crate::{Cmd, Result};
12
13/// Terminal state
14pub trait RawMode: Sized {
15    /// Disable RAW mode for the terminal.
16    fn disable_raw_mode(&self) -> Result<()>;
17}
18
19/// Input event
20pub enum Event {
21    KeyPress(KeyEvent),
22    ExternalPrint(String),
23    #[cfg(target_os = "macos")]
24    Timeout(bool),
25}
26
27/// Translate bytes read from stdin to keys.
28pub trait RawReader {
29    type Buffer;
30    /// Blocking wait for either a key press or an external print
31    fn wait_for_input(&mut self, single_esc_abort: bool) -> Result<Event>; // TODO replace calls to `next_key` by `wait_for_input` where relevant
32    /// Blocking read of key pressed.
33    fn next_key(&mut self, single_esc_abort: bool) -> Result<KeyEvent>;
34    /// For CTRL-V support
35    #[cfg(unix)]
36    fn next_char(&mut self) -> Result<char>;
37    /// Bracketed paste
38    fn read_pasted_text(&mut self) -> Result<String>;
39    /// Check if `key` is bound to a peculiar command
40    fn find_binding(&self, key: &KeyEvent) -> Option<Cmd>;
41    /// Backup type ahead
42    fn unbuffer(self) -> Option<Buffer>;
43}
44
45/// Display prompt, line and cursor in terminal output
46pub trait Renderer {
47    type Reader: RawReader;
48
49    fn move_cursor(&mut self, old: Position, new: Position) -> Result<()>;
50
51    /// Display `prompt`, line and cursor in terminal output
52    fn refresh_line(
53        &mut self,
54        prompt: &str,
55        line: &LineBuffer,
56        hint: Option<&str>,
57        old_layout: &Layout,
58        new_layout: &Layout,
59        highlighter: Option<&dyn Highlighter>,
60    ) -> Result<()>;
61
62    /// Compute layout for rendering prompt + line + some info (either hint,
63    /// validation msg, ...). on the screen. Depending on screen width, line
64    /// wrapping may be applied.
65    fn compute_layout(
66        &self,
67        prompt_size: Position,
68        default_prompt: bool,
69        line: &LineBuffer,
70        info: Option<&str>,
71    ) -> Layout {
72        // calculate the desired position of the cursor
73        let pos = line.pos();
74        let cursor = self.calculate_position(&line[..pos], prompt_size);
75        // calculate the position of the end of the input line
76        let mut end = if pos == line.len() {
77            cursor
78        } else {
79            self.calculate_position(&line[pos..], cursor)
80        };
81        if let Some(info) = info {
82            end = self.calculate_position(info, end);
83        }
84
85        let new_layout = Layout {
86            grapheme_cluster_mode: self.grapheme_cluster_mode(),
87            prompt_size,
88            default_prompt,
89            cursor,
90            end,
91        };
92        debug_assert!(new_layout.prompt_size <= new_layout.cursor);
93        debug_assert!(new_layout.cursor <= new_layout.end);
94        new_layout
95    }
96
97    /// Calculate the number of columns and rows used to display `s` on a
98    /// `cols` width terminal starting at `orig`.
99    fn calculate_position(&self, s: &str, orig: Position) -> Position;
100
101    fn write_and_flush(&mut self, buf: &str) -> Result<()>;
102
103    /// Beep, used for completion when there is nothing to complete or when all
104    /// the choices were already shown.
105    fn beep(&mut self) -> Result<()>;
106
107    /// Clear the screen. Used to handle ctrl+l
108    fn clear_screen(&mut self) -> Result<()>;
109    /// Clear rows used by prompt and edited line
110    fn clear_rows(&mut self, layout: &Layout) -> Result<()>;
111    /// Clear from cursor to the end of line
112    fn clear_to_eol(&mut self) -> Result<()>;
113
114    /// Update the number of columns/rows in the current terminal.
115    fn update_size(&mut self);
116    /// Get the number of columns in the current terminal.
117    fn get_columns(&self) -> Unit;
118    /// Get the number of rows in the current terminal.
119    fn get_rows(&self) -> Unit;
120    /// Check if output supports colors.
121    fn colors_enabled(&self) -> bool;
122    /// Tell how grapheme clusters are rendered.
123    fn grapheme_cluster_mode(&self) -> GraphemeClusterMode;
124
125    /// Make sure prompt is at the leftmost edge of the screen
126    fn move_cursor_at_leftmost(&mut self, rdr: &mut Self::Reader) -> Result<()>;
127    /// Begin synchronized update on unix platform
128    fn begin_synchronized_update(&mut self) -> Result<()> {
129        Ok(())
130    }
131    /// End synchronized update on unix platform
132    fn end_synchronized_update(&mut self) -> Result<()> {
133        Ok(())
134    }
135}
136
137// ignore ANSI escape sequence
138fn width(gcm: GraphemeClusterMode, s: &str, esc_seq: &mut u8) -> Unit {
139    if *esc_seq == 1 {
140        if s == "[" {
141            // CSI
142            *esc_seq = 2;
143        } else {
144            // two-character sequence
145            *esc_seq = 0;
146        }
147        0
148    } else if *esc_seq == 2 {
149        if s == ";" || (s.as_bytes()[0] >= b'0' && s.as_bytes()[0] <= b'9') {
150            /*} else if s == "m" {
151            // last
152             *esc_seq = 0;*/
153        } else {
154            // not supported
155            *esc_seq = 0;
156        }
157        0
158    } else if s == "\x1b" {
159        *esc_seq = 1;
160        0
161    } else if s == "\n" {
162        0
163    } else {
164        gcm.width(s)
165    }
166}
167
168/// External printer
169pub trait ExternalPrinter {
170    /// Print message to stdout
171    fn print(&mut self, msg: String) -> Result<()>;
172}
173
174/// Terminal contract
175pub trait Term {
176    type Buffer;
177    type KeyMap;
178    type Reader: RawReader<Buffer = Self::Buffer>; // rl_instream
179    type Writer: Renderer<Reader = Self::Reader>; // rl_outstream
180    type Mode: RawMode;
181    type ExternalPrinter: ExternalPrinter;
182    type CursorGuard;
183
184    fn new(config: &Config) -> Result<Self>
185    where
186        Self: Sized;
187    /// Check if current terminal can provide a rich line-editing user
188    /// interface.
189    fn is_unsupported(&self) -> bool;
190    /// check if input stream is connected to a terminal.
191    fn is_input_tty(&self) -> bool;
192    /// check if output stream is connected to a terminal.
193    fn is_output_tty(&self) -> bool;
194    /// Enable RAW mode for the terminal.
195    fn enable_raw_mode(&mut self, config: &Config) -> Result<(Self::Mode, Self::KeyMap)>;
196    /// Create a RAW reader
197    fn create_reader(
198        &self,
199        buffer: Option<Self::Buffer>,
200        config: &Config,
201        key_map: Self::KeyMap,
202    ) -> Self::Reader;
203    /// Create a writer
204    fn create_writer(&self, config: &Config) -> Self::Writer;
205    fn writeln(&self) -> Result<()>;
206    /// Create an external printer
207    fn create_external_printer(&mut self) -> Result<Self::ExternalPrinter>;
208    /// Change cursor visibility
209    fn set_cursor_visibility(&mut self, visible: bool) -> Result<Option<Self::CursorGuard>>;
210}
211
212/// Check TERM environment variable to see if current term is in our
213/// unsupported list
214fn is_unsupported_term() -> bool {
215    match std::env::var("TERM") {
216        Ok(term) => {
217            for iter in &UNSUPPORTED_TERM {
218                if (*iter).eq_ignore_ascii_case(&term) {
219                    return true;
220                }
221            }
222            false
223        }
224        Err(_) => false,
225    }
226}
227
228// If on Windows platform import Windows TTY module
229// and re-export into mod.rs scope
230#[cfg(all(windows, not(target_arch = "wasm32")))]
231mod windows;
232#[cfg(all(windows, not(target_arch = "wasm32"), not(test)))]
233pub use self::windows::*;
234
235// If on Unix platform import Unix TTY module
236// and re-export into mod.rs scope
237#[cfg(all(unix, not(target_arch = "wasm32")))]
238mod unix;
239#[cfg(all(unix, not(target_arch = "wasm32"), not(test)))]
240pub use self::unix::*;
241
242#[cfg(any(test, target_arch = "wasm32"))]
243mod test;
244#[cfg(any(test, target_arch = "wasm32"))]
245pub use self::test::*;
246
247#[cfg(test)]
248mod test_ {
249    #[test]
250    fn test_unsupported_term() {
251        std::env::set_var("TERM", "xterm");
252        assert!(!super::is_unsupported_term());
253
254        std::env::set_var("TERM", "dumb");
255        assert!(super::is_unsupported_term());
256    }
257}