Skip to content

Commit

Permalink
Configure alias style in --list with --alias-style (#2342)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcaddeo authored Dec 12, 2024
1 parent c56b654 commit 3442c2b
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 101 deletions.
8 changes: 8 additions & 0 deletions src/alias_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::*;

#[derive(Debug, PartialEq, Clone, ValueEnum)]
pub(crate) enum AliasStyle {
Left,
Right,
Separate,
}
2 changes: 1 addition & 1 deletion src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
Rc::clone(next)
}),
}),
doc,
doc: doc.filter(|doc| !doc.is_empty()),
groups: groups.into(),
loaded: loaded.into(),
modules: self.modules,
Expand Down
126 changes: 65 additions & 61 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,6 +71,14 @@ impl Color {
self.restyle(Style::new().fg(Cyan))
}

fn effective_style(&self) -> Style {
if self.active() {
self.style
} else {
Style::new()
}
}

pub(crate) fn error(self) -> Self {
self.restyle(Style::new().fg(Red).bold())
}
Expand All @@ -77,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
16 changes: 16 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {

#[derive(Debug, PartialEq)]
pub(crate) struct Config {
pub(crate) alias_style: AliasStyle,
pub(crate) allow_missing: bool,
pub(crate) check: bool,
pub(crate) color: Color,
Expand Down Expand Up @@ -86,6 +87,7 @@ mod cmd {
}

mod arg {
pub(crate) const ALIAS_STYLE: &str = "ALIAS_STYLE";
pub(crate) const ALLOW_MISSING: &str = "ALLOW-MISSING";
pub(crate) const ARGUMENTS: &str = "ARGUMENTS";
pub(crate) const CHECK: &str = "CHECK";
Expand Down Expand Up @@ -145,6 +147,16 @@ impl Config {
.usage(AnsiColor::Yellow.on_default() | Effects::BOLD)
.valid(AnsiColor::Green.on_default()),
)
.arg(
Arg::new(arg::ALIAS_STYLE)
.long("alias-style")
.env("JUST_ALIAS_STYLE")
.action(ArgAction::Set)
.value_parser(clap::value_parser!(AliasStyle))
.default_value("right")
.help("Set list command alias display style")
.conflicts_with(arg::NO_ALIASES),
)
.arg(
Arg::new(arg::CHECK)
.long("check")
Expand Down Expand Up @@ -739,6 +751,10 @@ impl Config {
let explain = matches.get_flag(arg::EXPLAIN);

Ok(Self {
alias_style: matches
.get_one::<AliasStyle>(arg::ALIAS_STYLE)
.unwrap()
.clone(),
allow_missing: matches.get_flag(arg::ALLOW_MISSING),
check: matches.get_flag(arg::CHECK),
color: (*matches.get_one::<UseColor>(arg::COLOR).unwrap()).into(),
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
pub(crate) use {
crate::{
alias::Alias,
alias_style::AliasStyle,
analyzer::Analyzer,
argument_parser::ArgumentParser,
assignment::Assignment,
Expand Down Expand Up @@ -179,6 +180,7 @@ pub mod summary;
pub mod request;

mod alias;
mod alias_style;
mod analyzer;
mod argument_parser;
mod assignment;
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ impl<'run, 'src> Parser<'run, 'src> {
attributes,
body,
dependencies,
doc,
doc: doc.filter(|doc| !doc.is_empty()),
file_depth: self.file_depth,
import_offsets: self.import_offsets.clone(),
name,
Expand Down
Loading

0 comments on commit 3442c2b

Please sign in to comment.