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<'_>
impl Console<'_>
Sourcepub fn new() -> Result<Self, Box<ConsoleError>>
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.
Sourcepub fn get_console_size(&self) -> (usize, usize)
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.
Sourcepub fn get_mouse_pos_clicked(&self) -> Option<(usize, usize)>
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.
Sourcepub fn draw_text(&self, text: impl Into<String>)
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.
Sourcepub fn set_color_invertible(&self, fg: Color, bg: Color, inverted: bool)
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
Sourcepub fn reset_color(&self)
pub fn reset_color(&self)
Resets the color for foreground and background to Color::Default
pub fn set_underline(&self, underline: bool)
Sourcepub fn set_cursor_pos(&self, x: usize, y: usize)
pub fn set_cursor_pos(&self, x: usize, y: usize)
Sets the cursor pos to x and y