@@ -10,7 +10,7 @@ use clap::{Arg, ArgAction, Command};
10
10
use rand:: prelude:: SliceRandom ;
11
11
use rand:: seq:: IndexedRandom ;
12
12
use rand:: { Rng , RngCore } ;
13
- use std:: collections:: HashSet ;
13
+ use std:: collections:: { HashMap , HashSet } ;
14
14
use std:: ffi:: { OsStr , OsString } ;
15
15
use std:: fs:: File ;
16
16
use std:: io:: { BufWriter , Error , Read , Write , stdin, stdout} ;
@@ -20,7 +20,7 @@ use std::str::FromStr;
20
20
use uucore:: display:: { OsWrite , Quotable } ;
21
21
use uucore:: error:: { FromIo , UResult , USimpleError , UUsageError } ;
22
22
use uucore:: format_usage;
23
- use uucore:: locale:: get_message;
23
+ use uucore:: locale:: { get_message, get_message_with_args } ;
24
24
25
25
mod rand_read_adapter;
26
26
@@ -71,7 +71,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
71
71
if let Some ( second_file) = operands. next ( ) {
72
72
return Err ( UUsageError :: new (
73
73
1 ,
74
- format ! ( "unexpected argument {} found" , second_file. quote( ) ) ,
74
+ get_message_with_args (
75
+ "shuf-error-unexpected-argument" ,
76
+ HashMap :: from ( [ ( "arg" . to_string ( ) , second_file. quote ( ) . to_string ( ) ) ] ) ,
77
+ ) ,
75
78
) ) ;
76
79
} ;
77
80
Mode :: Default ( file. into ( ) )
@@ -101,8 +104,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
101
104
let mut output = BufWriter :: new ( match options. output {
102
105
None => Box :: new ( stdout ( ) ) as Box < dyn OsWrite > ,
103
106
Some ( ref s) => {
104
- let file = File :: create ( s)
105
- . map_err_context ( || format ! ( "failed to open {} for writing" , s. quote( ) ) ) ?;
107
+ let file = File :: create ( s) . map_err_context ( || {
108
+ get_message_with_args (
109
+ "shuf-error-failed-to-open-for-writing" ,
110
+ HashMap :: from ( [ ( "file" . to_string ( ) , s. quote ( ) . to_string ( ) ) ] ) ,
111
+ )
112
+ } ) ?;
106
113
Box :: new ( file) as Box < dyn OsWrite >
107
114
}
108
115
} ) ;
@@ -114,8 +121,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
114
121
115
122
let mut rng = match options. random_source {
116
123
Some ( ref r) => {
117
- let file = File :: open ( r)
118
- . map_err_context ( || format ! ( "failed to open random source {}" , r. quote( ) ) ) ?;
124
+ let file = File :: open ( r) . map_err_context ( || {
125
+ get_message_with_args (
126
+ "shuf-error-failed-to-open-random-source" ,
127
+ HashMap :: from ( [ ( "file" . to_string ( ) , r. quote ( ) . to_string ( ) ) ] ) ,
128
+ )
129
+ } ) ?;
119
130
WrappedRng :: RngFile ( rand_read_adapter:: ReadRng :: new ( file) )
120
131
}
121
132
None => WrappedRng :: RngDefault ( rand:: rng ( ) ) ,
@@ -149,7 +160,7 @@ pub fn uu_app() -> Command {
149
160
Arg :: new ( options:: ECHO )
150
161
. short ( 'e' )
151
162
. long ( options:: ECHO )
152
- . help ( "treat each ARG as an input line" )
163
+ . help ( get_message ( "shuf-help-echo" ) )
153
164
. action ( ArgAction :: SetTrue )
154
165
. overrides_with ( options:: ECHO )
155
166
. conflicts_with ( options:: INPUT_RANGE ) ,
@@ -159,7 +170,7 @@ pub fn uu_app() -> Command {
159
170
. short ( 'i' )
160
171
. long ( options:: INPUT_RANGE )
161
172
. value_name ( "LO-HI" )
162
- . help ( "treat each number LO through HI as an input line" )
173
+ . help ( get_message ( "shuf-help- input-range" ) )
163
174
. value_parser ( parse_range)
164
175
. conflicts_with ( options:: FILE_OR_ARGS ) ,
165
176
)
@@ -169,39 +180,39 @@ pub fn uu_app() -> Command {
169
180
. long ( options:: HEAD_COUNT )
170
181
. value_name ( "COUNT" )
171
182
. action ( ArgAction :: Append )
172
- . help ( "output at most COUNT lines" )
183
+ . help ( get_message ( "shuf-help-head-count" ) )
173
184
. value_parser ( usize:: from_str) ,
174
185
)
175
186
. arg (
176
187
Arg :: new ( options:: OUTPUT )
177
188
. short ( 'o' )
178
189
. long ( options:: OUTPUT )
179
190
. value_name ( "FILE" )
180
- . help ( "write result to FILE instead of standard output")
191
+ . help ( get_message ( "shuf-help- output") )
181
192
. value_parser ( ValueParser :: path_buf ( ) )
182
193
. value_hint ( clap:: ValueHint :: FilePath ) ,
183
194
)
184
195
. arg (
185
196
Arg :: new ( options:: RANDOM_SOURCE )
186
197
. long ( options:: RANDOM_SOURCE )
187
198
. value_name ( "FILE" )
188
- . help ( "get random bytes from FILE" )
199
+ . help ( get_message ( "shuf-help- random-source" ) )
189
200
. value_parser ( ValueParser :: path_buf ( ) )
190
201
. value_hint ( clap:: ValueHint :: FilePath ) ,
191
202
)
192
203
. arg (
193
204
Arg :: new ( options:: REPEAT )
194
205
. short ( 'r' )
195
206
. long ( options:: REPEAT )
196
- . help ( "output lines can be repeated" )
207
+ . help ( get_message ( "shuf-help-repeat" ) )
197
208
. action ( ArgAction :: SetTrue )
198
209
. overrides_with ( options:: REPEAT ) ,
199
210
)
200
211
. arg (
201
212
Arg :: new ( options:: ZERO_TERMINATED )
202
213
. short ( 'z' )
203
214
. long ( options:: ZERO_TERMINATED )
204
- . help ( "line delimiter is NUL, not newline" )
215
+ . help ( get_message ( "shuf-help-zero-terminated" ) )
205
216
. action ( ArgAction :: SetTrue )
206
217
. overrides_with ( options:: ZERO_TERMINATED ) ,
207
218
)
@@ -218,7 +229,7 @@ fn read_input_file(filename: &Path) -> UResult<Vec<u8>> {
218
229
let mut data = Vec :: new ( ) ;
219
230
stdin ( )
220
231
. read_to_end ( & mut data)
221
- . map_err_context ( || " read error". into ( ) ) ?;
232
+ . map_err_context ( || get_message ( "shuf-error- read- error") ) ?;
222
233
Ok ( data)
223
234
} else {
224
235
std:: fs:: read ( filename) . map_err_context ( || filename. maybe_quote ( ) . to_string ( ) )
@@ -250,15 +261,18 @@ trait Shufable {
250
261
251
262
impl < ' a > Shufable for Vec < & ' a [ u8 ] > {
252
263
type Item = & ' a [ u8 ] ;
264
+
253
265
fn is_empty ( & self ) -> bool {
254
266
( * * self ) . is_empty ( )
255
267
}
268
+
256
269
fn choose ( & self , rng : & mut WrappedRng ) -> Self :: Item {
257
270
// Note: "copied()" only copies the reference, not the entire [u8].
258
271
// Returns None if the slice is empty. We checked this before, so
259
272
// this is safe.
260
273
( * * self ) . choose ( rng) . unwrap ( )
261
274
}
275
+
262
276
fn partial_shuffle < ' b > (
263
277
& ' b mut self ,
264
278
rng : & ' b mut WrappedRng ,
@@ -271,12 +285,15 @@ impl<'a> Shufable for Vec<&'a [u8]> {
271
285
272
286
impl < ' a > Shufable for Vec < & ' a OsStr > {
273
287
type Item = & ' a OsStr ;
288
+
274
289
fn is_empty ( & self ) -> bool {
275
290
( * * self ) . is_empty ( )
276
291
}
292
+
277
293
fn choose ( & self , rng : & mut WrappedRng ) -> Self :: Item {
278
294
( * * self ) . choose ( rng) . unwrap ( )
279
295
}
296
+
280
297
<
F438
/td> fn partial_shuffle < ' b > (
281
298
& ' b mut self ,
282
299
rng : & ' b mut WrappedRng ,
@@ -288,12 +305,15 @@ impl<'a> Shufable for Vec<&'a OsStr> {
288
305
289
306
impl Shufable for RangeInclusive < usize > {
290
307
type Item = usize ;
308
+
291
309
fn is_empty ( & self ) -> bool {
292
310
self . is_empty ( )
293
311
}
312
+
294
313
fn choose ( & self , rng : & mut WrappedRng ) -> usize {
295
314
rng. random_range ( self . clone ( ) )
296
315
}
316
+
297
317
fn partial_shuffle < ' b > (
298
318
& ' b mut self ,
299
319
rng : & ' b mut WrappedRng ,
@@ -420,10 +440,14 @@ fn shuf_exec(
420
440
rng : & mut WrappedRng ,
421
441
output : & mut BufWriter < Box < dyn OsWrite > > ,
422
442
) -> UResult < ( ) > {
423
- let ctx = || "write failed" . to_string ( ) ;
443
+ let ctx = || get_message ( "shuf-error-write-failed" ) ;
444
+
424
445
if opts. repeat {
425
446
if input. is_empty ( ) {
426
- return Err ( USimpleError :: new ( 1 , "no lines to repeat" ) ) ;
447
+ return Err ( USimpleError :: new (
448
+ 1 ,
449
+ get_message ( "shuf-error-no-lines-to-repeat" ) ,
450
+ ) ) ;
427
451
}
428
452
for _ in 0 ..opts. head_count {
429
453
let r = input. choose ( rng) ;
@@ -449,10 +473,10 @@ fn parse_range(input_range: &str) -> Result<RangeInclusive<usize>, String> {
449
473
if begin <= end || begin == end + 1 {
450
474
Ok ( begin..=end)
451
475
} else {
452
- Err ( " start exceeds end". into ( ) )
476
+ Err ( get_message ( "shuf-error- start- exceeds- end") )
453
477
}
454
478
} else {
455
- Err ( " missing '-'" . into ( ) )
479
+ Err ( get_message ( "shuf-error- missing-dash" ) )
456
480
}
457
481
}
458
482
0 commit comments