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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;

/// Authentication mode for lockscreen
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum LockscreenAuthMode {
    OsAuth, // PAM/macOS/Windows native auth
    #[default]
    Pin, // Alphanumeric PIN with local hash (default - always available)
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppConfig {
    #[serde(default = "default_auto_tiling_on_startup")]
    pub auto_tiling_on_startup: bool,
    #[serde(default = "default_tiling_gaps")]
    pub tiling_gaps: bool,
    #[serde(default = "default_show_date_in_clock")]
    pub show_date_in_clock: bool,
    #[serde(default = "default_theme")]
    pub theme: String,
    #[serde(default = "default_background_char_index")]
    pub background_char_index: usize,
    #[serde(default = "default_tint_terminal")]
    pub tint_terminal: bool,
    #[serde(default = "default_auto_save")]
    pub auto_save: bool,
    #[serde(default = "default_persist_enabled")]
    pub persist_enabled: bool,
    #[serde(default = "default_lockscreen_enabled")]
    pub lockscreen_enabled: bool,
    #[serde(default)]
    pub lockscreen_auth_mode: LockscreenAuthMode,
    #[serde(default)]
    pub lockscreen_pin_hash: Option<String>,
    #[serde(default)]
    pub lockscreen_salt: Option<String>,
    #[serde(default)]
    pub network_widget_enabled: bool,
    #[serde(default)]
    pub network_interface: String,
    #[serde(default = "default_keybinding_profile")]
    pub keybinding_profile: String,
}

fn default_keybinding_profile() -> String {
    "term39".to_string()
}

fn default_auto_tiling_on_startup() -> bool {
    false // Default to false (disabled at startup)
}

fn default_tiling_gaps() -> bool {
    true // Default to true (gaps between tiled windows for better visual separation)
}

fn default_show_date_in_clock() -> bool {
    true // Default to true (show date in clock)
}

fn default_theme() -> String {
    "classic".to_string()
}

fn default_background_char_index() -> usize {
    0 // Default to first option (light shade)
}

fn default_tint_terminal() -> bool {
    false // Default to false (preserve native ANSI colors)
}

fn default_auto_save() -> bool {
    true // Default to true (auto-save session on exit)
}

fn default_persist_enabled() -> bool {
    true // Default to true (background daemon keeps sessions alive)
}

fn default_lockscreen_enabled() -> bool {
    true // Default to true (maintains existing behavior)
}

impl Default for AppConfig {
    fn default() -> Self {
        Self {
            auto_tiling_on_startup: false,
            tiling_gaps: true,
            show_date_in_clock: true,
            theme: default_theme(),
            background_char_index: default_background_char_index(),
            tint_terminal: default_tint_terminal(),
            auto_save: default_auto_save(),
            persist_enabled: default_persist_enabled(),
            lockscreen_enabled: default_lockscreen_enabled(),
            lockscreen_auth_mode: LockscreenAuthMode::default(),
            lockscreen_pin_hash: None,
            lockscreen_salt: None,
            network_widget_enabled: false,
            network_interface: String::new(),
            keybinding_profile: default_keybinding_profile(),
        }
    }
}

impl AppConfig {
    /// Get the configuration file path
    /// Returns ~/Library/Application Support/term39/config.toml on macOS
    /// Returns ~/.config/term39/config.toml on Linux
    /// Returns %APPDATA%\term39\config.toml on Windows
    fn config_path() -> Option<PathBuf> {
        let config_dir = dirs::config_dir()?;
        let app_config_dir = config_dir.join("term39");
        Some(app_config_dir.join("config.toml"))
    }

    /// Load configuration from file, creating default if it doesn't exist
    pub fn load() -> Self {
        let path = match Self::config_path() {
            Some(p) => p,
            None => return Self::default(),
        };

        // If config file doesn't exist, create default
        if !path.exists() {
            let default_config = Self::default();
            let _ = default_config.save();
            return default_config;
        }

        // Read and parse config file
        match fs::read_to_string(&path) {
            Ok(contents) => toml::from_str(&contents).unwrap_or_default(),
            Err(_) => Self::default(),
        }
    }

    /// Save configuration to file
    pub fn save(&self) -> Result<(), Box<dyn std::error::Error>> {
        let path = Self::config_path().ok_or("Could not determine config path")?;

        // Create config directory if it doesn't exist
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }

        // Serialize and write config
        let toml_string = toml::to_string_pretty(self)?;
        fs::write(path, toml_string)?;

        Ok(())
    }

    /// Toggle auto tiling on startup setting and save
    pub fn toggle_auto_tiling_on_startup(&mut self) {
        self.auto_tiling_on_startup = !self.auto_tiling_on_startup;
        let _ = self.save();
    }

    /// Toggle tiling gaps setting and save
    pub fn toggle_tiling_gaps(&mut self) {
        self.tiling_gaps = !self.tiling_gaps;
        let _ = self.save();
    }

    /// Toggle show date in clock setting and save
    pub fn toggle_show_date_in_clock(&mut self) {
        self.show_date_in_clock = !self.show_date_in_clock;
        let _ = self.save();
    }

    /// Set theme and save
    #[allow(dead_code)]
    pub fn set_theme(&mut self, theme_name: String) {
        self.theme = theme_name;
        let _ = self.save();
    }

    /// Available background characters (5 options)
    pub const BACKGROUND_CHARS: [char; 5] = [
        'â–‘', // 0: Light shade (default)
        ' ', // 1: Empty/space (clean)
        'â–’', // 2: Medium shade
        'â–“', // 3: Dark shade
        'â–ˆ', // 4: Full block (100% solid)
    ];

    /// Available background character names for display
    pub const BACKGROUND_CHAR_NAMES: [&'static str; 5] = [
        "Light Shade",
        "Empty",
        "Medium Shade",
        "Dark Shade",
        "Full Block",
    ];

    /// Get the current background character
    pub fn get_background_char(&self) -> char {
        Self::BACKGROUND_CHARS
            .get(self.background_char_index)
            .copied()
            .unwrap_or(Self::BACKGROUND_CHARS[0])
    }

    /// Get the current background character name
    pub fn get_background_char_name(&self) -> &'static str {
        Self::BACKGROUND_CHAR_NAMES
            .get(self.background_char_index)
            .unwrap_or(&Self::BACKGROUND_CHAR_NAMES[0])
    }

    /// Cycle to the next background character and save
    pub fn cycle_background_char(&mut self) {
        self.background_char_index =
            (self.background_char_index + 1) % Self::BACKGROUND_CHARS.len();
        let _ = self.save();
    }

    /// Cycle to the previous background character and save
    pub fn cycle_background_char_backward(&mut self) {
        if self.background_char_index == 0 {
            self.background_char_index = Self::BACKGROUND_CHARS.len() - 1;
        } else {
            self.background_char_index -= 1;
        }
        let _ = self.save();
    }

    /// Toggle terminal tinting setting and save
    pub fn toggle_tint_terminal(&mut self) {
        self.tint_terminal = !self.tint_terminal;
        let _ = self.save();
    }

    /// Toggle auto-save setting and save
    pub fn toggle_auto_save(&mut self) {
        self.auto_save = !self.auto_save;
        let _ = self.save();
    }

    /// Toggle persist (background daemon) enabled state
    pub fn toggle_persist_enabled(&mut self) {
        self.persist_enabled = !self.persist_enabled;
        let _ = self.save();
    }

    /// Toggle lockscreen enabled state
    pub fn toggle_lockscreen_enabled(&mut self) {
        self.lockscreen_enabled = !self.lockscreen_enabled;
        if !self.lockscreen_enabled {
            // Clear PIN when disabling lockscreen
            self.lockscreen_pin_hash = None;
            self.lockscreen_salt = None;
        }
        let _ = self.save();
    }

    /// Cycle lockscreen auth mode (only switches if OS auth is available)
    pub fn cycle_lockscreen_auth_mode(&mut self, os_auth_available: bool) {
        self.lockscreen_auth_mode = match self.lockscreen_auth_mode {
            LockscreenAuthMode::OsAuth => LockscreenAuthMode::Pin,
            LockscreenAuthMode::Pin => {
                if os_auth_available {
                    LockscreenAuthMode::OsAuth
                } else {
                    LockscreenAuthMode::Pin // Stay on PIN if OS auth unavailable
                }
            }
        };
        let _ = self.save();
    }

    /// Check if PIN is configured
    pub fn has_pin_configured(&self) -> bool {
        self.lockscreen_pin_hash.is_some() && self.lockscreen_salt.is_some()
    }

    /// Set PIN hash and salt
    pub fn set_pin(&mut self, hash: String, salt: String) {
        self.lockscreen_pin_hash = Some(hash);
        self.lockscreen_salt = Some(salt);
        let _ = self.save();
    }

    /// Clear PIN
    #[allow(dead_code)]
    pub fn clear_pin(&mut self) {
        self.lockscreen_pin_hash = None;
        self.lockscreen_salt = None;
        let _ = self.save();
    }

    /// Get or create salt for PIN hashing
    pub fn get_or_create_salt(&mut self) -> String {
        if let Some(ref salt) = self.lockscreen_salt {
            return salt.clone();
        }

        // Try to read machine-id (Linux)
        #[cfg(target_os = "linux")]
        if let Ok(machine_id) = std::fs::read_to_string("/etc/machine-id") {
            let salt = machine_id.trim().to_string();
            self.lockscreen_salt = Some(salt.clone());
            let _ = self.save();
            return salt;
        }

        // Try to read hostid (FreeBSD/NetBSD)
        #[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
        {
            // First try /etc/hostid
            if let Ok(hostid) = std::fs::read_to_string("/etc/hostid") {
                let salt = hostid.trim().to_string();
                if !salt.is_empty() {
                    self.lockscreen_salt = Some(salt.clone());
                    let _ = self.save();
                    return salt;
                }
            }

            // FreeBSD: Try kern.hostuuid sysctl
            #[cfg(target_os = "freebsd")]
            {
                use std::process::Command;
                if let Ok(output) = Command::new("sysctl")
                    .args(["-n", "kern.hostuuid"])
                    .output()
                {
                    if output.status.success() {
                        let uuid = String::from_utf8_lossy(&output.stdout);
                        let salt = uuid.trim().to_string();
                        if !salt.is_empty() {
                            self.lockscreen_salt = Some(salt.clone());
                            let _ = self.save();
                            return salt;
                        }
                    }
                }
            }
        }

        // OpenBSD: No standard machine-id, fall through to random salt

        // Try to read machine UUID (macOS)
        #[cfg(target_os = "macos")]
        {
            use std::process::Command;
            if let Ok(output) = Command::new("ioreg")
                .args(["-rd1", "-c", "IOPlatformExpertDevice"])
                .output()
            {
                let stdout = String::from_utf8_lossy(&output.stdout);
                for line in stdout.lines() {
                    if line.contains("IOPlatformUUID") {
                        if let Some(uuid) = line.split('"').nth(3) {
                            self.lockscreen_salt = Some(uuid.to_string());
                            let _ = self.save();
                            return uuid.to_string();
                        }
                    }
                }
            }
        }

        // Fallback: generate random salt
        let random_salt = Self::generate_random_salt();
        self.lockscreen_salt = Some(random_salt.clone());
        let _ = self.save();
        random_salt
    }

    fn generate_random_salt() -> String {
        use sha2::{Digest, Sha256};
        use std::time::{SystemTime, UNIX_EPOCH};

        let mut hasher = Sha256::new();

        // Time-based entropy
        if let Ok(duration) = SystemTime::now().duration_since(UNIX_EPOCH) {
            hasher.update(duration.as_nanos().to_le_bytes());
        }

        // Process ID entropy
        hasher.update(std::process::id().to_le_bytes());

        // Additional entropy from environment if available
        if let Ok(home) = std::env::var("HOME") {
            hasher.update(home.as_bytes());
        }
        if let Ok(user) = std::env::var("USER") {
            hasher.update(user.as_bytes());
        }

        let result = hasher.finalize();
        result.iter().map(|b| format!("{:02x}", b)).collect()
    }

    /// Cycle to the next keybinding profile and save
    pub fn cycle_keybinding_profile(&mut self) {
        use crate::input::keybinding_profile::KeybindingProfile;
        self.keybinding_profile =
            KeybindingProfile::next_name(&self.keybinding_profile).to_string();
        let _ = self.save();
    }

    /// Cycle to the previous keybinding profile and save
    pub fn cycle_keybinding_profile_backward(&mut self) {
        use crate::input::keybinding_profile::KeybindingProfile;
        self.keybinding_profile =
            KeybindingProfile::prev_name(&self.keybinding_profile).to_string();
        let _ = self.save();
    }

    /// Toggle network widget enabled state and save
    pub fn toggle_network_widget(&mut self) {
        self.network_widget_enabled = !self.network_widget_enabled;
        let _ = self.save();
    }

    /// Set network interface name and save
    #[allow(dead_code)]
    pub fn set_network_interface(&mut self, interface: String) {
        self.network_interface = interface;
        let _ = self.save();
    }
}