use std::io::{self, Write};
pub const REQUEST_CURSOR_POSITION: &[u8] = b"\x1b[6n";
pub const REQUEST_EXTENDED_CURSOR_POSITION: &[u8] = b"\x1b[?6n";
pub const REQUEST_LIGHT_DARK_REPORT: &[u8] = b"\x1b[?996n";
pub const REQUEST_VISIBILITY_REPORT: &[u8] = b"\x1b[?998n";
pub fn write_request_cursor_position<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(REQUEST_CURSOR_POSITION)
}
pub fn write_request_extended_cursor_position<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(REQUEST_EXTENDED_CURSOR_POSITION)
}
pub fn write_request_light_dark_report<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(REQUEST_LIGHT_DARK_REPORT)
}
pub fn write_request_visibility_report<W: Write>(w: &mut W) -> io::Result<()> {
w.write_all(REQUEST_VISIBILITY_REPORT)
}
pub fn write_decrqss<W: Write>(w: &mut W, selector: &str) -> io::Result<()> {
write!(w, "\x1bP$q{selector}\x1b\\")
}
pub fn write_dsr_request<W: Write>(w: &mut W, dec: bool, ps: u16) -> io::Result<()> {
if dec {
write!(w, "\x1b[?{ps}n")
} else {
write!(w, "\x1b[{ps}n")
}
}
pub fn write_cpr<W: Write>(w: &mut W, line: u16, column: u16) -> io::Result<()> {
let l = line.max(1);
let c = column.max(1);
write!(w, "\x1b[{l};{c}R")
}
pub fn write_decxcpr<W: Write>(w: &mut W, line: u16, column: u16, page: u16) -> io::Result<()> {
let l = line.max(1);
let c = column.max(1);
if page == 0 {
write!(w, "\x1b[?{l};{c}R")
} else {
write!(w, "\x1b[?{l};{c};{page}R")
}
}
pub fn write_decrpss<W: Write>(w: &mut W, valid: bool, settings: &str) -> io::Result<()> {
if !valid {
return w.write_all(b"\x1bP0$r\x1b\\");
}
write!(w, "\x1bP1$r{settings}\x1b\\")
}
pub fn write_light_dark_report<W: Write>(w: &mut W, dark: bool) -> io::Result<()> {
if dark {
w.write_all(b"\x1b[?997;1n")
} else {
w.write_all(b"\x1b[?997;2n")
}
}
pub fn write_visibility_report<W: Write>(w: &mut W, visible: bool) -> io::Result<()> {
if visible {
w.write_all(b"\x1b[?999;1n")
} else {
w.write_all(b"\x1b[?999;2n")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cpr() {
let mut buf = Vec::new();
write_cpr(&mut buf, 10, 20).unwrap();
assert_eq!(buf, b"\x1b[10;20R");
}
#[test]
fn test_decxcpr() {
let mut buf = Vec::new();
write_decxcpr(&mut buf, 5, 6, 0).unwrap();
write_decxcpr(&mut buf, 5, 6, 2).unwrap();
assert_eq!(buf, b"\x1b[?5;6R\x1b[?5;6;2R");
}
#[test]
fn test_decrqss() {
let mut buf = Vec::new();
write_decrqss(&mut buf, "m").unwrap();
write_decrqss(&mut buf, " q").unwrap();
write_decrqss(&mut buf, "").unwrap();
assert_eq!(buf, b"\x1bP$qm\x1b\\\x1bP$q q\x1b\\\x1bP$q\x1b\\");
}
#[test]
fn test_decrpss() {
let mut buf = Vec::new();
write_decrpss(&mut buf, true, "0;4;5;7m").unwrap();
write_decrpss(&mut buf, true, "1;24r").unwrap();
write_decrpss(&mut buf, true, ">4;2m").unwrap();
write_decrpss(&mut buf, false, "0;1m").unwrap();
assert_eq!(
buf,
b"\x1bP1$r0;4;5;7m\x1b\\\x1bP1$r1;24r\x1b\\\x1bP1$r>4;2m\x1b\\\x1bP0$r\x1b\\"
);
}
#[test]
fn test_dsr_request() {
let mut buf = Vec::new();
write_dsr_request(&mut buf, false, 5).unwrap();
write_dsr_request(&mut buf, true, 996).unwrap();
assert_eq!(buf, b"\x1b[5n\x1b[?996n");
}
#[test]
fn test_visibility_report() {
let mut buf = Vec::new();
write_request_visibility_report(&mut buf).unwrap();
assert_eq!(buf, b"\x1b[?998n");
let mut generic = Vec::new();
write_dsr_request(&mut generic, true, 998).unwrap();
assert_eq!(buf, generic);
let mut buf = Vec::new();
write_visibility_report(&mut buf, true).unwrap();
write_visibility_report(&mut buf, false).unwrap();
assert_eq!(buf, b"\x1b[?999;1n\x1b[?999;2n");
}
}