term39 1.5.1

A modern, retro-styled terminal multiplexer with a classic MS-DOS aesthetic
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
//! Network widget for the top bar
//!
//! Shows network interface status with signal strength for WiFi
//! or connection indicator for Ethernet.

use super::{Widget, WidgetAlignment, WidgetClickResult, WidgetContext};
use crate::rendering::{Cell, Theme, VideoBuffer};
use crate::window::manager::FocusState;
use crossterm::style::Color;
use std::cell::RefCell;
use std::time::{Duration, Instant};

/// Network information including interface name, connection state, and signal
#[derive(Clone)]
pub struct NetworkInfo {
    pub interface: String,
    pub is_connected: bool,
    pub is_wifi: bool,
    pub signal_strength: Option<u8>, // 0-100 for WiFi, None for Ethernet
}

/// Cached network info with last update time
struct NetworkCache {
    info: Option<NetworkInfo>,
    interface: String,
    last_update: Instant,
}

thread_local! {
    static NETWORK_CACHE: RefCell<NetworkCache> = RefCell::new(NetworkCache {
        info: None,
        interface: String::new(),
        last_update: Instant::now() - Duration::from_secs(2), // Force initial fetch
    });
}

/// Get the current network info (cached for 1 second)
pub fn get_network_info(interface: &str) -> Option<NetworkInfo> {
    NETWORK_CACHE.with(|cache| {
        let mut cache = cache.borrow_mut();

        // Check if interface changed (avoid allocation if same)
        let interface_changed = cache.interface != interface;

        // Refresh if interface changed or more than 1 second has passed
        if interface_changed || cache.last_update.elapsed() >= Duration::from_secs(1) {
            cache.info = fetch_network_info(interface);
            // Only allocate new string when interface actually changed
            if interface_changed {
                cache.interface = interface.to_string();
            }
            cache.last_update = Instant::now();
        }

        cache.info.clone()
    })
}

/// Actually fetch network info from the system
fn fetch_network_info(interface: &str) -> Option<NetworkInfo> {
    if interface.is_empty() {
        return None;
    }

    let is_wifi = is_wifi_interface(interface);
    let is_connected = check_interface_connected(interface);
    let signal_strength = if is_wifi && is_connected {
        get_wifi_signal_strength(interface)
    } else {
        None
    };

    Some(NetworkInfo {
        interface: interface.to_string(),
        is_connected,
        is_wifi,
        signal_strength,
    })
}

/// Check if interface is a WiFi interface based on common naming conventions
fn is_wifi_interface(interface: &str) -> bool {
    // Linux WiFi interfaces
    interface.starts_with("wlan")
        || interface.starts_with("wlp")
        || interface.starts_with("wifi")
        || interface.starts_with("ath")
        || interface.starts_with("ra")
        // macOS: en0 is typically WiFi on laptops, but we need to check
        || (cfg!(target_os = "macos") && is_macos_wifi_interface(interface))
}

/// Check if a macOS interface is WiFi by looking at ifconfig output
#[cfg(target_os = "macos")]
fn is_macos_wifi_interface(interface: &str) -> bool {
    // On macOS, check if the interface type indicates WiFi
    if let Ok(output) = std::process::Command::new("networksetup")
        .args(["-listallhardwareports"])
        .output()
    {
        let stdout = String::from_utf8_lossy(&output.stdout);
        // Parse output looking for Wi-Fi followed by our interface
        let mut is_wifi_section = false;
        for line in stdout.lines() {
            if line.contains("Wi-Fi") || line.contains("AirPort") {
                is_wifi_section = true;
            } else if line.starts_with("Hardware Port:") {
                is_wifi_section = false;
            } else if is_wifi_section && line.contains("Device:") && line.contains(interface) {
                return true;
            }
        }
    }
    false
}

#[cfg(not(target_os = "macos"))]
fn is_macos_wifi_interface(_interface: &str) -> bool {
    false
}

/// Check if interface is connected (up and running) - Linux
#[cfg(target_os = "linux")]
fn check_interface_connected(interface: &str) -> bool {
    let operstate_path = format!("/sys/class/net/{}/operstate", interface);
    if let Ok(state) = std::fs::read_to_string(&operstate_path) {
        state.trim() == "up"
    } else {
        false
    }
}

/// Check if interface is connected - macOS
#[cfg(target_os = "macos")]
fn check_interface_connected(interface: &str) -> bool {
    // Use ifconfig to check interface status on macOS
    if let Ok(output) = std::process::Command::new("ifconfig")
        .arg(interface)
        .output()
    {
        let stdout = String::from_utf8_lossy(&output.stdout);
        // Check if interface is up and has an IP address
        stdout.contains("status: active") || (stdout.contains("UP") && stdout.contains("inet "))
    } else {
        false
    }
}

/// Check if interface is connected - other platforms (basic existence check)
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn check_interface_connected(_interface: &str) -> bool {
    // On other platforms, we can't easily check interface state
    // Return false to indicate unknown/disconnected
    false
}

/// Get WiFi signal strength from /proc/net/wireless (Linux only)
#[cfg(target_os = "linux")]
fn get_wifi_signal_strength(interface: &str) -> Option<u8> {
    let contents = std::fs::read_to_string("/proc/net/wireless").ok()?;

    for line in contents.lines().skip(2) {
        // Skip header lines
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.is_empty() {
            continue;
        }

        // Interface name ends with ':'
        let iface = parts[0].trim_end_matches(':');
        if iface == interface {
            // Column 3 is link quality (0-70 typically)
            if let Some(quality_str) = parts.get(2) {
                if let Ok(quality) = quality_str.trim_end_matches('.').parse::<f32>() {
                    // Convert to percentage (70 = 100%)
                    let percentage = ((quality / 70.0) * 100.0).min(100.0) as u8;
                    return Some(percentage);
                }
            }
        }
    }
    None
}

/// Get WiFi signal strength on macOS using airport utility
#[cfg(target_os = "macos")]
fn get_wifi_signal_strength(_interface: &str) -> Option<u8> {
    // Use the airport command-line tool to get signal strength
    let airport_path =
        "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport";
    if let Ok(output) = std::process::Command::new(airport_path).arg("-I").output() {
        let stdout = String::from_utf8_lossy(&output.stdout);
        for line in stdout.lines() {
            if line.contains("agrCtlRSSI:") {
                // RSSI is typically -30 (excellent) to -90 (poor)
                if let Some(rssi_str) = line.split(':').nth(1) {
                    if let Ok(rssi) = rssi_str.trim().parse::<i32>() {
                        // Convert RSSI to percentage: -30 = 100%, -90 = 0%
                        let percentage = ((rssi + 90) * 100 / 60).clamp(0, 100) as u8;
                        return Some(percentage);
                    }
                }
            }
        }
    }
    None
}

/// Get WiFi signal strength - other platforms (not supported)
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn get_wifi_signal_strength(_interface: &str) -> Option<u8> {
    None
}

use crate::rendering::Charset;

/// Get signal bars based on strength percentage using charset
fn get_signal_bars(strength: u8, charset: &Charset) -> [char; 4] {
    let s1 = charset.network_signal_1;
    let s2 = charset.network_signal_2;
    let s3 = charset.network_signal_3;
    let s4 = charset.network_signal_4;

    if strength >= 80 {
        [s1, s2, s3, s4] // full signal
    } else if strength >= 60 {
        [s1, s2, s3, ' '] // good signal
    } else if strength >= 40 {
        [s1, s2, ' ', ' '] // fair signal
    } else if strength >= 20 {
        [s1, ' ', ' ', ' '] // weak signal
    } else {
        [' ', ' ', ' ', ' '] // no bars - very weak
    }
}

/// Get color based on signal strength
fn get_signal_color(strength: u8) -> Color {
    if strength >= 60 {
        Color::Green
    } else if strength >= 40 {
        Color::Yellow
    } else {
        Color::Red
    }
}

/// Widget displaying network status
pub struct NetworkWidget {
    hovered: bool,
    cached_info: Option<NetworkInfo>,
    interface: String,
    enabled: bool,
}

impl NetworkWidget {
    pub fn new() -> Self {
        Self {
            hovered: false,
            cached_info: None,
            interface: String::new(),
            enabled: false,
        }
    }

    /// Configure the widget with interface name and enabled state
    pub fn configure(&mut self, interface: &str, enabled: bool) {
        self.interface = interface.to_string();
        self.enabled = enabled;
    }

    /// Returns whether the network widget is currently hovered
    #[allow(dead_code)]
    pub fn is_hovered(&self) -> bool {
        self.hovered
    }
}

impl Default for NetworkWidget {
    fn default() -> Self {
        Self::new()
    }
}

impl Widget for NetworkWidget {
    fn width(&self) -> u16 {
        if !self.enabled || self.interface.is_empty() {
            return 0;
        }

        if let Some(ref info) = self.cached_info {
            // Format: " wlan0 ▂▄▆█  " or " eth0 ▣  " or " wlan0 ✗  "
            let status_len = if info.is_wifi && info.is_connected && info.signal_strength.is_some()
            {
                4 // signal bars
            } else {
                1 // single char (▣ or ✗)
            };
            // " " + interface + " " + status + "  " (two trailing spaces for margin)
            (info.interface.len() + status_len + 4) as u16
        } else {
            0
        }
    }

    fn render(&self, buffer: &mut VideoBuffer, x: u16, theme: &Theme, ctx: &WidgetContext) {
        let info = match &self.cached_info {
            Some(info) => info,
            None => return,
        };

        let bg_color = match ctx.focus {
            FocusState::Desktop | FocusState::Topbar => theme.topbar_bg_focused,
            FocusState::Window(_) => theme.topbar_bg_unfocused,
        };
        let fg_color = theme.window_border_unfocused_fg;
        let charset = ctx.charset;

        let mut current_x = x;

        // Leading space
        buffer.set(current_x, 0, Cell::new_unchecked(' ', fg_color, bg_color));
        current_x += 1;

        // Interface name
        for ch in info.interface.chars() {
            buffer.set(current_x, 0, Cell::new_unchecked(ch, fg_color, bg_color));
            current_x += 1;
        }

        // Space before status
        buffer.set(current_x, 0, Cell::new_unchecked(' ', fg_color, bg_color));
        current_x += 1;

        if info.is_connected {
            if info.is_wifi {
                // WiFi: show signal bars if available
                if let Some(strength) = info.signal_strength {
                    let bars = get_signal_bars(strength, charset);
                    let color = get_signal_color(strength);
                    for ch in bars {
                        buffer.set(current_x, 0, Cell::new_unchecked(ch, color, bg_color));
                        current_x += 1;
                    }
                } else {
                    // Connected but no signal info - show connected icon
                    buffer.set(
                        current_x,
                        0,
                        Cell::new_unchecked(charset.network_connected, Color::Green, bg_color),
                    );
                    current_x += 1;
                }
            } else {
                // Ethernet: show connected icon
                buffer.set(
                    current_x,
                    0,
                    Cell::new_unchecked(charset.network_connected, Color::Green, bg_color),
                );
                current_x += 1;
            }
        } else {
            // Disconnected: show X
            buffer.set(
                current_x,
                0,
                Cell::new_unchecked(charset.network_disconnected, Color::Red, bg_color),
            );
            current_x += 1;
        }

        // Trailing spaces (margin)
        buffer.set(current_x, 0, Cell::new_unchecked(' ', fg_color, bg_color));
        current_x += 1;
        buffer.set(current_x, 0, Cell::new_unchecked(' ', fg_color, bg_color));
    }

    fn is_visible(&self, _ctx: &WidgetContext) -> bool {
        self.enabled && !self.interface.is_empty() && self.cached_info.is_some()
    }

    fn contains(&self, point_x: u16, point_y: u16, widget_x: u16) -> bool {
        point_y == 0 && point_x >= widget_x && point_x < widget_x + self.width()
    }

    fn update_hover(&mut self, mouse_x: u16, mouse_y: u16, widget_x: u16) {
        self.hovered = self.contains(mouse_x, mouse_y, widget_x);
    }

    fn handle_click(&mut self, _mouse_x: u16, _mouse_y: u16, _widget_x: u16) -> WidgetClickResult {
        // Network widget doesn't respond to clicks
        WidgetClickResult::NotHandled
    }

    fn reset_state(&mut self) {
        self.hovered = false;
    }

    fn update(&mut self, _ctx: &WidgetContext) {
        if self.enabled && !self.interface.is_empty() {
            self.cached_info = get_network_info(&self.interface);
        } else {
            self.cached_info = None;
        }
    }

    fn alignment(&self) -> WidgetAlignment {
        WidgetAlignment::Right
    }
}