use std::io::{self, Write};
pub fn write_cup<W: Write>(w: &mut W, row: u16, col: u16) -> io::Result<()> {
if row == 0 && col == 0 {
w.write_all(b"\x1b[H")
} else if col == 0 {
write!(w, "\x1b[{}H", row + 1)
} else {
write!(w, "\x1b[{};{}H", row + 1, col + 1)
}
}
pub fn write_cuu<W: Write>(w: &mut W, n: u16) -> io::Result<()> {
match n {
0 => Ok(()),
1 => w.write_all(b"\x1b[A"),
_ => write!(w, "\x1b[{n}A"),
}
}
pub fn write_cud<W: Write>(w: &mut W, n: u16) -> io::Result<()> {
match n {
0 => Ok(()),
1 => w.write_all(b"\x1b[B"),
_ => write!(w, "\x1b[{n}B"),
}
}
pub fn write_cuf<W: Write>(w: &mut W, n: u16) -> io::Result<()> {
match n {
0 => Ok(()),
1 => w.write_all(b"\x1b[C"),
_ => write!(w, "\x1b[{n}C"),
}
}
pub fn write_cub<W: Write>(w: &mut W, n: u16) -> io::Result<()> {
match n {
0 => Ok(()),
1 => w.write_all(b"\x1b[D"),
_ => write!(w, "\x1b[{n}D"),
}
}
pub fn write_cha<W: Write>(w: &mut W, col: u16) -> io::Result<()> {
if col == 0 {
w.write_all(b"\x1b[G")
} else {
write!(w, "\x1b[{}G", col + 1)
}
}
pub fn write_vpa<W: Write>(w: &mut W, row: u16) -> io::Result<()> {
if row == 0 {
w.write_all(b"\x1b[d")
} else {
write!(w, "\x1b[{}d", row + 1)
}
}
pub fn write_cnl<W: Write>(w: &mut W, n: u16) -> io::Result<()> {
match n {
0 => Ok(()),
1 => w.write_all(b"\x1b[E"),
_ => write!(w, "\x1b[{n}E"),
}
}
pub fn write_cpl<W: Write>(w: &mut W, n: u16) -> io::Result<()> {
match n {
0 => Ok(()),
1 => w.write_all(b"\x1b[F"),
_ => write!(w, "\x1b[{n}F"),
}
}
pub fn write_save_cursor<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(b"\x1b7")
}
pub fn write_restore_cursor<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(b"\x1b8")
}
pub fn write_reverse_index<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(b"\x1bM")
}
pub fn write_index<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(b"\x1bD")
}
pub fn write_hvp<W: Write>(w: &mut W, row: u16, col: u16) -> io::Result<()> {
if row == 0 && col == 0 {
w.write_all(b"\x1b[f")
} else if col == 0 {
write!(w, "\x1b[{}f", row + 1)
} else {
write!(w, "\x1b[{};{}f", row + 1, col + 1)
}
}
pub fn write_hpa<W: Write>(w: &mut W, col: u16) -> io::Result<()> {
if col == 0 {
w.write_all(b"\x1b[`")
} else {
write!(w, "\x1b[{}`", col + 1)
}
}
pub fn write_hpr<W: Write>(w: &mut W, n: u16) -> io::Result<()> {
match n {
0 => Ok(()),
1 => w.write_all(b"\x1b[a"),
_ => write!(w, "\x1b[{n}a"),
}
}
pub fn write_vpr<W: Write>(w: &mut W, n: u16) -> io::Result<()> {
match n {
0 => Ok(()),
1 => w.write_all(b"\x1b[e"),
_ => write!(w, "\x1b[{n}e"),
}
}
pub fn write_cht<W: Write>(w: &mut W, n: u16) -> io::Result<()> {
match n {
0 => Ok(()),
1 => w.write_all(b"\x1b[I"),
_ => write!(w, "\x1b[{n}I"),
}
}
pub fn write_save_cursor_position<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(b"\x1b[s")
}
pub fn write_restore_cursor_position<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(b"\x1b[u")
}
pub fn write_set_pointer_shape<W: Write>(w: &mut W, shape: &str) -> io::Result<()> {
write!(w, "\x1b]22;{shape}\x1b\\")
}
pub fn write_request_extended_cursor_position<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(b"\x1b[?6n")
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum CursorShape {
#[default]
Block,
Underline,
Bar,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum CursorStyle {
#[default]
Default,
BlinkingBlock,
SteadyBlock,
BlinkingUnderline,
SteadyUnderline,
BlinkingBar,
SteadyBar,
}
impl CursorStyle {
fn param(self) -> u8 {
match self {
CursorStyle::Default => 0,
CursorStyle::BlinkingBlock => 1,
CursorStyle::SteadyBlock => 2,
CursorStyle::BlinkingUnderline => 3,
CursorStyle::SteadyUnderline => 4,
CursorStyle::BlinkingBar => 5,
CursorStyle::SteadyBar => 6,
}
}
pub fn from_param(n: u32) -> Option<Self> {
Some(match n {
0 => CursorStyle::Default,
1 => CursorStyle::BlinkingBlock,
2 => CursorStyle::SteadyBlock,
3 => CursorStyle::BlinkingUnderline,
4 => CursorStyle::SteadyUnderline,
5 => CursorStyle::BlinkingBar,
6 => CursorStyle::SteadyBar,
_ => return None,
})
}
pub fn new(shape: CursorShape, blinking: bool) -> Self {
match (shape, blinking) {
(CursorShape::Block, true) => CursorStyle::BlinkingBlock,
(CursorShape::Block, false) => CursorStyle::SteadyBlock,
(CursorShape::Underline, true) => CursorStyle::BlinkingUnderline,
(CursorShape::Underline, false) => CursorStyle::SteadyUnderline,
(CursorShape::Bar, true) => CursorStyle::BlinkingBar,
(CursorShape::Bar, false) => CursorStyle::SteadyBar,
}
}
pub fn shape(self) -> Option<CursorShape> {
Some(match self {
CursorStyle::Default => return None,
CursorStyle::BlinkingBlock | CursorStyle::SteadyBlock => CursorShape::Block,
CursorStyle::BlinkingUnderline | CursorStyle::SteadyUnderline => CursorShape::Underline,
CursorStyle::BlinkingBar | CursorStyle::SteadyBar => CursorShape::Bar,
})
}
pub fn blinking(self) -> Option<bool> {
Some(match self {
CursorStyle::Default => return None,
CursorStyle::BlinkingBlock
| CursorStyle::BlinkingUnderline
| CursorStyle::BlinkingBar => true,
CursorStyle::SteadyBlock | CursorStyle::SteadyUnderline | CursorStyle::SteadyBar => {
false
}
})
}
}
pub fn write_cursor_style<W: Write>(w: &mut W, style: CursorStyle) -> io::Result<()> {
write!(w, "\x1b[{} q", style.param())
}
pub fn write_request_cursor_position<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(b"\x1b[6n")
}
pub fn write_tab<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(b"\t")
}
pub fn write_backtab<W: Write>(w: &mut W, n: u16) -> io::Result<()> {
match n {
0 => Ok(()),
1 => w.write_all(b"\x1b[Z"),
_ => write!(w, "\x1b[{n}Z"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cursor_style_round_trips_its_parameter() {
for style in [
CursorStyle::Default,
CursorStyle::BlinkingBlock,
CursorStyle::SteadyBlock,
CursorStyle::BlinkingUnderline,
CursorStyle::SteadyUnderline,
CursorStyle::BlinkingBar,
CursorStyle::SteadyBar,
] {
assert_eq!(CursorStyle::from_param(style.param() as u32), Some(style));
}
assert_eq!(CursorStyle::from_param(7), None);
assert_eq!(CursorStyle::from_param(u32::MAX), None);
}
#[test]
fn shape_and_blinking_round_trip_through_a_style() {
for shape in [CursorShape::Block, CursorShape::Underline, CursorShape::Bar] {
for blinking in [true, false] {
let style = CursorStyle::new(shape, blinking);
assert_eq!(style.shape(), Some(shape));
assert_eq!(style.blinking(), Some(blinking));
}
}
}
#[test]
fn the_terminal_default_names_neither_shape_nor_blink() {
assert_eq!(CursorStyle::Default.shape(), None);
assert_eq!(CursorStyle::Default.blinking(), None);
}
#[test]
fn test_cup_origin() {
let mut buf = Vec::new();
write_cup(&mut buf, 0, 0).unwrap();
assert_eq!(buf, b"\x1b[H");
}
#[test]
fn test_cup_row_only() {
let mut buf = Vec::new();
write_cup(&mut buf, 5, 0).unwrap();
assert_eq!(buf, b"\x1b[6H");
}
#[test]
fn test_cup_both() {
let mut buf = Vec::new();
write_cup(&mut buf, 10, 20).unwrap();
assert_eq!(buf, b"\x1b[11;21H");
}
#[test]
fn test_cuu_single() {
let mut buf = Vec::new();
write_cuu(&mut buf, 1).unwrap();
assert_eq!(buf, b"\x1b[A");
}
#[test]
fn test_cuu_multi() {
let mut buf = Vec::new();
write_cuu(&mut buf, 5).unwrap();
assert_eq!(buf, b"\x1b[5A");
}
#[test]
fn test_cup_cost() {
use crate::ansi::cost::cup_cost;
assert_eq!(cup_cost(9, 0), 5); assert_eq!(cup_cost(9, 9), 8); }
#[test]
fn cursor_style_decscusr_encoding() {
for (style, param) in [
(CursorStyle::BlinkingBlock, 1),
(CursorStyle::SteadyBlock, 2),
(CursorStyle::BlinkingUnderline, 3),
(CursorStyle::SteadyUnderline, 4),
(CursorStyle::BlinkingBar, 5),
(CursorStyle::SteadyBar, 6),
] {
let mut buf: Vec<u8> = Vec::new();
write_cursor_style(&mut buf, style).unwrap();
let want = format!("\x1b[{param} q");
assert_eq!(String::from_utf8(buf).unwrap(), want, "style {style:?}");
}
}
}