feat(host): list-monitors CLI, and two bugs it caught on-glass
An operator configuring an unattended host has to learn the connector
names from somewhere, and "curl the management API before the host is
configured" is not it. `punktfunk-host list-monitors` prints them with
geometry and flags the pinned one.
Running it under a real sway immediately caught two defects the unit tests
could not:
- the query used the `swaymsg` command helper, which inserts `--`, so
`-t get_outputs` came back as "Unknown/invalid command '-t'". Queries
now go through a `swaymsg_query` helper that cannot make that mistake.
- compositors write the literal "Unknown" rather than leaving make/model
empty, so the picker label read "Unknown Unknown". One `describe`
helper now treats that as absent, for all four backends.
Verified on a headless two-output sway: names, modes, positions, scale and
the case-insensitive pin all resolve.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -289,12 +289,13 @@ pub(crate) fn list_monitors() -> Result<Vec<crate::monitors::PhysicalMonitor>> {
|
||||
.filter_map(|m| {
|
||||
let connector = m.get("name")?.as_str()?.to_string();
|
||||
let num = |k: &str| m.get(k).and_then(|v| v.as_i64()).unwrap_or(0);
|
||||
let description = m
|
||||
.get("description")
|
||||
.and_then(|v| v.as_str())
|
||||
.filter(|s| !s.trim().is_empty())
|
||||
.unwrap_or(&connector)
|
||||
.to_string();
|
||||
// Hyprland's `description` is already a "make model (connector)" string; treat it as
|
||||
// the make and let the shared helper drop it when it is empty/Unknown.
|
||||
let description = crate::monitors::describe(
|
||||
m.get("description").and_then(|v| v.as_str()).unwrap_or(""),
|
||||
"",
|
||||
&connector,
|
||||
);
|
||||
Some(crate::monitors::PhysicalMonitor {
|
||||
connector,
|
||||
description,
|
||||
|
||||
@@ -493,15 +493,13 @@ pub(crate) fn list_monitors() -> anyhow::Result<Vec<crate::monitors::PhysicalMon
|
||||
.filter_map(|d| {
|
||||
let connector = d.name.clone()?;
|
||||
let dims = session.current_dims(d);
|
||||
let label = match (d.make.as_deref(), d.model.as_deref()) {
|
||||
(Some(make), Some(model)) if !make.is_empty() || !model.is_empty() => {
|
||||
format!("{make} {model}").trim().to_string()
|
||||
}
|
||||
_ => connector.clone(),
|
||||
};
|
||||
Some(crate::monitors::PhysicalMonitor {
|
||||
managed: connector.starts_with(MANAGED_PREFIX),
|
||||
description: label,
|
||||
description: crate::monitors::describe(
|
||||
d.make.as_deref().unwrap_or(""),
|
||||
d.model.as_deref().unwrap_or(""),
|
||||
&connector,
|
||||
),
|
||||
// A disabled output has no current mode — report 0s rather than inventing one.
|
||||
width: dims.map(|d| d.0).unwrap_or(0),
|
||||
height: dims.map(|d| d.1).unwrap_or(0),
|
||||
|
||||
@@ -840,14 +840,9 @@ pub(crate) fn list_monitors() -> Result<Vec<crate::monitors::PhysicalMonitor>> {
|
||||
let (w, h, refresh) = current_mode_full(&state, connector)
|
||||
.map(|(_id, w, h, hz)| (w.max(0) as u32, h.max(0) as u32, (hz * 1000.0) as u32))
|
||||
.unwrap_or((0, 0, 0));
|
||||
let label = format!("{vendor} {product}");
|
||||
crate::monitors::PhysicalMonitor {
|
||||
connector: connector.clone(),
|
||||
description: if label.trim().is_empty() {
|
||||
connector.clone()
|
||||
} else {
|
||||
label.trim().to_string()
|
||||
},
|
||||
description: crate::monitors::describe(vendor, product, connector),
|
||||
width: w,
|
||||
height: h,
|
||||
refresh_mhz: refresh,
|
||||
|
||||
@@ -207,20 +207,29 @@ fn swaymsg(args: &[&str]) -> Result<String> {
|
||||
Ok(String::from_utf8_lossy(&out.stdout).into_owned())
|
||||
}
|
||||
|
||||
/// Current output names from `swaymsg -t get_outputs` (JSON).
|
||||
fn output_names() -> Result<Vec<String>> {
|
||||
/// Run a swaymsg **query** (`-t <kind> --raw`) and parse its JSON.
|
||||
///
|
||||
/// ⚠️ Deliberately NOT [`swaymsg`]: that helper inserts `--` so its arguments are read as a sway
|
||||
/// *command*, which is right for `create_output` and wrong for a query — `-t` after `--` comes back
|
||||
/// as `Unknown/invalid command '-t'` (caught on-glass writing the monitor enumeration).
|
||||
fn swaymsg_query(kind: &str) -> Result<serde_json::Value> {
|
||||
let out = Command::new("swaymsg")
|
||||
.args(["-t", "get_outputs", "--raw"])
|
||||
.args(["-t", kind, "--raw"])
|
||||
.output()
|
||||
.context("run swaymsg (is sway installed?)")?;
|
||||
if !out.status.success() {
|
||||
bail!(
|
||||
"swaymsg -t get_outputs failed: {}",
|
||||
"swaymsg -t {kind} failed: {}",
|
||||
String::from_utf8_lossy(&out.stderr).trim()
|
||||
);
|
||||
}
|
||||
let raw = String::from_utf8_lossy(&out.stdout).into_owned();
|
||||
let outputs: serde_json::Value = serde_json::from_str(&raw).context("parse get_outputs")?;
|
||||
serde_json::from_str(&raw).with_context(|| format!("parse {kind}"))
|
||||
}
|
||||
|
||||
/// Current output names from `swaymsg -t get_outputs` (JSON).
|
||||
fn output_names() -> Result<Vec<String>> {
|
||||
let outputs = swaymsg_query("get_outputs")?;
|
||||
Ok(outputs
|
||||
.as_array()
|
||||
.context("get_outputs: not an array")?
|
||||
@@ -235,8 +244,7 @@ fn output_names() -> Result<Vec<String>> {
|
||||
/// post-transform) — what `crate::monitors` documents. An inactive output has no `current_mode`, so
|
||||
/// its mode reads as zeros rather than a guess.
|
||||
pub(crate) fn list_monitors() -> Result<Vec<crate::monitors::PhysicalMonitor>> {
|
||||
let raw = swaymsg(&["-t", "get_outputs", "--raw"])?;
|
||||
let parsed: serde_json::Value = serde_json::from_str(&raw).context("parse get_outputs")?;
|
||||
let parsed = swaymsg_query("get_outputs")?;
|
||||
let mut out: Vec<_> = parsed
|
||||
.as_array()
|
||||
.context("get_outputs: not an array")?
|
||||
@@ -256,13 +264,12 @@ pub(crate) fn list_monitors() -> Result<Vec<crate::monitors::PhysicalMonitor>> {
|
||||
.unwrap_or(0)
|
||||
};
|
||||
let str_field = |k: &str| o.get(k).and_then(|v| v.as_str()).unwrap_or("").trim();
|
||||
let label = format!("{} {}", str_field("make"), str_field("model"));
|
||||
Some(crate::monitors::PhysicalMonitor {
|
||||
description: if label.trim().is_empty() {
|
||||
connector.clone()
|
||||
} else {
|
||||
label.trim().to_string()
|
||||
},
|
||||
description: crate::monitors::describe(
|
||||
str_field("make"),
|
||||
str_field("model"),
|
||||
&connector,
|
||||
),
|
||||
width: mode("width").max(0) as u32,
|
||||
height: mode("height").max(0) as u32,
|
||||
// sway reports `refresh` in mHz already.
|
||||
|
||||
@@ -47,6 +47,29 @@ pub struct PhysicalMonitor {
|
||||
pub managed: bool,
|
||||
}
|
||||
|
||||
/// Build the picker label from a compositor's make/model, falling back to the connector.
|
||||
///
|
||||
/// Compositors fill unknown fields with the literal string `"Unknown"` rather than leaving them
|
||||
/// empty (seen on-glass: sway reports `"Unknown Unknown"` for a headless output), so treat that as
|
||||
/// absent too — a picker row reading "Unknown Unknown" is worse than one reading "DP-1".
|
||||
pub(crate) fn describe(make: &str, model: &str, connector: &str) -> String {
|
||||
let known = |s: &str| {
|
||||
let s = s.trim();
|
||||
!s.is_empty() && !s.eq_ignore_ascii_case("unknown")
|
||||
};
|
||||
let label = [make, model]
|
||||
.iter()
|
||||
.map(|s| s.trim())
|
||||
.filter(|s| known(s))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
if label.is_empty() {
|
||||
connector.to_string()
|
||||
} else {
|
||||
label
|
||||
}
|
||||
}
|
||||
|
||||
impl PhysicalMonitor {
|
||||
/// `1920x1080@60` — for logs and pickers.
|
||||
pub fn mode_label(&self) -> String {
|
||||
@@ -163,6 +186,18 @@ mod tests {
|
||||
assert!(err.contains("no monitors at all"), "{err}");
|
||||
}
|
||||
|
||||
/// Compositors write the literal "Unknown" instead of leaving make/model empty (sway does it
|
||||
/// for headless outputs), so a picker must not end up showing "Unknown Unknown".
|
||||
#[test]
|
||||
fn describe_falls_back_to_the_connector_for_empty_or_unknown_fields() {
|
||||
assert_eq!(describe("ACME", "U2720Q", "DP-1"), "ACME U2720Q");
|
||||
assert_eq!(describe("Unknown", "Unknown", "HEADLESS-1"), "HEADLESS-1");
|
||||
assert_eq!(describe("", "", "DP-1"), "DP-1");
|
||||
// A half-known pair keeps the half that means something.
|
||||
assert_eq!(describe("Unknown", "U2720Q", "DP-1"), "U2720Q");
|
||||
assert_eq!(describe(" ", "unknown", "DP-2"), "DP-2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mode_label_drops_an_unknown_refresh() {
|
||||
let mut m = mon("DP-1");
|
||||
|
||||
Reference in New Issue
Block a user