Skip to content

Commit 3442c2b

Browse files
authored
Configure alias style in --list with --alias-style (#2342)
1 parent c56b654 commit 3442c2b

File tree

10 files changed

+306
-101
lines changed

10 files changed

+306
-101
lines changed

src/alias_style.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use super::*;
2+
3+
#[derive(Debug, PartialEq, Clone, ValueEnum)]
4+
pub(crate) enum AliasStyle {
5+
Left,
6+
Right,
7+
Separate,
8+
}

src/analyzer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
201201
Rc::clone(next)
202202
}),
203203
}),
204-
doc,
204+
doc: doc.filter(|doc| !doc.is_empty()),
205205
groups: groups.into(),
206206
loaded: loaded.into(),
207207
modules: self.modules,

src/color.rs

+65-61
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,16 @@ pub(crate) struct Color {
1212
}
1313

1414
impl Color {
15-
fn restyle(self, style: Style) -> Self {
16-
Self { style, ..self }
17-
}
18-
19-
fn redirect(self, stream: impl IsTerminal) -> Self {
20-
Self {
21-
is_terminal: stream.is_terminal(),
22-
..self
23-
}
24-
}
25-
26-
fn effective_style(&self) -> Style {
27-
if self.active() {
28-
self.style
29-
} else {
30-
Style::new()
15+
pub(crate) fn active(&self) -> bool {
16+
match self.use_color {
17+
UseColor::Always => true,
18+
UseColor::Never => false,
19+
UseColor::Auto => self.is_terminal,
3120
}
3221
}
3322

34-
pub(crate) fn auto() -> Self {
35-
Self::default()
23+
pub(crate) fn alias(self) -> Self {
24+
self.restyle(Style::new().fg(Purple))
3625
}
3726

3827
pub(crate) fn always() -> Self {
@@ -42,25 +31,38 @@ impl Color {
4231
}
4332
}
4433

45-
pub(crate) fn never() -> Self {
46-
Self {
47-
use_color: UseColor::Never,
48-
..Self::default()
49-
}
34+
pub(crate) fn annotation(self) -> Self {
35+
self.restyle(Style::new().fg(Purple))
5036
}
5137

52-
pub(crate) fn stderr(self) -> Self {
53-
self.redirect(io::stderr())
38+
pub(crate) fn auto() -> Self {
39+
Self::default()
5440
}
5541

56-
pub(crate) fn stdout(self) -> Self {
57-
self.redirect(io::stdout())
42+
pub(crate) fn banner(self) -> Self {
43+
self.restyle(Style::new().fg(Cyan).bold())
44+
}
45+
46+
pub(crate) fn command(self, foreground: Option<ansi_term::Color>) -> Self {
47+
self.restyle(Style {
48+
foreground,
49+
is_bold: true,
50+
..Style::default()
51+
})
5852
}
5953

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

58+
pub(crate) fn diff_added(self) -> Self {
59+
self.restyle(Style::new().fg(Green))
60+
}
61+
62+
pub(crate) fn diff_deleted(self) -> Self {
63+
self.restyle(Style::new().fg(Red))
64+
}
65+
6466
pub(crate) fn doc(self) -> Self {
6567
self.restyle(Style::new().fg(Blue))
6668
}
@@ -69,6 +71,14 @@ impl Color {
6971
self.restyle(Style::new().fg(Cyan))
7072
}
7173

74+
fn effective_style(&self) -> Style {
75+
if self.active() {
76+
self.style
77+
} else {
78+
Style::new()
79+
}
80+
}
81+
7282
pub(crate) fn error(self) -> Self {
7383
self.restyle(Style::new().fg(Red).bold())
7484
}
@@ -77,65 +87,59 @@ impl Color {
7787
self.restyle(Style::new().fg(Yellow).bold())
7888
}
7989

80-
pub(crate) fn warning(self) -> Self {
81-
self.restyle(Style::new().fg(Yellow).bold())
90+
pub(crate) fn message(self) -> Self {
91+
self.restyle(Style::new().bold())
8292
}
8393

84-
pub(crate) fn banner(self) -> Self {
85-
self.restyle(Style::new().fg(Cyan).bold())
94+
pub(crate) fn never() -> Self {
95+
Self {
96+
use_color: UseColor::Never,
97+
..Self::default()
98+
}
8699
}
87100

88-
pub(crate) fn command(self, foreground: Option<ansi_term::Color>) -> Self {
89-
self.restyle(Style {
90-
foreground,
91-
is_bold: true,
92-
..Style::default()
93-
})
101+
pub(crate) fn paint<'a>(&self, text: &'a str) -> ANSIGenericString<'a, str> {
102+
self.effective_style().paint(text)
94103
}
95104

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

100-
pub(crate) fn message(self) -> Self {
101-
self.restyle(Style::new().bold())
102-
}
103-
104-
pub(crate) fn annotation(self) -> Self {
105-
self.restyle(Style::new().fg(Purple))
106-
}
107-
108-
pub(crate) fn string(self) -> Self {
109-
self.restyle(Style::new().fg(Green))
109+
pub(crate) fn prefix(&self) -> Prefix {
110+
self.effective_style().prefix()
110111
}
111112

112-
pub(crate) fn diff_added(self) -> Self {
113-
self.restyle(Style::new().fg(Green))
113+
fn redirect(self, stream: impl IsTerminal) -> Self {
114+
Self {
115+
is_terminal: stream.is_terminal(),
116+
..self
117+
}
114118
}
115119

116-
pub(crate) fn diff_deleted(self) -> Self {
117-
self.restyle(Style::new().fg(Red))
120+
fn restyle(self, style: Style) -> Self {
121+
Self { style, ..self }
118122
}
119123

120-
pub(crate) fn active(&self) -> bool {
121-
match self.use_color {
122-
UseColor::Always => true,
123-
UseColor::Never => false,
124-
UseColor::Auto => self.is_terminal,
125-
}
124+
pub(crate) fn stderr(self) -> Self {
125+
self.redirect(io::stderr())
126126
}
127127

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

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

136136
pub(crate) fn suffix(&self) -> Suffix {
137137
self.effective_style().suffix()
138138
}
139+
140+
pub(crate) fn warning(self) -> Self {
141+
self.restyle(Style::new().fg(Yellow).bold())
142+
}
139143
}
140144

141145
impl From<UseColor> for Color {

src/config.rs

+16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use {
1212

1313
#[derive(Debug, PartialEq)]
1414
pub(crate) struct Config {
15+
pub(crate) alias_style: AliasStyle,
1516
pub(crate) allow_missing: bool,
1617
pub(crate) check: bool,
1718
pub(crate) color: Color,
@@ -86,6 +87,7 @@ mod cmd {
8687
}
8788

8889
mod arg {
90+
pub(crate) const ALIAS_STYLE: &str = "ALIAS_STYLE";
8991
pub(crate) const ALLOW_MISSING: &str = "ALLOW-MISSING";
9092
pub(crate) const ARGUMENTS: &str = "ARGUMENTS";
9193
pub(crate) const CHECK: &str = "CHECK";
@@ -145,6 +147,16 @@ impl Config {
145147
.usage(AnsiColor::Yellow.on_default() | Effects::BOLD)
146148
.valid(AnsiColor::Green.on_default()),
147149
)
150+
.arg(
151+
Arg::new(arg::ALIAS_STYLE)
152+
.long("alias-style")
153+
.env("JUST_ALIAS_STYLE")
154+
.action(ArgAction::Set)
155+
.value_parser(clap::value_parser!(AliasStyle))
156+
.default_value("right")
157+
.help("Set list command alias display style")
158+
.conflicts_with(arg::NO_ALIASES),
159+
)
148160
.arg(
149161
Arg::new(arg::CHECK)
150162
.long("check")
@@ -739,6 +751,10 @@ impl Config {
739751
let explain = matches.get_flag(arg::EXPLAIN);
740752

741753
Ok(Self {
754+
alias_style: matches
755+
.get_one::<AliasStyle>(arg::ALIAS_STYLE)
756+
.unwrap()
757+
.clone(),
742758
allow_missing: matches.get_flag(arg::ALLOW_MISSING),
743759
check: matches.get_flag(arg::CHECK),
744760
color: (*matches.get_one::<UseColor>(arg::COLOR).unwrap()).into(),

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
pub(crate) use {
88
crate::{
99
alias::Alias,
10+
alias_style::AliasStyle,
1011
analyzer::Analyzer,
1112
argument_parser::ArgumentParser,
1213
assignment::Assignment,
@@ -179,6 +180,7 @@ pub mod summary;
179180
pub mod request;
180181

181182
mod alias;
183+
mod alias_style;
182184
mod analyzer;
183185
mod argument_parser;
184186
mod assignment;

src/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ impl<'run, 'src> Parser<'run, 'src> {
952952
attributes,
953953
body,
954954
dependencies,
955-
doc,
955+
doc: doc.filter(|doc| !doc.is_empty()),
956956
file_depth: self.file_depth,
957957
import_offsets: self.import_offsets.clone(),
958958
name,

0 commit comments

Comments
 (0)