code format

This commit is contained in:
2024-12-31 02:51:58 +00:00
parent c5f35ebf06
commit 20ec42d126
4 changed files with 98 additions and 66 deletions

View File

@@ -1,6 +1,6 @@
use kdl::KdlDocument;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
use kdl::KdlDocument;
/// Configuration for swayout. /// Configuration for swayout.
pub struct Config { pub struct Config {
@@ -16,7 +16,7 @@ pub struct Config {
pub struct Layout { pub struct Layout {
pub name: String, pub name: String,
pub automatic: bool, pub automatic: bool,
pub outputs: HashMap<String,OutputConfig>, pub outputs: HashMap<String, OutputConfig>,
} }
/// Defines a monitor by make, model, and serial. /// Defines a monitor by make, model, and serial.
@@ -56,44 +56,51 @@ pub fn get_config() -> Config {
} }
} }
} }
fn parse_config(kdl:String) -> Config { fn parse_config(kdl: String) -> Config {
let doc:KdlDocument = kdl.parse().expect("failed to parse config KDL"); let doc: KdlDocument = kdl.parse().expect("failed to parse config KDL");
let monitors:Vec<Monitor> = doc.nodes().iter() let monitors: Vec<Monitor> = doc
.nodes()
.iter()
.filter(|node| node.name().value() == "monitor") .filter(|node| node.name().value() == "monitor")
.map(|monitor_node| { .map(|monitor_node| Monitor {
Monitor { name: String::from(monitor_node[0].as_string().unwrap()),
name : String::from(monitor_node[0].as_string().unwrap()), make: String::from(monitor_node["make"].as_string().unwrap()),
make : String::from(monitor_node["make"].as_string().unwrap()), model: String::from(monitor_node["model"].as_string().unwrap()),
model : String::from(monitor_node["model"].as_string().unwrap()), serial: String::from(monitor_node["serial"].as_string().unwrap()),
serial : String::from(monitor_node["serial"].as_string().unwrap()),
}
}) })
.collect(); .collect();
let layouts:Vec<Layout> = doc.nodes().iter() let layouts: Vec<Layout> = doc
.nodes()
.iter()
.filter(|node| node.name().value() == "layout") .filter(|node| node.name().value() == "layout")
.map(|layout_node| { .map(|layout_node| {
let layout_name = String::from(layout_node[0].as_string().unwrap()); let name = String::from(layout_node[0].as_string().unwrap());
let automatic = layout_node.get("automatic").is_some_and(|v| v.as_bool().is_some_and(|a| a)); let automatic = layout_node
let monitor_name_to_output_config:HashMap<String,OutputConfig> = layout_node .get("automatic")
.children().unwrap().nodes().iter() .is_some_and(|v| v.as_bool().is_some_and(|a| a));
let outputs: HashMap<String, OutputConfig> = layout_node
.children()
.unwrap()
.nodes()
.iter()
.map(|output_node| { .map(|output_node| {
let output = String::from(output_node[0].as_string().unwrap()); let output = String::from(output_node[0].as_string().unwrap());
let output_config = OutputConfig { let output_config = OutputConfig {
mode : String::from(output_node["mode"].as_string().unwrap()), mode: String::from(output_node["mode"].as_string().unwrap()),
scale : String::from(output_node["scale"].as_string().unwrap()), scale: String::from(output_node["scale"].as_string().unwrap()),
transform : String::from(output_node["transform"].as_string().unwrap()), transform: String::from(output_node["transform"].as_string().unwrap()),
x : output_node["x"].as_integer().unwrap() as u16, x: output_node["x"].as_integer().unwrap() as u16,
y : output_node["y"].as_integer().unwrap() as u16, y: output_node["y"].as_integer().unwrap() as u16,
}; };
(output, output_config) (output, output_config)
}) })
.collect(); .collect();
Layout { Layout {
name : layout_name, name,
automatic: automatic, automatic,
outputs: monitor_name_to_output_config outputs,
} }
}) })
.collect(); .collect();

View File

@@ -27,8 +27,7 @@ fn get_layouts() -> HashSet<String> {
// add each individual output (by monitor name if the monitor is defined, else by output name) // add each individual output (by monitor name if the monitor is defined, else by output name)
available_outputs.iter().for_each(|output| { available_outputs.iter().for_each(|output| {
// if there is a monitor for the output, use the monitor name, else use the output name // if there is a monitor for the output, use the monitor name, else use the output name
let monitor_opt = config.monitors.iter() let monitor_opt = config.monitors.iter().find(|monitor| {
.find(|monitor| {
monitor.make == output.make monitor.make == output.make
&& monitor.model == output.model && monitor.model == output.model
&& monitor.serial == output.serial && monitor.serial == output.serial
@@ -44,28 +43,39 @@ fn get_layouts() -> HashSet<String> {
} }
/// Get the names of monitors that are available (that match an available output) /// Get the names of monitors that are available (that match an available output)
fn get_available_monitor_names<'a,'b>(config:&'a Config, available_outputs:&'b Vec<Output>) -> Vec<&'a String> { fn get_available_monitor_names<'a, 'b>(
config.monitors.iter() config: &'a Config,
.filter(|monitor| available_outputs: &'b Vec<Output>,
available_outputs.iter().any(|output| ) -> Vec<&'a String> {
config
.monitors
.iter()
.filter(|monitor| {
available_outputs.iter().any(|output| {
monitor.make == output.make monitor.make == output.make
&& monitor.model == output.model && monitor.model == output.model
&& output.serial == monitor.serial && output.serial == monitor.serial
) })
) })
.map(|monitor| &monitor.name) .map(|monitor| &monitor.name)
.collect() .collect()
} }
/// Get the layouts for which all monitors are available. /// Get the layouts for which all monitors are available.
fn get_available_layouts<'a,'b>(config:&'a Config, available_outputs:&'b Vec<Output>) -> Vec<&'a Layout> { fn get_available_layouts<'a, 'b>(
let available_monitor_names:Vec<&String> = get_available_monitor_names(&config, available_outputs); config: &'a Config,
available_outputs: &'b Vec<Output>,
) -> Vec<&'a Layout> {
let available_monitor_names: Vec<&String> =
get_available_monitor_names(&config, available_outputs);
let mut layouts: Vec<&'a Layout> = Vec::new(); let mut layouts: Vec<&'a Layout> = Vec::new();
// Add each layout defined in the config file for which all outputs are available // Add each layout defined in the config file for which all outputs are available
config.layouts.iter().for_each(|layout| { config.layouts.iter().for_each(|layout| {
if layout.outputs.iter() if layout
.outputs
.iter()
.all(|(output_name, _output)| available_monitor_names.contains(&output_name)) .all(|(output_name, _output)| available_monitor_names.contains(&output_name))
{ {
layouts.push(&layout); layouts.push(&layout);
@@ -92,10 +102,21 @@ pub fn apply_layout(layout_name: &String) {
let mut output_config_map: HashMap<&String, &OutputConfig> = HashMap::new(); let mut output_config_map: HashMap<&String, &OutputConfig> = HashMap::new();
// First check if the layout is defined in the rules // First check if the layout is defined in the rules
if let Some(layout) = rules.layouts.iter().find(|layout| &layout.name == layout_name) { if let Some(layout) = rules
.layouts
.iter()
.find(|layout| &layout.name == layout_name)
{
// The layout is defined in the rules. // The layout is defined in the rules.
layout.outputs.iter().for_each(|(monitor_name, output_config)| { layout
let monitor = &rules.monitors.iter().find(|monitor| &monitor.name == monitor_name).unwrap(); .outputs
.iter()
.for_each(|(monitor_name, output_config)| {
let monitor = &rules
.monitors
.iter()
.find(|monitor| &monitor.name == monitor_name)
.unwrap();
// find the output for the monitor to get the output name. // find the output for the monitor to get the output name.
let output_opt = outputs.iter().find(|output| { let output_opt = outputs.iter().find(|output| {
@@ -112,10 +133,15 @@ pub fn apply_layout(layout_name: &String) {
} else { } else {
// The layout is not defined in the rules. // The layout is not defined in the rules.
// See if it is a monitor name... // See if it is a monitor name...
if let Some(monitor) = rules.monitors.iter().find(|monitor| &monitor.name == layout_name) { if let Some(monitor) = rules
.monitors
.iter()
.find(|monitor| &monitor.name == layout_name)
{
// It is a monitor name. Find the matching output... // It is a monitor name. Find the matching output...
if let Some(output) = outputs.iter().find(|output| { if let Some(output) = outputs.iter().find(|output| {
output.make == monitor.make && output.model == monitor.model output.make == monitor.make
&& output.model == monitor.model
&& output.serial == monitor.serial && output.serial == monitor.serial
}) { }) {
output_config_map.insert(&output.name, &output.output_config); output_config_map.insert(&output.name, &output.output_config);
@@ -124,8 +150,7 @@ pub fn apply_layout(layout_name: &String) {
} }
} else { } else {
// See if it is an output name... // See if it is an output name...
if let Some(output) = outputs.iter() if let Some(output) = outputs.iter().find(|output| &output.name == layout_name) {
.find(|output| &output.name == layout_name) {
output_config_map.insert(&output.name, &output.output_config); output_config_map.insert(&output.name, &output.output_config);
} else { } else {
panic!("could not find layout, monitor, or output {layout_name}") panic!("could not find layout, monitor, or output {layout_name}")
@@ -144,7 +169,8 @@ pub fn apply_automatic() -> Option<String> {
if let Some(layout) = get_available_layouts(&config, &outputs) if let Some(layout) = get_available_layouts(&config, &outputs)
.iter() .iter()
.find(|layout| layout.automatic) { .find(|layout| layout.automatic)
{
apply_layout(&layout.name); apply_layout(&layout.name);
Some(String::from(&layout.name)) Some(String::from(&layout.name))
} else { } else {

View File

@@ -1,8 +1,8 @@
use crate::config::OutputConfig;
use serde_json::{from_str, Value};
use std::collections::HashMap; use std::collections::HashMap;
use std::process::Command; use std::process::Command;
use std::str::from_utf8; use std::str::from_utf8;
use serde_json::{from_str, Value};
use crate::config::OutputConfig;
/// An output, as returned by `swaymsg -t get_outputs`. /// An output, as returned by `swaymsg -t get_outputs`.
pub struct Output { pub struct Output {
@@ -68,12 +68,11 @@ fn get_mode(output: &Value) -> String {
/// Apply the specified outputs. Enable all outputs in [outputs], disable others. /// Apply the specified outputs. Enable all outputs in [outputs], disable others.
pub fn apply_outputs(all_outputs: &Vec<Output>, outputs: &HashMap<&String, &OutputConfig>) { pub fn apply_outputs(all_outputs: &Vec<Output>, outputs: &HashMap<&String, &OutputConfig>) {
// set enabled outputs first, then set disabled outputs. // set enabled outputs first, then set disabled outputs.
// That way if some work before an error, you have at least one output enabled. // That way if some work before an error, you have at least one output enabled.
// for outputs to be enabled: map of output name to config // for outputs to be enabled: map of output name to config
let mut enabled: HashMap<&String,&OutputConfig> = HashMap::new(); let mut enabled: HashMap<&String, &OutputConfig> = HashMap::new();
// for outputs to be disabled: output names // for outputs to be disabled: output names
let mut disabled: Vec<&String> = Vec::new(); let mut disabled: Vec<&String> = Vec::new();
@@ -86,7 +85,7 @@ pub fn apply_outputs(all_outputs: &Vec<Output>, outputs: &HashMap<&String, &Outp
}); });
let mut cmd = Command::new("swaymsg"); let mut cmd = Command::new("swaymsg");
enabled.iter().for_each(|(output_name,output_config)| { enabled.iter().for_each(|(output_name, output_config)| {
cmd.arg("output"); cmd.arg("output");
cmd.arg(&output_name); cmd.arg(&output_name);
cmd.arg("enable"); cmd.arg("enable");