pub const LINE_TERMINATOR: &str = "\x1b[0m\x1b]8;;\x07";
pub const ERASE_TO_END: &str = "\x1b[K";
#[must_use]
pub fn line_to_ansi(line: &str, _width: u16) -> String {
line.to_string()
}
pub fn write_line<W: std::io::Write>(w: &mut W, line: &str, width: u16) -> std::io::Result<()> {
w.write_all(line_to_ansi(line, width).as_bytes())?;
w.write_all(LINE_TERMINATOR.as_bytes())?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn line_to_ansi_passes_through() {
assert_eq!(line_to_ansi("hello", 80), "hello");
assert_eq!(line_to_ansi("\x1b[31mred\x1b[0m", 80), "\x1b[31mred\x1b[0m");
}
#[test]
fn write_line_appends_terminator() {
let mut buf = Vec::new();
write_line(&mut buf, "test", 80).unwrap();
let s = String::from_utf8(buf).unwrap();
assert!(s.starts_with("test"));
assert!(s.ends_with(LINE_TERMINATOR));
}
}