Skip to content

Commit 9da3a46

Browse files
authored
Merge pull request ogham#607 from de-vri-es/customize-size-scale-colours
Allow customizing size scale colours.
2 parents 6d8f690 + eaece8c commit 9da3a46

File tree

5 files changed

+180
-66
lines changed

5 files changed

+180
-66
lines changed

contrib/man/exa.1

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,29 @@ glob, including keys that happen to be two letters long.
341341
.IP \[bu] 2
342342
\f[B]xa\f[], the extended attribute indicator
343343
.IP \[bu] 2
344-
\f[B]sn\f[], the numbers of a file\[aq]s size
344+
\f[B]sn\f[], the numbers of a file\[aq]s size (sets nb, nk, nm, ng and nh)
345345
.IP \[bu] 2
346-
\f[B]sb\f[], the units of a file\[aq]s size
346+
\f[B]nb\f[], the numbers of a file\[aq]s size if it is lower than 1 KB/Kib
347+
.IP \[bu] 2
348+
\f[B]nk\f[], the numbers of a file\[aq]s size if it is between 1 KB/KiB and 1 MB/MiB
349+
.IP \[bu] 2
350+
\f[B]nm\f[], the numbers of a file\[aq]s size if it is between 1 MB/MiB and 1 GB/GiB
351+
.IP \[bu] 2
352+
\f[B]ng\f[], the numbers of a file\[aq]s size if it is between 1 GB/GiB and 1 TB/TiB
353+
.IP \[bu] 2
354+
\f[B]nt\f[], the numbers of a file\[aq]s size if it is 1 TB/TiB or higher
355+
.IP \[bu] 2
356+
\f[B]sb\f[], the units of a file\[aq]s size (sets ub, uk, um, ug and uh)
357+
.IP \[bu] 2
358+
\f[B]ub\f[], the units of a file\[aq]s size if it is lower than 1 KB/Kib
359+
.IP \[bu] 2
360+
\f[B]uk\f[], the units of a file\[aq]s size if it is between 1 KB/KiB and 1 MB/MiB
361+
.IP \[bu] 2
362+
\f[B]um\f[], the units of a file\[aq]s size if it is between 1 MB/MiB and 1 GB/GiB
363+
.IP \[bu] 2
364+
\f[B]ug\f[], the units of a file\[aq]s size if it is between 1 GB/GiB and 1 TB/TiB
365+
.IP \[bu] 2
366+
\f[B]ut\f[], the units of a file\[aq]s size if it is 1 TB/TiB or higher
347367
.IP \[bu] 2
348368
\f[B]df\f[], a device\[aq]s major ID
349369
.IP \[bu] 2

src/options/style.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,15 @@ mod colour_test {
325325
test!(width_7: [], || Some(80); Both => Ok(Colours::colourful(false)));
326326
test!(width_8: [], || None; Both => Ok(Colours::plain()));
327327

328-
test!(scale_1: ["--color=always", "--color-scale", "--colour-scale"], || None; Last => like Ok(Colours { scale: true, .. }));
329-
test!(scale_2: ["--color=always", "--color-scale", ], || None; Last => like Ok(Colours { scale: true, .. }));
330-
test!(scale_3: ["--color=always", "--colour-scale"], || None; Last => like Ok(Colours { scale: true, .. }));
331-
test!(scale_4: ["--color=always", ], || None; Last => like Ok(Colours { scale: false, .. }));
328+
test!(scale_1: ["--color=always", "--color-scale", "--colour-scale"], || None; Last => Ok(Colours::colourful(true)));
329+
test!(scale_2: ["--color=always", "--color-scale", ], || None; Last => Ok(Colours::colourful(true)));
330+
test!(scale_3: ["--color=always", "--colour-scale"], || None; Last => Ok(Colours::colourful(true)));
331+
test!(scale_4: ["--color=always", ], || None; Last => Ok(Colours::colourful(false)));
332332

333333
test!(scale_5: ["--color=always", "--color-scale", "--colour-scale"], || None; Complain => err Misfire::Duplicate(Flag::Long("color-scale"), Flag::Long("colour-scale")));
334-
test!(scale_6: ["--color=always", "--color-scale", ], || None; Complain => like Ok(Colours { scale: true, .. }));
335-
test!(scale_7: ["--color=always", "--colour-scale"], || None; Complain => like Ok(Colours { scale: true, .. }));
336-
test!(scale_8: ["--color=always", ], || None; Complain => like Ok(Colours { scale: false, .. }));
334+
test!(scale_6: ["--color=always", "--color-scale", ], || None; Complain => Ok(Colours::colourful(true)));
335+
test!(scale_7: ["--color=always", "--colour-scale"], || None; Complain => Ok(Colours::colourful(true)));
336+
test!(scale_8: ["--color=always", ], || None; Complain => Ok(Colours::colourful(false)));
337337
}
338338

339339

@@ -463,8 +463,33 @@ mod customs_test {
463463
test!(exa_sf: ls "", exa "sf=38;5;111" => colours c -> { c.perms.special_other = Fixed(111).normal(); });
464464
test!(exa_xa: ls "", exa "xa=38;5;112" => colours c -> { c.perms.attribute = Fixed(112).normal(); });
465465

466-
test!(exa_sn: ls "", exa "sn=38;5;113" => colours c -> { c.size.numbers = Fixed(113).normal(); });
467-
test!(exa_sb: ls "", exa "sb=38;5;114" => colours c -> { c.size.unit = Fixed(114).normal(); });
466+
test!(exa_sn: ls "", exa "sn=38;5;113" => colours c -> {
467+
c.size.number_byte = Fixed(113).normal();
468+
c.size.number_kilo = Fixed(113).normal();
469+
c.size.number_mega = Fixed(113).normal();
470+
c.size.number_giga = Fixed(113).normal();
471+
c.size.number_huge = Fixed(113).normal();
472+
});
473+
test!(exa_sb: ls "", exa "sb=38;5;114" => colours c -> {
474+
c.size.unit_byte = Fixed(114).normal();
475+
c.size.unit_kilo = Fixed(114).normal();
476+
c.size.unit_mega = Fixed(114).normal();
477+
c.size.unit_giga = Fixed(114).normal();
478+
c.size.unit_huge = Fixed(114).normal();
479+
});
480+
481+
test!(exa_nb: ls "", exa "nb=38;5;115" => colours c -> { c.size.number_byte = Fixed(115).normal(); });
482+
test!(exa_nk: ls "", exa "nk=38;5;116" => colours c -> { c.size.number_kilo = Fixed(116).normal(); });
483+
test!(exa_nm: ls "", exa "nm=38;5;117" => colours c -> { c.size.number_mega = Fixed(117).normal(); });
484+
test!(exa_ng: ls "", exa "ng=38;5;118" => colours c -> { c.size.number_giga = Fixed(118).normal(); });
485+
test!(exa_nh: ls "", exa "nh=38;5;119" => colours c -> { c.size.number_huge = Fixed(119).normal(); });
486+
487+
test!(exa_ub: ls "", exa "ub=38;5;115" => colours c -> { c.size.unit_byte = Fixed(115).normal(); });
488+
test!(exa_uk: ls "", exa "uk=38;5;116" => colours c -> { c.size.unit_kilo = Fixed(116).normal(); });
489+
test!(exa_um: ls "", exa "um=38;5;117" => colours c -> { c.size.unit_mega = Fixed(117).normal(); });
490+
test!(exa_ug: ls "", exa "ug=38;5;118" => colours c -> { c.size.unit_giga = Fixed(118).normal(); });
491+
test!(exa_uh: ls "", exa "uh=38;5;119" => colours c -> { c.size.unit_huge = Fixed(119).normal(); });
492+
468493
test!(exa_df: ls "", exa "df=38;5;115" => colours c -> { c.size.major = Fixed(115).normal(); });
469494
test!(exa_ds: ls "", exa "ds=38;5;116" => colours c -> { c.size.minor = Fixed(116).normal(); });
470495

src/output/render/size.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use ansi_term::Style;
22
use locale::Numeric as NumericLocale;
3+
use number_prefix::Prefix;
34

45
use crate::fs::fields as f;
56
use crate::output::cell::{TextCell, DisplayWidth};
67
use crate::output::table::SizeFormat;
78

89

9-
1010
impl f::Size {
1111
pub fn render<C: Colours>(&self, colours: &C, size_format: SizeFormat, numerics: &NumericLocale) -> TextCell {
1212
use number_prefix::{Prefixed, Standalone, NumberPrefix, PrefixNames};
@@ -21,13 +21,19 @@ impl f::Size {
2121
SizeFormat::DecimalBytes => NumberPrefix::decimal(size as f64),
2222
SizeFormat::BinaryBytes => NumberPrefix::binary(size as f64),
2323
SizeFormat::JustBytes => {
24+
// Use the binary prefix to select a style.
25+
let prefix = match NumberPrefix::binary(size as f64) {
26+
Standalone(_) => None,
27+
Prefixed(p, _) => Some(p),
28+
};
29+
// But format the number directly using the locale.
2430
let string = numerics.format_int(size);
25-
return TextCell::paint(colours.size(size), string);
31+
return TextCell::paint(colours.size(prefix), string);
2632
},
2733
};
2834

2935
let (prefix, n) = match result {
30-
Standalone(b) => return TextCell::paint(colours.size(b as u64), b.to_string()),
36+
Standalone(b) => return TextCell::paint(colours.size(None), b.to_string()),
3137
Prefixed(p, n) => (p, n)
3238
};
3339

@@ -42,8 +48,8 @@ impl f::Size {
4248
TextCell {
4349
width,
4450
contents: vec![
45-
colours.size(size).paint(number),
46-
colours.unit().paint(symbol),
51+
colours.size(Some(prefix)).paint(number),
52+
colours.unit(Some(prefix)).paint(symbol),
4753
].into(),
4854
}
4955
}
@@ -66,10 +72,9 @@ impl f::DeviceIDs {
6672
}
6773
}
6874

69-
7075
pub trait Colours {
71-
fn size(&self, size: u64) -> Style;
72-
fn unit(&self) -> Style;
76+
fn size(&self, prefix: Option<Prefix>) -> Style;
77+
fn unit(&self, prefix: Option<Prefix>) -> Style;
7378
fn no_size(&self) -> Style;
7479

7580
fn major(&self) -> Style;
@@ -88,13 +93,14 @@ pub mod test {
8893
use locale::Numeric as NumericLocale;
8994
use ansi_term::Colour::*;
9095
use ansi_term::Style;
96+
use number_prefix::Prefix;
9197

9298

9399
struct TestColours;
94100

95101
impl Colours for TestColours {
96-
fn size(&self, _size: u64) -> Style { Fixed(66).normal() }
97-
fn unit(&self) -> Style { Fixed(77).bold() }
102+
fn size(&self, _prefix: Option<Prefix>) -> Style { Fixed(66).normal() }
103+
fn unit(&self, _prefix: Option<Prefix>) -> Style { Fixed(77).bold() }
98104
fn no_size(&self) -> Style { Black.italic() }
99105

100106
fn major(&self) -> Style { Blue.on(Red) }

src/style/colours.rs

Lines changed: 107 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::style::lsc::Pair;
1010
#[derive(Debug, Default, PartialEq)]
1111
pub struct Colours {
1212
pub colourful: bool,
13-
pub scale: bool,
1413

1514
pub filekinds: FileKinds,
1615
pub perms: Permissions,
@@ -67,17 +66,20 @@ pub struct Permissions {
6766

6867
#[derive(Clone, Copy, Debug, Default, PartialEq)]
6968
pub struct Size {
70-
pub numbers: Style,
71-
pub unit: Style,
72-
7369
pub major: Style,
7470
pub minor: Style,
7571

76-
pub scale_byte: Style,
77-
pub scale_kilo: Style,
78-
pub scale_mega: Style,
79-
pub scale_giga: Style,
80-
pub scale_huge: Style,
72+
pub number_byte: Style,
73+
pub number_kilo: Style,
74+
pub number_mega: Style,
75+
pub number_giga: Style,
76+
pub number_huge: Style,
77+
78+
pub unit_byte: Style,
79+
pub unit_kilo: Style,
80+
pub unit_mega: Style,
81+
pub unit_giga: Style,
82+
pub unit_huge: Style,
8183
}
8284

8385
#[derive(Clone, Copy, Debug, Default, PartialEq)]
@@ -112,7 +114,6 @@ impl Colours {
112114
pub fn colourful(scale: bool) -> Colours {
113115
Colours {
114116
colourful: true,
115-
scale,
116117

117118
filekinds: FileKinds {
118119
normal: Style::default(),
@@ -146,19 +147,7 @@ impl Colours {
146147
attribute: Style::default(),
147148
},
148149

149-
size: Size {
150-
numbers: Green.bold(),
151-
unit: Green.normal(),
152-
153-
major: Green.bold(),
154-
minor: Green.normal(),
155-
156-
scale_byte: Fixed(118).normal(),
157-
scale_kilo: Fixed(190).normal(),
158-
scale_mega: Fixed(226).normal(),
159-
scale_giga: Fixed(220).normal(),
160-
scale_huge: Fixed(214).normal(),
161-
},
150+
size: Size::colourful(scale),
162151

163152
users: Users {
164153
user_you: Yellow.bold(),
@@ -195,6 +184,55 @@ impl Colours {
195184
}
196185
}
197186

187+
impl Size {
188+
pub fn colourful(scale: bool) -> Self {
189+
if scale {
190+
Self::colourful_scale()
191+
} else {
192+
Self::colourful_plain()
193+
}
194+
}
195+
196+
fn colourful_plain() -> Self {
197+
Self {
198+
major: Green.bold(),
199+
minor: Green.normal(),
200+
201+
number_byte: Green.bold(),
202+
number_kilo: Green.bold(),
203+
number_mega: Green.bold(),
204+
number_giga: Green.bold(),
205+
number_huge: Green.bold(),
206+
207+
unit_byte: Green.normal(),
208+
unit_kilo: Green.normal(),
209+
unit_mega: Green.normal(),
210+
unit_giga: Green.normal(),
211+
unit_huge: Green.normal(),
212+
}
213+
}
214+
215+
fn colourful_scale() -> Self {
216+
Self {
217+
major: Green.bold(),
218+
minor: Green.normal(),
219+
220+
number_byte: Fixed(118).normal(),
221+
number_kilo: Fixed(190).normal(),
222+
number_mega: Fixed(226).normal(),
223+
number_giga: Fixed(220).normal(),
224+
number_huge: Fixed(214).normal(),
225+
226+
unit_byte: Green.normal(),
227+
unit_kilo: Green.normal(),
228+
unit_mega: Green.normal(),
229+
unit_giga: Green.normal(),
230+
unit_huge: Green.normal(),
231+
}
232+
233+
}
234+
}
235+
198236

199237
/// Some of the styles are **overlays**: although they have the same attribute
200238
/// set as regular styles (foreground and background colours, bold, underline,
@@ -270,8 +308,18 @@ impl Colours {
270308
"sf" => self.perms.special_other = pair.to_style(),
271309
"xa" => self.perms.attribute = pair.to_style(),
272310

273-
"sn" => self.size.numbers = pair.to_style(),
274-
"sb" => self.size.unit = pair.to_style(),
311+
"sn" => self.set_number_style(pair.to_style()),
312+
"sb" => self.set_unit_style(pair.to_style()),
313+
"nb" => self.size.number_byte = pair.to_style(),
314+
"nk" => self.size.number_kilo = pair.to_style(),
315+
"nm" => self.size.number_mega = pair.to_style(),
316+
"ng" => self.size.number_giga = pair.to_style(),
317+
"nh" => self.size.number_huge = pair.to_style(),
318+
"ub" => self.size.unit_byte = pair.to_style(),
319+
"uk" => self.size.unit_kilo = pair.to_style(),
320+
"um" => self.size.unit_mega = pair.to_style(),
321+
"ug" => self.size.unit_giga = pair.to_style(),
322+
"uh" => self.size.unit_huge = pair.to_style(),
275323
"df" => self.size.major = pair.to_style(),
276324
"ds" => self.size.minor = pair.to_style(),
277325

@@ -302,6 +350,22 @@ impl Colours {
302350
}
303351
true
304352
}
353+
354+
pub fn set_number_style(&mut self, style: Style) {
355+
self.size.number_byte = style;
356+
self.size.number_kilo = style;
357+
self.size.number_mega = style;
358+
self.size.number_giga = style;
359+
self.size.number_huge = style;
360+
}
361+
362+
pub fn set_unit_style(&mut self, style: Style) {
363+
self.size.unit_byte = style;
364+
self.size.unit_kilo = style;
365+
self.size.unit_mega = style;
366+
self.size.unit_giga = style;
367+
self.size.unit_huge = style;
368+
}
305369
}
306370

307371

@@ -360,30 +424,28 @@ impl render::PermissionsColours for Colours {
360424
}
361425

362426
impl render::SizeColours for Colours {
363-
fn size(&self, size: u64) -> Style {
364-
if self.scale {
365-
if size < 1024 {
366-
self.size.scale_byte
367-
}
368-
else if size < 1024 * 1024 {
369-
self.size.scale_kilo
370-
}
371-
else if size < 1024 * 1024 * 1024 {
372-
self.size.scale_mega
373-
}
374-
else if size < 1024 * 1024 * 1024 * 1024 {
375-
self.size.scale_giga
376-
}
377-
else {
378-
self.size.scale_huge
379-
}
427+
fn size(&self, prefix: Option<number_prefix::Prefix>) -> Style {
428+
use number_prefix::Prefix::*;
429+
match prefix {
430+
None => self.size.number_byte,
431+
Some(Kilo) | Some(Kibi) => self.size.number_kilo,
432+
Some(Mega) | Some(Mibi) => self.size.number_mega,
433+
Some(Giga) | Some(Gibi) => self.size.number_giga,
434+
Some(_) => self.size.number_huge,
380435
}
381-
else {
382-
self.size.numbers
436+
}
437+
438+
fn unit(&self, prefix: Option<number_prefix::Prefix>) -> Style {
439+
use number_prefix::Prefix::*;
440+
match prefix {
441+
None => self.size.unit_byte,
442+
Some(Kilo) | Some(Kibi) => self.size.unit_kilo,
443+
Some(Mega) | Some(Mibi) => self.size.unit_mega,
444+
Some(Giga) | Some(Gibi) => self.size.unit_giga,
445+
Some(_) => self.size.unit_huge,
383446
}
384447
}
385448

386-
fn unit(&self) -> Style { self.size.unit }
387449
fn no_size(&self) -> Style { self.punctuation }
388450
fn major(&self) -> Style { self.size.major }
389451
fn comma(&self) -> Style { self.punctuation }

src/style/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod colours;
22
pub use self::colours::Colours;
3+
pub use self::colours::Size as SizeColours;
34

45
mod lsc;
56
pub use self::lsc::LSColors;

0 commit comments

Comments
 (0)