console_lib

Struct Console

Source
pub struct Console<'a> { /* private fields */ }
Expand description

An abstraction for the console lib which automatically changes the console / terminal mode in Console::new and in the Drop implementation of Console.

Implementations§

Source§

impl Console<'_>

Source

pub fn new() -> Result<Self, Box<ConsoleError>>

Creates a new console lib abstraction.

The Console::new method changes the console / terminal mode (Like disabling text echo).

The Drop implementation of Console will reset the console / terminal to the original state it was in prior to the creation of Console.

If a panic occurred if the console / terminal mode was already changed, the panic error message would be lost, because the Drop implementation of Console would be called after the panic message was printed.

Because of the mode changes there can only be one instance of a Console struct at once, the Err variant is returned if there exists another instance of Console.

§Custom Panic Hook

With the custom_panic_hook feature the lost panic error message problem can be prevented. If the custom_panic_hook feature is enabled a panic hook will be created after the first time the console / terminal mode is changed inside the Console::new method - the newly set panic hook persists until the whole program is terminated. When a panic occurs, the panic hook checks if a Console instance is still present. If this is the case, the console / terminal mode will be reset to the original state. Afterward the standard panic hook will be run which will print the panic error message.

If the custom_panic_hook feature is enabled and the program is panicking, the Drop implementation of Console would not reset the console / terminal mode again.

Source

pub fn repaint(&self)

Repaints the screen

Source

pub fn get_console_size(&self) -> (usize, usize)

Returns the size of the console in characters as (width, rows).

At the moment Console / Terminal resizing is currently not supported. The size is read once after the console is initialized and internal buffers are allocated for that size.

Source

pub fn has_input(&self) -> bool

Checks if key input is available

Source

pub fn get_key(&self) -> Option<Key>

Returns the key which was pressed or None

Source

pub fn get_mouse_pos_clicked(&self) -> Option<(usize, usize)>

Returns the coordinates of the pos where a left click occurred as (x, y).

x and y represent character positions.

If None, no left click occurred.

Source

pub fn draw_text(&self, text: impl Into<String>)

Draws text at the current cursor position.

Behavior for Non-ASCII strings is terminal dependent.

Characters which are out of bounds will be ignored and not drawn.

Source

pub fn set_color(&self, fg: Color, bg: Color)

Sets the color for foreground and background

Source

pub fn set_color_invertible(&self, fg: Color, bg: Color, inverted: bool)

Sets the color for foreground and background

Foreground and background colors are swapped if inverted is true

Source

pub fn reset_color(&self)

Resets the color for foreground and background to Color::Default

Source

pub fn set_underline(&self, underline: bool)

Source

pub fn set_cursor_pos(&self, x: usize, y: usize)

Sets the cursor pos to x and y

Trait Implementations§

Source§

impl Drop for Console<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Console<'a>

§

impl<'a> RefUnwindSafe for Console<'a>

§

impl<'a> !Send for Console<'a>

§

impl<'a> Sync for Console<'a>

§

impl<'a> Unpin for Console<'a>

§

impl<'a> UnwindSafe for Console<'a>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.