Use HashSet for available layout names to avoid monitor-layout dup

This commit is contained in:
2024-12-31 02:46:35 +00:00
parent 2ce2631bbc
commit c5f35ebf06
3 changed files with 8 additions and 9 deletions

2
Cargo.lock generated
View File

@@ -192,7 +192,7 @@ dependencies = [
[[package]] [[package]]
name = "swayout" name = "swayout"
version = "1.1.0" version = "1.1.1"
dependencies = [ dependencies = [
"kdl", "kdl",
"serde", "serde",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "swayout" name = "swayout"
version = "1.1.0" version = "1.1.1"
edition = "2021" edition = "2021"
description = "A tool to configure sway outputs" description = "A tool to configure sway outputs"
authors = ["Stephen Byrne"] authors = ["Stephen Byrne"]

View File

@@ -1,4 +1,4 @@
use std::collections::HashMap; use std::collections::{HashMap, HashSet};
mod config; mod config;
use crate::config::{get_config, Config, Layout, OutputConfig}; use crate::config::{get_config, Config, Layout, OutputConfig};
@@ -11,18 +11,17 @@ use crate::sway::{apply_outputs, get_outputs, Output};
/// for which all outputs are available, /// for which all outputs are available,
/// one for each monitor defined in the configuration file (for using just that monitor), /// one for each monitor defined in the configuration file (for using just that monitor),
/// and one for each available output that is not a configured monitor (for using just that output). /// and one for each available output that is not a configured monitor (for using just that output).
fn get_layouts() -> Vec<String> { fn get_layouts() -> HashSet<String> {
let available_outputs = get_outputs(); let available_outputs = get_outputs();
let config = get_config(); let config = get_config();
let mut layout_names: HashSet<String> = HashSet::new();
let mut layout_names: Vec<String> = 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
get_available_layouts(&config, &available_outputs) get_available_layouts(&config, &available_outputs)
.iter() .iter()
.for_each(|layout| { .for_each(|layout| {
layout_names.push(String::from(&layout.name)); layout_names.insert(String::from(&layout.name));
}); });
// 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)
@@ -35,9 +34,9 @@ fn get_layouts() -> Vec<String> {
&& monitor.serial == output.serial && monitor.serial == output.serial
}); });
if let Some(monitor) = monitor_opt { if let Some(monitor) = monitor_opt {
layout_names.push(String::from(&monitor.name)); layout_names.insert(String::from(&monitor.name));
} else { } else {
layout_names.push(String::from(&output.name)); layout_names.insert(String::from(&output.name));
} }
}); });