@@ -5,8 +5,6 @@ use lscolors::{Indicator, LsColors, Style};
5
5
6
6
use crate :: config:: Config ;
7
7
use crate :: dir_entry:: DirEntry ;
8
- use crate :: error:: print_error;
9
- use crate :: exit_codes:: ExitCode ;
10
8
use crate :: fmt:: FormatTemplate ;
11
9
use crate :: hyperlink:: PathUrl ;
12
10
@@ -15,24 +13,31 @@ fn replace_path_separator(path: &str, new_path_separator: &str) -> String {
15
13
}
16
14
17
15
// TODO: this function is performance critical and can probably be optimized
18
- pub fn print_entry < W : Write > ( stdout : & mut W , entry : & DirEntry , config : & Config ) {
19
- // TODO: use format if supplied
20
- let r = if let Some ( ref format) = config. format {
21
- print_entry_format ( stdout, entry, config, format)
16
+ pub fn print_entry < W : Write > ( stdout : & mut W , entry : & DirEntry , config : & Config ) -> io:: Result < ( ) > {
17
+ let mut has_hyperlink = false ;
18
+ if config. hyperlink {
19
+ if let Some ( url) = PathUrl :: new ( entry. path ( ) ) {
20
+ write ! ( stdout, "\x1B ]8;;{}\x1B \\ " , url) ?;
21
+ has_hyperlink = true ;
22
+ }
23
+ }
24
+
25
+ if let Some ( ref format) = config. format {
26
+ print_entry_format ( stdout, entry, config, format) ?;
22
27
} else if let Some ( ref ls_colors) = config. ls_colors {
23
- print_entry_colorized ( stdout, entry, config, ls_colors)
28
+ print_entry_colorized ( stdout, entry, config, ls_colors) ? ;
24
29
} else {
25
- print_entry_uncolorized ( stdout, entry, config)
30
+ print_entry_uncolorized ( stdout, entry, config) ? ;
26
31
} ;
27
32
28
- if let Err ( e ) = r {
29
- if e . kind ( ) == :: std :: io :: ErrorKind :: BrokenPipe {
30
- // Exit gracefully in case of a broken pipe (e.g. 'fd ... | head -n 3').
31
- ExitCode :: Success . exit ( ) ;
32
- } else {
33
- print_error ( format ! ( "Could not write to output: {}" , e ) ) ;
34
- ExitCode :: GeneralError . exit ( ) ;
35
- }
33
+ if has_hyperlink {
34
+ write ! ( stdout , " \x1B ]8;; \x1B \\ " ) ? ;
35
+ }
36
+
37
+ if config . null_separator {
38
+ write ! ( stdout , " \0 " )
39
+ } else {
40
+ writeln ! ( stdout )
36
41
}
37
42
}
38
43
@@ -66,13 +71,12 @@ fn print_entry_format<W: Write>(
66
71
config : & Config ,
67
72
format : & FormatTemplate ,
68
73
) -> io:: Result < ( ) > {
69
- let separator = if config. null_separator { "\0 " } else { "\n " } ;
70
74
let output = format. generate (
71
75
entry. stripped_path ( config) ,
72
76
config. path_separator . as_deref ( ) ,
73
77
) ;
74
78
// TODO: support writing raw bytes on unix?
75
- write ! ( stdout, "{}{} " , output. to_string_lossy( ) , separator )
79
+ write ! ( stdout, "{}" , output. to_string_lossy( ) )
76
80
}
77
81
78
82
// TODO: this function is performance critical and can probably be optimized
@@ -84,17 +88,9 @@ fn print_entry_colorized<W: Write>(
84
88
) -> io:: Result < ( ) > {
85
89
// Split the path between the parent and the last component
86
90
let mut offset = 0 ;
87
- let mut has_hyperlink = false ;
88
91
let path = entry. stripped_path ( config) ;
89
92
let path_str = path. to_string_lossy ( ) ;
90
93
91
- if config. hyperlink {
92
- if let Some ( url) = PathUrl :: new ( entry. path ( ) ) {
93
- write ! ( stdout, "\x1B ]8;;{}\x1B \\ " , url) ?;
94
- has_hyperlink = true ;
95
- }
96
- }
97
-
98
94
if let Some ( parent) = path. parent ( ) {
99
95
offset = parent. to_string_lossy ( ) . len ( ) ;
100
96
for c in path_str[ offset..] . chars ( ) {
@@ -132,16 +128,6 @@ fn print_entry_colorized<W: Write>(
132
128
ls_colors. style_for_indicator ( Indicator :: Directory ) ,
133
129
) ?;
134
130
135
- if has_hyperlink {
136
- write ! ( stdout, "\x1B ]8;;\x1B \\ " ) ?;
137
- }
138
-
139
- if config. null_separator {
140
- write ! ( stdout, "\0 " ) ?;
141
- } else {
142
- writeln ! ( stdout) ?;
143
- }
144
-
145
131
Ok ( ( ) )
146
132
}
147
133
@@ -151,16 +137,14 @@ fn print_entry_uncolorized_base<W: Write>(
151
137
entry : & DirEntry ,
152
138
config : & Config ,
153
139
) -> io:: Result < ( ) > {
154
- let separator = if config. null_separator { "\0 " } else { "\n " } ;
155
140
let path = entry. stripped_path ( config) ;
156
141
157
142
let mut path_string = path. to_string_lossy ( ) ;
158
143
if let Some ( ref separator) = config. path_separator {
159
144
* path_string. to_mut ( ) = replace_path_separator ( & path_string, separator) ;
160
145
}
161
146
write ! ( stdout, "{}" , path_string) ?;
162
- print_trailing_slash ( stdout, entry, config, None ) ?;
163
- write ! ( stdout, "{}" , separator)
147
+ print_trailing_slash ( stdout, entry, config, None )
164
148
}
165
149
166
150
#[ cfg( not( unix) ) ]
@@ -185,9 +169,7 @@ fn print_entry_uncolorized<W: Write>(
185
169
print_entry_uncolorized_base ( stdout, entry, config)
186
170
} else {
187
171
// Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processes
188
- let separator = if config. null_separator { b"\0 " } else { b"\n " } ;
189
172
stdout. write_all ( entry. stripped_path ( config) . as_os_str ( ) . as_bytes ( ) ) ?;
190
- print_trailing_slash ( stdout, entry, config, None ) ?;
191
- stdout. write_all ( separator)
173
+ print_trailing_slash ( stdout, entry, config, None )
192
174
}
193
175
}
0 commit comments