@@ -15,6 +15,7 @@ use lychee_lib::{InputSource, Result};
15
15
use lychee_lib:: { ResponseBody , Status } ;
16
16
17
17
use crate :: archive:: { Archive , Suggestion } ;
18
+ use crate :: formatters:: get_response_formatter;
18
19
use crate :: formatters:: response:: ResponseFormatter ;
19
20
use crate :: verbosity:: Verbosity ;
20
21
use crate :: { cache:: Cache , stats:: ResponseStats , ExitCode } ;
@@ -62,11 +63,13 @@ where
62
63
accept,
63
64
) ) ;
64
65
66
+ let formatter = get_response_formatter ( & params. cfg . mode ) ;
67
+
65
68
let show_results_task = tokio:: spawn ( progress_bar_task (
66
69
recv_resp,
67
70
params. cfg . verbose ,
68
71
pb. clone ( ) ,
69
- Arc :: new ( params . formatter ) ,
72
+ formatter,
70
73
stats,
71
74
) ) ;
72
75
@@ -178,11 +181,17 @@ async fn progress_bar_task(
178
181
mut recv_resp : mpsc:: Receiver < Response > ,
179
182
verbose : Verbosity ,
180
183
pb : Option < ProgressBar > ,
181
- formatter : Arc < Box < dyn ResponseFormatter > > ,
184
+ formatter : Box < dyn ResponseFormatter > ,
182
185
mut stats : ResponseStats ,
183
186
) -> Result < ( Option < ProgressBar > , ResponseStats ) > {
184
187
while let Some ( response) = recv_resp. recv ( ) . await {
185
- show_progress ( & mut io:: stderr ( ) , & pb, & response, & formatter, & verbose) ?;
188
+ show_progress (
189
+ & mut io:: stderr ( ) ,
190
+ & pb,
191
+ & response,
192
+ formatter. as_ref ( ) ,
193
+ & verbose,
194
+ ) ?;
186
195
stats. add ( response) ;
187
196
}
188
197
Ok ( ( pb, stats) )
@@ -289,10 +298,11 @@ fn show_progress(
289
298
output : & mut dyn Write ,
290
299
progress_bar : & Option < ProgressBar > ,
291
300
response : & Response ,
292
- formatter : & Arc < Box < dyn ResponseFormatter > > ,
301
+ formatter : & dyn ResponseFormatter ,
293
302
verbose : & Verbosity ,
294
303
) -> Result < ( ) > {
295
- let out = formatter. write_response ( response) ?;
304
+ let out = formatter. format_response ( response. body ( ) ) ;
305
+
296
306
if let Some ( pb) = progress_bar {
297
307
pb. inc ( 1 ) ;
298
308
pb. set_message ( out. clone ( ) ) ;
@@ -330,31 +340,26 @@ fn get_failed_urls(stats: &mut ResponseStats) -> Vec<(InputSource, Url)> {
330
340
331
341
#[ cfg( test) ]
332
342
mod tests {
343
+ use crate :: { formatters:: get_response_formatter, options} ;
333
344
use log:: info;
334
-
335
- use lychee_lib:: { CacheStatus , ClientBuilder , InputSource , ResponseBody , Uri } ;
336
-
337
- use crate :: formatters;
345
+ use lychee_lib:: { CacheStatus , ClientBuilder , InputSource , Uri } ;
338
346
339
347
use super :: * ;
340
348
341
349
#[ test]
342
350
fn test_skip_cached_responses_in_progress_output ( ) {
343
351
let mut buf = Vec :: new ( ) ;
344
- let response = Response (
352
+ let response = Response :: new (
353
+ Uri :: try_from ( "http://127.0.0.1" ) . unwrap ( ) ,
354
+ Status :: Cached ( CacheStatus :: Ok ( 200 ) ) ,
345
355
InputSource :: Stdin ,
346
- ResponseBody {
347
- uri : Uri :: try_from ( "http://127.0.0.1" ) . unwrap ( ) ,
348
- status : Status :: Cached ( CacheStatus :: Ok ( 200 ) ) ,
349
- } ,
350
356
) ;
351
- let formatter: Arc < Box < dyn ResponseFormatter > > =
352
- Arc :: new ( Box :: new ( formatters:: response:: Raw :: new ( ) ) ) ;
357
+ let formatter = get_response_formatter ( & options:: OutputMode :: Plain ) ;
353
358
show_progress (
354
359
& mut buf,
355
360
& None ,
356
361
& response,
357
- & formatter,
362
+ formatter. as_ref ( ) ,
358
363
& Verbosity :: default ( ) ,
359
364
)
360
365
. unwrap ( ) ;
@@ -366,20 +371,24 @@ mod tests {
366
371
#[ test]
367
372
fn test_show_cached_responses_in_progress_debug_output ( ) {
368
373
let mut buf = Vec :: new ( ) ;
369
- let response = Response (
374
+ let response = Response :: new (
375
+ Uri :: try_from ( "http://127.0.0.1" ) . unwrap ( ) ,
376
+ Status :: Cached ( CacheStatus :: Ok ( 200 ) ) ,
370
377
InputSource :: Stdin ,
371
- ResponseBody {
372
- uri : Uri :: try_from ( "http://127.0.0.1" ) . unwrap ( ) ,
373
- status : Status :: Cached ( CacheStatus :: Ok ( 200 ) ) ,
374
- } ,
375
378
) ;
376
- let formatter: Arc < Box < dyn ResponseFormatter > > =
377
- Arc :: new ( Box :: new ( formatters:: response:: Raw :: new ( ) ) ) ;
378
- show_progress ( & mut buf, & None , & response, & formatter, & Verbosity :: debug ( ) ) . unwrap ( ) ;
379
+ let formatter = get_response_formatter ( & options:: OutputMode :: Plain ) ;
380
+ show_progress (
381
+ & mut buf,
382
+ & None ,
383
+ & response,
384
+ formatter. as_ref ( ) ,
385
+ & Verbosity :: debug ( ) ,
386
+ )
387
+ . unwrap ( ) ;
379
388
380
389
assert ! ( !buf. is_empty( ) ) ;
381
390
let buf = String :: from_utf8_lossy ( & buf) ;
382
- assert_eq ! ( buf, "↻ [200] http://127.0.0.1/ | Cached: OK (cached)\n " ) ;
391
+ assert_eq ! ( buf, "[200] http://127.0.0.1/ | Cached: OK (cached)\n " ) ;
383
392
}
384
393
385
394
#[ tokio:: test]
0 commit comments