• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zhiburt / expectrl / 4342262992

pending completion
4342262992

push

github

GitHub
Patch interact to provide waitpid result (#57)

189 of 189 new or added lines in 8 files covered. (100.0%)

1478 of 2567 relevant lines covered (57.58%)

3.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

3.42
/src/control_code.rs
1
//! A module which contains [ControlCode] type.
2

3
use std::convert::TryFrom;
4

5
/// ControlCode represents the standard ASCII control codes [wiki]
6
///
7
/// [wiki]: https://en.wikipedia.org/wiki/C0_and_C1_control_codes
8
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9
pub enum ControlCode {
10
    /// Often used as a string terminator, especially in the programming language C.
11
    Null,
12
    /// In message transmission, delimits the start of a message header.
13
    StartOfHeading,
14
    /// First character of message text, and may be used to terminate the message heading.
15
    StartOfText,
16
    /// Often used as a "break" character (Ctrl-C) to interrupt or terminate a program or process.
17
    EndOfText,
18
    /// Often used on Unix to indicate end-of-file on a terminal (Ctrl-D).
19
    EndOfTransmission,
20
    /// Signal intended to trigger a response at the receiving end, to see if it is still present.
21
    Enquiry,
22
    /// Response to an Enquiry, or an indication of successful receipt of a message.
23
    Acknowledge,
24
    /// Used for a beep on systems that didn't have a physical bell.
25
    Bell,
26
    /// Move the cursor one position leftwards.
27
    /// On input, this may delete the character to the left of the cursor.
28
    Backspace,
29
    /// Position to the next character tab stop.
30
    HorizontalTabulation,
31
    /// On Unix, used to mark end-of-line.
32
    /// In DOS, Windows, and various network standards, LF is used following CR as part of the end-of-line mark.
33
    LineFeed,
34
    /// Position the form at the next line tab stop.
35
    VerticalTabulation,
36
    /// It appears in some common plain text files as a page break character.
37
    FormFeed,
38
    /// Originally used to move the cursor to column zero while staying on the same line.
39
    CarriageReturn,
40
    /// Switch to an alternative character set.
41
    ShiftOut,
42
    /// Return to regular character set after ShiftOut.
43
    ShiftIn,
44
    /// May cause a limited number of contiguously following octets to be interpreted in some different way.
45
    DataLinkEscape,
46
    /// A control code which is reserved for device control.
47
    DeviceControl1,
48
    /// A control code which is reserved for device control.
49
    DeviceControl2,
50
    /// A control code which is reserved for device control.
51
    DeviceControl3,
52
    /// A control code which is reserved for device control.
53
    DeviceControl4,
54
    /// In multipoint systems, the NAK is used as the not-ready reply to a poll.
55
    NegativeAcknowledge,
56
    /// Used in synchronous transmission systems to provide a signal from which synchronous correction may be achieved.
57
    SynchronousIdle,
58
    /// Indicates the end of a transmission block of data.
59
    EndOfTransmissionBlock,
60
    /// Indicates that the data preceding it are in error or are to be disregarded.
61
    Cancel,
62
    /// May mark the end of the used portion of the physical medium.
63
    EndOfMedium,
64
    /// Sometimes used to indicate the end of file, both when typing on the terminal and in text files stored on disk.
65
    Substitute,
66
    /// The Esc key on the keyboard will cause this character to be sent on most systems.
67
    /// In systems based on ISO/IEC 2022, even if another set of C0 control codes are used,
68
    /// this octet is required to always represent the escape character.
69
    Escape,
70
    /// Can be used as delimiters to mark fields of data structures.
71
    /// Also it used for hierarchical levels;
72
    /// FS == level 4
73
    FileSeparator,
74
    /// It used for hierarchical levels;
75
    /// GS == level 3
76
    GroupSeparator,
77
    /// It used for hierarchical levels;
78
    /// RS == level 2
79
    RecordSeparator,
80
    /// It used for hierarchical levels;
81
    /// US == level 1
82
    UnitSeparator,
83
    /// Space is a graphic character. It causes the active position to be advanced by one character position.
84
    Space,
85
    /// Usually called backspace on modern machines, and does not correspond to the PC delete key.
86
    Delete,
87
}
88

89
impl ControlCode {
90
    /// See [ControlCode::Null]
91
    pub const NUL: ControlCode = ControlCode::Null;
92
    /// See [ControlCode::StartOfHeading]
93
    pub const SOH: ControlCode = ControlCode::StartOfHeading;
94
    /// See [ControlCode::StartOfText]
95
    pub const STX: ControlCode = ControlCode::StartOfText;
96
    /// See [ControlCode::EndOfText]
97
    pub const ETX: ControlCode = ControlCode::EndOfText;
98
    /// See [ControlCode::EndOfTransmission]
99
    pub const EOT: ControlCode = ControlCode::EndOfTransmission;
100
    /// See [ControlCode::Enquiry]
101
    pub const ENQ: ControlCode = ControlCode::Enquiry;
102
    /// See [ControlCode::Acknowledge]
103
    pub const ACK: ControlCode = ControlCode::Acknowledge;
104
    /// See [ControlCode::Bell]
105
    pub const BEL: ControlCode = ControlCode::Bell;
106
    /// See [ControlCode::Backspace]
107
    pub const BS: ControlCode = ControlCode::Backspace;
108
    /// See [ControlCode::HorizontalTabulation]
109
    pub const HT: ControlCode = ControlCode::HorizontalTabulation;
110
    /// See [ControlCode::LineFeed]
111
    pub const LF: ControlCode = ControlCode::LineFeed;
112
    /// See [ControlCode::VerticalTabulation]
113
    pub const VT: ControlCode = ControlCode::VerticalTabulation;
114
    /// See [ControlCode::FormFeed]
115
    pub const FF: ControlCode = ControlCode::FormFeed;
116
    /// See [ControlCode::CarriageReturn]
117
    pub const CR: ControlCode = ControlCode::CarriageReturn;
118
    /// See [ControlCode::ShiftOut]
119
    pub const SO: ControlCode = ControlCode::ShiftOut;
120
    /// See [ControlCode::ShiftIn]
121
    pub const SI: ControlCode = ControlCode::ShiftIn;
122
    /// See [ControlCode::DataLinkEscape]
123
    pub const DLE: ControlCode = ControlCode::DataLinkEscape;
124
    /// See [ControlCode::DeviceControl1]
125
    pub const DC1: ControlCode = ControlCode::DeviceControl1;
126
    /// See [ControlCode::DeviceControl2]
127
    pub const DC2: ControlCode = ControlCode::DeviceControl2;
128
    /// See [ControlCode::DeviceControl3]
129
    pub const DC3: ControlCode = ControlCode::DeviceControl3;
130
    /// See [ControlCode::DeviceControl4]
131
    pub const DC4: ControlCode = ControlCode::DeviceControl4;
132
    /// See [ControlCode::NegativeAcknowledge]
133
    pub const NAK: ControlCode = ControlCode::NegativeAcknowledge;
134
    /// See [ControlCode::SynchronousIdle]
135
    pub const SYN: ControlCode = ControlCode::SynchronousIdle;
136
    /// See [ControlCode::EndOfTransmissionBlock]
137
    pub const ETB: ControlCode = ControlCode::EndOfTransmissionBlock;
138
    /// See [ControlCode::Cancel]
139
    pub const CAN: ControlCode = ControlCode::Cancel;
140
    /// See [ControlCode::EndOfMedium]
141
    pub const EM: ControlCode = ControlCode::EndOfMedium;
142
    /// See [ControlCode::Substitute]
143
    pub const SUB: ControlCode = ControlCode::Substitute;
144
    /// See [ControlCode::Escape]
145
    pub const ESC: ControlCode = ControlCode::Escape;
146
    /// See [ControlCode::FileSeparator]
147
    pub const FS: ControlCode = ControlCode::FileSeparator;
148
    /// See [ControlCode::GroupSeparator]
149
    pub const GS: ControlCode = ControlCode::GroupSeparator;
150
    /// See [ControlCode::RecordSeparator]
151
    pub const RS: ControlCode = ControlCode::RecordSeparator;
152
    /// See [ControlCode::UnitSeparator]
153
    pub const US: ControlCode = ControlCode::UnitSeparator;
154
    /// See [ControlCode::Space]
155
    pub const SP: ControlCode = ControlCode::Space;
156
    /// See [ControlCode::Delete]
157
    pub const DEL: ControlCode = ControlCode::Delete;
158
}
159

160
impl From<ControlCode> for u8 {
161
    fn from(val: ControlCode) -> Self {
2✔
162
        match val {
2✔
163
            ControlCode::Null => 0,
×
164
            ControlCode::StartOfHeading => 1,
×
165
            ControlCode::StartOfText => 2,
×
166
            ControlCode::EndOfText => 3,
1✔
167
            ControlCode::EndOfTransmission => 4,
2✔
168
            ControlCode::Enquiry => 5,
×
169
            ControlCode::Acknowledge => 6,
×
170
            ControlCode::Bell => 7,
×
171
            ControlCode::Backspace => 8,
×
172
            ControlCode::HorizontalTabulation => 9,
×
173
            ControlCode::LineFeed => 10,
×
174
            ControlCode::VerticalTabulation => 11,
×
175
            ControlCode::FormFeed => 12,
×
176
            ControlCode::CarriageReturn => 13,
×
177
            ControlCode::ShiftOut => 14,
×
178
            ControlCode::ShiftIn => 15,
×
179
            ControlCode::DataLinkEscape => 16,
×
180
            ControlCode::DeviceControl1 => 17,
×
181
            ControlCode::DeviceControl2 => 18,
×
182
            ControlCode::DeviceControl3 => 19,
×
183
            ControlCode::DeviceControl4 => 20,
×
184
            ControlCode::NegativeAcknowledge => 21,
×
185
            ControlCode::SynchronousIdle => 22,
×
186
            ControlCode::EndOfTransmissionBlock => 23,
×
187
            ControlCode::Cancel => 24,
×
188
            ControlCode::EndOfMedium => 25,
×
189
            ControlCode::Substitute => 26,
1✔
190
            ControlCode::Escape => 27,
×
191
            ControlCode::FileSeparator => 28,
×
192
            ControlCode::GroupSeparator => 29,
×
193
            ControlCode::RecordSeparator => 30,
×
194
            ControlCode::UnitSeparator => 31,
×
195
            ControlCode::Space => 32,
×
196
            ControlCode::Delete => 127,
×
197
        }
198
    }
199
}
200

201
impl TryFrom<char> for ControlCode {
202
    type Error = ();
203

204
    fn try_from(c: char) -> Result<ControlCode, ()> {
×
205
        use ControlCode::*;
206
        match c {
×
207
            '@' => Ok(Null),
×
208
            'A' | 'a' => Ok(StartOfHeading),
×
209
            'B' | 'b' => Ok(StartOfText),
×
210
            'C' | 'c' => Ok(EndOfText),
×
211
            'D' | 'd' => Ok(EndOfTransmission),
×
212
            'E' | 'e' => Ok(Enquiry),
×
213
            'F' | 'f' => Ok(Acknowledge),
×
214
            'G' | 'g' => Ok(Bell),
×
215
            'H' | 'h' => Ok(Backspace),
×
216
            'I' | 'i' => Ok(HorizontalTabulation),
×
217
            'J' | 'j' => Ok(LineFeed),
×
218
            'K' | 'k' => Ok(VerticalTabulation),
×
219
            'L' | 'l' => Ok(FormFeed),
×
220
            'M' | 'm' => Ok(CarriageReturn),
×
221
            'N' | 'n' => Ok(ShiftOut),
×
222
            'O' | 'o' => Ok(ShiftIn),
×
223
            'P' | 'p' => Ok(DataLinkEscape),
×
224
            'Q' | 'q' => Ok(DeviceControl1),
×
225
            'R' | 'r' => Ok(DeviceControl2),
×
226
            'S' | 's' => Ok(DeviceControl3),
×
227
            'T' | 't' => Ok(DeviceControl4),
×
228
            'U' | 'u' => Ok(NegativeAcknowledge),
×
229
            'V' | 'v' => Ok(SynchronousIdle),
×
230
            'W' | 'w' => Ok(EndOfTransmissionBlock),
×
231
            'X' | 'x' => Ok(Cancel),
×
232
            'Y' | 'y' => Ok(EndOfMedium),
×
233
            'Z' | 'z' => Ok(Substitute),
×
234
            '[' => Ok(Escape),
×
235
            '\\' => Ok(FileSeparator),
×
236
            ']' => Ok(GroupSeparator),
×
237
            '^' => Ok(RecordSeparator),
×
238
            '_' => Ok(UnitSeparator),
×
239
            ' ' => Ok(Space),
×
240
            '?' => Ok(Delete),
×
241
            _ => Err(()),
×
242
        }
243
    }
244
}
245

246
impl TryFrom<&str> for ControlCode {
247
    type Error = ();
248

249
    fn try_from(c: &str) -> Result<ControlCode, ()> {
×
250
        use ControlCode::*;
251
        match c {
×
252
            "^@" => Ok(Null),
×
253
            "^A" => Ok(StartOfHeading),
×
254
            "^B" => Ok(StartOfText),
×
255
            "^C" => Ok(EndOfText),
×
256
            "^D" => Ok(EndOfTransmission),
×
257
            "^E" => Ok(Enquiry),
×
258
            "^F" => Ok(Acknowledge),
×
259
            "^G" => Ok(Bell),
×
260
            "^H" => Ok(Backspace),
×
261
            "^I" => Ok(HorizontalTabulation),
×
262
            "^J" => Ok(LineFeed),
×
263
            "^K" => Ok(VerticalTabulation),
×
264
            "^L" => Ok(FormFeed),
×
265
            "^M" => Ok(CarriageReturn),
×
266
            "^N" => Ok(ShiftOut),
×
267
            "^O" => Ok(ShiftIn),
×
268
            "^P" => Ok(DataLinkEscape),
×
269
            "^Q" => Ok(DeviceControl1),
×
270
            "^R" => Ok(DeviceControl2),
×
271
            "^S" => Ok(DeviceControl3),
×
272
            "^T" => Ok(DeviceControl4),
×
273
            "^U" => Ok(NegativeAcknowledge),
×
274
            "^V" => Ok(SynchronousIdle),
×
275
            "^W" => Ok(EndOfTransmissionBlock),
×
276
            "^X" => Ok(Cancel),
×
277
            "^Y" => Ok(EndOfMedium),
×
278
            "^Z" => Ok(Substitute),
×
279
            "^[" => Ok(Escape),
×
280
            "^\\" => Ok(FileSeparator),
×
281
            "^]" => Ok(GroupSeparator),
×
282
            "^^" => Ok(RecordSeparator),
×
283
            "^_" => Ok(UnitSeparator),
×
284
            "^ " => Ok(Space),
×
285
            "^?" => Ok(Delete),
×
286
            _ => Err(()),
×
287
        }
288
    }
289
}
290

291
impl AsRef<str> for ControlCode {
292
    fn as_ref(&self) -> &str {
×
293
        match self {
×
294
            Self::Null => "^@",
×
295
            Self::StartOfHeading => "^A",
×
296
            Self::StartOfText => "^B",
×
297
            Self::EndOfText => "^C",
×
298
            Self::EndOfTransmission => "^D",
×
299
            Self::Enquiry => "^E",
×
300
            Self::Acknowledge => "^F",
×
301
            Self::Bell => "^G",
×
302
            Self::Backspace => "^H",
×
303
            Self::HorizontalTabulation => "^I",
×
304
            Self::LineFeed => "^J",
×
305
            Self::VerticalTabulation => "^K",
×
306
            Self::FormFeed => "^L",
×
307
            Self::CarriageReturn => "^M",
×
308
            Self::ShiftOut => "^N",
×
309
            Self::ShiftIn => "^O",
×
310
            Self::DataLinkEscape => "^P",
×
311
            Self::DeviceControl1 => "^Q",
×
312
            Self::DeviceControl2 => "^R",
×
313
            Self::DeviceControl3 => "^S",
×
314
            Self::DeviceControl4 => "^T",
×
315
            Self::NegativeAcknowledge => "^U",
×
316
            Self::SynchronousIdle => "^V",
×
317
            Self::EndOfTransmissionBlock => "^W",
×
318
            Self::Cancel => "^X",
×
319
            Self::EndOfMedium => "^Y",
×
320
            Self::Substitute => "^Z",
×
321
            Self::Escape => "^[",
×
322
            Self::FileSeparator => "^\\",
×
323
            Self::GroupSeparator => "^]",
×
324
            Self::RecordSeparator => "^^",
×
325
            Self::UnitSeparator => "^_",
×
326
            Self::Space => " ",
×
327
            Self::Delete => "^?",
×
328
        }
329
    }
330
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc