pub struct Console { /* private fields */ }
Expand description

A wrapper around a screen buffer.

Implementations§

source§

impl Console

source

pub fn output() -> Result<Console>

Create new instance of Console.

This created instance will use the default output handle (STD_OUTPUT_HANDLE) as handle for the function call it wraps.

source

pub fn set_text_attribute(&self, value: u16) -> Result<()>

Sets the attributes of characters written to the console screen buffer by the WriteFile or WriteConsole functions, or echoed by the ReadFile or ReadConsole functions. This function affects text written after the function call.

The attributes is a bitmask of possible character attributes.

This wraps SetConsoleTextAttribute.

Examples found in repository?
examples/coloring_example.rs (line 24)
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
fn set_background_color() -> Result<()> {
    // background value
    const BLUE_BACKGROUND: u16 = 0x0010;

    let screen_buffer = ScreenBuffer::current()?;
    let csbi = screen_buffer.info()?;

    // Notice that the color values are stored in wAttribute.
    // So wee need to use bitwise operators to check if the values exists or to get current console colors.
    let attrs = csbi.attributes();
    let fg_color = attrs & 0x0007;

    // apply the blue background flag to the current attributes
    let new_color = fg_color | BLUE_BACKGROUND;

    // set the console text attribute to the new color value.
    Console::from(screen_buffer.handle().clone()).set_text_attribute(new_color)?;

    Ok(())
}

#[cfg(windows)]
fn set_foreground_color() -> Result<()> {
    // background value
    const BLUE_FOREGROUND: u16 = 0x0001;

    let screen_buffer = ScreenBuffer::current()?;
    let csbi = screen_buffer.info()?;

    // Notice that the color values are stored in wAttribute.
    // So we need to use bitwise operators to check if the values exists or to get current console colors.
    let attrs = csbi.attributes();
    let bg_color = attrs & 0x0070;
    let mut color = BLUE_FOREGROUND | bg_color;

    // background intensity is a separate value in attrs,
    // wee need to check if this was applied to the current bg color.
    if (attrs & 0x0080 as u16) != 0 {
        color = color | 0x0080 as u16;
    }

    // set the console text attribute to the new color value.
    Console::from(screen_buffer.handle().clone()).set_text_attribute(color)?;

    Ok(())
}
source

pub fn set_console_info( &self, absolute: bool, rect: WindowPositions ) -> Result<()>

Sets the current size and position of a console screen buffer’s window.

This wraps SetConsoleWindowInfo.

source

pub fn fill_whit_character( &self, start_location: Coord, cells_to_write: u32, filling_char: char ) -> Result<u32>

Writes a character to the console screen buffer a specified number of times, beginning at the specified coordinates. Returns the number of characters that have been written.

This wraps FillConsoleOutputCharacterA.

source

pub fn fill_whit_attribute( &self, start_location: Coord, cells_to_write: u32, dw_attribute: u16 ) -> Result<u32>

Sets the character attributes for a specified number of character cells, beginning at the specified coordinates in a screen buffer. Returns the number of cells that have been modified.

This wraps FillConsoleOutputAttribute.

source

pub fn largest_window_size(&self) -> Result<Coord>

Retrieves the size of the largest possible console window, based on the current text and the size of the display.

This wraps GetLargestConsoleWindowSize

source

pub fn write_char_buffer(&self, buf: &[u8]) -> Result<usize>

Writes a character string to a console screen buffer beginning at the current cursor location.

This wraps WriteConsoleW.

source

pub fn read_single_input_event(&self) -> Result<InputRecord>

Read one input event.

This wraps ReadConsoleInputW.

source

pub fn read_console_input(&self) -> Result<Vec<InputRecord>>

Read all available input events without blocking.

This wraps ReadConsoleInputW.

source

pub fn number_of_console_input_events(&self) -> Result<u32>

Get the number of available input events that can be read without blocking.

This wraps GetNumberOfConsoleInputEvents.

Trait Implementations§

source§

impl Clone for Console

source§

fn clone(&self) -> Console

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Console

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<Handle> for Console

source§

fn from(handle: Handle) -> Self

Create a Console instance who’s functions will be executed on the the given Handle

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.