Skip to content

Commit cf4e6b3

Browse files
committed
add borders
1 parent 29190d7 commit cf4e6b3

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

src/display/ping_display.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,73 @@
1-
use colorful::{Color, Colorful};
2-
31
use crate::{
42
models::types::{
53
PerformIcmpResponseNodeInfo, PerformIcmpResponseResultsItem,
64
PerformIcmpResponseResultsItemResult,
75
},
86
options::Opts,
97
};
8+
use colorful::{Color, Colorful};
109
use std::*;
1110

12-
/*
13-
* PING bitping.com (76.76.21.21): 56 data bytes
14-
64 bytes from 76.76.21.21: icmp_seq=0 ttl=120 time=16.181 ms
15-
64 bytes from 76.76.21.21: icmp_seq=1 ttl=120 time=10.129 ms
16-
64 bytes from 76.76.21.21: icmp_seq=2 ttl=120 time=15.644 ms
17-
^C
18-
--- bitping.com ping statistics ---
19-
4 packets transmitted, 4 packets received, 0.0% packet loss
20-
round-trip min/avg/max/stddev = 10.127/13.020/16.181/2.898 ms
21-
*
22-
*/
11+
fn print_border(pb: &indicatif::ProgressBar, width: usize) {
12+
pb.println("┌".to_string() + &"─".repeat(width - 2) + "┐");
13+
}
14+
15+
fn print_footer(pb: &indicatif::ProgressBar, width: usize) {
16+
pb.println("└".to_string() + &"─".repeat(width - 2) + "┘");
17+
}
18+
2319
pub fn display_success_ping(
2420
pb: &indicatif::ProgressBar,
2521
config: &Opts,
2622
endpoint: &str,
2723
jobres: &PerformIcmpResponseResultsItemResult,
2824
node_info: &PerformIcmpResponseNodeInfo,
2925
) {
26+
let width = 80; // Adjust this value as needed
27+
print_border(pb, width);
3028
format_ping_header(pb, config, endpoint, &jobres.ip_address, node_info);
3129

3230
// Display individual ping results
3331
let trips = jobres.trips as usize;
3432
for i in 0..trips {
3533
let time = jobres.min + (jobres.max - jobres.min) * (i as f64 / (trips - 1) as f64);
3634
pb.println(format!(
37-
"64 bytes from {}: icmp_seq={} ttl=120 time={:.2} ms",
35+
"64 bytes from {}: icmp_seq={} ttl=120 time={:.2} ms",
3836
jobres.ip_address, i, time
3937
));
4038
std::thread::sleep(std::time::Duration::from_millis(500));
4139
}
4240

43-
pb.println(""); // Empty line for spacing
41+
pb.println(""); // Empty line for spacing
4442

4543
// Construct and print statistics line
46-
pb.println(format!("--- {endpoint} ping statistics ---"));
44+
pb.println(format!("--- {endpoint} ping statistics ---"));
4745

4846
// Print packet loss information
4947
pb.println(format!(
50-
"{} packets transmitted, {} packets received, {:.1}% packet loss",
48+
"{} packets transmitted, {} packets received, {:.1}% packet loss",
5149
jobres.packets_sent,
5250
jobres.packets_recv,
5351
jobres.packet_loss * 100.0
5452
));
5553

5654
// Print round-trip statistics
5755
pb.println(format!(
58-
"round-trip min/avg/max/stddev = {:.3}/{:.3}/{:.3}/{:.3} ms",
56+
"round-trip min/avg/max/stddev = {:.3}/{:.3}/{:.3}/{:.3} ms",
5957
jobres.min, jobres.avg, jobres.max, jobres.std_dev
6058
));
59+
60+
print_footer(pb, width);
6161
}
6262

63-
/*
64-
* PING asdasdasd.com (199.59.242.153): 56 data bytes
65-
Request timeout for icmp_seq 0
66-
Request timeout for icmp_seq 1
67-
Request timeout for icmp_seq 2
68-
Request timeout for icmp_seq 3
69-
^C
70-
--- asdasdasd.com ping statistics ---
71-
5 packets transmitted, 0 packets received, 100.0% packet loss
72-
*/
7363
pub fn display_failed_ping(
7464
pb: &indicatif::ProgressBar,
7565
config: &Opts,
7666
jobres: &PerformIcmpResponseResultsItem,
7767
node_info: &PerformIcmpResponseNodeInfo,
7868
) {
69+
let width = 80; // Adjust this value as needed
70+
print_border(pb, width);
7971
let ip_address = jobres
8072
.result
8173
.as_ref()
@@ -85,28 +77,30 @@ pub fn display_failed_ping(
8577
// Request timeout for icmp_seq 0, 1, 2, 3
8678
let attempts = jobres.result.as_ref().map_or(4, |r| r.attempts as usize);
8779
for index in 0..attempts {
88-
pb.println(format!("Request timeout for icmp_seq {}", index));
80+
pb.println(format!("Request timeout for icmp_seq {}", index));
8981
std::thread::sleep(std::time::Duration::from_millis(500));
9082
}
9183

9284
// --- asdasdasd.com ping statistics ---
93-
pb.println(format!("--- {} ping statistics ---", jobres.endpoint));
85+
pb.println(format!("--- {} ping statistics ---", jobres.endpoint));
9486

9587
// 5 packets transmitted, 0 packets received, 100.0% packet loss
9688
let error_string = if let Some(result) = &jobres.result {
9789
format!(
98-
"{} packets transmitted, {} packets received, {:.1}% packet loss",
90+
"{} packets transmitted, {} packets received, {:.1}% packet loss",
9991
result.packets_sent,
10092
result.packets_recv,
10193
result.packet_loss * 100.0
10294
)
10395
} else {
10496
format!(
105-
"{} packets transmitted, 0 packets received, 100% packet loss",
97+
"{} packets transmitted, 0 packets received, 100% packet loss",
10698
attempts
10799
)
108100
};
109101
pb.println(format!("{}", error_string.color(Color::Red)));
102+
103+
print_footer(pb, width);
110104
}
111105

112106
pub fn format_ping_header(
@@ -117,7 +111,7 @@ pub fn format_ping_header(
117111
node_info: &PerformIcmpResponseNodeInfo,
118112
) {
119113
// PING line
120-
let ping_line = format!("PING {} ({}): 56 data bytes", endpoint, ip_address);
114+
let ping_line = format!("PING {} ({}): 56 data bytes", endpoint, ip_address);
121115
pb.println(ping_line);
122116

123117
// Origin line
@@ -138,7 +132,7 @@ pub fn format_ping_header(
138132
};
139133

140134
let origin_line = format!(
141-
"├── Origin: {} {}, {} {}",
135+
"├── Origin: {} {}, {} {}",
142136
country_emoji.unwrap_or(""),
143137
node_info.region_name.as_deref().unwrap_or("Unknown"),
144138
country_name,
@@ -147,16 +141,19 @@ pub fn format_ping_header(
147141
pb.println(origin_line);
148142

149143
// ISP line
150-
let isp_line = format!("├── ISP: {}", node_info.isp.as_deref().unwrap_or("Unknown"));
144+
let isp_line = format!(
145+
"│ ├── ISP: {}",
146+
node_info.isp.as_deref().unwrap_or("Unknown")
147+
);
151148
pb.println(isp_line);
152149

153150
// System line
154151
let system_line = format!(
155-
"└── System: {}",
152+
"└── System: {}",
156153
node_info.operating_system.as_deref().unwrap_or("Unknown")
157154
);
158155
pb.println(system_line);
159156

160157
// Separator line
161-
pb.println("---");
158+
pb.println("---");
162159
}

0 commit comments

Comments
 (0)