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
// MIT License
//
// Copyright (c) 2021 Ferhat Geçdoğan All Rights Reserved.
// Distributed under the terms of the MIT License.
//
//

pub struct CPUInfos {
    pub model_name: String,
    pub vendor_id : String,
    pub cores     : u32
}

impl CPUInfos {
    pub fn init(&mut self) {
        self.get_linux_cpu_infos("/proc/cpuinfo");
    }

    pub fn get_linux_cpu_infos(&mut self, file: &str) {
        #[cfg(target_os = "linux")]
        if std::path::Path::new(file).exists() {
            if let Ok(lines) =
            crate::helpers::helpers::read_lines(file) {
                for line in lines {
                    if let Ok(ip) = line {
                        if ip.starts_with('v') {
                            if ip.contains("vendor_id") {
                                self.vendor_id =
                                    ip
                                    .replace("vendor_id", "")
                                    .replace(":", "")
                                    .trim_start().to_string();
                            }

                            continue;
                        }

                        if ip.starts_with('c') {
                            if ip.contains("cpu cores") {
                                let ip =
                                    ip
                                    .replace("cpu cores", "")
                                    .replace(":", "")
                                    .trim_start().to_string();

                                if !ip.is_empty() {
                                    self.cores = ip.parse::<u32>().unwrap();
                                }
                            }

                            continue;
                        }

                        if ip.starts_with('m') {
                            if ip.contains("model name") {
                                self.model_name =
                                    ip
                                    .replace("model name", "")
                                    .replace(":", "")
                                    .trim_start().to_string();
                            }

                            continue;
                        }
                    }
                }
            }
        }
    }
}