remove-except 0.5.1

A Rust CLI to keep selected paths or glob matches and remove everything else under a root.
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use std::fs::{File, OpenOptions};
use std::io;

use console::Key;

pub(super) struct Terminal {
    input: File,
    #[cfg(windows)]
    original: u32,
    #[cfg(unix)]
    original: libc::termios,
    active: bool,
}

impl Terminal {
    pub(super) fn open() -> io::Result<Self> {
        #[cfg(windows)]
        let path = "CONIN$";
        #[cfg(unix)]
        let path = "/dev/tty";
        let input = OpenOptions::new()
            .read(true)
            .write(true)
            .open(path)
            .map_err(|error| {
                io::Error::new(
                    error.kind(),
                    format!("Cannot open the controlling terminal for viewer input: {error}"),
                )
            })?;
        let original = platform::mode(&input)?;
        Ok(Self {
            input,
            original,
            active: false,
        })
    }

    pub(super) fn start(&mut self) -> io::Result<()> {
        // Mark active before changing the mode so a partial failure also restores it.
        self.active = true;
        platform::start(&self.input, &self.original)?;
        self.discard_input()
    }

    pub(super) fn read_key(&self) -> io::Result<Option<Key>> {
        platform::read_key(&self.input)
    }

    pub(super) fn discard_input(&self) -> io::Result<()> {
        platform::discard_input(&self.input)
    }

    pub(super) fn finish(&mut self) -> io::Result<()> {
        if self.active {
            let discarded = self.discard_input();
            let restored = platform::restore(&self.input, &self.original);
            if restored.is_ok() {
                self.active = false;
            }
            match (discarded, restored) {
                (Ok(()), Ok(())) => {}
                (discarded, restored) => {
                    return Err(io::Error::other(format!(
                        "Terminal cleanup failed: input={discarded:?}, mode={restored:?}"
                    )));
                }
            }
        }
        Ok(())
    }
}

impl Drop for Terminal {
    fn drop(&mut self) {
        if self.active {
            // Normal exits use finish(), where errors can prevent deletion.
            if let Err(error) = platform::restore(&self.input, &self.original) {
                eprintln!("Failed to restore viewer terminal input: {error}");
            }
        }
    }
}

#[cfg(windows)]
mod platform {
    use super::*;
    use std::os::windows::io::AsRawHandle;
    use windows_sys::Win32::Foundation::{WAIT_FAILED, WAIT_TIMEOUT};
    use windows_sys::Win32::System::Console::{
        ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT, FlushConsoleInputBuffer,
        GetConsoleMode, INPUT_RECORD, KEY_EVENT, LEFT_ALT_PRESSED, LEFT_CTRL_PRESSED,
        PeekConsoleInputW, RIGHT_ALT_PRESSED, RIGHT_CTRL_PRESSED, ReadConsoleInputW, SHIFT_PRESSED,
        SetConsoleMode,
    };
    use windows_sys::Win32::System::Threading::WaitForSingleObject;

    pub(super) fn mode(input: &File) -> io::Result<u32> {
        let mut mode = 0;
        // The file owns a valid console input handle; mode is writable for this call.
        if unsafe { GetConsoleMode(input.as_raw_handle(), &mut mode) } == 0 {
            return Err(io::Error::last_os_error());
        }
        Ok(mode)
    }

    pub(super) fn start(input: &File, original: &u32) -> io::Result<()> {
        restore(
            input,
            &(original & !(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT)),
        )
    }

    pub(super) fn restore(input: &File, mode: &u32) -> io::Result<()> {
        // The owned handle stays open throughout the synchronous API call.
        if unsafe { SetConsoleMode(input.as_raw_handle(), *mode) } == 0 {
            return Err(io::Error::last_os_error());
        }
        Ok(())
    }

    pub(super) fn discard_input(input: &File) -> io::Result<()> {
        // Only the dedicated console input handle is flushed, never pattern stdin.
        if unsafe { FlushConsoleInputBuffer(input.as_raw_handle()) } == 0 {
            return Err(io::Error::last_os_error());
        }
        Ok(())
    }

    pub(super) fn read_key(input: &File) -> io::Result<Option<Key>> {
        // The console handle is waitable and remains owned by input.
        match unsafe { WaitForSingleObject(input.as_raw_handle(), 100) } {
            WAIT_TIMEOUT => return Ok(None),
            WAIT_FAILED => return Err(io::Error::last_os_error()),
            _ => {}
        }
        let mut record = INPUT_RECORD::default();
        let mut read = 0;
        // A signaled console handle can have no readable records after mode/event
        // processing. Do not enter a blocking read and stop resize polling.
        if unsafe { PeekConsoleInputW(input.as_raw_handle(), &mut record, 1, &mut read) } == 0 {
            return Err(io::Error::last_os_error());
        }
        if read == 0 {
            return Ok(None);
        }
        // record has space for exactly the one requested console record.
        if unsafe { ReadConsoleInputW(input.as_raw_handle(), &mut record, 1, &mut read) } == 0 {
            return Err(io::Error::last_os_error());
        }
        if read == 0 {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "Console input closed",
            ));
        }
        if record.EventType != KEY_EVENT as u16 {
            return Ok(None);
        }
        // EventType identifies the active union member.
        let event = unsafe { record.Event.KeyEvent };
        // UnicodeChar is the UTF-16 variant of the console key event.
        let character = unsafe { event.uChar.UnicodeChar };
        Ok(translate_key(
            event.bKeyDown != 0,
            event.wVirtualKeyCode,
            character,
            event.dwControlKeyState,
        ))
    }

    /// Converts one console key event into a viewer key.
    ///
    /// Precedence: key releases are ignored; character 3 is always Ctrl+C; keys
    /// without a mapping or character (modifier-only and function keys) are ignored
    /// without a redraw; left or right Alt without any Ctrl yields `Key::Unknown`, so
    /// Alt+c and Alt+q never act as viewer commands; Ctrl+Alt (including AltGr, which
    /// arrives as right Alt plus left Ctrl) keeps its typed character.
    pub(super) fn translate_key(
        key_down: bool,
        virtual_key: u16,
        character: u16,
        control_state: u32,
    ) -> Option<Key> {
        if !key_down {
            return None;
        }
        if character == 3 {
            return Some(Key::CtrlC);
        }
        let navigation = match virtual_key {
            0x26 => Some(Key::ArrowUp),
            0x28 => Some(Key::ArrowDown),
            0x21 => Some(Key::PageUp),
            0x22 => Some(Key::PageDown),
            0x24 => Some(Key::Home),
            0x23 => Some(Key::End),
            0x1b => Some(Key::Escape),
            0x0d => Some(Key::Enter),
            0x09 if control_state & SHIFT_PRESSED != 0 => Some(Key::BackTab),
            0x09 => Some(Key::Tab),
            _ => None,
        };
        if navigation.is_none() && character == 0 {
            return None;
        }
        let alt = control_state & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED) != 0;
        let ctrl = control_state & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED) != 0;
        if alt && !ctrl {
            return Some(Key::Unknown);
        }
        Some(navigation.unwrap_or_else(|| {
            char::from_u32(u32::from(character)).map_or(Key::Unknown, Key::Char)
        }))
    }
}

#[cfg(unix)]
mod platform {
    use super::*;
    use std::os::fd::AsRawFd;

    pub(super) fn mode(input: &File) -> io::Result<libc::termios> {
        let mut mode = std::mem::MaybeUninit::uninit();
        // tcgetattr initializes the complete termios value on success.
        if unsafe { libc::tcgetattr(input.as_raw_fd(), mode.as_mut_ptr()) } == -1 {
            return Err(io::Error::last_os_error());
        }
        Ok(unsafe { mode.assume_init() })
    }

    pub(super) fn start(input: &File, original: &libc::termios) -> io::Result<()> {
        let mut mode = *original;
        // mode is an initialized termios value. Preserve output processing on this TTY.
        unsafe { libc::cfmakeraw(&mut mode) };
        mode.c_oflag = original.c_oflag;
        restore(input, &mode)
    }

    pub(super) fn restore(input: &File, mode: &libc::termios) -> io::Result<()> {
        // input owns the TTY fd and mode is initialized for this platform.
        if unsafe { libc::tcsetattr(input.as_raw_fd(), libc::TCSANOW, mode) } == -1 {
            return Err(io::Error::last_os_error());
        }
        Ok(())
    }

    pub(super) fn discard_input(input: &File) -> io::Result<()> {
        // Discard only pending control-terminal input, not stdin's patterns.
        if unsafe { libc::tcflush(input.as_raw_fd(), libc::TCIFLUSH) } == -1 {
            return Err(io::Error::last_os_error());
        }
        Ok(())
    }

    fn read_byte(input: &File, timeout: i32) -> io::Result<Option<u8>> {
        let mut descriptor = libc::pollfd {
            fd: input.as_raw_fd(),
            events: libc::POLLIN,
            revents: 0,
        };
        // descriptor is a valid one-element pollfd array.
        let result = unsafe { libc::poll(&mut descriptor, 1, timeout) };
        if result == -1 {
            return Err(io::Error::last_os_error());
        }
        if result == 0 {
            return Ok(None);
        }
        if descriptor.revents & (libc::POLLERR | libc::POLLHUP | libc::POLLNVAL) != 0 {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "Terminal input closed",
            ));
        }
        let mut byte = 0;
        // byte provides exactly the requested single byte of writable storage.
        match unsafe { libc::read(input.as_raw_fd(), (&mut byte as *mut u8).cast(), 1) } {
            1 => Ok(Some(byte)),
            0 => Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "Terminal input closed",
            )),
            _ => Err(io::Error::last_os_error()),
        }
    }

    pub(super) fn read_key(input: &File) -> io::Result<Option<Key>> {
        let Some(byte) = read_byte(input, 100)? else {
            return Ok(None);
        };
        let key = match byte {
            3 => Key::CtrlC,
            // Raw mode delivers Ctrl+D as an ordinary byte; only poll/read report a
            // closed terminal.
            4 => Key::Unknown,
            b'\r' | b'\n' => Key::Enter,
            b'\t' => Key::Tab,
            0x1b => {
                let Some(next) = read_byte(input, 30)? else {
                    return Ok(Some(Key::Escape));
                };
                if next != b'[' && next != b'O' {
                    return Ok(Some(Key::Unknown));
                }
                let mut sequence = Vec::new();
                while let Some(next) = read_byte(input, 30)? {
                    sequence.push(next);
                    if next.is_ascii_alphabetic() || next == b'~' || sequence.len() >= 16 {
                        break;
                    }
                }
                match sequence.as_slice() {
                    b"A" => Key::ArrowUp,
                    b"B" => Key::ArrowDown,
                    b"H" | b"1~" | b"7~" => Key::Home,
                    b"F" | b"4~" | b"8~" => Key::End,
                    b"5~" => Key::PageUp,
                    b"6~" => Key::PageDown,
                    b"Z" => Key::BackTab,
                    _ => Key::Unknown,
                }
            }
            byte if byte.is_ascii() => Key::Char(char::from(byte)),
            _ => Key::Unknown,
        };
        Ok(Some(key))
    }
}

#[cfg(all(test, windows))]
mod windows_tests {
    use super::platform::translate_key;
    use console::Key;
    use windows_sys::Win32::System::Console::{
        LEFT_ALT_PRESSED, LEFT_CTRL_PRESSED, RIGHT_ALT_PRESSED, RIGHT_CTRL_PRESSED, SHIFT_PRESSED,
    };

    fn letter(character: char, state: u32) -> Option<Key> {
        let virtual_key = character.to_ascii_uppercase() as u16;
        translate_key(true, virtual_key, character as u16, state)
    }

    #[test]
    fn releases_and_keys_without_characters_are_ignored() {
        assert_eq!(translate_key(false, 0x43, u16::from(b'c'), 0), None);
        assert_eq!(translate_key(false, 0x23, 0, 0), None);
        // Shift, Ctrl, Alt, Caps Lock, and F1 pressed on their own.
        for (virtual_key, state) in [
            (0x10, SHIFT_PRESSED),
            (0x11, LEFT_CTRL_PRESSED),
            (0x12, LEFT_ALT_PRESSED),
            (0x14, 0),
            (0x70, 0),
        ] {
            assert_eq!(translate_key(true, virtual_key, 0, state), None);
        }
    }

    #[test]
    fn alt_without_ctrl_never_produces_viewer_commands() {
        for alt in [LEFT_ALT_PRESSED, RIGHT_ALT_PRESSED] {
            for character in ['c', 'q', 'y'] {
                assert_eq!(letter(character, alt), Some(Key::Unknown));
                assert_eq!(letter(character, alt | SHIFT_PRESSED), Some(Key::Unknown));
            }
            assert_eq!(translate_key(true, 0x23, 0, alt), Some(Key::Unknown));
            assert_eq!(translate_key(true, 0x09, 9, alt), Some(Key::Unknown));
        }
    }

    #[test]
    fn ctrl_alt_and_altgr_keep_typed_characters() {
        // AltGr arrives as right Alt plus left Ctrl; any Ctrl+Alt pair is treated alike.
        for state in [
            RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED,
            LEFT_ALT_PRESSED | RIGHT_CTRL_PRESSED,
            LEFT_ALT_PRESSED | LEFT_CTRL_PRESSED,
            RIGHT_ALT_PRESSED | RIGHT_CTRL_PRESSED,
        ] {
            assert_eq!(
                translate_key(true, 0x51, u16::from(b'@'), state),
                Some(Key::Char('@'))
            );
            assert_eq!(
                translate_key(true, 0x45, 0x20ac, state),
                Some(Key::Char('\u{20ac}'))
            );
        }
    }

    #[test]
    fn ctrl_c_shift_tab_and_plain_characters_are_preserved() {
        for state in [
            LEFT_CTRL_PRESSED,
            RIGHT_CTRL_PRESSED,
            LEFT_CTRL_PRESSED | LEFT_ALT_PRESSED,
            LEFT_ALT_PRESSED,
        ] {
            assert_eq!(translate_key(true, 0x43, 3, state), Some(Key::CtrlC));
        }
        assert_eq!(translate_key(true, 0x09, 9, 0), Some(Key::Tab));
        assert_eq!(
            translate_key(true, 0x09, 9, SHIFT_PRESSED),
            Some(Key::BackTab)
        );
        assert_eq!(translate_key(true, 0x23, 0, 0), Some(Key::End));
        assert_eq!(translate_key(true, 0x1b, 0x1b, 0), Some(Key::Escape));
        assert_eq!(letter('c', 0), Some(Key::Char('c')));
        assert_eq!(letter('q', 0), Some(Key::Char('q')));
        assert_eq!(letter('C', SHIFT_PRESSED), Some(Key::Char('C')));
        assert_eq!(
            translate_key(true, 0x44, 4, LEFT_CTRL_PRESSED),
            Some(Key::Char('\x04'))
        );
    }
}

#[cfg(all(test, unix))]
mod unix_tests {
    use super::*;
    use std::io::Write;
    use std::os::fd::FromRawFd;

    fn pipe() -> (File, File) {
        let mut descriptors = [0; 2];
        // pipe writes two new owned descriptors into the two-element array.
        assert_eq!(unsafe { libc::pipe(descriptors.as_mut_ptr()) }, 0);
        // Each descriptor is transferred to exactly one File.
        unsafe {
            (
                File::from_raw_fd(descriptors[0]),
                File::from_raw_fd(descriptors[1]),
            )
        }
    }

    #[test]
    fn ctrl_d_is_an_ignored_key_but_a_closed_input_is_end_of_file() {
        let (reader, mut writer) = pipe();
        writer.write_all(b"\x04q").unwrap();
        assert_eq!(platform::read_key(&reader).unwrap(), Some(Key::Unknown));
        assert_eq!(platform::read_key(&reader).unwrap(), Some(Key::Char('q')));
        drop(writer);
        let error = platform::read_key(&reader).unwrap_err();
        assert_eq!(error.kind(), io::ErrorKind::UnexpectedEof);
    }
}