From fedb4879a00a8c8daaefd6d8a59fe0fb2364be32 Mon Sep 17 00:00:00 2001 From: xscriptor Date: Tue, 17 Nov 2026 00:05:42 +0100 Subject: [PATCH] Fix xrandr resolution parsing --- CHANGELOG.md | 6 ++ Cargo.lock | 2 +- index.json | 2 +- plugins/display-resolution/CHANGELOG.md | 6 ++ plugins/display-resolution/Cargo.toml | 2 +- plugins/display-resolution/src/main.rs | 122 +++++++++++++++++++----- 6 files changed, 115 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82ddb6b..8af9d07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 2026-11-17 — display-resolution 0.2.1 + +- Fixed xrandr parsing on Xwayland and multi-monitor setups: geometry offsets + such as `1920x1200+0+0` are no longer reported as `x`, and the first monitor + is only counted when a resolution is found. + ## 2026-11-07 — banner-text 0.1.2 - Reverted the zero-width padding added in 0.1.1: it rendered as tofu squares diff --git a/Cargo.lock b/Cargo.lock index ee5fa04..1cb130e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -510,7 +510,7 @@ dependencies = [ [[package]] name = "xfetch-plugin-display-resolution" -version = "0.2.0" +version = "0.2.1" dependencies = [ "serde", "serde_json", diff --git a/index.json b/index.json index 42e64b2..8e22c20 100644 --- a/index.json +++ b/index.json @@ -41,7 +41,7 @@ { "name": "display-resolution", "description": "Xfetch plugin that reports the display resolution via xrandr/wayland probes", - "version": "0.2.0", + "version": "0.2.1", "kind": "info_provider", "runtime": "native", "path": "plugins/display-resolution", diff --git a/plugins/display-resolution/CHANGELOG.md b/plugins/display-resolution/CHANGELOG.md index a9779ac..4c2461b 100644 --- a/plugins/display-resolution/CHANGELOG.md +++ b/plugins/display-resolution/CHANGELOG.md @@ -1,4 +1,10 @@ # Changelog +## 2026-11-17 — 0.2.1 + +- Fixed xrandr parsing on Xwayland and multi-monitor setups: the geometry + offset (`1920x1200+0+0`) is no longer mistaken for the resolution, and the + bare `x` token from the rotation list is no longer reported as `x`. + ## 2026-08-19 - Wrapped work in `with_timeout` with a 2 s budget; on timeout it responds with a fallback line. diff --git a/plugins/display-resolution/Cargo.toml b/plugins/display-resolution/Cargo.toml index 14b2fce..0219239 100644 --- a/plugins/display-resolution/Cargo.toml +++ b/plugins/display-resolution/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xfetch-plugin-display-resolution" -version = "0.2.0" +version = "0.2.1" edition = "2024" license = "GPL-3.0-only" description = "Xfetch plugin that reports the display resolution via xrandr/wayland probes" diff --git a/plugins/display-resolution/src/main.rs b/plugins/display-resolution/src/main.rs index f1f60ee..45abb9b 100644 --- a/plugins/display-resolution/src/main.rs +++ b/plugins/display-resolution/src/main.rs @@ -56,21 +56,21 @@ fn get_xrandr_display_info() -> Option> { for line in stdout.lines() { let line = line.trim(); - if line.contains(" connected ") && line.contains('x') { + if line.contains(" connected ") + && let Some(res) = extract_resolution_xrandr(line) + { display_count += 1; - if let Some(res) = extract_resolution_xrandr(line) { - let parts: Vec<&str> = line.split_whitespace().collect(); - let name = parts[0]; - let primary = if line.contains("primary") { - " (primary)" - } else { - "" - }; - if display_count == 1 { - result.push(format!(" {}: {}{}", name, res, primary)); - } else { - result.push(format!("  {}: {}{}", name, res, primary)); - } + let parts: Vec<&str> = line.split_whitespace().collect(); + let name = parts[0]; + let primary = if line.contains("primary") { + " (primary)" + } else { + "" + }; + if display_count == 1 { + result.push(format!(" {}: {}{}", name, res, primary)); + } else { + result.push(format!("  {}: {}{}", name, res, primary)); } } } @@ -85,21 +85,50 @@ fn get_xrandr_display_info() -> Option> { fn extract_resolution_xrandr(line: &str) -> Option { let parts: Vec<&str> = line.split_whitespace().collect(); for (i, part) in parts.iter().enumerate() { - if part.contains('x') && part.chars().all(|c| c.is_ascii_digit() || c == 'x') { - if let Some(ref_rate) = parts.get(i + 1) - && ref_rate - .chars() - .all(|c| c.is_ascii_digit() || c == '.' || c == '*') + if let Some(resolution) = resolution_from_xrandr_token(part) { + if let Some(rate) = parts + .get(i + 1) + .and_then(|token| rate_from_xrandr_token(token)) { - let rate = ref_rate.trim_end_matches('*').trim_end_matches('+'); - return Some(format!("{} @ {} Hz", part, rate)); + return Some(format!("{resolution} @ {rate} Hz")); } - return Some(part.to_string()); + return Some(resolution.to_string()); } } None } +/// Extracts the resolution from an xrandr geometry token. +/// +/// `1920x1200+0+0` becomes `1920x1200`. The bare `x` token from the +/// `(normal left inverted right x axis y axis)` suffix is not a resolution. +fn resolution_from_xrandr_token(token: &str) -> Option<&str> { + let end = token + .find(|c: char| !(c.is_ascii_digit() || c == 'x')) + .unwrap_or(token.len()); + let resolution = &token[..end]; + let (width, height) = resolution.split_once('x')?; + if width.len() < 2 + || height.len() < 2 + || !width.chars().all(|c| c.is_ascii_digit()) + || !height.chars().all(|c| c.is_ascii_digit()) + { + return None; + } + Some(resolution) +} + +fn rate_from_xrandr_token(token: &str) -> Option<&str> { + let rate = token.trim_end_matches(['*', '+']); + if rate.is_empty() + || !rate.chars().all(|c| c.is_ascii_digit() || c == '.') + || !rate.contains('.') + { + return None; + } + Some(rate) +} + fn get_x11_display_info() -> Option> { let output = Command::new("xdpyinfo").output().ok()?; @@ -223,3 +252,52 @@ Write-Output "${w}x${h}" Some(vec![format!(" Display: {}", res)]) } + +#[cfg(test)] +mod tests { + use super::{extract_resolution_xrandr, resolution_from_xrandr_token}; + + #[test] + fn xrandr_geometry_offset_is_not_part_of_the_resolution() { + assert_eq!( + extract_resolution_xrandr( + "eDP-1 connected 1920x1200+0+0 (normal left inverted right x axis y axis) 340mm x 220mm" + ) + .as_deref(), + Some("1920x1200") + ); + } + + #[test] + fn xrandr_primary_display_ignores_auto_position() { + assert_eq!( + extract_resolution_xrandr( + "HDMI-1 connected primary 3840x2160+1920+0 (normal left inverted right x axis y axis) 600mm x 340mm" + ) + .as_deref(), + Some("3840x2160") + ); + } + + #[test] + fn xrandr_prefers_explicit_refresh_rate() { + assert_eq!( + extract_resolution_xrandr("DP-1 connected 2560x1440 144.00*+").as_deref(), + Some("2560x1440 @ 144.00 Hz") + ); + } + + #[test] + fn xrandr_interlaced_mode_is_accepted() { + assert_eq!( + extract_resolution_xrandr("DP-2 connected 1920x1080i+0+0").as_deref(), + Some("1920x1080") + ); + } + + #[test] + fn bare_x_token_is_not_a_resolution() { + assert_eq!(resolution_from_xrandr_token("x"), None); + assert_eq!(resolution_from_xrandr_token("axis"), None); + } +}