Skip to content

Commit

Permalink
feat: Added functionality to display detailed information about preset
Browse files Browse the repository at this point in the history
Implemented 'show' subcommand to display detailed information about specified preset.
  • Loading branch information
alexdemb committed May 14, 2024
1 parent 5be7828 commit af52856
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
37 changes: 34 additions & 3 deletions code/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{config_file, defaults, mutter};
use crate::model::Preset;
use super::model::Preset;
use super::{config_file, defaults, mutter, out};
use clap::{arg, command, Arg, ArgAction};
use log::{debug, info};
use std::error::Error;
Expand Down Expand Up @@ -201,6 +201,27 @@ impl Command for RenameCommand {
}
}

struct ShowCommand {
name: String,
}

impl Command for ShowCommand {
fn execute(&self, options: &GenericOptions) -> Result<(), Box<dyn Error>> {
info!("Printing information about preset '{}'", self.name);

let configuration = config_file::read_config(&options.config_path)?;

match configuration.get_preset(&self.name) {
Some(preset) => {
out::print_preset(preset);
}
None => Err(format!("Preset '{}' was not found", self.name))?,
}

Ok(())
}
}

pub struct Cli {
pub command: Box<dyn Command>,
pub options: GenericOptions,
Expand Down Expand Up @@ -269,7 +290,14 @@ impl Cli {
.help("Override existing preset with the same name if exist")
.action(ArgAction::SetTrue)
.required(false)
)
),
clap::Command::new("show")
.about("Print information about preset")
.arg(
arg!([NAME])
.required(true)
.help("Preset name")
),
])
.arg(Arg::new("verbose")
.short('v')
Expand Down Expand Up @@ -316,6 +344,9 @@ impl Cli {
.to_string(),
force: sub_matches.get_flag("force"),
}),
Some(("show", sub_matches)) => Box::new(ShowCommand {
name: sub_matches.get_one::<String>("NAME").unwrap().to_string(),
}),
_ => Err("Unknown command")?,
};

Expand Down
1 change: 1 addition & 0 deletions code/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod defaults;
mod model;
mod mutter;
mod mutter_dbus;
mod out;

fn main() {
if let Ok(cli) = cli::Cli::parse() {
Expand Down
60 changes: 60 additions & 0 deletions code/src/out.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use super::model::Preset;
use std::collections::HashMap;

pub fn print_preset(preset: &Preset) {
println!("Preset: '{}'", preset.name);
println!();

let display_config = &preset.display_config;

println!("Physical displays:");
println!();

let mut current_mode_by_connector = HashMap::<String, String>::new();

for monitor in &display_config.monitors {
println!("{}:", monitor.monitor_info.connector);
println!("Vendor: {}", monitor.monitor_info.vendor);
println!("Model: {}", monitor.monitor_info.product);
print!("Supported modes: ");

for mode in &monitor.modes {
if let Some(v) = mode.properties.get("is-current") {
if v == "1" {
current_mode_by_connector.insert(
monitor.monitor_info.connector.clone(),
format!("{}x{}@{}", mode.width, mode.height, mode.refresh_rate),
);
}
}
print!("{}x{}@{} ", mode.width, mode.height, mode.refresh_rate);
}
println!();
println!();
}

println!("Logical displays:");
println!();
for lm in &display_config.logical_monitors {
let connectors: Vec<&String> = lm.monitors.iter().map(|m| &m.connector).collect();
println!("Connectors: {:?}", connectors);
println!("X: {}", lm.x);
println!("Y: {}", lm.y);
println!("Scale: {}", lm.scale);
println!("Primary: {}", lm.primary);
println!("Transform: {}", lm.transform);
println!("Mode:");

for connector in connectors {
println!(
"{}: {}",
connector,
current_mode_by_connector
.get(connector)
.unwrap_or(&"".to_string())
);
}

println!();
}
}

0 comments on commit af52856

Please sign in to comment.