@@ -6,91 +6,90 @@ use crate::{
6
6
options:: Opts ,
7
7
} ;
8
8
use colorful:: { Color , Colorful } ;
9
+ use indicatif:: ProgressBar ;
9
10
use std:: * ;
11
+ use sync:: Arc ;
10
12
11
- fn print_border ( pb : & indicatif :: ProgressBar , width : usize ) {
13
+ fn print_border ( pb : & ProgressBar , width : usize ) {
12
14
pb. println ( "┌" . to_string ( ) + & "─" . repeat ( width - 2 ) + "┐" ) ;
13
15
}
14
16
15
- fn print_footer ( pb : & indicatif :: ProgressBar , width : usize ) {
17
+ fn print_footer ( pb : & ProgressBar , width : usize ) {
16
18
pb. println ( "└" . to_string ( ) + & "─" . repeat ( width - 2 ) + "┘" ) ;
17
19
}
18
20
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 ,
22
30
endpoint : & str ,
23
31
jobres : & PerformIcmpResponseResultsItemResult ,
24
32
node_info : & PerformIcmpResponseNodeInfo ,
25
33
) {
26
- let width = 80 ; // Adjust this value as needed
34
+ let width = 80 ;
27
35
print_border ( pb, width) ;
28
36
format_ping_header ( pb, config, endpoint, & jobres. ip_address , node_info) ;
29
37
30
- // Display individual ping results
31
38
let trips = jobres. trips as usize ;
32
39
for i in 0 ..trips {
33
40
let time = jobres. min + ( jobres. max - jobres. min ) * ( i as f64 / ( trips - 1 ) as f64 ) ;
34
41
pb. println ( format ! (
35
42
"│ 64 bytes from {}: icmp_seq={} ttl=120 time={:.2} ms" ,
36
43
jobres. ip_address, i, time
37
44
) ) ;
38
- std :: thread :: sleep ( std :: time:: Duration :: from_millis ( time as u64 ) ) ;
45
+ sleep_if_enabled ( config , time as u64 ) . await ;
39
46
}
40
47
41
- pb. println ( "│" ) ; // Empty line for spacing
42
-
43
- // Construct and print statistics line
48
+ pb. println ( "│" ) ;
44
49
pb. println ( format ! ( "│ --- {endpoint} ping statistics ---" ) ) ;
45
50
46
- std :: thread :: sleep ( std :: time :: Duration :: from_millis ( 250 ) ) ;
51
+ sleep_if_enabled ( config , 250 ) . await ;
47
52
48
- // Print packet loss information
49
53
pb. println ( format ! (
50
54
"│ {} packets transmitted, {} packets received, {:.1}% packet loss" ,
51
55
jobres. packets_sent,
52
56
jobres. packets_recv,
53
57
jobres. packet_loss * 100.0
54
58
) ) ;
55
- std :: thread :: sleep ( std :: time :: Duration :: from_millis ( 250 ) ) ;
59
+ sleep_if_enabled ( config , 250 ) . await ;
56
60
57
- // Print round-trip statistics
58
61
pb. println ( format ! (
59
62
"│ round-trip min/avg/max/stddev = {:.3}/{:.3}/{:.3}/{:.3} ms" ,
60
63
jobres. min, jobres. avg, jobres. max, jobres. std_dev
61
64
) ) ;
62
- std :: thread :: sleep ( std :: time :: Duration :: from_millis ( 250 ) ) ;
65
+ sleep_if_enabled ( config , 250 ) . await ;
63
66
64
67
print_footer ( pb, width) ;
65
68
}
66
69
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 ,
70
73
jobres : & PerformIcmpResponseResultsItem ,
71
74
node_info : & PerformIcmpResponseNodeInfo ,
72
75
) {
73
- let width = 80 ; // Adjust this value as needed
76
+ let width = 80 ;
74
77
print_border ( pb, width) ;
75
78
let ip_address = jobres
76
79
. result
77
80
. as_ref ( )
78
81
. map_or ( "Unknown" . to_string ( ) , |r| r. ip_address . clone ( ) ) ;
79
82
format_ping_header ( pb, config, & jobres. endpoint , & ip_address, node_info) ;
80
83
81
- // Request timeout for icmp_seq 0, 1, 2, 3
82
84
let attempts = jobres. result . as_ref ( ) . map_or ( 4 , |r| r. attempts as usize ) ;
83
85
for index in 0 ..attempts {
84
86
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 ;
86
88
}
87
89
88
- // --- asdasdasd.com ping statistics ---
89
90
pb. println ( format ! ( "│ --- {} ping statistics ---" , jobres. endpoint) ) ;
91
+ sleep_if_enabled ( config, 250 ) . await ;
90
92
91
- std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 250 ) ) ;
92
-
93
- // 5 packets transmitted, 0 packets received, 100.0% packet loss
94
93
let error_string = if let Some ( result) = & jobres. result {
95
94
format ! (
96
95
"│ {} packets transmitted, {} packets received, {:.1}% packet loss" ,
@@ -104,16 +103,16 @@ pub fn display_failed_ping(
104
103
attempts
105
104
)
106
105
} ;
107
- std :: thread :: sleep ( std :: time :: Duration :: from_millis ( 250 ) ) ;
106
+ sleep_if_enabled ( config , 250 ) . await ;
108
107
109
108
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 ;
111
110
112
111
print_footer ( pb, width) ;
113
112
}
114
113
115
114
pub fn format_ping_header (
116
- pb : & indicatif :: ProgressBar ,
115
+ pb : & ProgressBar ,
117
116
config : & Opts ,
118
117
endpoint : & str ,
119
118
ip_address : & str ,
0 commit comments