Dynamically map connection.

This commit is contained in:
Bill Thiede 2020-11-05 18:02:19 -08:00
parent ab33174956
commit a39c6c78da
2 changed files with 26 additions and 25 deletions

View File

@ -80,15 +80,14 @@ impl fmt::Debug for Orientation {
#[derive(Debug, Default)]
pub struct Screen {
pub name: Option<String>,
pub connection: String,
pub name: String,
pub resolution: Resolution,
pub offset: Offset,
pub orientation: Orientation,
}
impl fmt::Display for Screen {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl Screen {
fn metamode(&self, map: &ScreenMapping) -> Result<String, CommandError> {
let Resolution { width, height } = self.resolution;
let Offset { x, y } = self.offset;
let (in_w, in_h) = match self.orientation {
@ -102,8 +101,13 @@ impl fmt::Display for Screen {
Orientation::Left => "90",
};
write!(f, "{connection}: {w}x{h} @{in_w}x{in_h} +{x}+{y} {{ViewPortIn={in_w}x{in_h}, ViewPortOut={w}x{h}+0+0, Rotation={rotation}}}",
connection=self.connection,
let connection = match map.get(&self.name) {
Some(connection) => connection,
None => return Err(CommandError::MissingMonitor(self.name.to_string())),
};
Ok(format!("{connection}: {w}x{h} @{in_w}x{in_h} +{x}+{y} {{ViewPortIn={in_w}x{in_h}, ViewPortOut={w}x{h}+0+0, Rotation={rotation}}}",
connection=connection,
w=width,
h=height,
in_w=in_w,
@ -111,7 +115,7 @@ impl fmt::Display for Screen {
x=x,
y=y,
rotation=rotation,
)
))
}
}
@ -120,7 +124,7 @@ pub struct Config {
pub screens: Vec<Screen>,
}
pub fn run_cmd(screen_mapping: ScreenMapping, cfg: Config) -> Result<Output, CommandError> {
pub fn run_cmd(screen_mapping: &ScreenMapping, cfg: Config) -> Result<Output, CommandError> {
let args = build_cmd_args(screen_mapping, cfg)?;
if cfg!(debug_assertions) {
Ok(Command::new("echo").args(args).output()?)
@ -129,12 +133,15 @@ pub fn run_cmd(screen_mapping: ScreenMapping, cfg: Config) -> Result<Output, Com
}
}
fn build_cmd_args(screen_mapping: ScreenMapping, cfg: Config) -> Result<Vec<String>, CommandError> {
fn build_cmd_args(
screen_mapping: &ScreenMapping,
cfg: Config,
) -> Result<Vec<String>, CommandError> {
let metamode = cfg
.screens
.iter()
.map(|c| c.to_string())
.collect::<Vec<String>>()
.map(|c| c.metamode(screen_mapping))
.collect::<Result<Vec<String>, CommandError>>()?
.join(", ");
Ok(vec![
"nvidia-settings",
@ -155,8 +162,7 @@ mod tests {
let cfg = Config {
screens: vec![
Screen {
name: Some("DELL U2415".to_string()),
connection: "DFP-5.8".to_string(),
name: "DELL U2415".to_string(),
resolution: Resolution {
width: 1920,
height: 1200,
@ -165,8 +171,7 @@ mod tests {
..Default::default()
},
Screen {
name: Some("LG Electronics 34UM95".to_string()),
connection: "DFP-0.1".to_string(),
name: "LG Electronics 34UM95".to_string(),
resolution: Resolution {
width: 3440,
height: 1440,
@ -175,8 +180,7 @@ mod tests {
..Default::default()
},
Screen {
name: Some("Lenovo Group Limited P27h-20".to_string()),
connection: "DFP-0.8".to_string(),
name: "Lenovo Group Limited P27h-20".to_string(),
resolution: Resolution {
width: 2560,
height: 1440,
@ -195,7 +199,7 @@ mod tests {
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
assert_eq!(
build_cmd_args(map, cfg).expect("failed build_cmd_args"),
build_cmd_args(&map, cfg).expect("failed build_cmd_args"),
vec![
"nvidia-settings",
"--assign",

View File

@ -6,8 +6,7 @@ fn main() -> Result<()> {
let cfg = Config {
screens: vec![
Screen {
name: Some("DELL U2415".to_string()),
connection: "DFP-5.8".to_string(),
name: ("DELL U2415".to_string()),
resolution: Resolution {
width: 1920,
height: 1200,
@ -16,8 +15,7 @@ fn main() -> Result<()> {
..Default::default()
},
Screen {
name: Some("LG Electronics 34UM95".to_string()),
connection: "DFP-0.1".to_string(),
name: ("LG Electronics 34UM95".to_string()),
resolution: Resolution {
width: 3440,
height: 1440,
@ -26,8 +24,7 @@ fn main() -> Result<()> {
..Default::default()
},
Screen {
name: Some("Lenovo Group Limited P27h-20".to_string()),
connection: "DFP-0.8".to_string(),
name: ("Lenovo Group Limited P27h-20".to_string()),
resolution: Resolution {
width: 2560,
height: 1440,
@ -39,7 +36,7 @@ fn main() -> Result<()> {
};
let map = screen_mapping_from_xorg_log("/var/log/Xorg.0.log")?;
let cmd = run_cmd(map, cfg)?;
let cmd = run_cmd(&map, cfg)?;
println!("cmd {:#?}", cmd);
Ok(())