| 1 | //! Device Control String (DCS) escape sequences. |
| 2 | //! |
| 3 | //! Device Control String sequences are framed by [`DCS`] and [`ST`]. Termina currently models the |
| 4 | //! [DECRQSS] request and [DECRPSS] response forms used for terminal state queries. |
| 5 | //! |
| 6 | //! # Examples |
| 7 | //! |
| 8 | //! ``` |
| 9 | //! use termina::escape::dcs::{Dcs, DcsRequest}; |
| 10 | //! |
| 11 | //! let request = Dcs::Request(DcsRequest::GraphicRendition); |
| 12 | //! assert_eq!(request.to_string(), "\x1bP$qm\x1b\\"); |
| 13 | //! ``` |
| 14 | //! |
| 15 | //! [`DCS`]: super::DCS |
| 16 | //! [DECRPSS]: https://vt100.net/docs/vt510-rm/DECRPSS.html |
| 17 | //! [DECRQSS]: https://vt100.net/docs/vt510-rm/DECRQSS.html |
| 18 | //! [`ST`]: super::ST |
| 19 | |
| 20 | use std::fmt::{self, Display}; |
| 21 | |
| 22 | use crate::style::CursorStyle; |
| 23 | |
| 24 | #[cfg(doc)] |
| 25 | use crate::escape::csi::Sgr; |
| 26 | |
| 27 | /// A Device Control String command. |
| 28 | /// |
| 29 | /// Termina uses DCS for terminal state queries that have structured request and response forms. |
| 30 | /// Formatting writes the DCS introducer, the request or response payload, and the string |
| 31 | /// terminator. |
| 32 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 33 | pub enum Dcs { |
| 34 | /// Request a terminal setting with [DECRQSS] using a [`DcsRequest`] selector. |
| 35 | /// |
| 36 | /// For example, [`DcsRequest::GraphicRendition`] asks for the current [`Sgr`] state. |
| 37 | /// |
| 38 | /// [DECRQSS]: https://vt100.net/docs/vt510-rm/DECRQSS.html |
| 39 | Request(DcsRequest), |
| 40 | |
| 41 | /// Report a terminal setting with [DECRPSS] using a [`DcsResponse`] payload. |
| 42 | /// |
| 43 | /// Terminals use this response to answer a DECRQSS request. A valid response contains the |
| 44 | /// setting encoded as the terminal would send it in the corresponding control sequence. |
| 45 | /// |
| 46 | /// [DECRPSS]: https://vt100.net/docs/vt510-rm/DECRPSS.html |
| 47 | Response { |
| 48 | /// Whether the terminal recognized the original request. |
| 49 | is_request_valid: bool, |
| 50 | |
| 51 | /// The setting value returned by the terminal. |
| 52 | value: DcsResponse, |
| 53 | }, |
| 54 | } |
| 55 | |
| 56 | impl Display for Dcs { |
| 57 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 58 | // DCS |
| 59 | f.write_str(super::DCS)?; |
| 60 | match self { |
| 61 | // DCS $ q D...D ST |
| 62 | Self::Request(request) => write!(f, "$q{request}")?, |
| 63 | // DCS Ps $ r D...D ST |
| 64 | Self::Response { |
| 65 | is_request_valid, |
| 66 | value, |
| 67 | } => write!(f, "{}$r{value}", if *is_request_valid { 1 } else { 0 })?, |
| 68 | } |
| 69 | // ST |
| 70 | f.write_str(super::ST) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// Request selectors for [DECRQSS]. |
| 75 | /// |
| 76 | /// Each variant names the setting being queried and shows the selector bytes sent after `DCS $ q`. |
| 77 | /// The selector bytes come from [DECRQSS]. |
| 78 | /// |
| 79 | /// [DECRQSS]: https://vt100.net/docs/vt510-rm/DECRQSS.html |
| 80 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 81 | pub enum DcsRequest { |
| 82 | /// DECRQSS `$}`: request the active status display. |
| 83 | ActiveStatusDisplay, |
| 84 | /// DECRQSS `*x`: request the attribute change extent. |
| 85 | AttributeChangeExtent, |
| 86 | /// DECRQSS `"q`: request the character attribute. |
| 87 | CharacterAttribute, |
| 88 | /// DECRQSS `"p`: request the conformance level. |
| 89 | ConformanceLevel, |
| 90 | /// DECRQSS `$|`: request the number of columns per page. |
| 91 | ColumnsPerPage, |
| 92 | /// DECRQSS `t`: request the number of lines per page. |
| 93 | LinesPerPage, |
| 94 | /// DECRQSS `*|`: request the number of lines per screen. |
| 95 | NumberOfLinesPerScreen, |
| 96 | /// DECRQSS `$~`: request the status line type. |
| 97 | StatusLineType, |
| 98 | /// DECRQSS `s`: request the left and right margins. |
| 99 | LeftAndRightMargins, |
| 100 | /// DECRQSS `r`: request the top and bottom margins. |
| 101 | TopAndBottomMargins, |
| 102 | /// DECRQSS `m`: request [`Sgr`] state. |
| 103 | GraphicRendition, |
| 104 | /// DECRQSS `p`: request the setup language. |
| 105 | SetUpLanguage, |
| 106 | /// DECRQSS `$s`: request the printer type. |
| 107 | PrinterType, |
| 108 | /// DECRQSS `"t`: request the refresh rate. |
| 109 | RefreshRate, |
| 110 | /// DECRQSS `(p`: request the digital printed data type. |
| 111 | DigitalPrintedDataType, |
| 112 | /// DECRQSS `*p`: request the ProPrinter character set. |
| 113 | ProPrinterCharacterSet, |
| 114 | /// DECRQSS `*r`: request the communication speed. |
| 115 | CommunicationSpeed, |
| 116 | /// DECRQSS `*u`: request the communication port. |
| 117 | CommunicationPort, |
| 118 | /// DECRQSS ` p`: request the scroll speed. |
| 119 | ScrollSpeed, |
| 120 | /// DECRQSS ` q`: request the cursor style. |
| 121 | CursorStyle, |
| 122 | /// DECRQSS ` r`: request the key-click volume. |
| 123 | KeyClickVolume, |
| 124 | /// DECRQSS ` t`: request the warning-bell volume. |
| 125 | WarningBellVolume, |
| 126 | /// DECRQSS ` u`: request the margin-bell volume. |
| 127 | MarginBellVolume, |
| 128 | /// DECRQSS ` v`: request the lock-key style. |
| 129 | LockKeyStyle, |
| 130 | /// DECRQSS `*s`: request the flow-control type. |
| 131 | FlowControlType, |
| 132 | /// DECRQSS `$q`: request the disconnect delay time. |
| 133 | DisconnectDelayTime, |
| 134 | /// DECRQSS `"u`: request the transmit-rate limit. |
| 135 | TransmitRateLimit, |
| 136 | /// DECRQSS `+w`: request the port parameter. |
| 137 | PortParameter, |
| 138 | } |
| 139 | |
| 140 | impl Display for DcsRequest { |
| 141 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 142 | match self { |
| 143 | Self::ActiveStatusDisplay => f.write_str("$}"), |
| 144 | Self::AttributeChangeExtent => write!(f, "*x"), |
| 145 | Self::CharacterAttribute => write!(f, "\"q"), |
| 146 | Self::ConformanceLevel => write!(f, "\"p"), |
| 147 | Self::ColumnsPerPage => write!(f, "$|"), |
| 148 | Self::LinesPerPage => write!(f, "t"), |
| 149 | Self::NumberOfLinesPerScreen => write!(f, "*|"), |
| 150 | Self::StatusLineType => write!(f, "$~"), |
| 151 | Self::LeftAndRightMargins => write!(f, "s"), |
| 152 | Self::TopAndBottomMargins => write!(f, "r"), |
| 153 | Self::GraphicRendition => write!(f, "m"), |
| 154 | Self::SetUpLanguage => write!(f, "p"), |
| 155 | Self::PrinterType => write!(f, "$s"), |
| 156 | Self::RefreshRate => write!(f, "\"t"), |
| 157 | Self::DigitalPrintedDataType => write!(f, "(p"), |
| 158 | Self::ProPrinterCharacterSet => write!(f, "*p"), |
| 159 | Self::CommunicationSpeed => write!(f, "*r"), |
| 160 | Self::CommunicationPort => write!(f, "*u"), |
| 161 | // NOTE: space char is intentional - written as SP in |
| 162 | // <https://vt100.net/docs/vt510-rm/DECRPSS.html> |
| 163 | Self::ScrollSpeed => write!(f, " p"), |
| 164 | Self::CursorStyle => write!(f, " q"), |
| 165 | Self::KeyClickVolume => write!(f, " r"), |
| 166 | Self::WarningBellVolume => write!(f, " t"), |
| 167 | Self::MarginBellVolume => write!(f, " u"), |
| 168 | Self::LockKeyStyle => write!(f, " v"), |
| 169 | Self::FlowControlType => write!(f, "*s"), |
| 170 | Self::DisconnectDelayTime => write!(f, "$q"), |
| 171 | Self::TransmitRateLimit => write!(f, "\"u"), |
| 172 | Self::PortParameter => write!(f, "+w"), |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /// Response payloads from [DECRPSS] parsed by Termina. |
| 178 | /// |
| 179 | /// [DECRPSS] is the response form terminals use for [DECRQSS] status-string queries. |
| 180 | /// |
| 181 | /// [DECRPSS]: https://vt100.net/docs/vt510-rm/DECRPSS.html |
| 182 | /// [DECRQSS]: https://vt100.net/docs/vt510-rm/DECRQSS.html |
| 183 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 184 | pub enum DcsResponse { |
| 185 | /// A DECRPSS response containing [`Sgr`] attributes. |
| 186 | /// |
| 187 | /// [`DcsRequest::GraphicRendition`] produces this response. The payload carries the same |
| 188 | /// values that would appear in a [`Sgr`] sequence. |
| 189 | /// |
| 190 | /// [`Sgr`]: crate::escape::csi::Sgr |
| 191 | GraphicRendition(Vec<super::csi::Sgr>), |
| 192 | |
| 193 | /// A DECRPSS response containing the terminal's current cursor style. |
| 194 | /// |
| 195 | /// [`DcsRequest::CursorStyle`] produces this response. The payload corresponds to the |
| 196 | /// [`CursorStyle`] setting. |
| 197 | CursorStyle(CursorStyle), |
| 198 | // There are others but adding them would mean adding a lot of parsing code... |
| 199 | } |
| 200 | |
| 201 | impl Display for DcsResponse { |
| 202 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 203 | match self { |
| 204 | Self::GraphicRendition(sgrs) => { |
| 205 | let mut first = true; |
| 206 | for sgr in sgrs { |
| 207 | if !first { |
| 208 | write!(f, ";")?; |
| 209 | } |
| 210 | first = false; |
| 211 | write!(f, "{sgr}")?; |
| 212 | } |
| 213 | Ok(()) |
| 214 | } |
| 215 | Self::CursorStyle(style) => write!(f, "{style} q"), |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | #[cfg(test)] |
| 221 | mod test { |
| 222 | use super::*; |
| 223 | |
| 224 | #[test] |
| 225 | fn encoding() { |
| 226 | assert_eq!( |
| 227 | Dcs::Request(DcsRequest::GraphicRendition).to_string(), |
| 228 | "\x1bP$qm\x1b\\" |
| 229 | ); |
| 230 | assert_eq!( |
| 231 | Dcs::Request(DcsRequest::CursorStyle).to_string(), |
| 232 | "\x1bP$q q\x1b\\" |
| 233 | ); |
| 234 | } |
| 235 | } |