diff --git a/src/lib.rs b/src/lib.rs index 1b13932..796174d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -80,15 +80,14 @@ impl fmt::Debug for Orientation { #[derive(Debug, Default)] pub struct Screen { - pub name: Option, - 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 { 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, } -pub fn run_cmd(screen_mapping: ScreenMapping, cfg: Config) -> Result { +pub fn run_cmd(screen_mapping: &ScreenMapping, cfg: Config) -> Result { 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 Result, CommandError> { +fn build_cmd_args( + screen_mapping: &ScreenMapping, + cfg: Config, +) -> Result, CommandError> { let metamode = cfg .screens .iter() - .map(|c| c.to_string()) - .collect::>() + .map(|c| c.metamode(screen_mapping)) + .collect::, 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", diff --git a/src/main.rs b/src/main.rs index dc2d370..4c106b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(())