@@ -14,6 +14,7 @@ use lscolors::{LsColors, Style};
14
14
15
15
use std:: { cell:: OnceCell , num:: IntErrorKind } ;
16
16
use std:: { collections:: HashSet , io:: IsTerminal } ;
17
+ use ansi_width:: ansi_width;
17
18
18
19
#[ cfg( windows) ]
19
20
use std:: os:: windows:: fs:: MetadataExt ;
@@ -33,8 +34,7 @@ use std::{
33
34
os:: unix:: fs:: { FileTypeExt , MetadataExt } ,
34
35
time:: Duration ,
35
36
} ;
36
- use term_grid:: { Cell , Direction , Filling , Grid , GridOptions } ;
37
- use unicode_width:: UnicodeWidthStr ;
37
+ use term_grid:: { Direction , Filling , Grid , GridOptions } ;
38
38
use uucore:: error:: USimpleError ;
39
39
use uucore:: format:: human:: { human_readable, SizeFormat } ;
40
40
#[ cfg( all( unix, not( any( target_os = "android" , target_os = "macos" ) ) ) ) ]
@@ -2527,7 +2527,7 @@ fn display_items(
2527
2527
names_vec. push ( cell) ;
2528
2528
}
2529
2529
2530
- let names = names_vec. into_iter ( ) ;
2530
+ let mut names = names_vec. into_iter ( ) ;
2531
2531
2532
2532
match config. format {
2533
2533
Format :: Columns => {
@@ -2538,20 +2538,19 @@ fn display_items(
2538
2538
}
2539
2539
Format :: Commas => {
2540
2540
let mut current_col = 0 ;
2541
- let mut names = names;
2542
2541
if let Some ( name) = names. next ( ) {
2543
- write ! ( out, "{}" , name. contents ) ?;
2544
- current_col = name. width as u16 + 2 ;
2542
+ write ! ( out, "{}" , name) ?;
2543
+ current_col = ansi_width ( & name) as u16 + 2 ;
2545
2544
}
2546
2545
for name in names {
2547
- let name_width = name. width as u16 ;
2546
+ let name_width = ansi_width ( & name) as u16 ;
2548
2547
// If the width is 0 we print one single line
2549
2548
if config. width != 0 && current_col + name_width + 1 > config. width {
2550
2549
current_col = name_width + 2 ;
2551
- write ! ( out, ",\n {}" , name. contents ) ?;
2550
+ write ! ( out, ",\n {}" , name) ?;
2552
2551
} else {
2553
2552
current_col += name_width + 2 ;
2554
- write ! ( out, ", {}" , name. contents ) ?;
2553
+ write ! ( out, ", {}" , name) ?;
2555
2554
}
2556
2555
}
2557
2556
// Current col is never zero again if names have been printed.
@@ -2562,7 +2561,7 @@ fn display_items(
2562
2561
}
2563
2562
_ => {
2564
2563
for name in names {
2565
- write ! ( out, "{}{}" , name. contents , config. line_ending) ?;
2564
+ write ! ( out, "{}{}" , name, config. line_ending) ?;
2566
2565
}
2567
2566
}
2568
2567
} ;
@@ -2596,7 +2595,7 @@ fn get_block_size(md: &Metadata, config: &Config) -> u64 {
2596
2595
}
2597
2596
2598
2597
fn display_grid (
2599
- names : impl Iterator < Item = Cell > ,
2598
+ names : impl Iterator < Item = String > ,
2600
2599
width : u16 ,
2601
2600
direction : Direction ,
2602
2601
out : & mut BufWriter < Stdout > ,
@@ -2610,38 +2609,36 @@ fn display_grid(
2610
2609
write ! ( out, " " ) ?;
2611
2610
}
2612
2611
printed_something = true ;
2613
- write ! ( out, "{}" , name . contents ) ?;
2612
+ write ! ( out, "{name}" ) ?;
2614
2613
}
2615
2614
if printed_something {
2616
2615
writeln ! ( out) ?;
2617
2616
}
2618
2617
} else {
2619
- // TODO: To match gnu/tests/ls/stat-dtype.sh
2620
- // we might want to have Filling::Text("\t".to_string());
2621
- let filling = Filling :: Spaces ( 2 ) ;
2622
- let mut grid = Grid :: new ( GridOptions { filling, direction } ) ;
2623
-
2624
- for name in names {
2625
- let formatted_name = Cell {
2626
- contents : if quoted && !name. contents . starts_with ( '\'' ) {
2627
- format ! ( " {}" , name. contents)
2628
- } else {
2629
- name. contents
2630
- } ,
2631
- width : name. width ,
2632
- } ;
2633
- grid. add ( formatted_name) ;
2634
- }
2635
-
2636
- match grid. fit_into_width ( width as usize ) {
2637
- Some ( output) => {
2638
- write ! ( out, "{output}" ) ?;
2639
- }
2640
- // Width is too small for the grid, so we fit it in one column
2641
- None => {
2642
- write ! ( out, "{}" , grid. fit_into_columns( 1 ) ) ?;
2643
- }
2644
- }
2618
+ let names = if quoted {
2619
+ names
2620
+ . map ( |n| {
2621
+ if n. starts_with ( '\'' ) {
2622
+ format ! ( " {n}" )
2623
+ } else {
2624
+ n
2625
+ }
2626
+ } )
2627
+ . collect ( )
2628
+ } else {
2629
+ names. collect ( )
2630
+ } ;
2631
+ let grid = Grid :: new (
2632
+ names,
2633
+ GridOptions {
2634
+ // TODO: To match gnu/tests/ls/stat-dtype.sh
2635
+ // we might want to have Filling::Text("\t".to_string());
2636
+ filling : Filling :: Spaces ( 2 ) ,
2637
+ direction,
2638
+ width : width as usize ,
2639
+ } ,
2640
+ ) ;
2641
+ write ! ( out, "{grid}" ) ?;
2645
2642
}
2646
2643
Ok ( ( ) )
2647
2644
}
@@ -2786,8 +2783,7 @@ fn display_item_long(
2786
2783
2787
2784
write ! ( output_display, " {} " , display_date( md, config) ) . unwrap ( ) ;
2788
2785
2789
- let item_name =
2790
- display_item_name ( item, config, None , String :: new ( ) , out, style_manager) . contents ;
2786
+ let item_name = display_item_name ( item, config, None , String :: new ( ) , out, style_manager) ;
2791
2787
2792
2788
let displayed_item = if quoted && !item_name. starts_with ( '\'' ) {
2793
2789
format ! ( " {}" , item_name)
@@ -2877,7 +2873,7 @@ fn display_item_long(
2877
2873
}
2878
2874
2879
2875
let displayed_item =
2880
- display_item_name ( item, config, None , String :: new ( ) , out, style_manager) . contents ;
2876
+ display_item_name ( item, config, None , String :: new ( ) , out, style_manager) ;
2881
2877
let date_len = 12 ;
2882
2878
2883
2879
write ! (
@@ -3138,14 +3134,10 @@ fn display_item_name(
3138
3134
more_info : String ,
3139
3135
out : & mut BufWriter < Stdout > ,
3140
3136
style_manager : & mut StyleManager ,
3141
- ) -> Cell {
3137
+ ) -> String {
3142
3138
// This is our return value. We start by `&path.display_name` and modify it along the way.
3143
3139
let mut name = escape_name ( & path. display_name , & config. quoting_style ) ;
3144
3140
3145
- // We need to keep track of the width ourselves instead of letting term_grid
3146
- // infer it because the color codes mess up term_grid's width calculation.
3147
- let mut width = name. width ( ) ;
3148
-
3149
3141
if config. hyperlink {
3150
3142
name = create_hyperlink ( & name, path) ;
3151
3143
}
@@ -3155,9 +3147,6 @@ fn display_item_name(
3155
3147
}
3156
3148
3157
3149
if config. format != Format :: Long && !more_info. is_empty ( ) {
3158
- // increment width here b/c name was given colors and name.width() is now the wrong
3159
- // size for display
3160
- width += more_info. width ( ) ;
3161
3150
name = more_info + & name;
3162
3151
}
3163
3152
@@ -3185,7 +3174,6 @@ fn display_item_name(
3185
3174
3186
3175
if let Some ( c) = char_opt {
3187
3176
name. push ( c) ;
3188
- width += 1 ;
3189
3177
}
3190
3178
}
3191
3179
@@ -3257,14 +3245,10 @@ fn display_item_name(
3257
3245
pad_left ( & path. security_context , pad_count)
3258
3246
} ;
3259
3247
name = format ! ( "{security_context} {name}" ) ;
3260
- width += security_context. len ( ) + 1 ;
3261
3248
}
3262
3249
}
3263
3250
3264
- Cell {
3265
- contents : name,
3266
- width,
3267
- }
3251
+ name
3268
3252
}
3269
3253
3270
3254
fn create_hyperlink ( name : & str , path : & PathData ) -> String {
0 commit comments