Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyTurtleDev committed Feb 19, 2024
1 parent eef0c2d commit 95308f1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
8 changes: 4 additions & 4 deletions more-wallpapers/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(target_os = "linux")]
use std::env;
use std::io;
use std::{io, process::Command, ffi::OsString};
use thiserror::Error;

#[cfg(target_os = "linux")]
Expand All @@ -12,13 +12,13 @@ use xrandr;
#[derive(Debug, Error)]
pub enum CommandError {
#[cfg(target_os = "linux")]
#[error("failed to execute command {0}: {1}")]
CommandIO(&'static str, std::io::Error),
#[error("failed to execute program {0:?}: {1}")]
CommandIO(OsString, std::io::Error),

#[cfg(target_os = "linux")]
#[error("{command:?} exit with code {exit_code:?}:\n{}", String::from_utf8_lossy(.stderr))]
CommandStatus {
command: &'static str,
command: Command,
exit_code: Option<i32>,
stderr: Vec<u8>,
},
Expand Down
18 changes: 10 additions & 8 deletions more-wallpapers/src/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{error::CommandError, load_env_var, Environment, WallpaperBuilder, WallpaperError};
use std::{ffi::OsStr, process, process::Command};
use std::{ffi::OsStr, process::Command};

mod cinnamon;
mod kde;
Expand Down Expand Up @@ -77,23 +77,25 @@ pub(crate) fn set_screens_from_builder(builder: WallpaperBuilder) -> Result<(),
}

/// run a command, check error code and convert the result
fn run<I, S>(program: &'static str, args: I) -> Result<Vec<u8>, CommandError>
fn run<I, S>(program: &str, args: I) -> Result<Vec<u8>, CommandError>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
check_command_error(Command::new(program).args(args).output(), program)
let mut command = Command::new(program);
command.args(args);
run_command(command)
}

/// allow also checking more complex commands
fn check_command_error(
output: Result<process::Output, std::io::Error>,
program: &'static str,
fn run_command(
mut command: Command,
) -> Result<Vec<u8>, CommandError> {
let output = output.map_err(|err| CommandError::CommandIO(program, err))?;
let output = command.output();
let output = output.map_err(|err| CommandError::CommandIO(command.get_program().into(), err))?;
if !output.status.success() {
return Err(CommandError::CommandStatus {
command: program,
command,
exit_code: output.status.code(),
stderr: output.stderr,
});
Expand Down
6 changes: 3 additions & 3 deletions more-wallpapers/src/linux/sway.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::check_command_error;
use super::run_command;
use crate::{error::WallpaperError, Mode, Screen};
use serde::Deserialize;
use std::process::Command;
Expand Down Expand Up @@ -47,7 +47,7 @@ struct OutputScreens {
pub(crate) fn get_screens() -> Result<Vec<Screen>, WallpaperError> {
let mut command = Command::new("swaymsg");
command.args(["-t", "get_outputs"]);
let output = check_command_error(command.output(), "swaymsg")?;
let output = run_command(command)?;
let output = String::from_utf8(output).unwrap();
println!("{output}");
let output: Vec<OutputScreens> = serde_json::from_str(&output)?;
Expand All @@ -72,7 +72,7 @@ pub(crate) fn set_screens(screens: Vec<Screen>) -> Result<(), WallpaperError> {
.arg("bg")
.arg(screen.wallpaper.unwrap())
.arg(format!("{}", SMode::from(screen.mode.unwrap())));
check_command_error(command.output(), "swaymsg")?;
run_command(command)?;
}
Ok(())
}
4 changes: 2 additions & 2 deletions more-wallpapers/src/linux/x11.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{error::CommandError, linux::check_command_error, Mode, Screen};
use crate::{error::CommandError, linux::run_command, Mode, Screen};
use std::process::Command;

pub(crate) fn get_screens() -> Result<Vec<Screen>, xrandr::XrandrError> {
Expand Down Expand Up @@ -34,6 +34,6 @@ pub(crate) fn set_screens(screens: Vec<Screen>) -> Result<(), CommandError> {
screen.wallpaper.as_ref().unwrap().as_str(),
]);
}
check_command_error(command.output(), "xwallpaper")?;
run_command(command)?;
Ok(())
}
10 changes: 5 additions & 5 deletions more-wallpapers/src/linux/xfce.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use super::check_command_error;
use crate::{error::CommandError, load_env_var, Environment, Mode, Screen, WallpaperBuilder, WallpaperError};
use super::run_command;
use crate::{Mode, Screen, WallpaperError};
use std::{collections::HashMap, ffi::OsStr, process::Command};

fn load_property(property: &str) -> Result<String, WallpaperError> {
let mut command = Command::new("xfconf-query");
command.args(["--channel", "xfce4-desktop", "p"]);
command.arg(property);
let output = check_command_error(command.output(), "xfconf-query")?;
let output = run_command(command)?;
let output = String::from_utf8(output).unwrap();
Ok(output)
}

pub(crate) fn get_screens() -> Result<Vec<Screen>, WallpaperError> {
let mut command = Command::new("xfconf-query");
command.args(["--channel", "xfce4-desktop", "--list"]);
let output = check_command_error(command.output(), "xfconf-query")?;
let output = run_command(command)?;
let output = String::from_utf8(output).unwrap();
// the outpult looks like the following:
//
Expand Down Expand Up @@ -82,7 +82,7 @@ pub(crate) fn set_screens(screens: Vec<Screen>) -> Result<(), WallpaperError> {
fn set_key<P: AsRef<OsStr>>(key: String, property: P) -> Result<(), WallpaperError> {
let mut command = Command::new("xfconf-query");
command.args(["--channel", "xfce4-desktop", "--set"]).arg(key).arg(property);
check_command_error(command.output(), "xfconf-query")?;
run_command(command)?;
Ok(())
}

Expand Down

0 comments on commit 95308f1

Please sign in to comment.