use crate::{
pure::RelativePosition,
util::{spawn, spawn_for_output, spawn_with_args},
Error, Result,
};
pub mod debug;
pub mod dmenu;
pub fn update_monitors_via_xrandr(
primary: &str,
secondary: &str,
position: RelativePosition,
) -> Result<()> {
let raw = spawn_for_output("xrandr")?;
let status = raw
.lines()
.find(|line| line.starts_with(secondary))
.ok_or_else(|| {
Error::Custom("unable to find secondary monitor in xrandr output".to_owned())
})?
.split(' ')
.nth(1)
.ok_or_else(|| Error::Custom("unexpected xrandr output".to_owned()))?;
let pos = match position {
RelativePosition::Left => "--left-of",
RelativePosition::Right => "--right-of",
RelativePosition::Above => "--above",
RelativePosition::Below => "--below",
};
spawn(format!("xrandr --output {} --primary --auto", primary))?;
match status {
"disconnected" => spawn(format!("xrandr --output {secondary} --off")),
"connected" => spawn(format!(
"xrandr --output {secondary} --auto {pos} {primary}",
)),
_ => Ok(()),
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum NotifyLevel {
Low,
Normal,
Critical,
}
pub fn notify_send(title: impl AsRef<str>, body: impl AsRef<str>) -> Result<()> {
notify_send_custom(title, body, NotifyLevel::Normal, 5000)
}
pub fn notify_send_custom(
title: impl AsRef<str>,
body: impl AsRef<str>,
level: NotifyLevel,
duration: usize,
) -> Result<()> {
let level = match level {
NotifyLevel::Low => "low",
NotifyLevel::Normal => "normal",
NotifyLevel::Critical => "critical",
};
spawn_with_args(
"notify-send",
&[
"-u",
level,
"-t",
&duration.to_string(),
title.as_ref(),
body.as_ref(),
],
)
}