pub struct ColouredStr<'a> {
pub string: &'a str,
pub coloured_string: String,
/* private fields */
}Expand description
This struct is the main way to utilize the coloring sceme in this crate.
This struct contains a number of parameters inquiring to the strings state, such as style,color,base string, reset actions, background and several internal things. this structs, as it say’s below implements the fmt::Display trait, that means that you will not need to do anything special in order to print after you have changed the color you can use only one color but you can have even all the styles together!
here is a blue,bold,underlined and blinking string example(just a fromatted output) :
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.blue();
str1.bold();
str1.underline();
str1.blink();
let str2 = format!("{}{}{}","\u{1b}[1;4;5;49;34m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);here is a white,bold and hidden string example(good for passwords):
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.white();
str1.bold();
str1.hidden();
let str2 = format!("{}{}{}","\u{1b}[1;8;49;97m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Fields§
§string: &'a str§coloured_string: StringImplementations§
Source§impl<'a> ColouredStr<'a>
impl<'a> ColouredStr<'a>
Sourcepub fn new(string: &str) -> ColouredStr<'_>
pub fn new(string: &str) -> ColouredStr<'_>
This functions recieves one parameter - a string and creates a defult struct for it. the color is natural(no change), as well as the styles and background.
use ansi_colors::*;
let str1 = ColouredStr::new("hello world");
assert_eq!(str1.string,"hello world");Examples found in repository?
More examples
Sourcepub fn to_error(&mut self)
pub fn to_error(&mut self)
This functions takes self and presents the color string in error format
Sourcepub fn to_success(&mut self)
pub fn to_success(&mut self)
This functions takes self and presents the color string in success format
Sourcepub fn to_password(&mut self)
pub fn to_password(&mut self)
This functions takes self and presents the color string in password format
Sourcepub fn blue(&mut self) -> Self
pub fn blue(&mut self) -> Self
This functions takes self and changes coloured string color into blue
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.blue();
let str2 = format!("{}{}{}","\u{1b}[49;34m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn black(&mut self)
pub fn black(&mut self)
This functions takes self and changes coloured string color into black
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.black();
let str2 = format!("{}{}{}","\u{1b}[49;30m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn red(&mut self)
pub fn red(&mut self)
This functions takes self and changes coloured string color into red
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.red();
let str2 = format!("{}{}{}","\u{1b}[49;31m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn green(&mut self)
pub fn green(&mut self)
This functions takes self and changes coloured string color into green
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.green();
let str2 = format!("{}{}{}","\u{1b}[49;32m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn yellow(&mut self)
pub fn yellow(&mut self)
This functions takes self and changes coloured string color into yellow
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.yellow();
let str2 = format!("{}{}{}","\u{1b}[49;33m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn magenta(&mut self)
pub fn magenta(&mut self)
This functions takes self and changes coloured string color into magenta
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.magenta();
let str2 = format!("{}{}{}","\u{1b}[49;35m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn cyan(&mut self)
pub fn cyan(&mut self)
This functions takes self and changes coloured string color into cyan
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.cyan();
let str2 = format!("{}{}{}","\u{1b}[49;36m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn gray(&mut self)
pub fn gray(&mut self)
This functions takes self and changes coloured string color into gray
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.gray();
let str2 = format!("{}{}{}","\u{1b}[49;37m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn dark_gray(&mut self)
pub fn dark_gray(&mut self)
This functions takes self and changes coloured string color into dark gray
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.dark_gray();
let str2 = format!("{}{}{}","\u{1b}[49;90m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn light_red(&mut self)
pub fn light_red(&mut self)
This functions takes self and changes coloured string color into light red
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.light_red();
let str2 = format!("{}{}{}","\u{1b}[49;91m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn light_green(&mut self)
pub fn light_green(&mut self)
This functions takes self and changes coloured string color into light green
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.light_green();
let str2 = format!("{}{}{}","\u{1b}[49;92m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn light_yellow(&mut self)
pub fn light_yellow(&mut self)
This functions takes self and changes coloured string color into light yellow
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.light_yellow();
let str2 = format!("{}{}{}","\u{1b}[49;93m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn light_blue(&mut self)
pub fn light_blue(&mut self)
This functions takes self and changes coloured string color into light blue
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.light_blue();
let str2 = format!("{}{}{}","\u{1b}[49;94m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn pink(&mut self)
pub fn pink(&mut self)
This functions takes self and changes coloured string color into pink
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.pink();
let str2 = format!("{}{}{}","\u{1b}[49;95m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn light_cyan(&mut self)
pub fn light_cyan(&mut self)
This functions takes self and changes coloured string color into light cyan
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.light_cyan();
let str2 = format!("{}{}{}","\u{1b}[49;96m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn white(&mut self)
pub fn white(&mut self)
This functions takes self and changes coloured string color into white
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.white();
let str2 = format!("{}{}{}","\u{1b}[49;97m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn none(&mut self)
pub fn none(&mut self)
This functions takes self and resets the string’s color
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.none();
let str2 = format!("{}{}{}","\u{1b}[49;39m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn bold(&mut self) -> Self
pub fn bold(&mut self) -> Self
This functions takes self and adds the bold style to the string
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.bold();
let str2 = format!("{}{}{}","\u{1b}[1;49;97m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Examples found in repository?
More examples
Sourcepub fn dim(&mut self)
pub fn dim(&mut self)
This functions takes self and adds the dim style to the string
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.dim();
let str2 = format!("{}{}{}","\u{1b}[2;49;97m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn underline(&mut self) -> Self
pub fn underline(&mut self) -> Self
This functions takes self and adds the underline style to the string
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.underline();
let str2 = format!("{}{}{}","\u{1b}[4;49;97m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn blink(&mut self)
pub fn blink(&mut self)
This functions takes self and adds the blinking style to the string
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.blink();
let str2 = format!("{}{}{}","\u{1b}[5;49;97m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn reverse(&mut self)
pub fn reverse(&mut self)
This functions takes self and adds the reverse style to the string
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.reverse();
let str2 = format!("{}{}{}","\u{1b}[7;49;97m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);This functions takes self and adds the hidden( ) style to the string
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.hidden();
let str2 = format!("{}{}{}","\u{1b}[8;49;97m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_black(&mut self)
pub fn back_black(&mut self)
This functions takes self and changes the background to be black
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.white();
str1.back_black();
let str2 = format!("{}{}{}","\u{1b}[40;97m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_red(&mut self)
pub fn back_red(&mut self)
This functions takes self and changes the background to be red
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.blue();
str1.back_red();
let str2 = format!("{}{}{}","\u{1b}[41;34m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_blue(&mut self)
pub fn back_blue(&mut self)
This functions takes self and changes the background to be blue
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.red();
str1.back_blue();
let str2 = format!("{}{}{}","\u{1b}[44;31m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_green(&mut self)
pub fn back_green(&mut self)
This functions takes self and changes the background to be green
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.red();
str1.back_green();
let str2 = format!("{}{}{}","\u{1b}[42;31m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_yellow(&mut self)
pub fn back_yellow(&mut self)
This functions takes self and changes the background to be yellow
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.magenta();
str1.back_yellow();
let str2 = format!("{}{}{}","\u{1b}[43;35m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_magenta(&mut self)
pub fn back_magenta(&mut self)
This functions takes self and changes the background to be magenta
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.black();
str1.back_magenta();
let str2 = format!("{}{}{}","\u{1b}[45;30m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_cyan(&mut self)
pub fn back_cyan(&mut self)
This functions takes self and changes the background to be cyan
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.red();
str1.back_cyan();
let str2 = format!("{}{}{}","\u{1b}[46;31m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_gray(&mut self)
pub fn back_gray(&mut self)
This functions takes self and changes the background to be gray
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.yellow();
str1.back_gray();
let str2 = format!("{}{}{}","\u{1b}[47;33m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_dark_gray(&mut self)
pub fn back_dark_gray(&mut self)
This functions takes self and changes the background to be dark gray
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.white();
str1.back_dark_gray();
let str2 = format!("{}{}{}","\u{1b}[100;97m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_light_red(&mut self)
pub fn back_light_red(&mut self)
This functions takes self and changes the background to be light red
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.black();
str1.back_light_red();
let str2 = format!("{}{}{}","\u{1b}[101;30m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_light_green(&mut self)
pub fn back_light_green(&mut self)
This functions takes self and changes the background to be light green
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.red();
str1.back_light_green();
let str2 = format!("{}{}{}","\u{1b}[102;31m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_light_yellow(&mut self)
pub fn back_light_yellow(&mut self)
This functions takes self and changes the background to be light yellow
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.blue();
str1.back_light_yellow();
let str2 = format!("{}{}{}","\u{1b}[103;34m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_light_blue(&mut self)
pub fn back_light_blue(&mut self)
This functions takes self and changes the background to be light blue
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.black();
str1.back_light_blue();
let str2 = format!("{}{}{}","\u{1b}[104;30m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_pink(&mut self)
pub fn back_pink(&mut self)
This functions takes self and changes the background to be pink
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.black();
str1.back_pink();
let str2 = format!("{}{}{}","\u{1b}[105;30m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_light_cyan(&mut self)
pub fn back_light_cyan(&mut self)
This functions takes self and changes the background to be light cyan
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.red();
str1.back_light_green();
let str2 = format!("{}{}{}","\u{1b}[102;31m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_white(&mut self)
pub fn back_white(&mut self)
This functions takes self and changes the background to be white
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.black();
str1.back_white();
let str2 = format!("{}{}{}","\u{1b}[107;30m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn back_none(&mut self)
pub fn back_none(&mut self)
This functions takes self and changes the background to be none
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.red();
str1.back_none();
let str2 = format!("{}{}{}","\u{1b}[49;31m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn reset_all(&mut self)
pub fn reset_all(&mut self)
This functions takes self and adds the reset all operation to the reset
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.blink();
str1.reset_all();
let str2 = format!("{}{}{}","\u{1b}[5;49;97m","hello world","\u{1b}[0m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn reset_bold(&mut self)
pub fn reset_bold(&mut self)
This functions takes self and adds the reset bold operation to the reset it means it resets only the bold style, not any other active styles or colors.
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.bold();
str1.red();
str1.reset_bold();
let str2 = format!("{}{}{}","\u{1b}[1;49;31m","hello world","\u{1b}[21m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn reset_dim(&mut self)
pub fn reset_dim(&mut self)
This functions takes self and adds the reset dim operation to the reset it means it resets only the dim style, not any other active styles or colors.
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.dim();
str1.bold();
str1.reset_dim();
let str2 = format!("{}{}{}","\u{1b}[2;1;49;97m","hello world","\u{1b}[22m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn reset_underline(&mut self)
pub fn reset_underline(&mut self)
This functions takes self and adds the reset underline operation to the reset it means it resets only the underline style, not any other active styles or colors.
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.underline();
str1.back_green();
str1.reset_underline();
let str2 = format!("{}{}{}","\u{1b}[4;42;97m","hello world","\u{1b}[24m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn reset_blink(&mut self)
pub fn reset_blink(&mut self)
This functions takes self and adds the reset blink operation to the reset it means it resets only the blink style, not any other active styles or colors.
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.blink();
str1.red();
str1.reset_blink();
let str2 = format!("{}{}{}","\u{1b}[5;49;31m","hello world","\u{1b}[25m");
assert_eq!(str1.coloured_string,str2);Sourcepub fn reset_reverse(&mut self)
pub fn reset_reverse(&mut self)
This functions takes self and adds the reset reverse operation to the reset it means it resets only the reverse style, not any other active styles or colors.
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.reverse();
str1.back_red();
str1.reset_reverse();
let str2 = format!("{}{}{}","\u{1b}[7;41;97m","hello world","\u{1b}[27m");
assert_eq!(str1.coloured_string,str2);This functions takes self and adds the reset hidden operation to the reset it means it resets only the hidden style, not any other active styles or colors.
use ansi_colors::*;
let mut str1 = ColouredStr::new("hello world");
str1.hidden();
str1.back_black();
str1.reset_hidden();
let str2 = format!("{}{}{}","\u{1b}[8;40;97m","hello world","\u{1b}[28m");
assert_eq!(str1.coloured_string,str2);Trait Implementations§
Source§impl<'a> Clone for ColouredStr<'a>
impl<'a> Clone for ColouredStr<'a>
Source§fn clone(&self) -> ColouredStr<'a>
fn clone(&self) -> ColouredStr<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more