|
| 1 | +//! Helper functions for sending ANSI sequences |
| 2 | +
|
| 3 | +// ============================================================================ |
| 4 | +// Imports |
| 5 | +// ============================================================================ |
| 6 | + |
| 7 | +use core::fmt::Write; |
| 8 | + |
| 9 | +use crate::File; |
| 10 | + |
| 11 | +// ============================================================================ |
| 12 | +// Types |
| 13 | +// ============================================================================ |
| 14 | + |
| 15 | +/// Represents a position on a screen. |
| 16 | +/// |
| 17 | +/// A position is 0-indexed. That is (0, 0) is the top-left corner. |
| 18 | +/// |
| 19 | +/// Translation to 1-based is performed before sending the ANSI sequences. |
| 20 | +#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 21 | +pub struct Position { |
| 22 | + pub row: u8, |
| 23 | + pub col: u8, |
| 24 | +} |
| 25 | + |
| 26 | +impl Position { |
| 27 | + pub const fn origin() -> Position { |
| 28 | + Position { row: 0, col: 0 } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// Represents a Select Graphic Rendition parameter you can send in an SGR ANSI |
| 33 | +/// sequence. |
| 34 | +#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 35 | +#[repr(u8)] |
| 36 | +pub enum SgrParam { |
| 37 | + Reset = 0, |
| 38 | + Bold = 1, |
| 39 | + Reverse = 7, |
| 40 | + NotBold = 22, |
| 41 | + NotReverse = 27, |
| 42 | + FgBlack = 30, |
| 43 | + FgRed = 31, |
| 44 | + FgGreen = 32, |
| 45 | + FgYellow = 33, |
| 46 | + FgBlue = 34, |
| 47 | + FgMagenta = 35, |
| 48 | + FgCyan = 36, |
| 49 | + FgWhite = 37, |
| 50 | + BgBlack = 40, |
| 51 | + BgRed = 41, |
| 52 | + BgGreen = 42, |
| 53 | + BgYellow = 43, |
| 54 | + BgBlue = 44, |
| 55 | + BgMagenta = 45, |
| 56 | + BgCyan = 46, |
| 57 | + BgWhite = 47, |
| 58 | +} |
| 59 | + |
| 60 | +// ============================================================================ |
| 61 | +// Functions |
| 62 | +// ============================================================================ |
| 63 | + |
| 64 | +/// Erase the screen |
| 65 | +pub fn clear_screen(f: &mut File) { |
| 66 | + let _ = f.write_str("\u{001b}[2J"); |
| 67 | +} |
| 68 | + |
| 69 | +/// Turn the cursor on |
| 70 | +pub fn cursor_on(f: &mut File) { |
| 71 | + let _ = f.write_str("\u{001b}[?25h"); |
| 72 | +} |
| 73 | + |
| 74 | +/// Turn the cursor off |
| 75 | +pub fn cursor_off(f: &mut File) { |
| 76 | + let _ = f.write_str("\u{001b}[?25l"); |
| 77 | +} |
| 78 | + |
| 79 | +/// Move the cursor to the given position |
| 80 | +pub fn move_cursor(f: &mut File, pos: Position) { |
| 81 | + let _ = write!(f, "\u{001b}[{};{}H", 1 + pos.row, 1 + pos.col); |
| 82 | +} |
| 83 | + |
| 84 | +/// Change the background |
| 85 | +/// |
| 86 | +/// Only values 0..8 will work. |
| 87 | +pub fn set_sgr<T>(f: &mut File, values: T) |
| 88 | +where |
| 89 | + T: IntoIterator<Item = SgrParam>, |
| 90 | +{ |
| 91 | + let _ = write!(f, "\u{001b}["); |
| 92 | + let mut iter = values.into_iter(); |
| 93 | + if let Some(value) = iter.next() { |
| 94 | + let _ = write!(f, "{}", value as u8); |
| 95 | + } |
| 96 | + while let Some(value) = iter.next() { |
| 97 | + let _ = write!(f, ";{}", value as u8); |
| 98 | + } |
| 99 | + let _ = write!(f, "m"); |
| 100 | +} |
| 101 | + |
| 102 | +// ============================================================================ |
| 103 | +// End of File |
| 104 | +// ============================================================================ |
0 commit comments