@@ -6,91 +6,90 @@ use crate::{
66 options:: Opts ,
77} ;
88use colorful:: { Color , Colorful } ;
9+ use indicatif:: ProgressBar ;
910use std:: * ;
11+ use sync:: Arc ;
1012
11- fn print_border ( pb : & indicatif :: ProgressBar , width : usize ) {
13+ fn print_border ( pb : & ProgressBar , width : usize ) {
1214 pb. println ( "┌" . to_string ( ) + & "─" . repeat ( width - 2 ) + "┐" ) ;
1315}
1416
15- fn print_footer ( pb : & indicatif :: ProgressBar , width : usize ) {
17+ fn print_footer ( pb : & ProgressBar , width : usize ) {
1618 pb. println ( "└" . to_string ( ) + & "─" . repeat ( width - 2 ) + "┘" ) ;
1719}
1820
19- pub fn display_success_ping (
20- pb : & indicatif:: ProgressBar ,
21- config : & Opts ,
21+ async fn sleep_if_enabled ( config : & ' static Opts , duration : u64 ) {
22+ if !config. no_delay {
23+ tokio:: time:: sleep ( std:: time:: Duration :: from_millis ( duration) ) . await ;
24+ }
25+ }
26+
27+ pub async fn display_success_ping (
28+ pb : & ProgressBar ,
29+ config : & ' static Opts ,
2230 endpoint : & str ,
2331 jobres : & PerformIcmpResponseResultsItemResult ,
2432 node_info : & PerformIcmpResponseNodeInfo ,
2533) {
26- let width = 80 ; // Adjust this value as needed
34+ let width = 80 ;
2735 print_border ( pb, width) ;
2836 format_ping_header ( pb, config, endpoint, & jobres. ip_address , node_info) ;
2937
30- // Display individual ping results
3138 let trips = jobres. trips as usize ;
3239 for i in 0 ..trips {
3340 let time = jobres. min + ( jobres. max - jobres. min ) * ( i as f64 / ( trips - 1 ) as f64 ) ;
3441 pb. println ( format ! (
3542 "│ 64 bytes from {}: icmp_seq={} ttl=120 time={:.2} ms" ,
3643 jobres. ip_address, i, time
3744 ) ) ;
38- std :: thread :: sleep ( std :: time:: Duration :: from_millis ( time as u64 ) ) ;
45+ sleep_if_enabled ( config , time as u64 ) . await ;
3946 }
4047
41- pb. println ( "│" ) ; // Empty line for spacing
42-
43- // Construct and print statistics line
48+ pb. println ( "│" ) ;
4449 pb. println ( format ! ( "│ --- {endpoint} ping statistics ---" ) ) ;
4550
46- std :: thread :: sleep ( std :: time :: Duration :: from_millis ( 250 ) ) ;
51+ sleep_if_enabled ( config , 250 ) . await ;
4752
48- // Print packet loss information
4953 pb. println ( format ! (
5054 "│ {} packets transmitted, {} packets received, {:.1}% packet loss" ,
5155 jobres. packets_sent,
5256 jobres. packets_recv,
5357 jobres. packet_loss * 100.0
5458 ) ) ;
55- std :: thread :: sleep ( std :: time :: Duration :: from_millis ( 250 ) ) ;
59+ sleep_if_enabled ( config , 250 ) . await ;
5660
57- // Print round-trip statistics
5861 pb. println ( format ! (
5962 "│ round-trip min/avg/max/stddev = {:.3}/{:.3}/{:.3}/{:.3} ms" ,
6063 jobres. min, jobres. avg, jobres. max, jobres. std_dev
6164 ) ) ;
62- std :: thread :: sleep ( std :: time :: Duration :: from_millis ( 250 ) ) ;
65+ sleep_if_enabled ( config , 250 ) . await ;
6366
6467 print_footer ( pb, width) ;
6568}
6669
67- pub fn display_failed_ping (
68- pb : & indicatif :: ProgressBar ,
69- config : & Opts ,
70+ pub async fn display_failed_ping (
71+ pb : & ProgressBar ,
72+ config : & ' static Opts ,
7073 jobres : & PerformIcmpResponseResultsItem ,
7174 node_info : & PerformIcmpResponseNodeInfo ,
7275) {
73- let width = 80 ; // Adjust this value as needed
76+ let width = 80 ;
7477 print_border ( pb, width) ;
7578 let ip_address = jobres
7679 . result
7780 . as_ref ( )
7881 . map_or ( "Unknown" . to_string ( ) , |r| r. ip_address . clone ( ) ) ;
7982 format_ping_header ( pb, config, & jobres. endpoint , & ip_address, node_info) ;
8083
81- // Request timeout for icmp_seq 0, 1, 2, 3
8284 let attempts = jobres. result . as_ref ( ) . map_or ( 4 , |r| r. attempts as usize ) ;
8385 for index in 0 ..attempts {
8486 pb. println ( format ! ( "│ Request timeout for icmp_seq {}" , index) ) ;
85- std :: thread :: sleep ( std :: time :: Duration :: from_millis ( 500 ) ) ;
87+ sleep_if_enabled ( config , 500 ) . await ;
8688 }
8789
88- // --- asdasdasd.com ping statistics ---
8990 pb. println ( format ! ( "│ --- {} ping statistics ---" , jobres. endpoint) ) ;
91+ sleep_if_enabled ( config, 250 ) . await ;
9092
91- std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 250 ) ) ;
92-
93- // 5 packets transmitted, 0 packets received, 100.0% packet loss
9493 let error_string = if let Some ( result) = & jobres. result {
9594 format ! (
9695 "│ {} packets transmitted, {} packets received, {:.1}% packet loss" ,
@@ -104,16 +103,16 @@ pub fn display_failed_ping(
104103 attempts
105104 )
106105 } ;
107- std :: thread :: sleep ( std :: time :: Duration :: from_millis ( 250 ) ) ;
106+ sleep_if_enabled ( config , 250 ) . await ;
108107
109108 pb. println ( format ! ( "{}" , error_string. color( Color :: Red ) ) ) ;
110- std :: thread :: sleep ( std :: time :: Duration :: from_millis ( 250 ) ) ;
109+ sleep_if_enabled ( config , 250 ) . await ;
111110
112111 print_footer ( pb, width) ;
113112}
114113
115114pub fn format_ping_header (
116- pb : & indicatif :: ProgressBar ,
115+ pb : & ProgressBar ,
117116 config : & Opts ,
118117 endpoint : & str ,
119118 ip_address : & str ,
0 commit comments