Skip to content

Commit

Permalink
Sort color functions
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Dec 12, 2024
1 parent 0b518b2 commit 7df991d
Showing 1 changed file with 63 additions and 63 deletions.
126 changes: 63 additions & 63 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,16 @@ pub(crate) struct Color {
}

impl Color {
fn restyle(self, style: Style) -> Self {
Self { style, ..self }
}

fn redirect(self, stream: impl IsTerminal) -> Self {
Self {
is_terminal: stream.is_terminal(),
..self
}
}

fn effective_style(&self) -> Style {
if self.active() {
self.style
} else {
Style::new()
pub(crate) fn active(&self) -> bool {
match self.use_color {
UseColor::Always => true,
UseColor::Never => false,
UseColor::Auto => self.is_terminal,
}
}

pub(crate) fn auto() -> Self {
Self::default()
pub(crate) fn alias(self) -> Self {
self.restyle(Style::new().fg(Purple))
}

pub(crate) fn always() -> Self {
Expand All @@ -42,25 +31,38 @@ impl Color {
}
}

pub(crate) fn never() -> Self {
Self {
use_color: UseColor::Never,
..Self::default()
}
pub(crate) fn annotation(self) -> Self {
self.restyle(Style::new().fg(Purple))
}

pub(crate) fn stderr(self) -> Self {
self.redirect(io::stderr())
pub(crate) fn auto() -> Self {
Self::default()
}

pub(crate) fn stdout(self) -> Self {
self.redirect(io::stdout())
pub(crate) fn banner(self) -> Self {
self.restyle(Style::new().fg(Cyan).bold())
}

pub(crate) fn command(self, foreground: Option<ansi_term::Color>) -> Self {
self.restyle(Style {
foreground,
is_bold: true,
..Style::default()
})
}

pub(crate) fn context(self) -> Self {
self.restyle(Style::new().fg(Blue).bold())
}

pub(crate) fn diff_added(self) -> Self {
self.restyle(Style::new().fg(Green))
}

pub(crate) fn diff_deleted(self) -> Self {
self.restyle(Style::new().fg(Red))
}

pub(crate) fn doc(self) -> Self {
self.restyle(Style::new().fg(Blue))
}
Expand All @@ -69,8 +71,12 @@ impl Color {
self.restyle(Style::new().fg(Cyan))
}

pub(crate) fn alias(self) -> Self {
self.restyle(Style::new().fg(Purple))
fn effective_style(&self) -> Style {
if self.active() {
self.style
} else {
Style::new()
}
}

pub(crate) fn error(self) -> Self {
Expand All @@ -81,65 +87,59 @@ impl Color {
self.restyle(Style::new().fg(Yellow).bold())
}

pub(crate) fn warning(self) -> Self {
self.restyle(Style::new().fg(Yellow).bold())
pub(crate) fn message(self) -> Self {
self.restyle(Style::new().bold())
}

pub(crate) fn banner(self) -> Self {
self.restyle(Style::new().fg(Cyan).bold())
pub(crate) fn never() -> Self {
Self {
use_color: UseColor::Never,
..Self::default()
}
}

pub(crate) fn command(self, foreground: Option<ansi_term::Color>) -> Self {
self.restyle(Style {
foreground,
is_bold: true,
..Style::default()
})
pub(crate) fn paint<'a>(&self, text: &'a str) -> ANSIGenericString<'a, str> {
self.effective_style().paint(text)
}

pub(crate) fn parameter(self) -> Self {
self.restyle(Style::new().fg(Cyan))
}

pub(crate) fn message(self) -> Self {
self.restyle(Style::new().bold())
}

pub(crate) fn annotation(self) -> Self {
self.restyle(Style::new().fg(Purple))
}

pub(crate) fn string(self) -> Self {
self.restyle(Style::new().fg(Green))
pub(crate) fn prefix(&self) -> Prefix {
self.effective_style().prefix()
}

pub(crate) fn diff_added(self) -> Self {
self.restyle(Style::new().fg(Green))
fn redirect(self, stream: impl IsTerminal) -> Self {
Self {
is_terminal: stream.is_terminal(),
..self
}
}

pub(crate) fn diff_deleted(self) -> Self {
self.restyle(Style::new().fg(Red))
fn restyle(self, style: Style) -> Self {
Self { style, ..self }
}

pub(crate) fn active(&self) -> bool {
match self.use_color {
UseColor::Always => true,
UseColor::Never => false,
UseColor::Auto => self.is_terminal,
}
pub(crate) fn stderr(self) -> Self {
self.redirect(io::stderr())
}

pub(crate) fn paint<'a>(&self, text: &'a str) -> ANSIGenericString<'a, str> {
self.effective_style().paint(text)
pub(crate) fn stdout(self) -> Self {
self.redirect(io::stdout())
}

pub(crate) fn prefix(&self) -> Prefix {
self.effective_style().prefix()
pub(crate) fn string(self) -> Self {
self.restyle(Style::new().fg(Green))
}

pub(crate) fn suffix(&self) -> Suffix {
self.effective_style().suffix()
}

pub(crate) fn warning(self) -> Self {
self.restyle(Style::new().fg(Yellow).bold())
}
}

impl From<UseColor> for Color {
Expand Down

0 comments on commit 7df991d

Please sign in to comment.