ergonomic-windows 0.1.0

Ergonomic, safe Rust wrappers for Windows APIs - handles, processes, registry, file system, UI controls, Direct2D graphics, and more
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! Console I/O utilities.
//!
//! Provides safe wrappers for Windows console operations including
//! reading, writing, colors, cursor positioning, and screen buffers.

use crate::error::Result;
use windows::Win32::Foundation::HANDLE;
use windows::Win32::System::Console::{
    AllocConsole, FillConsoleOutputAttribute, FillConsoleOutputCharacterW, FreeConsole,
    GetConsoleCursorInfo, GetConsoleMode, GetConsoleScreenBufferInfo, GetConsoleTitleW,
    GetStdHandle, ReadConsoleW, SetConsoleCursorInfo, SetConsoleCursorPosition, SetConsoleMode,
    SetConsoleTextAttribute, SetConsoleTitleW, WriteConsoleW, CONSOLE_CHARACTER_ATTRIBUTES,
    CONSOLE_CURSOR_INFO, CONSOLE_MODE, CONSOLE_SCREEN_BUFFER_INFO, COORD, ENABLE_ECHO_INPUT,
    ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT, ENABLE_PROCESSED_OUTPUT,
    ENABLE_VIRTUAL_TERMINAL_PROCESSING, STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
};

/// Standard console handles.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StdHandle {
    /// Standard input (stdin).
    Input,
    /// Standard output (stdout).
    Output,
    /// Standard error (stderr).
    Error,
}

impl StdHandle {
    fn to_id(self) -> windows::Win32::System::Console::STD_HANDLE {
        match self {
            StdHandle::Input => STD_INPUT_HANDLE,
            StdHandle::Output => STD_OUTPUT_HANDLE,
            StdHandle::Error => STD_ERROR_HANDLE,
        }
    }
}

/// Console text colors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum Color {
    /// Black (0x0).
    Black = 0,
    /// Dark blue (0x1).
    DarkBlue = 1,
    /// Dark green (0x2).
    DarkGreen = 2,
    /// Dark cyan (0x3).
    DarkCyan = 3,
    /// Dark red (0x4).
    DarkRed = 4,
    /// Dark magenta (0x5).
    DarkMagenta = 5,
    /// Dark yellow (0x6).
    DarkYellow = 6,
    /// Gray (0x7).
    Gray = 7,
    /// Dark gray (0x8).
    DarkGray = 8,
    /// Blue (0x9).
    Blue = 9,
    /// Green (0xA).
    Green = 10,
    /// Cyan (0xB).
    Cyan = 11,
    /// Red (0xC).
    Red = 12,
    /// Magenta (0xD).
    Magenta = 13,
    /// Yellow (0xE).
    Yellow = 14,
    /// White (0xF).
    White = 15,
}

impl Color {
    fn from_u16(value: u16) -> Self {
        match value & 0x0F {
            0 => Color::Black,
            1 => Color::DarkBlue,
            2 => Color::DarkGreen,
            3 => Color::DarkCyan,
            4 => Color::DarkRed,
            5 => Color::DarkMagenta,
            6 => Color::DarkYellow,
            7 => Color::Gray,
            8 => Color::DarkGray,
            9 => Color::Blue,
            10 => Color::Green,
            11 => Color::Cyan,
            12 => Color::Red,
            13 => Color::Magenta,
            14 => Color::Yellow,
            _ => Color::White,
        }
    }
}

/// Console text attributes.
#[derive(Debug, Clone, Copy)]
pub struct TextAttribute {
    foreground: Color,
    background: Color,
}

impl TextAttribute {
    /// Creates a new text attribute with the given colors.
    pub fn new(foreground: Color, background: Color) -> Self {
        Self {
            foreground,
            background,
        }
    }

    /// Creates a text attribute with default colors (gray on black).
    pub fn default_colors() -> Self {
        Self::new(Color::Gray, Color::Black)
    }

    fn to_u16(self) -> u16 {
        (self.foreground as u16) | ((self.background as u16) << 4)
    }
}

impl Default for TextAttribute {
    fn default() -> Self {
        Self::default_colors()
    }
}

/// A Windows console.
pub struct Console {
    input: HANDLE,
    output: HANDLE,
}

impl Console {
    /// Gets the console for the current process.
    pub fn current() -> Result<Self> {
        let input = get_std_handle(StdHandle::Input)?;
        let output = get_std_handle(StdHandle::Output)?;

        Ok(Self { input, output })
    }

    /// Allocates a new console for the current process.
    ///
    /// A process can only have one console, so this will fail if one already exists.
    pub fn alloc() -> Result<Self> {
        // SAFETY: AllocConsole is safe to call
        unsafe {
            AllocConsole()?;
        }
        Self::current()
    }

    /// Frees the console.
    pub fn free() -> Result<()> {
        // SAFETY: FreeConsole is safe to call
        unsafe {
            FreeConsole()?;
        }
        Ok(())
    }

    /// Gets the console title.
    pub fn title() -> Result<String> {
        let mut buffer = vec![0u16; 1024];
        // SAFETY: GetConsoleTitle is safe with a valid buffer
        let len = unsafe { GetConsoleTitleW(&mut buffer) } as usize;

        if len == 0 {
            return Ok(String::new());
        }

        crate::string::from_wide(&buffer[..len])
    }

    /// Sets the console title.
    pub fn set_title(title: &str) -> Result<()> {
        let title_wide = crate::string::WideString::new(title);
        // SAFETY: SetConsoleTitleW is safe with a valid string
        unsafe {
            SetConsoleTitleW(title_wide.as_pcwstr())?;
        }
        Ok(())
    }

    /// Writes a string to the console.
    pub fn write(&self, text: &str) -> Result<usize> {
        let wide: Vec<u16> = text.encode_utf16().collect();
        let mut written = 0u32;

        // SAFETY: WriteConsoleW is safe with valid parameters
        unsafe {
            WriteConsoleW(self.output, &wide, Some(&mut written), None)?;
        }

        Ok(written as usize)
    }

    /// Writes a line to the console (adds newline).
    pub fn write_line(&self, text: &str) -> Result<usize> {
        let mut total = self.write(text)?;
        total += self.write("\r\n")?;
        Ok(total)
    }

    /// Reads a line from the console.
    pub fn read_line(&self) -> Result<String> {
        let mut buffer = vec![0u16; 4096];
        let mut read = 0u32;

        // SAFETY: ReadConsoleW is safe with valid parameters
        unsafe {
            ReadConsoleW(
                self.input,
                buffer.as_mut_ptr() as *mut _,
                buffer.len() as u32,
                &mut read,
                None,
            )?;
        }

        // Trim trailing \r\n
        let len = read as usize;
        let end = if len >= 2 && buffer[len - 2] == 0x0D && buffer[len - 1] == 0x0A {
            len - 2
        } else {
            len
        };

        crate::string::from_wide(&buffer[..end])
    }

    /// Sets the text color.
    pub fn set_text_attribute(&self, attr: TextAttribute) -> Result<()> {
        // SAFETY: SetConsoleTextAttribute is safe with valid handle
        unsafe {
            SetConsoleTextAttribute(self.output, CONSOLE_CHARACTER_ATTRIBUTES(attr.to_u16()))?;
        }
        Ok(())
    }

    /// Sets the foreground color.
    pub fn set_foreground(&self, color: Color) -> Result<()> {
        let info = self.screen_buffer_info()?;
        let current_bg = (info.attributes >> 4) & 0x0F;
        let attr = TextAttribute::new(color, Color::from_u16(current_bg));
        self.set_text_attribute(attr)
    }

    /// Sets the background color.
    pub fn set_background(&self, color: Color) -> Result<()> {
        let info = self.screen_buffer_info()?;
        let current_fg = info.attributes & 0x0F;
        let attr = TextAttribute::new(Color::from_u16(current_fg), color);
        self.set_text_attribute(attr)
    }

    /// Gets the cursor position.
    pub fn cursor_position(&self) -> Result<(i16, i16)> {
        let info = self.screen_buffer_info()?;
        Ok((info.cursor_x, info.cursor_y))
    }

    /// Sets the cursor position.
    pub fn set_cursor_position(&self, x: i16, y: i16) -> Result<()> {
        let coord = COORD { X: x, Y: y };
        // SAFETY: SetConsoleCursorPosition is safe with valid parameters
        unsafe {
            SetConsoleCursorPosition(self.output, coord)?;
        }
        Ok(())
    }

    /// Gets cursor visibility and size.
    pub fn cursor_info(&self) -> Result<(bool, u32)> {
        let mut info = CONSOLE_CURSOR_INFO::default();
        // SAFETY: GetConsoleCursorInfo is safe with valid parameters
        unsafe {
            GetConsoleCursorInfo(self.output, &mut info)?;
        }
        Ok((info.bVisible.as_bool(), info.dwSize))
    }

    /// Sets cursor visibility.
    pub fn set_cursor_visible(&self, visible: bool) -> Result<()> {
        let (_, size) = self.cursor_info()?;
        let info = CONSOLE_CURSOR_INFO {
            dwSize: size,
            bVisible: visible.into(),
        };
        // SAFETY: SetConsoleCursorInfo is safe with valid parameters
        unsafe {
            SetConsoleCursorInfo(self.output, &info)?;
        }
        Ok(())
    }

    /// Gets the screen buffer info.
    pub fn screen_buffer_info(&self) -> Result<ScreenBufferInfo> {
        let mut info = CONSOLE_SCREEN_BUFFER_INFO::default();
        // SAFETY: GetConsoleScreenBufferInfo is safe with valid parameters
        unsafe {
            GetConsoleScreenBufferInfo(self.output, &mut info)?;
        }

        Ok(ScreenBufferInfo {
            size_x: info.dwSize.X,
            size_y: info.dwSize.Y,
            cursor_x: info.dwCursorPosition.X,
            cursor_y: info.dwCursorPosition.Y,
            attributes: info.wAttributes.0,
            window_left: info.srWindow.Left,
            window_top: info.srWindow.Top,
            window_right: info.srWindow.Right,
            window_bottom: info.srWindow.Bottom,
            max_window_x: info.dwMaximumWindowSize.X,
            max_window_y: info.dwMaximumWindowSize.Y,
        })
    }

    /// Clears the screen.
    pub fn clear(&self) -> Result<()> {
        let info = self.screen_buffer_info()?;
        let size = (info.size_x as u32) * (info.size_y as u32);
        let coord = COORD { X: 0, Y: 0 };
        let mut written = 0u32;

        // Fill with spaces
        // SAFETY: FillConsoleOutputCharacterW is safe with valid parameters
        unsafe {
            FillConsoleOutputCharacterW(self.output, ' ' as u16, size, coord, &mut written)?;
        }

        // Reset attributes
        // SAFETY: FillConsoleOutputAttribute is safe with valid parameters
        unsafe {
            FillConsoleOutputAttribute(self.output, info.attributes, size, coord, &mut written)?;
        }

        // Move cursor to top-left
        self.set_cursor_position(0, 0)?;

        Ok(())
    }

    /// Enables virtual terminal processing (ANSI escape codes).
    pub fn enable_virtual_terminal(&self) -> Result<()> {
        let mut mode = CONSOLE_MODE(0);
        // SAFETY: GetConsoleMode is safe with valid handle
        unsafe {
            GetConsoleMode(self.output, &mut mode)?;
        }

        let new_mode =
            CONSOLE_MODE(mode.0 | ENABLE_VIRTUAL_TERMINAL_PROCESSING.0 | ENABLE_PROCESSED_OUTPUT.0);
        // SAFETY: SetConsoleMode is safe with valid handle
        unsafe {
            SetConsoleMode(self.output, new_mode)?;
        }

        Ok(())
    }

    /// Enables raw input mode (no line buffering or echo).
    pub fn enable_raw_input(&self) -> Result<()> {
        let mut mode = CONSOLE_MODE(0);
        // SAFETY: GetConsoleMode is safe with valid handle
        unsafe {
            GetConsoleMode(self.input, &mut mode)?;
        }

        // Disable line input and echo
        let new_mode = CONSOLE_MODE(
            mode.0 & !(ENABLE_LINE_INPUT.0 | ENABLE_ECHO_INPUT.0 | ENABLE_PROCESSED_INPUT.0),
        );
        // SAFETY: SetConsoleMode is safe with valid handle
        unsafe {
            SetConsoleMode(self.input, new_mode)?;
        }

        Ok(())
    }

    /// Restores normal input mode.
    pub fn restore_input_mode(&self) -> Result<()> {
        let mode =
            CONSOLE_MODE(ENABLE_LINE_INPUT.0 | ENABLE_ECHO_INPUT.0 | ENABLE_PROCESSED_INPUT.0);
        // SAFETY: SetConsoleMode is safe with valid handle
        unsafe {
            SetConsoleMode(self.input, mode)?;
        }
        Ok(())
    }
}

/// Information about the screen buffer.
#[derive(Debug, Clone)]
pub struct ScreenBufferInfo {
    /// Buffer width in characters.
    pub size_x: i16,
    /// Buffer height in characters.
    pub size_y: i16,
    /// Cursor X position.
    pub cursor_x: i16,
    /// Cursor Y position.
    pub cursor_y: i16,
    /// Current text attributes.
    pub attributes: u16,
    /// Window left edge.
    pub window_left: i16,
    /// Window top edge.
    pub window_top: i16,
    /// Window right edge.
    pub window_right: i16,
    /// Window bottom edge.
    pub window_bottom: i16,
    /// Maximum window width.
    pub max_window_x: i16,
    /// Maximum window height.
    pub max_window_y: i16,
}

impl ScreenBufferInfo {
    /// Gets the visible window width.
    pub fn window_width(&self) -> i16 {
        self.window_right - self.window_left + 1
    }

    /// Gets the visible window height.
    pub fn window_height(&self) -> i16 {
        self.window_bottom - self.window_top + 1
    }
}

/// Gets a standard handle.
pub fn get_std_handle(handle: StdHandle) -> Result<HANDLE> {
    // SAFETY: GetStdHandle is safe to call
    let h = unsafe { GetStdHandle(handle.to_id())? };
    Ok(h)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_text_attribute() {
        let attr = TextAttribute::new(Color::White, Color::DarkBlue);
        assert_eq!(attr.to_u16(), 0x1F);
    }

    #[test]
    fn test_screen_buffer_info() {
        // This test only works if we have a console with valid handles
        if let Ok(console) = Console::current() {
            // Screen buffer info may fail in CI/non-console environments
            if let Ok(info) = console.screen_buffer_info() {
                assert!(info.size_x > 0);
                assert!(info.size_y > 0);
            }
        }
    }

    #[test]
    fn test_console_title() {
        // This test only works if we have a console
        if Console::current().is_ok() {
            let original = Console::title().unwrap_or_default();
            let _ = Console::set_title("Test Title");
            let _ = Console::set_title(&original);
        }
    }
}