@@ -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) )
@@ -275,10 +284,11 @@ fn show_progress(
275
284
output : & mut dyn Write ,
276
285
progress_bar : & Option < ProgressBar > ,
277
286
response : & Response ,
278
- formatter : & Arc < Box < dyn ResponseFormatter > > ,
287
+ formatter : & dyn ResponseBodyFormatter ,
279
288
verbose : & Verbosity ,
280
289
) -> Result < ( ) > {
281
- let out = formatter. write_response ( response) ?;
290
+ let out = formatter. format_response ( response. body ( ) ) ;
291
+
282
292
if let Some ( pb) = progress_bar {
283
293
pb. inc ( 1 ) ;
284
294
pb. set_message ( out. clone ( ) ) ;
@@ -317,30 +327,26 @@ fn get_failed_urls(stats: &mut ResponseStats) -> Vec<(InputSource, Url)> {
317
327
#[ cfg( test) ]
318
328
mod tests {
319
329
use log:: info;
330
+ use lychee_lib:: { CacheStatus , InputSource , Uri } ;
320
331
321
- use lychee_lib:: { CacheStatus , InputSource , ResponseBody , Uri } ;
322
-
323
- use crate :: formatters;
332
+ use crate :: { formatters:: get_response_formatter, options} ;
324
333
325
334
use super :: * ;
326
335
327
336
#[ test]
328
337
fn test_skip_cached_responses_in_progress_output ( ) {
329
338
let mut buf = Vec :: new ( ) ;
330
- let response = Response (
339
+ let response = Response :: new (
340
+ Uri :: try_from ( "http://127.0.0.1" ) . unwrap ( ) ,
341
+ Status :: Cached ( CacheStatus :: Ok ( 200 ) ) ,
331
342
InputSource :: Stdin ,
332
- ResponseBody {
333
- uri : Uri :: try_from ( "http://127.0.0.1" ) . unwrap ( ) ,
334
- status : Status :: Cached ( CacheStatus :: Ok ( 200 ) ) ,
335
- } ,
336
343
) ;
337
- let formatter: Arc < Box < dyn ResponseFormatter > > =
338
- Arc :: new ( Box :: new ( formatters:: response:: Raw :: new ( ) ) ) ;
344
+ let formatter = get_response_formatter ( & options:: ResponseFormat :: Plain ) ;
339
345
show_progress (
340
346
& mut buf,
341
347
& None ,
342
348
& response,
343
- & formatter,
349
+ formatter. as_ref ( ) ,
344
350
& Verbosity :: default ( ) ,
345
351
)
346
352
. unwrap ( ) ;
@@ -352,19 +358,23 @@ mod tests {
352
358
#[ test]
353
359
fn test_show_cached_responses_in_progress_debug_output ( ) {
354
360
let mut buf = Vec :: new ( ) ;
355
- let response = Response (
361
+ let response = Response :: new (
362
+ Uri :: try_from ( "http://127.0.0.1" ) . unwrap ( ) ,
363
+ Status :: Cached ( CacheStatus :: Ok ( 200 ) ) ,
356
364
InputSource :: Stdin ,
357
- ResponseBody {
358
- uri : Uri :: try_from ( "http://127.0.0.1" ) . unwrap ( ) ,
359
- status : Status :: Cached ( CacheStatus :: Ok ( 200 ) ) ,
360
- } ,
361
365
) ;
362
- let formatter: Arc < Box < dyn ResponseFormatter > > =
363
- Arc :: new ( Box :: new ( formatters:: response:: Raw :: new ( ) ) ) ;
364
- show_progress ( & mut buf, & None , & response, & formatter, & Verbosity :: debug ( ) ) . unwrap ( ) ;
366
+ let formatter = get_response_formatter ( & options:: ResponseFormat :: Plain ) ;
367
+ show_progress (
368
+ & mut buf,
369
+ & None ,
370
+ & response,
371
+ formatter. as_ref ( ) ,
372
+ & Verbosity :: debug ( ) ,
373
+ )
374
+ . unwrap ( ) ;
365
375
366
376
assert ! ( !buf. is_empty( ) ) ;
367
377
let buf = String :: from_utf8_lossy ( & buf) ;
368
- assert_eq ! ( buf, "↻ [200] http://127.0.0.1/ | Cached: OK (cached)\n " ) ;
378
+ assert_eq ! ( buf, "[200] http://127.0.0.1/ | Cached: OK (cached)\n " ) ;
369
379
}
370
380
}
0 commit comments