tinycrossterm 0.1.0

Minimal, feature-gated, WASM-compatible subset of crossterm
Documentation
use std::io::Write;

use crate::Command;

/// Hides the terminal cursor.
#[derive(Debug, Clone, Copy)]
pub struct Hide;

impl Command for Hide {
    fn write_ansi(&self, w: &mut impl Write) -> std::io::Result<()> {
        w.write_all(b"\x1b[?25l")
    }
}

/// Shows the terminal cursor.
#[derive(Debug, Clone, Copy)]
pub struct Show;

impl Command for Show {
    fn write_ansi(&self, w: &mut impl Write) -> std::io::Result<()> {
        w.write_all(b"\x1b[?25h")
    }
}

/// Moves the cursor to the given column and row.
///
/// Top-left is (0, 0). The ANSI sequence uses 1-based coordinates.
#[derive(Debug, Clone, Copy)]
pub struct MoveTo(pub u16, pub u16);

impl Command for MoveTo {
    fn write_ansi(&self, w: &mut impl Write) -> std::io::Result<()> {
        // ANSI CUP is 1-based: ESC [ row ; col H
        write!(w, "\x1b[{};{}H", self.1 + 1, self.0 + 1)
    }
}

/// Sets the cursor style.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SetCursorStyle {
    DefaultUserShape,
    BlinkingBlock,
    SteadyBlock,
    BlinkingUnderScore,
    SteadyUnderScore,
    BlinkingBar,
    SteadyBar,
}

impl Command for SetCursorStyle {
    fn write_ansi(&self, w: &mut impl Write) -> std::io::Result<()> {
        let n = match self {
            SetCursorStyle::DefaultUserShape => 0,
            SetCursorStyle::BlinkingBlock => 1,
            SetCursorStyle::SteadyBlock => 2,
            SetCursorStyle::BlinkingUnderScore => 3,
            SetCursorStyle::SteadyUnderScore => 4,
            SetCursorStyle::BlinkingBar => 5,
            SetCursorStyle::SteadyBar => 6,
        };
        write!(w, "\x1b[{n} q")
    }
}