Struct crossterm::TerminalColor
source · pub struct TerminalColor<'stdout> { /* private fields */ }
Expand description
Struct that stores a platform-specific implementation for color related actions.
For styling text use the ::crossterm::style()
function. TerminalColor
will set the colors of the screen permanently and the style()
will only style the text given.
Check /examples/color
in the library for more specific examples.
use crossterm::style::color;
let colored_terminal = color();
// set foreground color
colored_terminal.set_fg(Color::Red);
// set background color
colored_terminal.set_bg(Color::Red);
// reset color to default
colored_terminal.reset();
When you want to use ‘color’ on ‘alternate screen’ use the Screen
type instead and pass it to the color::from_screen()
function.
By doing that styling actions will be performed on the alternate screen.
Implementations§
source§impl<'stdout> TerminalColor<'stdout>
impl<'stdout> TerminalColor<'stdout>
sourcepub fn new() -> TerminalColor<'stdout>
pub fn new() -> TerminalColor<'stdout>
Create new instance whereon color related actions can be performed.
sourcepub fn from_output(
stdout: &'stdout Arc<TerminalOutput>
) -> TerminalColor<'stdout>
pub fn from_output(
stdout: &'stdout Arc<TerminalOutput>
) -> TerminalColor<'stdout>
Create a new instance of TerminalColor
whereon coloring could be preformed on the given output.
Note
Use this function when you want your terminal to operate with a specific output.
This could be useful when you have a screen which is in ‘alternate mode’.
And you want your actions from the TerminalColor
, created by this function, to operate on the ‘alternate screen’.
Example
let screen = Screen::default();
if let Ok(alternate) = screen.enable_alternate_modes(false) {
let terminal = TerminalColor::from_output(&alternate.screen.stdout);
}
sourcepub fn set_fg(&self, color: Color) -> Result<(), ErrorKind>
pub fn set_fg(&self, color: Color) -> Result<(), ErrorKind>
Set the foreground color to the given color.
let colored_terminal = color();
// Set foreground color of the font
colored_terminal.set_fg(Color::Red);
// crossterm provides to set the background from &str or String
colored_terminal.set_fg(Color::from("Red"));
sourcepub fn set_bg(&self, color: Color) -> Result<(), ErrorKind>
pub fn set_bg(&self, color: Color) -> Result<(), ErrorKind>
Set the background color to the given color.
let colored_terminal = color();
// Set background color of the font
colored_terminal.set_bg(Color::Red);
// crossterm provides to set the background from &str or String
colored_terminal.set_bg(Color::from("Red"));
sourcepub fn reset(&self) -> Result<(), ErrorKind>
pub fn reset(&self) -> Result<(), ErrorKind>
Reset the terminal colors and attributes to default.
let colored_terminal = color();
colored_terminal.reset();
sourcepub fn get_available_color_count(&self) -> Result<u16>
pub fn get_available_color_count(&self) -> Result<u16>
Get available color count.