1
0
mirror of https://github.com/emersion/kanshi synced 2024-11-23 00:02:16 +01:00

Pretty-print connected outputs

This commit is contained in:
emersion 2017-08-08 20:10:23 +02:00
parent 3922251786
commit d10ca2c66b
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 40 additions and 23 deletions

@ -1,6 +1,7 @@
extern crate edid;
use std::error::Error;
use std::fmt;
use std::fs::{File, read_dir};
use std::io::prelude::*;
@ -10,6 +11,38 @@ pub struct ConnectedOutput {
pub edid: edid::EDID,
}
impl ConnectedOutput {
pub fn vendor(&self) -> String {
self.edid.header.vendor[..].iter().collect::<String>()
}
pub fn product(&self) -> String {
self.edid.descriptors.iter()
.filter_map(|d| match d {
&edid::Descriptor::ProductName(ref s) => Some(s.to_string()),
_ => None,
})
.nth(0)
.unwrap_or(format!("0x{:X}", self.edid.header.product))
}
pub fn serial(&self) -> String {
self.edid.descriptors.iter()
.filter_map(|d| match d {
&edid::Descriptor::SerialNumber(ref s) => Some(s.to_string()),
_ => None,
})
.nth(0)
.unwrap_or(format!("0x{:X}", self.edid.header.serial))
}
}
impl fmt::Display for ConnectedOutput {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "output {} vendor {} product {} serial {}", self.name, self.vendor(), self.product(), self.serial())
}
}
pub trait Backend {
fn list_outputs(&self) -> Result<Vec<ConnectedOutput>, Box<Error>>;
}

@ -42,8 +42,7 @@ impl PartialEq<SavedOutput> for ConnectedOutput {
}
}
if other.vendor != "" {
let vendor = self.edid.header.vendor[..].iter().collect::<String>();
if vendor != other.vendor {
if self.vendor() != other.vendor {
return false;
}
}
@ -53,16 +52,7 @@ impl PartialEq<SavedOutput> for ConnectedOutput {
return false;
}
} else if other.product != "" {
let ok = self.edid.descriptors.iter()
.filter_map(|d| match d {
&edid::Descriptor::ProductName(ref s) => Some(s.as_ref()),
_ => None,
})
.nth(0)
.map(|product| product == other.product)
.unwrap_or(false);
if !ok {
if self.product() != other.product {
return false;
}
}
@ -72,16 +62,7 @@ impl PartialEq<SavedOutput> for ConnectedOutput {
return false;
}
} else if other.serial != "" {
let ok = self.edid.descriptors.iter()
.filter_map(|d| match d {
&edid::Descriptor::SerialNumber(ref s) => Some(s.as_ref()),
_ => None,
})
.nth(0)
.map(|serial| serial == other.serial)
.unwrap_or(false);
if !ok {
if self.serial() != other.serial {
return false;
}
}
@ -122,7 +103,10 @@ fn main() {
},
};
writeln!(&mut stderr, "Connected outputs: {:?}", connected_outputs).unwrap();
writeln!(&mut stderr, "Connected outputs:").unwrap();
for o in &connected_outputs {
writeln!(&mut stderr, "{}", o).unwrap();
}
//let store = GnomeStore{};
let store = store::KanshiStore{};