pingmon 0.1.8

Real-time ping monitor with beautiful ASCII charts, TTL display, and statistics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// File: src/pingmon2.rs
// Real-time Ping Monitor with BAR CHART
// Author: Hadi Cahyadi <cumulus13@gmail.com>

use std::collections::VecDeque;
use std::net::IpAddr;
use std::env;
use std::path::PathBuf;
use std::fs;
use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
use std::time::{Duration, Instant};
use std::thread;
use std::io::{self, Write};
use clap::Parser;
use colored::*;
use surge_ping::{Client, Config as PingConfig, PingIdentifier, PingSequence, IcmpPacket};
use gntp::{GntpClient, NotificationType, NotifyOptions, Resource};
use serde::Deserialize;

#[derive(Parser, Debug)]
#[clap(author, version, about = "Ping monitor with bar chart")]
struct Args {
    /// Target host to ping
    #[clap(default_value = "8.8.8.8")]
    host: String,

    /// Chart height
    #[clap(short = 'H', long, default_value = "12")]
    height: usize,

    /// Chart width (0 = auto)
    #[clap(short = 'W', long, default_value = "0")]
    width: usize,

    /// Interval between pings (seconds)
    #[clap(short, long, default_value = "1.0")]
    interval: f64,

    /// Static mode: simple line-by-line output without chart
    #[clap(short, long)]
    static_mode: bool,

    /// Chart-only mode: only show chart and current status
    #[clap(short, long)]
    chart_only: bool,
    
    /// Timeout threshold in seconds before sending notification
    #[clap(short, long)]
    timeout_threshold: Option<u64>,
}

#[derive(Debug, Deserialize, Default)]
struct AppConfig {
    #[serde(default = "default_timeout")]
    pingmon_timeout: u64,
}

fn default_timeout() -> u64 {
    60 // 1 minute default
}

impl AppConfig {
    fn load() -> Self {
        // Try to get from environment variable first
        if let Ok(timeout_str) = env::var("PINGMON_TIMEOUT") {
            if let Ok(timeout) = timeout_str.parse::<u64>() {
                return AppConfig {
                    pingmon_timeout: timeout,
                };
            }
        }

        // Try to load from config file
        if let Some(config_path) = get_config_file() {
            if let Ok(contents) = fs::read_to_string(&config_path) {
                // Try different formats based on file extension
                let ext = config_path.extension()
                    .and_then(|e| e.to_str())
                    .unwrap_or("");
                
                match ext {
                    "toml" => {
                        if let Ok(config) = toml::from_str::<AppConfig>(&contents) {
                            return config;
                        }
                    }
                    "json" => {
                        if let Ok(config) = serde_json::from_str::<AppConfig>(&contents) {
                            return config;
                        }
                    }
                    "yml" | "yaml" => {
                        if let Ok(config) = serde_yaml::from_str::<AppConfig>(&contents) {
                            return config;
                        }
                    }
                    "ini" => {
                        // Simple INI parser for pingmon_timeout
                        for line in contents.lines() {
                            let line = line.trim();
                            if line.starts_with("pingmon_timeout") {
                                if let Some(value) = line.split('=').nth(1) {
                                    if let Ok(timeout) = value.trim().parse::<u64>() {
                                        return AppConfig {
                                            pingmon_timeout: timeout,
                                        };
                                    }
                                }
                            }
                        }
                    }
                    "env" => {
                        // Parse .env file
                        for line in contents.lines() {
                            let line = line.trim();
                            if line.starts_with("PINGMON_TIMEOUT") {
                                if let Some(value) = line.split('=').nth(1) {
                                    if let Ok(timeout) = value.trim().parse::<u64>() {
                                        return AppConfig {
                                            pingmon_timeout: timeout,
                                        };
                                    }
                                }
                            }
                        }
                    }
                    _ => {}
                }
            }
        }

        // Return default if nothing found
        AppConfig::default()
    }
}

fn get_config_file() -> Option<PathBuf> {
    let config_files = if cfg!(windows) {
        vec![
            dirs::home_dir()?.join(".pingmon").join(".env"),
            dirs::config_dir()?.join(".pingmon").join(".env"),
            
            dirs::home_dir()?.join(".pingmon").join("pingmon.ini"),
            dirs::config_dir()?.join(".pingmon").join("pingmon.ini"),
            
            dirs::home_dir()?.join(".pingmon").join("pingmon.toml"),
            dirs::config_dir()?.join(".pingmon").join("pingmon.toml"),
            
            dirs::home_dir()?.join(".pingmon").join("pingmon.json"),
            dirs::config_dir()?.join(".pingmon").join("pingmon.json"),
            
            dirs::home_dir()?.join(".pingmon").join("pingmon.yml"),
            dirs::config_dir()?.join(".pingmon").join("pingmon.yml"),
        ]
    } else {
        vec![
            dirs::home_dir()?.join(".pingmon").join(".env"),
            dirs::config_dir()?.join(".pingmon").join(".env"),
            dirs::config_dir()?.join(".env"),
            
            dirs::home_dir()?.join(".pingmon").join("pingmon.ini"),
            dirs::config_dir()?.join(".pingmon").join("pingmon.ini"),
            dirs::config_dir()?.join("pingmon.ini"),
            
            dirs::home_dir()?.join(".pingmon").join("pingmon.toml"),
            dirs::config_dir()?.join(".pingmon").join("pingmon.toml"),
            dirs::config_dir()?.join("pingmon.toml"),
            
            dirs::home_dir()?.join(".pingmon").join("pingmon.json"),
            dirs::config_dir()?.join(".pingmon").join("pingmon.json"),
            dirs::config_dir()?.join("pingmon.json"),
            
            dirs::home_dir()?.join(".pingmon").join("pingmon.yml"),
            dirs::config_dir()?.join(".pingmon").join("pingmon.yml"),
            dirs::config_dir()?.join("pingmon.yml"),
        ]
    };

    for path in config_files {
        if path.exists() {
            return Some(path);
        }
    }

    None
}

#[derive(Clone)]
struct Stats {
    sent: u64,
    received: u64,
    lost: u64,
    min: f64,
    max: f64,
    latencies: Vec<f64>,
}

impl Stats {
    fn new() -> Self {
        Self {
            sent: 0,
            received: 0,
            lost: 0,
            min: f64::INFINITY,
            max: 0.0,
            latencies: Vec::new(),
        }
    }

    fn avg(&self) -> f64 {
        if self.latencies.is_empty() {
            0.0
        } else {
            self.latencies.iter().sum::<f64>() / self.latencies.len() as f64
        }
    }

    fn stddev(&self) -> f64 {
        if self.latencies.len() < 2 {
            return 0.0;
        }
        let avg = self.avg();
        let variance = self.latencies.iter()
            .map(|&x| (x - avg).powi(2))
            .sum::<f64>() / (self.latencies.len() - 1) as f64;
        variance.sqrt()
    }

    fn loss_pct(&self) -> f64 {
        if self.sent == 0 {
            0.0
        } else {
            (self.lost as f64 / self.sent as f64) * 100.0
        }
    }
}

struct TimeoutTracker {
    timeout_start: Option<Instant>,
    notification_sent: bool,
    threshold_seconds: u64,
}

impl TimeoutTracker {
    fn new(threshold_seconds: u64) -> Self {
        Self {
            timeout_start: None,
            notification_sent: false,
            threshold_seconds,
        }
    }

    fn update(&mut self, is_timeout: bool) -> bool {
        if is_timeout {
            if self.timeout_start.is_none() {
                // First timeout detected
                self.timeout_start = Some(Instant::now());
                self.notification_sent = false;
            } else if !self.notification_sent {
                // Check if timeout duration exceeds threshold
                if let Some(start) = self.timeout_start {
                    let elapsed = start.elapsed().as_secs();
                    if elapsed >= self.threshold_seconds {
                        self.notification_sent = true;
                        return true; // Signal to send notification
                    }
                }
            }
        } else {
            // Connection restored
            self.timeout_start = None;
            self.notification_sent = false;
        }
        false
    }

    fn get_timeout_duration(&self) -> Option<u64> {
        self.timeout_start.map(|start| start.elapsed().as_secs())
    }
}

fn get_icon_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
    if cfg!(debug_assertions) {
        // Development mode
        Ok(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("pingmon.png"))
    } else {
        // Production mode
        let exe_path = env::current_exe()?;
        let exe_dir = exe_path.parent()
            .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "Cannot get executable directory"))?;
        
        let possible_paths = vec![
            exe_dir.join("pingmon.png"),
            exe_dir.join("icons").join("pingmon.png"),
            exe_dir.join("resources").join("pingmon.png"),
            env::current_dir()?.join("pingmon.png"),
            PathBuf::from("pingmon.png"),
        ];
        
        // Find first existing file
        for path in possible_paths {
            if path.exists() {
                return Ok(path);
            }
        }
        
        // Return default even if not exists
        Ok(PathBuf::from("pingmon.png"))
    }
}

fn send_notification(title: &str, message: &str) -> Result<(), Box<dyn std::error::Error>> {
    let icon_mode = gntp::IconMode::Binary;
    let mut client = GntpClient::new("pingmon").with_icon_mode(icon_mode);
    let icon_path = get_icon_path()?;

    let notif_icon = Resource::from_file(&icon_path).ok();

    let mut notification = if let Some(ref icon) = notif_icon {
        NotificationType::new("alert")
            .with_display_name("Alert")
            .with_enabled(true)
            .with_icon(icon.clone())
    } else {
        NotificationType::new("alert")
            .with_display_name("Alert")
            .with_enabled(true)
    };
    
    if let Some(ref icon) = notif_icon {
        notification = notification.with_icon(icon.clone());
    }

    client.register(vec![notification])?;

    let mut options = NotifyOptions::new();
    if let Some(ref icon) = notif_icon {
        options = options.with_icon(icon.clone());
    }

    client.notify_with_options(
        "alert",
        title,
        message,
        options
    )?;

    Ok(())
}

fn ping_once_sync(host: &str) -> (f64, u8, bool) {
    let rt = match tokio::runtime::Runtime::new() {
        Ok(rt) => rt,
        Err(_) => return (0.0, 0, false),
    };

    rt.block_on(async {
        let addr: IpAddr = match host.parse() {
            Ok(ip) => ip,
            Err(_) => {
                match tokio::net::lookup_host(format!("{}:0", host)).await {
                    Ok(mut addrs) => {
                        if let Some(addr) = addrs.next() {
                            addr.ip()
                        } else {
                            return (0.0, 0, false);
                        }
                    }
                    Err(_) => return (0.0, 0, false),
                }
            }
        };

        let client = match Client::new(&PingConfig::default()) {
            Ok(c) => c,
            Err(_) => return (0.0, 0, false),
        };

        let mut pinger = client.pinger(addr, PingIdentifier(rand::random())).await;
        match pinger.ping(PingSequence(0), &[0; 56]).await {
            Ok((IcmpPacket::V4(packet), duration)) => {
                let ttl = packet.get_ttl().unwrap_or(0);
                (duration.as_secs_f64() * 1000.0, ttl, true)
            },
            Ok((IcmpPacket::V6(_packet), duration)) => {
                // IPv6 uses hop limit instead of TTL
                (duration.as_secs_f64() * 1000.0, 0, true)
            }
            Err(_) => (0.0, 0, false),
        }
    })
}

fn draw_bar_chart(history: &VecDeque<f64>, height: usize, width: usize) -> Vec<String> {
    let mut lines = Vec::new();

    if history.len() < 2 {
        lines.push("Collecting data...".to_string());
        return lines;
    }

    let max_val = history.iter().fold(0.0f64, |a, &b| a.max(b));
    if max_val == 0.0 {
        lines.push("No data".to_string());
        return lines;
    }

    let step = (max_val / height as f64).max(1.0);

    // Draw bars from top to bottom
    for h in (0..height).rev() {
        let threshold = (h + 1) as f64 * step;
        let mut line = format!("{:>6.1} │", threshold);

        for (i, &val) in history.iter().enumerate() {
            if i >= width { break; }

            let bar_height = if val > 0.0 {
                ((val / step).ceil() as usize).min(height)
            } else {
                0
            };

            // Full fill graph - use block characters
            if h < bar_height {
                line.push('â–ˆ');
            } else {
                line.push(' ');
            }
        }
        lines.push(line);
    }

    // FIX: Bottom axis aligned with vertical line (add space for alignment)
    let axis_width = width.min(history.len());
    lines.push(format!("{:>6}  └{}", "0.0", "─".repeat(axis_width)));

    lines
}

fn get_term_size() -> (u16, u16) {
    term_size::dimensions()
        .map(|(w, h)| (w as u16, h as u16))
        .unwrap_or((80, 24))
}

fn move_cursor_home() {
    print!("\x1B[H");
    let _ = io::stdout().flush();
}

fn clear_line_to_end() {
    print!("\x1B[K");
}

fn render_static_line(stats: &Stats, lat: f64, ttl: u8, ok: bool, timeout_duration: Option<u64>) {
    // Simple one-line output for static mode
    let status = if ok {
        format!("{:.2}ms", lat).green()
    } else {
        let mut timeout_msg = "TIMEOUT".to_string();
        if let Some(duration) = timeout_duration {
            timeout_msg.push_str(&format!(" ({}s)", duration));
        }
        timeout_msg.red()
    };

    let ttl_str = if ok {
        format!("ttl={}", ttl).yellow()
    } else {
        "ttl=-".yellow()
    };

    print!("seq={} {} {} ",
        format!("{}", stats.sent).cyan(),
        status,
        ttl_str
    );

    if stats.received > 0 {
        print!("(loss={:.1}% avg={:.2}ms)",
            stats.loss_pct(),
            stats.avg()
        );
    }

    println!();
}

fn render_chart_only(args: &Args, history: &VecDeque<f64>, lat: f64, ttl: u8, ok: bool, chart_width: usize, timeout_duration: Option<u64>) {
    move_cursor_home();

    // Single status line
    print!("{} ", "Latency:".bright_cyan().bold());
    if ok {
        print!("{}", format!(" {:.2} ms ", lat).white().on_blue());
    } else {
        let mut timeout_msg = " TIMEOUT ".to_string();
        if let Some(duration) = timeout_duration {
            timeout_msg = format!(" TIMEOUT ({}s) ", duration);
        }
        print!("{}", timeout_msg.white().on_red());
    }
    print!(" | ");
    print!("{} ", "TTL:".bright_green().bold());
    print!("{}", format!(" {} ", ttl).black().on_green());
    print!(" | ");
    print!("{} ", "Status:".bold());
    if ok {
        print!("{}", " CONNECTED ".black().on_truecolor(0, 255, 255));
    } else {
        print!("{}", " TIMEOUT ".white().on_red());
    }
    print!(" | ");
    print!("{} ", "Host:".bright_magenta().bold());
    print!("{}", args.host.yellow());
    clear_line_to_end();
    println!();

    // Chart only (NO duplicate header)
    if history.len() > 1 {
        print!("{}", "Latency History (ms):".bright_cyan().bold());
        clear_line_to_end();
        println!();

        let chart_lines = draw_bar_chart(history, args.height, chart_width);

        let lines_count = chart_lines.len();
        for (i, line) in chart_lines.into_iter().enumerate() {
            print!("{}", line.yellow());
            clear_line_to_end();
            // NO newline after last line
            if i < lines_count - 1 {
                println!();
            }
        }
    }

    // Clear rest of screen (NO extra newline)
    print!("\x1B[J");
    let _ = io::stdout().flush();
}

fn render_dynamic_screen(args: &Args, stats: &Stats, history: &VecDeque<f64>, lat: f64, ttl: u8, ok: bool, chart_width: usize, timeout_duration: Option<u64>) {
    move_cursor_home();

    // Header
    print!("{}", format!("=== Real-time Ping Monitor: {} ===", args.host).bright_magenta().bold());
    clear_line_to_end();
    println!();

    // Status line (NO extra newline before)
    print!("{} ", "Latency:".bright_cyan().bold());
    if ok {
        print!("{}", format!(" {:.2} ms ", lat).white().on_blue());
    } else {
        let mut timeout_msg = " TIMEOUT ".to_string();
        if let Some(duration) = timeout_duration {
            timeout_msg = format!(" TIMEOUT ({}s) ", duration);
        }
        print!("{}", timeout_msg.white().on_red());
    }
    print!(" | ");
    print!("{} ", "TTL:".bright_green().bold());
    print!("{}", format!(" {} ", ttl).black().on_green());
    print!(" | ");
    print!("{} ", "Status:".bold());
    if ok {
        print!("{}", " CONNECTED ".black().on_truecolor(0, 255, 255));
    } else {
        print!("{}", " TIMEOUT ".white().on_red());
    }
    clear_line_to_end();
    println!();

    // Stats
    print!("{}", "Statistics:".bright_yellow().bold());
    clear_line_to_end();
    println!();
    print!("  Sent: {} | Received: {} | Lost: {} ({})",
        format!("{}", stats.sent).cyan(),
        format!("{}", stats.received).green(),
        format!("{}", stats.lost).red(),
        format!("{:.1}%", stats.loss_pct()).red()
    );
    clear_line_to_end();
    println!();

    if stats.received > 0 {
        print!("  Min: {} | Avg: {} | Max: {} | StdDev: {}",
            format!("{:.2}ms", stats.min).green(),
            format!("{:.2}ms", stats.avg()).yellow(),
            format!("{:.2}ms", stats.max).red(),
            format!("{:.2}ms", stats.stddev()).cyan()
        );
        clear_line_to_end();
        println!();
    }

    // BAR Chart (NO extra newline before header)
    if history.len() > 1 {
        print!("{}", "Latency History (ms):".bright_cyan().bold());
        clear_line_to_end();
        println!();

        let chart_lines = draw_bar_chart(history, args.height, chart_width);

        let lines_count = chart_lines.len();
        for (i, line) in chart_lines.into_iter().enumerate() {
            print!("{}", line.yellow());
            clear_line_to_end();
            // NO newline after last line
            if i < lines_count - 1 {
                println!();
            }
        }
    }

    // Clear rest of screen (NO extra newline)
    print!("\x1B[J");
    let _ = io::stdout().flush();
}

fn print_final_stats(stats: &Stats) {
    println!("\n{}", "✓ Stopped".green().bold());
    println!("\n{}", "Final Statistics:".bright_yellow().bold());
    println!("  Packets: Sent = {}, Received = {}, Lost = {} ({})",
        stats.sent,
        stats.received,
        stats.lost,
        format!("{:.1}%", stats.loss_pct()).red()
    );

    if stats.received > 0 {
        println!("  Latency: Min = {}, Avg = {}, Max = {}, StdDev = {}",
            format!("{:.2}ms", stats.min).green(),
            format!("{:.2}ms", stats.avg()).yellow(),
            format!("{:.2}ms", stats.max).red(),
            format!("{:.2}ms", stats.stddev()).cyan()
        );
    }
}

fn main() {
    let args = Args::parse();
    let config = AppConfig::load();
    
    // Determine timeout threshold: CLI arg > config/env > default (60)
    let timeout_threshold = args.timeout_threshold.unwrap_or(config.pingmon_timeout);
    
    println!("{}", format!("Timeout notification threshold: {} seconds", timeout_threshold).bright_yellow());
    
    let running = Arc::new(AtomicBool::new(true));
    let r = running.clone();

    ctrlc::set_handler(move || {
        r.store(false, Ordering::SeqCst);
    }).expect("Error setting Ctrl-C handler");

    let (term_w, _) = get_term_size();
    let hist_size = if args.width > 0 {
        args.width
    } else {
        (term_w as usize).saturating_sub(14).max(50)
    };

    // FIX: chart_width must follow hist_size (current terminal width)
    let chart_width = hist_size;

    let mut history: VecDeque<f64> = VecDeque::with_capacity(hist_size);
    let mut stats = Stats::new();
    let mut timeout_tracker = TimeoutTracker::new(timeout_threshold);

    // Initial setup
    if !args.static_mode && !args.chart_only {
        // Clear screen once for dynamic mode
        print!("\x1B[2J\x1B[H");
        let _ = io::stdout().flush();
    } else if args.static_mode {
        // Print header for static mode
        println!("{}", format!("Pinging {} ...", args.host).bright_magenta().bold());
    } else if args.chart_only {
        // Clear screen once for chart-only mode
        print!("\x1B[2J\x1B[H");
        let _ = io::stdout().flush();
    }

    // Main loop
    while running.load(Ordering::SeqCst) {
        let (lat, ttl, ok) = ping_once_sync(&args.host);
        stats.sent += 1;

        if ok {
            stats.received += 1;
            stats.min = stats.min.min(lat);
            stats.max = stats.max.max(lat);
            stats.latencies.push(lat);
            history.push_back(lat);
        } else {
            stats.lost += 1;
            history.push_back(0.0);
        }

        if history.len() > hist_size {
            history.pop_front();
        }

        // Check timeout and send notification if threshold exceeded
        let should_notify = timeout_tracker.update(!ok);
        if should_notify {
            let duration = timeout_tracker.get_timeout_duration().unwrap_or(0);
            let message = format!("Connection timeout for {} seconds to {}", duration, args.host);
            let _ = send_notification("Pingmon: Connection Timeout", &message);
        }

        let timeout_duration = timeout_tracker.get_timeout_duration();

        // Render output based on mode
        if args.static_mode {
            render_static_line(&stats, lat, ttl, ok, timeout_duration);
        } else if args.chart_only {
            render_chart_only(&args, &history, lat, ttl, ok, chart_width, timeout_duration);
        } else {
            render_dynamic_screen(&args, &stats, &history, lat, ttl, ok, chart_width, timeout_duration);
        }

        if !running.load(Ordering::SeqCst) {
            break;
        }

        thread::sleep(Duration::from_secs_f64(args.interval));
    }

    // Print final statistics
    print_final_stats(&stats);
}