Struct grep_printer::UserColorSpec

source ·
pub struct UserColorSpec { /* private fields */ }
Expand description

A single color specification provided by the user.

§Format

The format of a Spec is a triple: {type}:{attribute}:{value}. Each component is defined as follows:

  • {type} can be one of path, line, column or match.
  • {attribute} can be one of fg, bg or style. {attribute} may also be the special value none, in which case, {value} can be omitted.
  • {value} is either a color name (for fg/bg) or a style instruction.

{type} controls which part of the output should be styled.

When {attribute} is none, then this should cause any existing style settings to be cleared for the specified type.

{value} should be a color when {attribute} is fg or bg, or it should be a style instruction when {attribute} is style. When {attribute} is none, {value} must be omitted.

Valid colors are black, blue, green, red, cyan, magenta, yellow, white. Extended colors can also be specified, and are formatted as x (for 256-bit colors) or x,x,x (for 24-bit true color), where x is a number between 0 and 255 inclusive. x may be given as a normal decimal number of a hexadecimal number, where the latter is prefixed by 0x.

Valid style instructions are nobold, bold, intense, nointense, underline, nounderline.

§Example

The standard way to build a UserColorSpec is to parse it from a string. Once multiple UserColorSpecs have been constructed, they can be provided to the standard printer where they will automatically be applied to the output.

A UserColorSpec can also be converted to a termcolor::ColorSpec:

use termcolor::{Color, ColorSpec};
use grep_printer::UserColorSpec;

let user_spec1: UserColorSpec = "path:fg:blue".parse().unwrap();
let user_spec2: UserColorSpec = "match:bg:0xff,0x7f,0x00".parse().unwrap();

let spec1 = user_spec1.to_color_spec();
let spec2 = user_spec2.to_color_spec();

assert_eq!(spec1.fg(), Some(&Color::Blue));
assert_eq!(spec2.bg(), Some(&Color::Rgb(0xFF, 0x7F, 0x00)));

Implementations§

source§

impl UserColorSpec

source

pub fn to_color_spec(&self) -> ColorSpec

Convert this user provided color specification to a specification that can be used with termcolor. This drops the type of this specification (where the type indicates where the color is applied in the standard printer, e.g., to the file path or the line numbers, etc.).

Trait Implementations§

source§

impl Clone for UserColorSpec

source§

fn clone(&self) -> UserColorSpec

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for UserColorSpec

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl FromStr for UserColorSpec

source§

type Err = ColorError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<UserColorSpec, ColorError>

Parses a string s to return a value of this type. Read more
source§

impl PartialEq for UserColorSpec

source§

fn eq(&self, other: &UserColorSpec) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for UserColorSpec

source§

impl StructuralPartialEq for UserColorSpec

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.