Skip to content

Commit

Permalink
refactor: combine process column code (#1622)
Browse files Browse the repository at this point in the history
* rename some files

* refactor: combine process column code

* rename some and sort the schema columns
  • Loading branch information
ClementTsang authored Nov 14, 2024
1 parent 103c4f6 commit 6d37d57
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 212 deletions.
27 changes: 13 additions & 14 deletions schema/nightly/bottom.json
Original file line number Diff line number Diff line change
Expand Up @@ -674,32 +674,31 @@
}
},
"ProcColumn": {
"description": "A column in the process widget.",
"type": "string",
"enum": [
"PID",
"Count",
"Name",
"Command",
"CPU%",
"Command",
"Count",
"GMem",
"GMem%",
"GPU%",
"Mem",
"Mem%",
"Name",
"PID",
"R/s",
"Read",
"Rps",
"W/s",
"Write",
"Wps",
"State",
"T.Read",
"TWrite",
"T.Write",
"TRead",
"State",
"User",
"TWrite",
"Time",
"GMem",
"GMem%",
"GPU%"
"User",
"W/s",
"Wps",
"Write"
]
},
"ProcessesConfig": {
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,10 @@ fn generate_schema() -> anyhow::Result<()> {
match proc_columns {
schemars::schema::Schema::Object(proc_columns) => {
let enums = proc_columns.enum_values.as_mut().unwrap();
*enums = options::config::process::ProcColumn::VARIANTS
*enums = widgets::ProcColumn::VARIANTS
.iter()
.flat_map(|var| var.get_schema_names())
.sorted()
.map(|v| serde_json::Value::String(v.to_string()))
.dedup()
.collect();
Expand Down
112 changes: 1 addition & 111 deletions src/options/config/process.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::Deserialize;

use crate::widgets::ProcWidgetColumn;
use crate::widgets::ProcColumn;

/// Process configuration.
#[derive(Clone, Debug, Default, Deserialize)]
Expand All @@ -12,116 +12,6 @@ pub struct ProcessesConfig {
pub columns: Vec<ProcColumn>,
}

/// A column in the process widget.
#[derive(Clone, Debug)]
#[cfg_attr(
feature = "generate_schema",
derive(schemars::JsonSchema, strum::VariantArray)
)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum ProcColumn {
Pid,
Count,
Name,
Command,
CpuPercent,
Mem,
MemPercent,
Read,
Write,
TotalRead,
TotalWrite,
State,
User,
Time,
#[cfg(feature = "gpu")]
GpuMem,
#[cfg(feature = "gpu")]
GpuPercent,
}

impl ProcColumn {
/// An ugly hack to generate the JSON schema.
#[cfg(feature = "generate_schema")]
pub fn get_schema_names(&self) -> &[&'static str] {
match self {
ProcColumn::Pid => &["PID"],
ProcColumn::Count => &["Count"],
ProcColumn::Name => &["Name"],
ProcColumn::Command => &["Command"],
ProcColumn::CpuPercent => &["CPU%"],
ProcColumn::Mem => &["Mem"],
ProcColumn::MemPercent => &["Mem%"],
ProcColumn::Read => &["R/s", "Read", "Rps"],
ProcColumn::Write => &["W/s", "Write", "Wps"],
ProcColumn::TotalRead => &["T.Read", "TWrite"],
ProcColumn::TotalWrite => &["T.Write", "TRead"],
ProcColumn::State => &["State"],
ProcColumn::User => &["User"],
ProcColumn::Time => &["Time"],
#[cfg(feature = "gpu")]
ProcColumn::GpuMem => &["GMem", "GMem%"],
#[cfg(feature = "gpu")]
ProcColumn::GpuPercent => &["GPU%"],
}
}
}

impl<'de> Deserialize<'de> for ProcColumn {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = String::deserialize(deserializer)?.to_lowercase();
match value.as_str() {
"cpu%" => Ok(ProcColumn::CpuPercent),
"mem" => Ok(ProcColumn::Mem),
"mem%" => Ok(ProcColumn::Mem),
"pid" => Ok(ProcColumn::Pid),
"count" => Ok(ProcColumn::Count),
"name" => Ok(ProcColumn::Name),
"command" => Ok(ProcColumn::Command),
"read" | "r/s" | "rps" => Ok(ProcColumn::Read),
"write" | "w/s" | "wps" => Ok(ProcColumn::Write),
"tread" | "t.read" => Ok(ProcColumn::TotalRead),
"twrite" | "t.write" => Ok(ProcColumn::TotalWrite),
"state" => Ok(ProcColumn::State),
"user" => Ok(ProcColumn::User),
"time" => Ok(ProcColumn::Time),
#[cfg(feature = "gpu")]
"gmem" | "gmem%" => Ok(ProcColumn::GpuMem),
#[cfg(feature = "gpu")]
"gpu%" => Ok(ProcColumn::GpuPercent),
_ => Err(serde::de::Error::custom("doesn't match any column type")),
}
}
}

impl From<&ProcColumn> for ProcWidgetColumn {
fn from(value: &ProcColumn) -> Self {
match value {
ProcColumn::Pid => ProcWidgetColumn::PidOrCount,
ProcColumn::Count => ProcWidgetColumn::PidOrCount,
ProcColumn::Name => ProcWidgetColumn::ProcNameOrCommand,
ProcColumn::Command => ProcWidgetColumn::ProcNameOrCommand,
ProcColumn::CpuPercent => ProcWidgetColumn::Cpu,
ProcColumn::Mem => ProcWidgetColumn::Mem,
ProcColumn::MemPercent => ProcWidgetColumn::Mem,
ProcColumn::Read => ProcWidgetColumn::ReadPerSecond,
ProcColumn::Write => ProcWidgetColumn::WritePerSecond,
ProcColumn::TotalRead => ProcWidgetColumn::TotalRead,
ProcColumn::TotalWrite => ProcWidgetColumn::TotalWrite,
ProcColumn::State => ProcWidgetColumn::State,
ProcColumn::User => ProcWidgetColumn::User,
ProcColumn::Time => ProcWidgetColumn::Time,
#[cfg(feature = "gpu")]
ProcColumn::GpuMem => ProcWidgetColumn::GpuMem,
#[cfg(feature = "gpu")]
ProcColumn::GpuPercent => ProcWidgetColumn::GpuUtil,
}
}
}

#[cfg(test)]
mod test {
use super::{ProcColumn, ProcessesConfig};
Expand Down
Loading

0 comments on commit 6d37d57

Please sign in to comment.