use crate::Sink;
use katui_proto::{clipboard, kitty};
use std::io;
pub fn enter<W: Sink>(out: &mut W) -> io::Result<()> {
out.write_all(b"\x1b[?1049h\x1b[?25l")
}
pub fn leave<W: Sink>(out: &mut W) -> io::Result<()> {
out.write_all(b"\x1b[?25h\x1b[?1049l")
}
pub fn enable_input<W: Sink>(out: &mut W) -> io::Result<()> {
out.write_all(b"\x1b[?1003h\x1b[?1006h\x1b[?1016h\x1b[?1004h\x1b[?2004h\x1b[>1u\x1b[?1016$p")
}
pub fn disable_input<W: Sink>(out: &mut W) -> io::Result<()> {
out.write_all(b"\x1b[=0;1u\x1b[<u\x1b[?2004l\x1b[?1004l\x1b[?1016l\x1b[?1006l\x1b[?1003l")
}
pub fn report_key_releases<W: Sink>(out: &mut W) -> io::Result<()> {
out.write_all(b"\x1b[=11;1u")
}
pub fn terminal_query<W: Sink>(out: &mut W, id: u32) -> io::Result<()> {
out.write_all(&kitty::barrier(kitty::ImageId(id)))
}
pub fn copy_to_clipboard<W: Sink>(out: &mut W, text: &str, multiplexed: bool) -> io::Result<()> {
out.write_all(&clipboard::copy(text, multiplexed))
}
#[cfg(test)]
mod tests {
use super::{disable_input, enable_input, enter, leave, report_key_releases};
#[test]
fn disabling_input_undoes_the_key_release_flags() {
let (mut flags, mut off) = (Vec::new(), Vec::new());
report_key_releases(&mut flags).expect("writing to a Vec cannot fail");
disable_input(&mut off).expect("writing to a Vec cannot fail");
assert_eq!(flags, b"\x1b[=11;1u");
assert!(
off.starts_with(b"\x1b[=0;1u\x1b[<u"),
"clearing the flags before popping covers terminals that keep them"
);
}
#[test]
fn disabling_input_turns_off_every_mode_enabling_set() {
let (mut on, mut off) = (Vec::new(), Vec::new());
enable_input(&mut on).expect("writing to a Vec cannot fail");
disable_input(&mut off).expect("writing to a Vec cannot fail");
let (on, off) = (
String::from_utf8_lossy(&on).into_owned(),
String::from_utf8_lossy(&off).into_owned(),
);
for mode in ["1003", "1006", "1016", "1004", "2004"] {
assert!(on.contains(&format!("?{mode}h")), "{mode} goes on");
assert!(off.contains(&format!("?{mode}l")), "{mode} goes off");
}
}
#[test]
fn enter_and_leave_write_their_exact_sequences() {
let mut out = Vec::new();
enter(&mut out).expect("writing to a Vec cannot fail");
leave(&mut out).expect("writing to a Vec cannot fail");
assert_eq!(
String::from_utf8_lossy(&out),
"\x1b[?1049h\x1b[?25l\x1b[?25h\x1b[?1049l"
);
}
}