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
// EndBASIC
// Copyright 2020 Julio Merino
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//! Console representation and manipulation.
use async_trait::async_trait;
use endbasic_core::exec::Clearable;
use endbasic_core::syms::Symbols;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::env;
use std::io;
use std::rc::Rc;
use std::str;
mod cmds;
pub(crate) use cmds::add_all;
mod colors;
pub use colors::{ansi_color_to_rgb, AnsiColor, RGB};
mod format;
pub use format::refill_and_print;
mod readline;
pub use readline::{read_line, read_line_secure};
mod trivial;
pub use trivial::TrivialConsole;
mod linebuffer;
pub use linebuffer::LineBuffer;
/// Decoded key presses as returned by the console.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Key {
/// The cursor down key.
ArrowDown,
/// The cursor left key.
ArrowLeft,
/// The cursor right key.
ArrowRight,
/// The cursor up key.
ArrowUp,
/// Deletes the previous character.
Backspace,
/// Accepts the current line.
CarriageReturn,
/// A printable character.
Char(char),
/// The end key or `Ctrl-E`.
End,
/// Indicates a request for termination (e.g. `Ctrl-D`).
Eof,
/// The escape key.
Escape,
/// Indicates a request for interrupt (e.g. `Ctrl-C`).
// TODO(jmmv): This (and maybe Eof too) should probably be represented as a more generic
// Control(char) value so that we can represent other control sequences and allow the logic in
// here to determine what to do with each.
Interrupt,
/// The home key or `Ctrl-A`.
Home,
/// Accepts the current line.
NewLine,
/// The Page Down key.
PageDown,
/// The Page Up key.
PageUp,
/// The Tab key.
Tab,
/// An unknown character or sequence. The text describes what went wrong.
Unknown(String),
}
/// Indicates what part of the console to clear on a `Console::clear()` call.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ClearType {
/// Clears the whole console and moves the cursor to the top left corner.
All,
/// Clears only the current line without moving the cursor.
CurrentLine,
/// Clears the previous character.
PreviousChar,
/// Clears from the cursor position to the end of the line without moving the cursor.
UntilNewLine,
}
/// Represents a coordinate for character-based console operations.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct CharsXY {
/// The column number, starting from zero.
pub x: u16,
/// The row number, starting from zero.
pub y: u16,
}
impl CharsXY {
/// Constructs a new coordinate at the given `(x, y)` position.
pub fn new(x: u16, y: u16) -> Self {
Self { x, y }
}
}
/// Represents a coordinate for pixel-based console operations.
///
/// Coordinates can be off-screen, which means they can be negative and/or can exceed the
/// bottom-right margin.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PixelsXY {
/// The column number.
pub x: i16,
/// The row number.
pub y: i16,
}
impl PixelsXY {
/// Constructs a new coordinate at the given `(x, y)` position.
pub fn new(x: i16, y: i16) -> Self {
Self { x, y }
}
}
/// Represents a rectangular size in pixels.
#[derive(Clone, Copy, Debug)]
pub struct SizeInPixels {
/// The width in pixels.
pub width: u16,
/// The height in pixels.
pub height: u16,
}
/// Hooks to implement the commands that manipulate the console.
#[async_trait(?Send)]
pub trait Console {
/// Clears the part of the console given by `how`.
fn clear(&mut self, how: ClearType) -> io::Result<()>;
/// Gets the console's current foreground and background colors.
fn color(&self) -> (Option<u8>, Option<u8>);
/// Sets the console's foreground and background colors to `fg` and `bg`.
///
/// If any of the colors is `None`, the color is left unchanged.
fn set_color(&mut self, fg: Option<u8>, bg: Option<u8>) -> io::Result<()>;
/// Enters the alternate console.
// TODO(jmmv): This API leads to misuse as callers can forget to leave the alternate console.
fn enter_alt(&mut self) -> io::Result<()>;
/// Hides the cursor.
// TODO(jmmv): This API leads to misuse as callers can forget to show the cursor again.
fn hide_cursor(&mut self) -> io::Result<()>;
/// Returns true if the console is attached to an interactive terminal. This controls whether
/// reading a line echoes back user input, for example.
fn is_interactive(&self) -> bool;
/// Leaves the alternate console.
fn leave_alt(&mut self) -> io::Result<()>;
/// Moves the cursor to the given position, which must be within the screen.
fn locate(&mut self, pos: CharsXY) -> io::Result<()>;
/// Moves the cursor within the line. Positive values move right, negative values move left.
fn move_within_line(&mut self, off: i16) -> io::Result<()>;
/// Writes `text` to the console, followed by a newline or CRLF pair depending on the needs of
/// the console to advance a line.
///
/// The input `text` is not supposed to contain any control characters, such as CR or LF.
// TODO(jmmv): Remove this in favor of write?
fn print(&mut self, text: &str) -> io::Result<()>;
/// Returns the next key press if any is available.
async fn poll_key(&mut self) -> io::Result<Option<Key>>;
/// Waits for and returns the next key press.
async fn read_key(&mut self) -> io::Result<Key>;
/// Shows the cursor.
fn show_cursor(&mut self) -> io::Result<()>;
/// Queries the size of the text console.
///
/// The returned position represents the first row and column that lay *outside* of the console.
fn size_chars(&self) -> io::Result<CharsXY>;
/// Queries the size of the graphical console.
fn size_pixels(&self) -> io::Result<SizeInPixels> {
Err(io::Error::new(io::ErrorKind::Other, "No graphics support in this console"))
}
/// Writes the text into the console at the position of the cursor.
///
fn write(&mut self, text: &str) -> io::Result<()>;
/// Draws the outline of a circle at `_center` with `_radius` using the current drawing color.
fn draw_circle(&mut self, _center: PixelsXY, _radius: u16) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "No graphics support in this console"))
}
/// Draws a filled circle at `_center` with `_radius` using the current drawing color.
fn draw_circle_filled(&mut self, _center: PixelsXY, _radius: u16) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "No graphics support in this console"))
}
/// Draws a line from `_x1y1` to `_x2y2` using the current drawing color.
fn draw_line(&mut self, _x1y1: PixelsXY, _x2y2: PixelsXY) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "No graphics support in this console"))
}
/// Draws a single pixel at `_xy` using the current drawing color.
fn draw_pixel(&mut self, _xy: PixelsXY) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "No graphics support in this console"))
}
/// Draws the outline of a rectangle from `_x1y1` to `_x2y2` using the current drawing color.
fn draw_rect(&mut self, _x1y1: PixelsXY, _x2y2: PixelsXY) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "No graphics support in this console"))
}
/// Draws a filled rectangle from `_x1y1` to `_x2y2` using the current drawing color.
fn draw_rect_filled(&mut self, _x1y1: PixelsXY, _x2y2: PixelsXY) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "No graphics support in this console"))
}
/// Causes any buffered output to be synced.
///
/// This is a no-op when video syncing is enabled because output is never buffered in that case.
fn sync_now(&mut self) -> io::Result<()>;
/// Enables or disables video syncing.
///
/// When enabled, all graphical operations immediately updated the rendering target, which is
/// useful for interactive behavior because we want to see an immediate response. When
/// disabled, all operations are buffered, which is useful for scripts (as otherwise rendering
/// is too slow).
///
/// Flushes any pending updates when enabled.
///
/// Returns the previous status of the video syncing flag.
fn set_sync(&mut self, _enabled: bool) -> io::Result<bool>;
}
/// Resets the state of a console in a best-effort manner.
pub(crate) struct ConsoleClearable {
console: Rc<RefCell<dyn Console>>,
}
impl ConsoleClearable {
/// Creates a new clearable for `console`.
pub(crate) fn new(console: Rc<RefCell<dyn Console>>) -> Box<Self> {
Box::from(Self { console })
}
}
impl Clearable for ConsoleClearable {
fn reset_state(&self, _syms: &mut Symbols) {
let mut console = self.console.borrow_mut();
let _ = console.leave_alt();
let _ = console.set_color(None, None);
let _ = console.show_cursor();
let _ = console.set_sync(true);
}
}
/// Checks if a given string has control characters.
pub fn has_control_chars(s: &str) -> bool {
for ch in s.chars() {
if ch.is_control() {
return true;
}
}
false
}
/// Removes control characters from a string to make it suitable for printing.
pub fn remove_control_chars<S: Into<String>>(s: S) -> String {
let s = s.into();
// Handle the expected common case first. We use this function to strip control characters
// before printing them to the console, and thus we expect such input strings to rarely include
// control characters.
if !has_control_chars(&s) {
return s;
}
let mut o = String::with_capacity(s.len());
for ch in s.chars() {
if ch.is_control() {
o.push(' ');
} else {
o.push(ch);
}
}
o
}
/// Gets the value of the environment variable `name` and interprets it as a `u16`. Returns
/// `None` if the variable is not set or if its contents are invalid.
pub fn get_env_var_as_u16(name: &str) -> Option<u16> {
match env::var_os(name) {
Some(value) => value.as_os_str().to_string_lossy().parse::<u16>().map(Some).unwrap_or(None),
None => None,
}
}
/// Converts a line of text into a collection of keys.
fn line_to_keys(s: String) -> VecDeque<Key> {
let mut keys = VecDeque::default();
for ch in s.chars() {
if ch == '\x1b' {
keys.push_back(Key::Escape);
} else if ch == '\n' {
keys.push_back(Key::NewLine);
} else if ch == '\r' {
// Ignore. When we run under Windows and use golden test input files, we end up
// seeing two separate characters to terminate a newline (CRLF) and these confuse
// our tests. I am not sure why this doesn't seem to be a problem for interactive
// usage though, but it might just be that crossterm hides this from us.
} else if !ch.is_control() {
keys.push_back(Key::Char(ch));
} else {
keys.push_back(Key::Unknown(format!("{}", ch)));
}
}
keys
}
/// Reads a single key from stdin when not attached to a TTY. Because characters are not
/// visible to us until a newline is received, this reads complete lines and buffers them in
/// memory inside the given `buffer`.
pub fn read_key_from_stdin(buffer: &mut VecDeque<Key>) -> io::Result<Key> {
if buffer.is_empty() {
let mut line = String::new();
if io::stdin().read_line(&mut line)? == 0 {
return Ok(Key::Eof);
}
*buffer = line_to_keys(line);
}
match buffer.pop_front() {
Some(key) => Ok(key),
None => Ok(Key::Eof),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_has_control_chars() {
assert!(!has_control_chars(""));
assert!(!has_control_chars("foo bar^baz"));
assert!(has_control_chars("foo\nbar"));
assert!(has_control_chars("foo\rbar"));
assert!(has_control_chars("foo\x08bar"));
}
#[test]
fn test_remove_control_chars() {
assert_eq!("", remove_control_chars(""));
assert_eq!("foo bar", remove_control_chars("foo bar"));
assert_eq!("foo bar baz ", remove_control_chars("foo\r\nbar\rbaz\n"));
}
}