prettytty 0.3.0

Simple, lightweight terminal I/O and configuration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use core::ffi::c_void;
use core::ptr::{from_mut, null};
use std::fs::OpenOptions;
use std::io::{stderr, stdin, stdout, Error, ErrorKind, IsTerminal, Read, Result, Write};
use std::os::windows::io::{AsRawHandle, OwnedHandle};

use windows_sys::Win32::Foundation;
use windows_sys::Win32::Globalization;
use windows_sys::Win32::System::Console::{self, CONSOLE_MODE as ConsoleMode};
use windows_sys::Win32::System::Threading;

use super::util::{IdentList, IntoResult};
use super::RawHandle;
use crate::opt::{Mode, Options};

// ----------------------------------------------------------------------------------------------------------

/// A raw connection handle.
///
/// This enumeration covers dedicated connections with two owned handles as well
/// as virtual connections based on standard I/O with two raw handles. On drop,
/// only the former are closed, which is the desired behavior.
#[derive(Debug)]
enum RawConnectionHandle {
    Owned(OwnedHandle, OwnedHandle),
    #[allow(dead_code)]
    StdIo(RawHandle, RawHandle),
}

impl RawConnectionHandle {
    fn input(&self) -> RawHandle {
        match *self {
            Self::Owned(ref input, _) => input.as_raw_handle(),
            Self::StdIo(ref input, _) => *input,
        }
    }

    fn output(&self) -> RawHandle {
        match *self {
            Self::Owned(_, ref output) => output.as_raw_handle(),
            Self::StdIo(_, ref output) => *output,
        }
    }
}

// SAFETY: Windows HANDLE is defined as a *mut c_void but most instances are
// thread-safe. In fact, Rust's standard library [implements `Send` and
// `Sync`](https://github.com/rust-lang/rust/blob/8e37e151835d96d6a7415e93e6876561485a3354/library/std/src/os/windows/io/handle.rs#L111),
// for wrapped handles, too. Also, access to raw input is gated by a mutex.
unsafe impl Send for RawConnectionHandle {}
// SAFETY: See previous comment.
unsafe impl Sync for RawConnectionHandle {}

/// A connection to a terminal device.
#[derive(Debug)]
pub(crate) struct RawConnection {
    timeout: u32,
    handle: RawConnectionHandle,
}

impl RawConnection {
    /// Open a new owned connection to the terminal device.
    pub fn open(options: &Options) -> Result<Self> {
        let timeout = 100 * (options.timeout() as u32);
        let input = OpenOptions::new()
            .read(true)
            .write(true)
            .open("CONIN$")?
            .into();
        let output = OpenOptions::new()
            .read(true)
            .write(true)
            .open("CONOUT$")?
            .into();

        Ok(Self {
            timeout,
            handle: RawConnectionHandle::Owned(input, output),
        })
    }

    /// Use standard I/O to simulate a dedicated terminal connection.
    #[allow(dead_code)]
    pub fn with_stdio(options: &Options) -> Option<Self> {
        if stdin().is_terminal() {
            let output = if stdout().is_terminal() {
                stdout().as_raw_handle()
            } else if stderr().is_terminal() {
                stderr().as_raw_handle()
            } else {
                return None;
            };

            Some(Self {
                timeout: 100 * (options.timeout() as u32),
                handle: RawConnectionHandle::StdIo(stdin().as_raw_handle(), output),
            })
        } else {
            None
        }
    }

    /// Get the process group ID.
    #[inline]
    pub fn group(&self) -> Result<u32> {
        Err(ErrorKind::Unsupported.into())
    }

    /// Get a handle for the terminal's input.
    #[inline]
    pub fn input(&self) -> RawInput {
        RawInput::new(self.handle.input(), self.timeout)
    }

    /// Get a handle for the terminal's output.
    #[inline]
    pub fn output(&self) -> RawOutput {
        RawOutput::new(self.handle.output())
    }
}

// ----------------------------------------------------------------------------------------------------------

/// A grouping of configuration flags.
enum ModeGroup {
    Input,
    Output,
}

impl ModeGroup {
    pub fn all() -> impl core::iter::Iterator<Item = ModeGroup> {
        core::iter::successors(Some(Self::Input), |g| {
            Some(match *g {
                Self::Input => Self::Output,
                Self::Output => return None,
            })
        })
    }

    pub fn name(&self) -> &'static str {
        match *self {
            Self::Input => "input_modes",
            Self::Output => "output_modes",
        }
    }
}

/// A terminal configuration.
pub(crate) struct RawConfig {
    input_modes: ConsoleMode,
    input_encoding: u32,
    output_modes: ConsoleMode,
    output_encoding: u32,
}

impl RawConfig {
    pub fn read(connection: &RawConnection) -> Result<Self> {
        let input_modes = Self::read_mode(connection.input())?;
        // SAFETY: We are passing no arguments, just as expected.
        let input_encoding = unsafe { Console::GetConsoleCP() }.into_result()?;
        let output_modes = Self::read_mode(connection.output())?;
        // SAFETY: We are passing no arguments, just as expected.
        let output_encoding = unsafe { Console::GetConsoleOutputCP() }.into_result()?;

        Ok(Self {
            input_modes,
            input_encoding,
            output_modes,
            output_encoding,
        })
    }

    fn read_mode(handle: impl Into<RawHandle>) -> Result<ConsoleMode> {
        let mut mode = 0;
        // SAFETY: We are passing the expected arguments in the expected order.
        unsafe { Console::GetConsoleMode(handle.into(), from_mut(&mut mode)) }.into_result()?;
        Ok(mode)
    }

    /// Apply the terminal mode to this configuration.
    ///
    /// For Unix, charred and cooked mode are the same; they make no changes.
    /// For Windows, charred mode makes no changes, but cooked mode switches
    /// to the UTF-8 encoding, `ENABLE_VIRTUAL_TERMINAL_INPUT`,
    /// `ENABLE_PROCESSED_OUTPUT`, and `ENABLE_VIRTUAL_TERMINAL_PROCESSING`.
    /// These options ensure that the terminal actually processed ANSI
    /// escape sequences.
    pub fn apply(&self, options: &Options) -> Option<Self> {
        // Charred mode means "do not touch"
        if options.mode() == Mode::Charred {
            return None;
        }

        let mut input_modes = self.input_modes | Console::ENABLE_VIRTUAL_TERMINAL_INPUT;
        if options.mode() != Mode::Cooked {
            input_modes &= !Console::ENABLE_ECHO_INPUT & !Console::ENABLE_LINE_INPUT;
        }
        if options.mode() == Mode::Raw {
            input_modes &= !Console::ENABLE_PROCESSED_INPUT;
        }
        let input_encoding = Globalization::CP_UTF8;

        let output_modes = self.output_modes
            | Console::ENABLE_PROCESSED_OUTPUT
            | Console::ENABLE_VIRTUAL_TERMINAL_PROCESSING
            | Console::DISABLE_NEWLINE_AUTO_RETURN;
        let output_encoding = Globalization::CP_UTF8;

        Some(Self {
            input_modes,
            input_encoding,
            output_modes,
            output_encoding,
        })
    }

    pub fn write(&self, connection: &RawConnection) -> Result<()> {
        let result1 = Self::write_mode(connection.input(), self.input_modes);
        // SAFETY: We are passing the expected arguments in the expected order.
        let result2 = unsafe { Console::SetConsoleCP(self.input_encoding) }.into_result();
        let result3 = Self::write_mode(connection.output(), self.output_modes);
        // SAFETY: We are passing the expected arguments in the expected order.
        let result4 = unsafe { Console::SetConsoleOutputCP(self.output_encoding) }.into_result();

        result1.and(result2).and(result3).and(result4)?;
        Ok(())
    }

    fn write_mode(handle: impl Into<RawHandle>, mode: ConsoleMode) -> Result<()> {
        // SAFETY: We are passing the expected arguments in the expected order.
        unsafe { Console::SetConsoleMode(handle.into(), mode) }.into_result()?;
        Ok(())
    }

    /// Get labels for active modes in given group.
    fn labels(&self, group: &ModeGroup) -> Vec<&'static str> {
        let mut labels = Vec::new();

        macro_rules! maybe_add {
            ($field:expr, $mask:expr, $label:expr) => {
                if $field & $mask != 0 {
                    labels.push($label);
                }
            };
        }

        match *group {
            ModeGroup::Input => {
                for (label, mask) in [
                    ("ENABLE_ECHO_INPUT", Console::ENABLE_ECHO_INPUT),
                    ("ENABLE_INSERT_MODE", Console::ENABLE_INSERT_MODE),
                    ("ENABLE_LINE_INPUT", Console::ENABLE_LINE_INPUT),
                    ("ENABLE_MOUSE_INPUT", Console::ENABLE_MOUSE_INPUT),
                    ("ENABLE_PROCESSED_INPUT", Console::ENABLE_PROCESSED_INPUT),
                    ("ENABLE_QUICK_EDIT_MODE", Console::ENABLE_QUICK_EDIT_MODE),
                    ("ENABLE_WINDOW_INPUT", Console::ENABLE_WINDOW_INPUT),
                    (
                        "ENABLE_VIRTUAL_TERMINAL_INPUT",
                        Console::ENABLE_VIRTUAL_TERMINAL_INPUT,
                    ),
                ] {
                    maybe_add!(self.input_modes, mask, label);
                }
            }
            ModeGroup::Output => {
                for (label, mask) in [
                    ("ENABLE_PROCESSED_OUTPUT", Console::ENABLE_PROCESSED_OUTPUT),
                    (
                        "ENABLE_WRAP_AT_EOL_OUTPUT",
                        Console::ENABLE_WRAP_AT_EOL_OUTPUT,
                    ),
                    (
                        "ENABLE_VIRTUAL_TERMINAL_PROCESSING",
                        Console::ENABLE_VIRTUAL_TERMINAL_PROCESSING,
                    ),
                    (
                        "DISABLE_NEWLINE_AUTO_RETURN",
                        Console::DISABLE_NEWLINE_AUTO_RETURN,
                    ),
                    (
                        "ENABLE_LVB_GRID_WORLDWIDE",
                        Console::ENABLE_LVB_GRID_WORLDWIDE,
                    ),
                ] {
                    maybe_add!(self.output_modes, mask, label);
                }
            }
        }

        labels
    }
}

impl core::fmt::Debug for RawConfig {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut debugger = f.debug_struct("RawConfig");
        for group in ModeGroup::all() {
            debugger.field(
                group.name(),
                &IdentList::new(self.labels(&group).as_slice()),
            );
            match group {
                ModeGroup::Input => debugger.field("input_encoding", &self.input_encoding),
                ModeGroup::Output => debugger.field("output_encoding", &self.output_encoding),
            };
        }
        debugger.finish()
    }
}

// ----------------------------------------------------------------------------------------------------------

#[derive(Debug)]
pub(crate) struct RawInput {
    timeout: u32,
    handle: RawHandle,
}

impl RawInput {
    #[inline]
    fn new(handle: RawHandle, timeout: u32) -> Self {
        Self { handle, timeout }
    }

    #[allow(dead_code)]
    #[inline]
    fn handle(&self) -> RawHandle {
        self.handle
    }
}

// SAFETY: Windows HANDLE is defined as a *mut c_void but most instances are
// thread-safe. In fact, Rust's standard library [implements `Send` and
// `Sync`](https://github.com/rust-lang/rust/blob/8e37e151835d96d6a7415e93e6876561485a3354/library/std/src/os/windows/io/handle.rs#L111),
// for wrapped handles, too. Also, access to raw input is gated by a mutex.
unsafe impl Send for RawInput {}

impl From<RawInput> for RawHandle {
    fn from(value: RawInput) -> Self {
        value.handle
    }
}

impl Read for RawInput {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        // SAFETY: We are passing the expected arguments in the expected order.
        let status = unsafe { Threading::WaitForSingleObject(self.handle, self.timeout) };
        if status == Foundation::WAIT_OBJECT_0 {
            let mut did_read: u32 = 0;
            // SAFETY: We are passing the expected arguments in the expected order.
            unsafe {
                Console::ReadConsoleA(
                    self.handle,
                    buf.as_mut_ptr().cast::<c_void>(),
                    buf.len() as u32,
                    from_mut(&mut did_read),
                    null(),
                )
            }
            .into_result()?;
            Ok(did_read as usize)
        } else if status == Foundation::WAIT_TIMEOUT {
            Ok(0)
        } else if status == Foundation::WAIT_FAILED {
            Err(Error::last_os_error())
        } else {
            Err(ErrorKind::Other.into())
        }
    }
}

// ------------------------------------------------------------------------------------------------

#[derive(Debug)]
pub(crate) struct RawOutput {
    //#[allow(dead_code)]
    handle: RawHandle,
}

impl RawOutput {
    /// Create a new writer with a raw file descriptor.
    #[inline]
    pub fn new(handle: RawHandle) -> Self {
        Self { handle }
    }

    #[allow(dead_code)]
    #[inline]
    pub fn handle(&self) -> RawHandle {
        self.handle
    }
}

// SAFETY: Windows HANDLE is defined as a *mut c_void but most instances are
// thread-safe. In fact, Rust's standard library [implements `Send` and
// `Sync`](https://github.com/rust-lang/rust/blob/8e37e151835d96d6a7415e93e6876561485a3354/library/std/src/os/windows/io/handle.rs#L111),
// for wrapped handles, too. Also, access to raw input is gated by a mutex.
unsafe impl Send for RawOutput {}

impl From<RawOutput> for RawHandle {
    fn from(value: RawOutput) -> Self {
        value.handle
    }
}

impl Write for RawOutput {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        let mut did_write: u32 = 0;
        // SAFETY: We are passing the expected arguments in the expected order.
        unsafe {
            Console::WriteConsoleA(
                self.handle,
                // https://learn.microsoft.com/en-us/windows/console/writeconsole
                // says this pointer is *const c_void. That would be consistent
                // with ReadConsoleA (see above) as well. Alas, windows-sys
                // insists on the pointer being *const u8.
                buf.as_ptr(),
                buf.len() as u32,
                from_mut(&mut did_write),
                null(),
            )
        }
        .into_result()?;
        Ok(did_write as usize)
    }

    fn flush(&mut self) -> Result<()> {
        Ok(())
    }
}