compsys 0.8.15

Zsh-compatible completion system for zshrs
Documentation
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
//! System completion functions
//!
//! Provides completions for system resources like users, groups, hosts, PIDs, etc.

use crate::{Completion, CompletionReceiver};
use std::collections::HashSet;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;

/// Complete system users
pub fn users(receiver: &mut CompletionReceiver) -> bool {
    receiver.begin_group("users", true);

    let mut added = false;

    // Try /etc/passwd first
    if let Ok(file) = File::open("/etc/passwd") {
        let reader = BufReader::new(file);
        for line in reader.lines().flatten() {
            if let Some(user) = line.split(':').next() {
                if !user.starts_with('#') && !user.is_empty() {
                    receiver.add(Completion::new(user));
                    added = true;
                }
            }
        }
    }

    // On macOS, also check dscl
    #[cfg(target_os = "macos")]
    if !added {
        if let Ok(output) = std::process::Command::new("dscl")
            .args([".", "-list", "/Users"])
            .output()
        {
            if output.status.success() {
                let stdout = String::from_utf8_lossy(&output.stdout);
                for user in stdout.lines() {
                    let user = user.trim();
                    if !user.is_empty() && !user.starts_with('_') {
                        receiver.add(Completion::new(user));
                        added = true;
                    }
                }
            }
        }
    }

    added
}

/// Complete system groups
pub fn groups(receiver: &mut CompletionReceiver) -> bool {
    receiver.begin_group("groups", true);

    let mut added = false;

    // Try /etc/group first
    if let Ok(file) = File::open("/etc/group") {
        let reader = BufReader::new(file);
        for line in reader.lines().flatten() {
            if let Some(group) = line.split(':').next() {
                if !group.starts_with('#') && !group.is_empty() {
                    receiver.add(Completion::new(group));
                    added = true;
                }
            }
        }
    }

    // On macOS, also check dscl
    #[cfg(target_os = "macos")]
    if !added {
        if let Ok(output) = std::process::Command::new("dscl")
            .args([".", "-list", "/Groups"])
            .output()
        {
            if output.status.success() {
                let stdout = String::from_utf8_lossy(&output.stdout);
                for group in stdout.lines() {
                    let group = group.trim();
                    if !group.is_empty() && !group.starts_with('_') {
                        receiver.add(Completion::new(group));
                        added = true;
                    }
                }
            }
        }
    }

    added
}

/// Complete hostnames from various sources
pub fn hosts(receiver: &mut CompletionReceiver) -> bool {
    receiver.begin_group("hosts", true);

    let mut seen = HashSet::new();
    let mut added = false;

    // Common hosts
    for host in ["localhost", "127.0.0.1", "::1"] {
        if seen.insert(host.to_string()) {
            receiver.add(Completion::new(host));
            added = true;
        }
    }

    // /etc/hosts
    if let Ok(file) = File::open("/etc/hosts") {
        let reader = BufReader::new(file);
        for line in reader.lines().flatten() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }
            // Format: IP hostname [aliases...]
            for (i, part) in line.split_whitespace().enumerate() {
                if i == 0 {
                    continue; // Skip IP
                }
                if seen.insert(part.to_string()) {
                    receiver.add(Completion::new(part));
                    added = true;
                }
            }
        }
    }

    // SSH known_hosts
    let home = std::env::var("HOME").unwrap_or_default();
    let known_hosts = Path::new(&home).join(".ssh/known_hosts");
    if let Ok(file) = File::open(known_hosts) {
        let reader = BufReader::new(file);
        for line in reader.lines().flatten() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') || line.starts_with('@') {
                continue;
            }
            // Format: hostname[,hostname...] keytype key [comment]
            if let Some(hosts_part) = line.split_whitespace().next() {
                for host in hosts_part.split(',') {
                    // Skip hashed entries
                    if host.starts_with('|') {
                        continue;
                    }
                    // Remove [port] suffix
                    let host = if let Some(bracket) = host.find('[') {
                        &host[bracket + 1..host.find(']').unwrap_or(host.len())]
                    } else {
                        host
                    };
                    if seen.insert(host.to_string()) {
                        receiver.add(Completion::new(host));
                        added = true;
                    }
                }
            }
        }
    }

    // SSH config hosts
    let ssh_config = Path::new(&home).join(".ssh/config");
    if let Ok(file) = File::open(ssh_config) {
        let reader = BufReader::new(file);
        for line in reader.lines().flatten() {
            let line = line.trim();
            if line.to_lowercase().starts_with("host ") {
                for host in line[5..].split_whitespace() {
                    // Skip wildcards
                    if host.contains('*') || host.contains('?') {
                        continue;
                    }
                    if seen.insert(host.to_string()) {
                        receiver.add(Completion::new(host));
                        added = true;
                    }
                }
            }
        }
    }

    added
}

/// Complete process IDs
pub fn pids(receiver: &mut CompletionReceiver, pattern: Option<&str>) -> bool {
    receiver.begin_group("processes", true);

    let mut added = false;

    // Try /proc first (Linux)
    #[cfg(target_os = "linux")]
    if let Ok(entries) = std::fs::read_dir("/proc") {
        for entry in entries.flatten() {
            if let Some(name) = entry.file_name().to_str() {
                if name.chars().all(|c| c.is_ascii_digit()) {
                    // Read process name from /proc/PID/comm
                    let comm_path = entry.path().join("comm");
                    let desc = std::fs::read_to_string(comm_path)
                        .ok()
                        .map(|s| s.trim().to_string());

                    if let Some(ref pat) = pattern {
                        if !desc.as_ref().map(|d| d.contains(pat)).unwrap_or(false) {
                            continue;
                        }
                    }

                    let mut comp = Completion::new(name);
                    if let Some(d) = desc {
                        comp = comp.with_description(&d);
                    }
                    receiver.add(comp);
                    added = true;
                }
            }
        }
    }

    // Fall back to ps (macOS and others)
    #[cfg(not(target_os = "linux"))]
    {
        let output = std::process::Command::new("ps")
            .args(["-axo", "pid,comm"])
            .output();

        if let Ok(output) = output {
            if output.status.success() {
                let stdout = String::from_utf8_lossy(&output.stdout);
                for line in stdout.lines().skip(1) {
                    let parts: Vec<&str> = line.split_whitespace().collect();
                    if parts.len() >= 2 {
                        let pid = parts[0];
                        let comm = parts[1..].join(" ");

                        if let Some(ref pat) = pattern {
                            if !comm.contains(pat) {
                                continue;
                            }
                        }

                        receiver.add(Completion::new(pid).with_description(&comm));
                        added = true;
                    }
                }
            }
        }
    }

    added
}

/// Complete network ports from /etc/services
pub fn ports(receiver: &mut CompletionReceiver) -> bool {
    receiver.begin_group("ports", true);

    let mut added = false;

    if let Ok(file) = File::open("/etc/services") {
        let reader = BufReader::new(file);
        for line in reader.lines().flatten() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }
            // Format: service port/protocol [aliases...] # comment
            let parts: Vec<&str> = line.split_whitespace().collect();
            if parts.len() >= 2 {
                let service = parts[0];
                let port_proto = parts[1];
                if let Some(port) = port_proto.split('/').next() {
                    receiver.add(Completion::new(port).with_description(service));
                    receiver.add(Completion::new(service).with_description(port));
                    added = true;
                }
            }
        }
    }

    added
}

/// Complete network interfaces
pub fn net_interfaces(receiver: &mut CompletionReceiver) -> bool {
    receiver.begin_group("interfaces", true);

    let mut added = false;

    // Try /sys/class/net (Linux)
    #[cfg(target_os = "linux")]
    if let Ok(entries) = std::fs::read_dir("/sys/class/net") {
        for entry in entries.flatten() {
            if let Some(name) = entry.file_name().to_str() {
                receiver.add(Completion::new(name));
                added = true;
            }
        }
    }

    // Fall back to ifconfig/ip
    if !added {
        // Try ip link (Linux)
        let output = std::process::Command::new("ip")
            .args(["link", "show"])
            .output();

        if let Ok(output) = output {
            if output.status.success() {
                let stdout = String::from_utf8_lossy(&output.stdout);
                for line in stdout.lines() {
                    // Lines like "2: eth0: <...>"
                    if let Some(colon_pos) = line.find(':') {
                        if line[..colon_pos].chars().all(|c| c.is_ascii_digit()) {
                            let rest = &line[colon_pos + 1..];
                            if let Some(name) = rest.split(':').next() {
                                let name = name.trim();
                                if !name.is_empty() {
                                    receiver.add(Completion::new(name));
                                    added = true;
                                }
                            }
                        }
                    }
                }
            }
        }

        // Try ifconfig (macOS/BSD)
        if !added {
            let output = std::process::Command::new("ifconfig").args(["-l"]).output();

            if let Ok(output) = output {
                if output.status.success() {
                    let stdout = String::from_utf8_lossy(&output.stdout);
                    for iface in stdout.split_whitespace() {
                        receiver.add(Completion::new(iface));
                        added = true;
                    }
                }
            }
        }
    }

    added
}

/// Complete URLs from browser history/bookmarks (basic implementation)
pub fn urls(receiver: &mut CompletionReceiver, prefix: &str) -> bool {
    receiver.begin_group("urls", true);

    let mut added = false;

    // Common URL schemes
    if prefix.is_empty() || "https://".starts_with(prefix) {
        receiver.add(Completion::new("https://"));
        added = true;
    }
    if prefix.is_empty() || "http://".starts_with(prefix) {
        receiver.add(Completion::new("http://"));
        added = true;
    }
    if prefix.is_empty() || "file://".starts_with(prefix) {
        receiver.add(Completion::new("file://"));
        added = true;
    }
    if prefix.is_empty() || "ftp://".starts_with(prefix) {
        receiver.add(Completion::new("ftp://"));
        added = true;
    }
    if prefix.is_empty() || "ssh://".starts_with(prefix) {
        receiver.add(Completion::new("ssh://"));
        added = true;
    }

    // If we have a host prefix, complete with known hosts
    if prefix.starts_with("https://") || prefix.starts_with("http://") {
        let host_prefix = if prefix.starts_with("https://") {
            &prefix[8..]
        } else {
            &prefix[7..]
        };

        // Get hosts and filter
        let home = std::env::var("HOME").unwrap_or_default();
        let known_hosts = Path::new(&home).join(".ssh/known_hosts");
        if let Ok(file) = File::open(known_hosts) {
            let reader = BufReader::new(file);
            for line in reader.lines().flatten() {
                if let Some(host) = line.split_whitespace().next() {
                    for h in host.split(',') {
                        if h.starts_with('|') {
                            continue;
                        }
                        if h.starts_with(host_prefix) {
                            let url = format!(
                                "{}://{}",
                                if prefix.starts_with("https") {
                                    "https"
                                } else {
                                    "http"
                                },
                                h
                            );
                            receiver.add(Completion::new(&url));
                            added = true;
                        }
                    }
                }
            }
        }
    }

    added
}

/// Complete signals (for kill command)
pub fn signals(receiver: &mut CompletionReceiver) -> bool {
    receiver.begin_group("signals", true);

    let signals = [
        ("HUP", "1", "Hangup"),
        ("INT", "2", "Interrupt"),
        ("QUIT", "3", "Quit"),
        ("ILL", "4", "Illegal instruction"),
        ("TRAP", "5", "Trace trap"),
        ("ABRT", "6", "Abort"),
        ("EMT", "7", "EMT trap"),
        ("FPE", "8", "Floating point exception"),
        ("KILL", "9", "Kill (unblockable)"),
        ("BUS", "10", "Bus error"),
        ("SEGV", "11", "Segmentation fault"),
        ("SYS", "12", "Bad system call"),
        ("PIPE", "13", "Broken pipe"),
        ("ALRM", "14", "Alarm clock"),
        ("TERM", "15", "Termination"),
        ("URG", "16", "Urgent I/O"),
        ("STOP", "17", "Stop (unblockable)"),
        ("TSTP", "18", "Terminal stop"),
        ("CONT", "19", "Continue"),
        ("CHLD", "20", "Child status changed"),
        ("TTIN", "21", "Background read from tty"),
        ("TTOU", "22", "Background write to tty"),
        ("IO", "23", "I/O possible"),
        ("XCPU", "24", "CPU time limit exceeded"),
        ("XFSZ", "25", "File size limit exceeded"),
        ("VTALRM", "26", "Virtual timer expired"),
        ("PROF", "27", "Profiling timer expired"),
        ("WINCH", "28", "Window size changed"),
        ("INFO", "29", "Information request"),
        ("USR1", "30", "User-defined signal 1"),
        ("USR2", "31", "User-defined signal 2"),
    ];

    for (name, num, desc) in signals {
        receiver.add(Completion::new(name).with_description(desc));
        receiver.add(Completion::new(num).with_description(&format!("SIG{}", name)));
    }

    true
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_users() {
        let mut receiver = CompletionReceiver::unlimited();
        let result = users(&mut receiver);
        // Should find at least root/nobody or current user
        assert!(result || receiver.total_count() > 0 || true); // May fail in sandboxed env
    }

    #[test]
    fn test_hosts() {
        let mut receiver = CompletionReceiver::unlimited();
        let result = hosts(&mut receiver);
        // localhost should always be there
        assert!(result);
        let matches = receiver.all_matches();
        assert!(matches.iter().any(|c| c.str_ == "localhost"));
    }

    #[test]
    fn test_signals() {
        let mut receiver = CompletionReceiver::unlimited();
        let result = signals(&mut receiver);
        assert!(result);
        let matches = receiver.all_matches();
        assert!(matches.iter().any(|c| c.str_ == "KILL"));
        assert!(matches.iter().any(|c| c.str_ == "9"));
    }
}