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
//! With this module you can perform actions that are color related. //! Like styling the font, foreground color and background. use super::*; use std::io; use Screen; /// Struct that stores an specific platform 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. /// /// /// ```rust /// use crossterm::{Screen} /// use crossterm::style::color; /// /// let screen = Screen::default(); /// let colored_terminal = color(&screen); /// /// // 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(); /// ``` pub struct TerminalColor<'stdout> { color: Box<ITerminalColor + Sync + Send>, stdout: &'stdout Arc<TerminalOutput>, } impl<'stdout> TerminalColor<'stdout> { /// Create new instance whereon color related actions can be performed. pub fn new(stdout: &'stdout Arc<TerminalOutput>) -> TerminalColor<'stdout> { #[cfg(target_os = "windows")] let color = functions::get_module::<Box<ITerminalColor + Sync + Send>>( Box::from(WinApiColor::new()), Box::from(AnsiColor::new()), ).unwrap(); #[cfg(not(target_os = "windows"))] let color = Box::from(AnsiColor::new()) as Box<ITerminalColor + Sync + Send>; TerminalColor { color, stdout: stdout, } } /// Set the foreground color to the given color. /// /// ```rust /// let screen = Screen::default(); /// let colored_terminal = color(&screen); /// /// // 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")); /// /// ``` pub fn set_fg(&self, color: Color) { self.color.set_fg(color, &self.stdout); } /// Set the background color to the given color. /// /// ```rust /// let screen = Screen::default(); /// let colored_terminal = color(&screen); /// /// // 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")); /// /// ``` pub fn set_bg(&self, color: Color) { self.color.set_bg(color, &self.stdout); } /// Reset the terminal colors and attributes to default. /// /// ```rust /// let screen = Screen::default(); /// let colored_terminal = color(&screen); /// colored_terminal.reset(); /// ``` pub fn reset(&self) { self.color.reset(&self.stdout); } /// Get available color count. pub fn get_available_color_count(&self) -> io::Result<u16> { use std::env; Ok(match env::var_os("TERM") { Some(val) => { if val.to_str().unwrap_or("").contains("256color") { 256 } else { 8 } } None => 8, }) } } /// Get an Terminal Color implementation whereon color related actions can be performed. /// Pass the reference to any screen you want this type to perform actions on. pub fn color<'stdout>(screen: &'stdout Screen) -> TerminalColor<'stdout> { TerminalColor::new(&screen.stdout) }