@@ -15,7 +15,8 @@ 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:: response:: ResponseFormatter ;
18
+ use crate :: formatters:: get_response_formatter;
19
+ use crate :: formatters:: response:: ResponseBodyFormatter ;
19
20
use crate :: verbosity:: Verbosity ;
20
21
use crate :: { cache:: Cache , stats:: ResponseStats , ExitCode } ;
21
22
@@ -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 ResponseBodyFormatter > ,
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 ResponseBodyFormatter ,
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,27 @@ fn get_failed_urls(stats: &mut ResponseStats) -> Vec<(InputSource, Url)> {
330
340
331
341
#[ cfg( test) ]
332
342
mod tests {
343
+ use crate :: formatters;
344
+ use crate :: { formatters:: get_response_formatter, options} ;
333
345
use log:: info;
334
-
335
346
use lychee_lib:: { CacheStatus , ClientBuilder , InputSource , ResponseBody , Uri } ;
336
347
337
- use crate :: formatters;
338
-
339
348
use super :: * ;
340
349
341
350
#[ test]
342
351
fn test_skip_cached_responses_in_progress_output ( ) {
343
352
let mut buf = Vec :: new ( ) ;
344
- let response = Response (
353
+ let response = Response :: new (
354
+ Uri :: try_from ( "http://127.0.0.1" ) . unwrap ( ) ,
355
+ Status :: Cached ( CacheStatus :: Ok ( 200 ) ) ,
345
356
InputSource :: Stdin ,
346
- ResponseBody {
347
- uri : Uri :: try_from ( "http://127.0.0.1" ) . unwrap ( ) ,
348
- status : Status :: Cached ( CacheStatus :: Ok ( 200 ) ) ,
349
- } ,
350
357
) ;
351
- let formatter: Arc < Box < dyn ResponseFormatter > > =
352
- Arc :: new ( Box :: new ( formatters:: response:: Raw :: new ( ) ) ) ;
358
+ let formatter = get_response_formatter ( & options:: ResponseFormat :: Plain ) ;
353
359
show_progress (
354
360
& mut buf,
355
361
& None ,
356
362
& response,
357
- & formatter,
363
+ formatter. as_ref ( ) ,
358
364
& Verbosity :: default ( ) ,
359
365
)
360
366
. unwrap ( ) ;
@@ -366,20 +372,24 @@ mod tests {
366
372
#[ test]
367
373
fn test_show_cached_responses_in_progress_debug_output ( ) {
368
374
let mut buf = Vec :: new ( ) ;
369
- let response = Response (
375
+ let response = Response :: new (
376
+ Uri :: try_from ( "http://127.0.0.1" ) . unwrap ( ) ,
377
+ Status :: Cached ( CacheStatus :: Ok ( 200 ) ) ,
370
378
InputSource :: Stdin ,
371
- ResponseBody {
372
- uri : Uri :: try_from ( "http://127.0.0.1" ) . unwrap ( ) ,
373
- status : Status :: Cached ( CacheStatus :: Ok ( 200 ) ) ,
374
- } ,
375
379
) ;
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 ( ) ;
380
+ let formatter = get_response_formatter ( & options:: ResponseFormat :: Plain ) ;
381
+ show_progress (
382
+ & mut buf,
383
+ & None ,
384
+ & response,
385
+ formatter. as_ref ( ) ,
386
+ & Verbosity :: debug ( ) ,
387
+ )
388
+ . unwrap ( ) ;
379
389
380
390
assert ! ( !buf. is_empty( ) ) ;
381
391
let buf = String :: from_utf8_lossy ( & buf) ;
382
- assert_eq ! ( buf, "↻ [200] http://127.0.0.1/ | Cached: OK (cached)\n " ) ;
392
+ assert_eq ! ( buf, "[200] http://127.0.0.1/ | Cached: OK (cached)\n " ) ;
383
393
}
384
394
385
395
#[ tokio:: test]
0 commit comments