@@ -7,7 +7,6 @@ use output::{Grid, Details, GridDetails};
7
7
use output:: column:: { Columns , TimeTypes , SizeFormat } ;
8
8
use output:: file_name:: Classify ;
9
9
use options:: { FileFilter , DirAction , Misfire } ;
10
- use term:: dimensions;
11
10
use fs:: feature:: xattr;
12
11
13
12
@@ -45,10 +44,6 @@ impl Mode {
45
44
pub fn deduce ( matches : & getopts:: Matches , filter : FileFilter , dir_action : DirAction ) -> Result < ( Mode , Colours ) , Misfire > {
46
45
use options:: misfire:: Misfire :: * ;
47
46
48
- let colour_scale = || {
49
- matches. opt_present ( "color-scale" ) || matches. opt_present ( "colour-scale" )
50
- } ;
51
-
52
47
let long = || {
53
48
if matches. opt_present ( "across" ) && !matches. opt_present ( "grid" ) {
54
49
Err ( Useless ( "across" , true , "long" ) )
@@ -57,19 +52,7 @@ impl Mode {
57
52
Err ( Useless ( "oneline" , true , "long" ) )
58
53
}
59
54
else {
60
- let term_colours = TerminalColours :: deduce ( matches) ?;
61
- let colours = match term_colours {
62
- TerminalColours :: Always => Colours :: colourful ( colour_scale ( ) ) ,
63
- TerminalColours :: Never => Colours :: plain ( ) ,
64
- TerminalColours :: Automatic => {
65
- if dimensions ( ) . is_some ( ) {
66
- Colours :: colourful ( colour_scale ( ) )
67
- }
68
- else {
69
- Colours :: plain ( )
70
- }
71
- } ,
72
- } ;
55
+ let colours = Colours :: deduce ( matches) ?;
73
56
74
57
let details = Details {
75
58
columns : Some ( Columns :: deduce ( matches) ?) ,
@@ -105,15 +88,8 @@ impl Mode {
105
88
} ;
106
89
107
90
let other_options_scan = || {
108
- let term_colours = TerminalColours :: deduce ( matches) ?;
109
- let term_width = TerminalWidth :: deduce ( ) ?;
110
-
111
- if let Some ( width) = term_width. width ( ) {
112
- let colours = match term_colours {
113
- TerminalColours :: Always |
114
- TerminalColours :: Automatic => Colours :: colourful ( colour_scale ( ) ) ,
115
- TerminalColours :: Never => Colours :: plain ( ) ,
116
- } ;
91
+ if let Some ( width) = TerminalWidth :: deduce ( ) ?. width ( ) {
92
+ let colours = Colours :: deduce ( matches) ?;
117
93
118
94
if matches. opt_present ( "oneline" ) {
119
95
if matches. opt_present ( "across" ) {
@@ -148,10 +124,7 @@ impl Mode {
148
124
// as the program’s stdout being connected to a file, then
149
125
// fallback to the lines view.
150
126
151
- let colours = match term_colours {
152
- TerminalColours :: Always => Colours :: colourful ( colour_scale ( ) ) ,
153
- TerminalColours :: Never | TerminalColours :: Automatic => Colours :: plain ( ) ,
154
- } ;
127
+ let colours = Colours :: deduce ( matches) ?;
155
128
156
129
if matches. opt_present ( "tree" ) {
157
130
let details = Details {
@@ -222,7 +195,7 @@ impl TerminalWidth {
222
195
Err ( e) => Err ( Misfire :: FailedParse ( e) ) ,
223
196
}
224
197
}
225
- else if let Some ( ( width, _ ) ) = dimensions ( ) {
198
+ else if let Some ( width) = * TERM_WIDTH {
226
199
Ok ( TerminalWidth :: Terminal ( width) )
227
200
}
228
201
else {
@@ -373,10 +346,37 @@ impl TerminalColours {
373
346
}
374
347
375
348
349
+ impl Colours {
350
+ fn deduce ( matches : & getopts:: Matches ) -> Result < Colours , Misfire > {
351
+ use self :: TerminalColours :: * ;
352
+
353
+ let tc = TerminalColours :: deduce ( matches) ?;
354
+ if tc == Always || ( tc == Automatic && TERM_WIDTH . is_some ( ) ) {
355
+ let scale = matches. opt_present ( "color-scale" ) || matches. opt_present ( "colour-scale" ) ;
356
+ Ok ( Colours :: colourful ( scale) )
357
+ }
358
+ else {
359
+ Ok ( Colours :: plain ( ) )
360
+ }
361
+ }
362
+ }
363
+
364
+
376
365
377
366
impl Classify {
378
367
fn deduce ( matches : & getopts:: Matches ) -> Classify {
379
368
if matches. opt_present ( "classify" ) { Classify :: AddFileIndicators }
380
369
else { Classify :: JustFilenames }
381
370
}
382
371
}
372
+
373
+
374
+ // Gets, then caches, the width of the terminal that exa is running in.
375
+ // This gets used multiple times above, with no real guarantee of order,
376
+ // so it’s easier to just cache it the first time it runs.
377
+ lazy_static ! {
378
+ static ref TERM_WIDTH : Option <usize > = {
379
+ use term:: dimensions;
380
+ dimensions( ) . map( |t| t. 0 )
381
+ } ;
382
+ }
0 commit comments