rocketfetch 0.7.5

A WIP command line system information tool written asynchronously in rust for performance with toml file configuration.
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
// use crate::utils::handle_error_result;
use console::Style;
use libmacchina::traits::GeneralReadout as _;
use libmacchina::traits::KernelReadout as _;
use libmacchina::traits::PackageReadout as _;
use libmacchina::GeneralReadout;
use libmacchina::KernelReadout;
use libmacchina::PackageReadout;
use user_error::{UserFacingError, UFE};

use crate::cli::Mode;
use crate::config::Config;
use crate::handle_error;

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct Format {
    pub mode: Mode,
    pub top_left_corner_char: char,
    pub top_right_corner_char: char,
    pub bottom_left_corner_char: char,
    pub bottom_right_corner_char: char,
    pub horizontal_char: char,
    pub vertical_char: char,
    pub padding_right: usize,
    pub padding_left: usize,
    pub padding_top: usize,
}

impl Default for Format {
    fn default() -> Self {
        Self {
            mode: Mode::Classic,
            top_left_corner_char: '',
            top_right_corner_char: '',
            bottom_left_corner_char: '',
            bottom_right_corner_char: '',
            horizontal_char: '',
            vertical_char: '',
            padding_right: 1,
            padding_left: 1,
            padding_top: 0,
        }
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct User {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
    separator_style: String,
    separator_char: String,
}

impl Default for User {
    fn default() -> Self {
        Self {
            pre_text_style: String::from("bold.yellow"),
            pre_text: String::new(),
            output_style: String::from("bold.yellow"),
            separator_style: String::from("white"),
            separator_char: String::from("@"),
        }
    }
}

impl User {
    pub fn get_info(&self) -> String {
        let general_readout = GeneralReadout::new();
        let hostname = handle_error!(general_readout.hostname(), "Failed to get hostname");
        let username = handle_error!(general_readout.username(), "Failed to get username");
        format!(
            "{}{}{}{}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            Style::from_dotted_str(&self.output_style).apply_to(username),
            Style::from_dotted_str(&self.separator_style).apply_to(&self.separator_char),
            Style::from_dotted_str(&self.output_style).apply_to(hostname.trim())
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct Delimiter {
    style: String,
    repeat_num: usize,
    char: char,
}

impl Default for Delimiter {
    fn default() -> Self {
        Self {
            style: String::from("white"),
            repeat_num: 0,
            char: '-',
        }
    }
}

impl Delimiter {
    pub fn get_info(&self, num: usize) -> String {
        let mut repeat = self.repeat_num;
        if repeat == 0 {
            repeat = num;
        }
        format!(
            "{}",
            Style::from_dotted_str(&self.style).apply_to(self.char.to_string().repeat(repeat))
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct Os {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
}

impl Default for Os {
    fn default() -> Self {
        Self {
            pre_text_style: String::from("bold.yellow"),
            pre_text: String::from("OS: "),
            output_style: String::from("white"),
        }
    }
}

impl Os {
    pub fn get_os() -> String {
        let general_readout = GeneralReadout::new();
        if cfg!(target_os = "linux") {
            return handle_error!(general_readout.distribution(), "Failed to find distro");
        } else if cfg!(target_os = "windows") {
            let version = handle_error!(
                KernelReadout::new().os_release(),
                "Failed to get windows version"
            );
            return format!("Windows {version}");
        }
        handle_error!(general_readout.os_name(), "Failed to find OS name")
    }
    pub fn get_info(&self) -> String {
        let os = Self::get_os();
        let build_version = Config::run_cmd("sw_vers -buildVersion", "Failed to get build version");
        let arch = Config::run_cmd("machine", "Failed to get arch");

        let output_style = Style::from_dotted_str(&self.output_style);
        format!(
            "{}{} {} {}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            output_style.apply_to(os.trim()),
            output_style.apply_to(build_version.trim()),
            output_style.apply_to(arch.trim())
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct Host {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
}

impl Default for Host {
    fn default() -> Self {
        Self {
            pre_text_style: String::from("bold.yellow"),
            pre_text: String::from("Host: "),
            output_style: String::from("white"),
        }
    }
}

impl Host {
    pub fn get_info(&self) -> String {
        let general_readout = GeneralReadout::new();
        let machine = handle_error!(general_readout.machine(), "Failed to find machine name");
        format!(
            "{}{}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            Style::from_dotted_str(&self.output_style).apply_to(machine.trim())
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct Kernel {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
}

impl Default for Kernel {
    fn default() -> Self {
        Self {
            pre_text_style: String::from("bold.yellow"),
            pre_text: String::from("Kernel: "),
            output_style: String::from("white"),
        }
    }
}

impl Kernel {
    pub fn get_info(&self) -> String {
        let kernel_readout = KernelReadout::new();
        let kernel = handle_error!(
            kernel_readout.pretty_kernel(),
            "Failed to find kernel version"
        );
        format!(
            "{}{}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            Style::from_dotted_str(&self.output_style).apply_to(kernel.trim())
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct Uptime {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
    time_format: String,
}

impl Default for Uptime {
    fn default() -> Self {
        Self {
            pre_text_style: String::from("bold.yellow"),
            pre_text: String::from("Uptime: "),
            output_style: String::from("white"),
            time_format: String::from("$days days, $hours hours, $minutes minutes"),
        }
    }
}

impl Uptime {
    pub fn get_info(&self) -> String {
        let general_readout = GeneralReadout::new();
        let uptime = handle_error!(general_readout.uptime(), "Failed to get uptime");
        let shr = secfmt::from(uptime as u64);
        let mut time = self.time_format.clone();
        time = time.replace("$years", &shr.years.to_string());
        time = time.replace("${years}", &shr.years.to_string());
        time = time.replace("$days", &shr.days.to_string());
        time = time.replace("${days}", &shr.days.to_string());
        time = time.replace("$hours", &shr.hours.to_string());
        time = time.replace("${hours}", &shr.hours.to_string());
        time = time.replace("$minutes", &shr.minutes.to_string());
        time = time.replace("${minutes}", &shr.minutes.to_string());
        time = time.replace("$seconds", &shr.seconds.to_string());
        time = time.replace("${seconds}", &shr.seconds.to_string());
        format!(
            "{}{}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            Style::from_dotted_str(&self.output_style).apply_to(time.trim())
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct Packages {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
}

impl Default for Packages {
    fn default() -> Self {
        Self {
            pre_text_style: String::from("bold.yellow"),
            pre_text: String::from("Packages: "),
            output_style: String::from("white"),
        }
    }
}

impl Packages {
    pub fn get_info(&self) -> String {
        let package_readout = PackageReadout::new();
        let package = package_readout.count_pkgs();
        let mut packages = String::new();
        for (name, num) in package {
            packages.push_str(format!("{} ({}) ", num, name.to_string()).as_str());
        }
        format!(
            "{}{}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            Style::from_dotted_str(&self.output_style).apply_to(packages.trim())
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct Shell {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
}

impl Default for Shell {
    fn default() -> Self {
        Self {
            pre_text_style: String::from("bold.yellow"),
            pre_text: String::from("Shell: "),
            output_style: String::from("white"),
        }
    }
}

impl Shell {
    pub fn get_info(&self) -> String {
        use regex::Regex;
        let ver_regex = Regex::new(r"(0|[1-9]\d*)\.(0|[1-9]\d*)\.?(0|[1-9]\d*)?(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?").unwrap();
        let general_readout = &GeneralReadout::new();
        let shell = general_readout.shell(
            libmacchina::traits::ShellFormat::Relative,
            libmacchina::traits::ShellKind::Default,
        );
        let shell = handle_error!(shell, "Failed to get shell");
        let version_output = Config::run_cmd(
            format!("{shell} --version").as_str(),
            "Failed to get shell version",
        );
        let version: String;
        if let Some(locations) = ver_regex.find(&version_output) {
            version = version_output[locations.start()..locations.end()].to_string();
        } else {
            version = String::from("(Unknown Version)");
        }
        format!(
            "{}{} {}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            Style::from_dotted_str(&self.output_style).apply_to(shell.trim()),
            Style::from_dotted_str(&self.output_style).apply_to(version.trim())
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct Resolution {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
}

impl Default for Resolution {
    fn default() -> Self {
        Self {
            pre_text_style: String::from("bold.yellow"),
            pre_text: String::from("Resolution: "),
            output_style: String::from("white"),
        }
    }
}

impl Resolution {
    pub fn get_info(&self) -> String {
        let general_readout = GeneralReadout::new();
        let resolution = handle_error!(general_readout.resolution(), "Failed to get resolution");

        format!(
            "{}{}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            Style::from_dotted_str(&self.output_style).apply_to(resolution.trim()),
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct DesktopEnvironment {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
}

impl Default for DesktopEnvironment {
    fn default() -> Self {
        Self {
            pre_text_style: String::from("bold.yellow"),
            pre_text: String::from("Desktop Environment: "),
            output_style: String::from("white"),
        }
    }
}

impl DesktopEnvironment {
    pub fn get_info(&self) -> String {
        let general_readout = GeneralReadout::new();
        let desktop_environment = handle_error!(
            general_readout.desktop_environment(),
            "Failed to get desktop environment"
        );

        format!(
            "{}{}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            Style::from_dotted_str(&self.output_style).apply_to(desktop_environment.trim()),
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct WindowManager {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
}

impl Default for WindowManager {
    fn default() -> Self {
        Self {
            pre_text_style: String::from("bold.yellow"),
            pre_text: String::from("Window Manager: "),
            output_style: String::from("white"),
        }
    }
}

impl WindowManager {
    pub fn get_info(&self) -> String {
        let general_readout = GeneralReadout::new();
        let window_manager = handle_error!(
            general_readout.window_manager(),
            "Failed to get window manager"
        );

        format!(
            "{}{}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            Style::from_dotted_str(&self.output_style).apply_to(window_manager.trim()),
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct Terminal {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
}

impl Default for Terminal {
    fn default() -> Self {
        Self {
            pre_text_style: String::from("bold.yellow"),
            pre_text: String::from("Terminal: "),
            output_style: String::from("white"),
        }
    }
}

impl Terminal {
    pub fn get_info(&self) -> String {
        let general_readout = GeneralReadout::new();
        let terminal = handle_error!(general_readout.terminal(), "Failed to get terminal name");

        format!(
            "{}{}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            Style::from_dotted_str(&self.output_style).apply_to(terminal.trim()),
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct Cpu {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
}

impl Default for Cpu {
    fn default() -> Self {
        Self {
            pre_text_style: String::from("bold.yellow"),
            pre_text: String::from("CPU: "),
            output_style: String::from("white"),
        }
    }
}

impl Cpu {
    pub fn get_info(&self) -> String {
        let general_readout = GeneralReadout::new();
        let cpu = handle_error!(general_readout.cpu_model_name(), "Failed to get CPU name");

        format!(
            "{}{}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            Style::from_dotted_str(&self.output_style).apply_to(cpu.trim()),
        )
    }
}

#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct Module {
    pre_text_style: String,
    pre_text: String,
    output_style: String,
    command: String,
}

impl Module {
    pub fn get_info(&self) -> String {
        let output = Config::run_cmd(&self.command, "Failed to run module command");

        format!(
            "{}{}",
            Style::from_dotted_str(&self.pre_text_style).apply_to(&self.pre_text),
            Style::from_dotted_str(&self.output_style).apply_to(output.trim())
        )
    }
}

impl Default for Module {
    fn default() -> Self {
        Self {
            command: String::new(),
            output_style: String::from("white"),
            pre_text: String::new(),
            pre_text_style: String::from("bold.yellow"),
        }
    }
}

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

    fn run_cmd_unsafe(cmd: &str) -> String {
        use std::process::Command;
        let output = Command::new("sh")
            .args(["-c", cmd])
            .output()
            .unwrap()
            .stdout;
        String::from_utf8(output).unwrap().trim().to_string()
    }

    #[test]
    fn get_username() {
        let general_readout = GeneralReadout::new();
        println!("Username: {}", general_readout.username().unwrap());
    }

    #[test]
    fn get_hostname() {
        let general_readout = GeneralReadout::new();
        println!("Hostname: {}", general_readout.hostname().unwrap());
    }

    #[test]
    fn get_os() {
        let general_readout = GeneralReadout::new();
        if cfg!(target_os = "linux") {
            println!("Linux Distro: {}", general_readout.distribution().unwrap());
        } else {
            println!("OS Name: {}", general_readout.os_name().unwrap());
            println!("OS Release: {}", KernelReadout::new().os_release().unwrap());
        }
    }

    #[test]
    fn get_build_version() {
        println!("Build Version: {}", run_cmd_unsafe("sw_vers -buildVersion"));
    }

    #[test]
    fn get_arch() {
        println!("Arch: {}", run_cmd_unsafe("machine"));
    }

    #[test]
    #[cfg(not(target_os = "linux"))]
    fn get_host() {
        let general_readout = GeneralReadout::new();
        println!("Host: {}", general_readout.machine().unwrap());
    }

    #[test]
    fn get_kernel() {
        let kernel_readout = KernelReadout::new();
        println!("Kernel: {}", kernel_readout.pretty_kernel().unwrap());
    }

    #[test]
    fn get_uptime() {
        let general_readout = GeneralReadout::new();
        println!("Uptime: {}", general_readout.uptime().unwrap());
    }

    #[test]
    #[cfg(not(target_os = "linux"))]
    fn get_packages() {
        let package_readout = PackageReadout::new();
        let package = package_readout.count_pkgs();
        let mut packages = String::new();
        for (name, num) in package {
            packages.push_str(format!("{} ({}) ", num, name.to_string()).as_str());
        }
        println!("Packages: {packages}");
    }

    #[test]
    #[cfg(not(target_os = "windows"))]
    fn get_shell() {
        use regex::Regex;
        let ver_regex = Regex::new(r"(0|[1-9]\d*)\.(0|[1-9]\d*)\.?(0|[1-9]\d*)?(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?").unwrap();
        let general_readout = &GeneralReadout::new();
        let shell = general_readout
            .shell(
                libmacchina::traits::ShellFormat::Relative,
                libmacchina::traits::ShellKind::Default,
            )
            .unwrap();
        let version = run_cmd_unsafe(format!("{shell} --version").as_str());
        let locations = ver_regex.find(&version).unwrap();
        let version = &version[locations.start()..locations.end()];
        println!("Shell: {shell} version {version}");
    }

    #[test]
    #[cfg(not(target_os = "windows"))]
    fn get_resolution() {
        let general_readout = GeneralReadout::new();
        println!("Resolution: {}", general_readout.resolution().unwrap());
    }

    #[test]
    #[cfg(all(not(target_os = "linux"), not(target_os = "windows")))]
    fn get_desktop_environment() {
        let general_readout = GeneralReadout::new();
        println!(
            "Desktop Environment: {}",
            general_readout.desktop_environment().unwrap()
        );
    }

    #[test]
    #[cfg(all(not(target_os = "linux"), not(target_os = "windows")))]
    fn get_window_manager() {
        let general_readout = GeneralReadout::new();
        println!(
            "Window Manager: {}",
            general_readout.window_manager().unwrap()
        );
    }

    #[test]
    #[cfg(all(not(target_os = "windows"), not(target_os = "macos")))]
    fn get_terminal_name() {
        let general_readout = GeneralReadout::new();
        println!("Terminal: {}", general_readout.terminal().unwrap());
    }

    #[test]
    fn get_cpu() {
        let general_readout = GeneralReadout::new();
        println!("CPU: {}", general_readout.cpu_model_name().unwrap());
    }
}