Skip to main content

uncurses/ansi/
xterm.rs

1//! Key-modifier option controls.
2//!
3//! ## Category
4//!
5//! This module emits CSI `m` private controls for modifying or querying keyboard
6//! modifier reporting resources, including the common modifyOtherKeys resource.
7//!
8//! ## CSI format
9//!
10//! Set/reset uses `ESC [ > resource [;value] m`; query uses `ESC [ ? resource m`.
11//! The fixed constants cover resource `4` with values `1`, `2`, reset, and query.
12//!
13//! ## Mode interaction
14//!
15//! These controls manage keyboard-reporting behavior independently from the
16//! progressive keyboard enhancement stack in [`crate::ansi::kitty`].
17
18use std::io::{self, Write};
19
20/// Enable modifyOtherKeys resource `4` at value `1`: exact bytes `ESC [ > 4 ; 1 m` (`b"\x1b[>4;1m"`).
21pub const SET_MODIFY_OTHER_KEYS_1: &[u8] = b"\x1b[>4;1m";
22
23/// Enable modifyOtherKeys resource `4` at value `2`: exact bytes `ESC [ > 4 ; 2 m` (`b"\x1b[>4;2m"`).
24pub const SET_MODIFY_OTHER_KEYS_2: &[u8] = b"\x1b[>4;2m";
25
26/// Reset modifyOtherKeys resource `4`: exact bytes `ESC [ > 4 m` (`b"\x1b[>4m"`).
27pub const RESET_MODIFY_OTHER_KEYS: &[u8] = b"\x1b[>4m";
28
29/// Query modifyOtherKeys resource `4`: exact bytes `ESC [ ? 4 m` (`b"\x1b[?4m"`).
30pub const QUERY_MODIFY_OTHER_KEYS: &[u8] = b"\x1b[?4m";
31
32/// Set or reset a key-modifier resource with `ESC [ > <resource> [;<value>] m`.
33///
34/// When `value` is `Some`, it is emitted as the decimal resource value. When `None`, only the resource number is emitted, requesting a reset to default.
35pub fn write_set_key_modifier_options<W: Write>(
36    w: &mut W,
37    resource: u16,
38    value: Option<u16>,
39) -> io::Result<()> {
40    match value {
41        Some(v) => write!(w, "\x1b[>{resource};{v}m"),
42        None => write!(w, "\x1b[>{resource}m"),
43    }
44}
45
46/// Query a key-modifier resource with `ESC [ ? <resource> m`.
47///
48/// The terminal response, when supported, is parsed elsewhere; this function only emits the query bytes.
49pub fn write_query_key_modifier_options<W: Write>(w: &mut W, resource: u16) -> io::Result<()> {
50    write!(w, "\x1b[?{resource}m")
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_set() {
59        let mut buf = Vec::new();
60        write_set_key_modifier_options(&mut buf, 4, Some(2)).unwrap();
61        assert_eq!(buf, b"\x1b[>4;2m");
62    }
63
64    #[test]
65    fn test_reset() {
66        let mut buf = Vec::new();
67        write_set_key_modifier_options(&mut buf, 4, None).unwrap();
68        assert_eq!(buf, b"\x1b[>4m");
69    }
70}