|
| 1 | +use std::fmt::Display; |
| 2 | +use std::fs::File; |
| 3 | +use std::io::{BufReader, BufWriter, Write}; |
| 4 | +use std::path::PathBuf; |
| 5 | + |
| 6 | +use crate::colourblocks::colourblocks; |
| 7 | + |
| 8 | +use crate::cpu::Cpu; |
| 9 | +use crate::disk::Disk; |
| 10 | +use crate::fetcherror::FetchError; |
| 11 | +use crate::fetchsection::{FetchArray, SEPARATOR}; |
| 12 | +use crate::hostname::HostName; |
| 13 | +use crate::kernel::Kernel; |
| 14 | +use crate::memory::Memory; |
| 15 | +use crate::model::Model; |
| 16 | +use crate::osinfo::OsInfo; |
| 17 | +use crate::platform::Profile; |
| 18 | +use crate::shell::Shell; |
| 19 | +use crate::uptime::Uptime; |
| 20 | + |
| 21 | +use serde::{Deserialize, Serialize}; |
| 22 | + |
| 23 | +#[derive(Serialize, Deserialize)] |
| 24 | +pub struct Machine { |
| 25 | + pub kernel: Option<Kernel>, |
| 26 | + pub cpu: Option<Cpu>, |
| 27 | + pub memory: Option<Memory>, |
| 28 | + pub os: Option<OsInfo>, |
| 29 | + pub hostname: Option<HostName>, |
| 30 | + pub uptime: Option<Uptime>, |
| 31 | + pub model: Option<Model>, |
| 32 | + pub shell: Option<Shell>, |
| 33 | + pub platform: Option<Profile>, |
| 34 | + pub disk: Option<Disk>, |
| 35 | +} |
| 36 | + |
| 37 | +impl Machine { |
| 38 | + pub fn new() -> Self { |
| 39 | + Self::default() |
| 40 | + } |
| 41 | + |
| 42 | + pub fn from_file(path: PathBuf) -> Result<Self, FetchError> { |
| 43 | + let f = File::open(path)?; |
| 44 | + let r = BufReader::new(f); |
| 45 | + Ok(serde_json::from_reader(r)?) |
| 46 | + } |
| 47 | + |
| 48 | + pub fn to_file(&self, path: PathBuf) -> Result<(), FetchError> { |
| 49 | + let f = File::create(path)?; |
| 50 | + let mut w = BufWriter::new(f); |
| 51 | + serde_json::to_writer_pretty(&mut w, self)?; |
| 52 | + w.flush()?; |
| 53 | + Ok(()) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl Default for Machine { |
| 58 | + fn default() -> Self { |
| 59 | + Self { |
| 60 | + kernel: Kernel::new().ok(), |
| 61 | + cpu: Cpu::new().ok(), |
| 62 | + memory: Memory::new(None).ok(), |
| 63 | + os: OsInfo::new().ok(), |
| 64 | + hostname: HostName::new().ok(), |
| 65 | + uptime: Uptime::new().ok(), |
| 66 | + model: Model::new().ok(), |
| 67 | + shell: Shell::new().ok(), |
| 68 | + platform: Profile::new().ok(), |
| 69 | + disk: Disk::new().ok(), |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl Display for Machine { |
| 75 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 76 | + let mut array = FetchArray::default(); |
| 77 | + |
| 78 | + if let Some(r) = &self.os { |
| 79 | + array.set_colour(r.color.clone()); |
| 80 | + array.push(("OS", r)); |
| 81 | + } |
| 82 | + |
| 83 | + if let Some(r) = &self.shell { |
| 84 | + array.push(("Shell", r)); |
| 85 | + } |
| 86 | + |
| 87 | + if let Some(r) = &self.kernel { |
| 88 | + array.push(("Kernel", r)); |
| 89 | + } |
| 90 | + |
| 91 | + if let Some(r) = &self.model { |
| 92 | + array.push(("Model", r)); |
| 93 | + } |
| 94 | + |
| 95 | + if let Some(r) = &self.hostname { |
| 96 | + array.push(("Hostname", r)); |
| 97 | + } |
| 98 | + |
| 99 | + if let Some(r) = &self.uptime { |
| 100 | + array.push(("Uptime", r)); |
| 101 | + } |
| 102 | + |
| 103 | + if let Some(r) = &self.cpu { |
| 104 | + array.push(("CPU", r)); |
| 105 | + } |
| 106 | + |
| 107 | + if let Some(r) = &self.memory { |
| 108 | + array.push(("Memory", &r)); |
| 109 | + array.push(("Swap", r.display_swap())); |
| 110 | + } |
| 111 | + |
| 112 | + if let Some(r) = &self.platform { |
| 113 | + array.push(("Profile", r)); |
| 114 | + } |
| 115 | + |
| 116 | + if let Some(r) = &self.disk { |
| 117 | + array.push(("Disk", r)); |
| 118 | + } |
| 119 | + |
| 120 | + write!( |
| 121 | + f, |
| 122 | + "{}\n{}", |
| 123 | + array, |
| 124 | + colourblocks(array.get_indent() + SEPARATOR.len(), 16, 8) |
| 125 | + ) |
| 126 | + } |
| 127 | +} |
0 commit comments