2024-02-29 20:36:38 +00:00
|
|
|
use std::{
|
|
|
|
convert::Infallible,
|
|
|
|
fmt::Display,
|
|
|
|
num::{ParseFloatError, ParseIntError},
|
2024-02-29 20:40:21 +00:00
|
|
|
process::{self, Stdio},
|
2024-02-29 20:36:38 +00:00
|
|
|
str::FromStr,
|
|
|
|
};
|
|
|
|
|
|
|
|
use log::{debug, error};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use strum::IntoEnumIterator;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
2024-03-01 18:06:52 +00:00
|
|
|
use crate::cmd;
|
|
|
|
|
2024-02-29 20:36:38 +00:00
|
|
|
use self::{
|
|
|
|
attribute::{Attribute, AttributeError},
|
|
|
|
command::{CommandError, HlwmCommand},
|
|
|
|
key::KeyParseError,
|
|
|
|
setting::{Setting, SettingName},
|
2024-03-02 21:18:21 +00:00
|
|
|
tag::TagStatus,
|
2024-02-29 20:36:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub mod attribute;
|
|
|
|
pub mod color;
|
|
|
|
pub mod command;
|
|
|
|
mod hex;
|
|
|
|
mod hlwmbool;
|
|
|
|
pub mod hook;
|
|
|
|
pub mod key;
|
|
|
|
mod octal;
|
2024-03-01 18:06:52 +00:00
|
|
|
pub mod pad;
|
2024-02-29 20:36:38 +00:00
|
|
|
pub mod rule;
|
|
|
|
pub mod setting;
|
2024-03-02 21:18:21 +00:00
|
|
|
pub mod tag;
|
2024-02-29 20:36:38 +00:00
|
|
|
pub mod theme;
|
|
|
|
pub mod window;
|
|
|
|
#[macro_use]
|
|
|
|
mod macros;
|
|
|
|
|
|
|
|
pub use hlwmbool::ToggleBool;
|
|
|
|
|
|
|
|
pub type Monitor = u32;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct Client;
|
|
|
|
|
|
|
|
impl Client {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn herbstclient() -> std::process::Command {
|
|
|
|
std::process::Command::new("herbstclient")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Run the command and wait for it to finish.
|
|
|
|
pub fn execute(&self, command: HlwmCommand) -> Result<process::Output, CommandError> {
|
2024-02-29 20:40:21 +00:00
|
|
|
let args = command.args();
|
2024-02-29 20:36:38 +00:00
|
|
|
debug!("running command: [{}]", (&args).join(" "),);
|
|
|
|
let output = Self::herbstclient()
|
2024-03-01 18:06:52 +00:00
|
|
|
.arg("--no-newline")
|
2024-02-29 20:36:38 +00:00
|
|
|
.args(args)
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.stdin(Stdio::null())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()?
|
|
|
|
.wait_with_output()?;
|
2024-03-01 18:06:52 +00:00
|
|
|
cmd::check_status(&output)?;
|
|
|
|
Ok(output)
|
2024-02-29 20:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn execute_iter<I>(&self, commands: I) -> Result<(), (HlwmCommand, CommandError)>
|
|
|
|
where
|
|
|
|
I: IntoIterator<Item = HlwmCommand>,
|
|
|
|
{
|
|
|
|
for cmd in commands.into_iter() {
|
|
|
|
if let Err(err) = self.execute(cmd.clone()) {
|
|
|
|
return Err((cmd, err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-03-01 22:26:21 +00:00
|
|
|
pub fn get_str_attr(&self, attr: String) -> Result<String, CommandError> {
|
|
|
|
self.query(HlwmCommand::GetAttr(attr))?
|
|
|
|
.first()
|
|
|
|
.cloned()
|
|
|
|
.ok_or(CommandError::Empty)
|
|
|
|
}
|
|
|
|
|
2024-02-29 20:36:38 +00:00
|
|
|
pub fn get_attr(&self, attr: String) -> Result<Attribute, CommandError> {
|
|
|
|
let attr_type = self
|
|
|
|
.query(HlwmCommand::AttrType(attr.clone()))?
|
|
|
|
.first()
|
|
|
|
.cloned()
|
|
|
|
.ok_or(CommandError::Empty)?;
|
|
|
|
let attr_val = self
|
|
|
|
.query(HlwmCommand::GetAttr(attr))?
|
|
|
|
.first()
|
|
|
|
.cloned()
|
|
|
|
.ok_or(CommandError::Empty)?;
|
|
|
|
Ok(Attribute::new(&attr_type, &attr_val)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_setting(&self, setting: SettingName) -> Result<Setting, CommandError> {
|
2024-03-01 19:36:29 +00:00
|
|
|
Ok(Setting::from_str(&format!(
|
|
|
|
"{setting}\t{}",
|
|
|
|
String::from_utf8(self.execute(HlwmCommand::Get(setting))?.stdout,)?
|
|
|
|
))
|
|
|
|
.map_err(|err| {
|
|
|
|
error!("failed getting setting [{setting}]: {err}");
|
|
|
|
StringParseError::UnknownValue
|
|
|
|
})?)
|
2024-02-29 20:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn query(&self, command: HlwmCommand) -> Result<Vec<String>, CommandError> {
|
|
|
|
let lines = String::from_utf8(self.execute(command)?.stdout)?
|
|
|
|
.split("\n")
|
|
|
|
.map(|l| l.trim())
|
|
|
|
.filter(|l| !l.is_empty())
|
|
|
|
.map(|l| l.to_owned())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if lines.is_empty() {
|
|
|
|
return Err(CommandError::Empty);
|
|
|
|
}
|
|
|
|
Ok(lines)
|
|
|
|
}
|
|
|
|
|
2024-03-02 21:18:21 +00:00
|
|
|
#[allow(unused)]
|
2024-02-29 20:36:38 +00:00
|
|
|
pub fn tag_status(&self) -> Result<Vec<TagStatus>, CommandError> {
|
|
|
|
Ok(self
|
|
|
|
.query(HlwmCommand::TagStatus { monitor: None })?
|
|
|
|
.first()
|
|
|
|
.ok_or(CommandError::Empty)?
|
|
|
|
.split("\t")
|
|
|
|
.filter(|f| !f.is_empty())
|
|
|
|
.map(|i| i.parse())
|
|
|
|
.collect::<Result<Vec<_>, _>>()?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ToCommandString {
|
|
|
|
fn to_command_string(&self) -> String;
|
|
|
|
}
|
|
|
|
|
2024-03-01 19:36:29 +00:00
|
|
|
#[derive(Debug, Clone, Error)]
|
2024-02-29 20:36:38 +00:00
|
|
|
pub enum StringParseError {
|
|
|
|
#[error("unknown value")]
|
|
|
|
UnknownValue,
|
|
|
|
#[error("failed parsing float: [{0}]")]
|
|
|
|
FloatError(#[from] ParseFloatError),
|
|
|
|
#[error("invalid bool value: [{0}]")]
|
|
|
|
BoolError(String),
|
|
|
|
#[error("failed parsing int: [{0}]")]
|
|
|
|
IntError(#[from] ParseIntError),
|
|
|
|
#[error("command parse error")]
|
|
|
|
CommandParseError(String),
|
|
|
|
#[error("invalid length for part [{1}]: [{0}]")]
|
|
|
|
InvalidLength(usize, &'static str),
|
|
|
|
#[error("required arguments missing: [{0}]")]
|
|
|
|
RequiredArgMissing(String),
|
|
|
|
#[error("attribute error: [{0}]")]
|
|
|
|
AttributeError(#[from] AttributeError),
|
|
|
|
#[error("key parse error: [{0}]")]
|
|
|
|
KeyParseError(#[from] KeyParseError),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
2024-03-02 21:18:21 +00:00
|
|
|
pub enum TagSelect {
|
2024-02-29 20:36:38 +00:00
|
|
|
Index(i32),
|
|
|
|
Name(String),
|
|
|
|
}
|
|
|
|
|
2024-03-02 21:18:21 +00:00
|
|
|
impl FromStr for TagSelect {
|
2024-02-29 20:36:38 +00:00
|
|
|
type Err = Infallible;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
if let Ok(val) = i32::from_str(s) {
|
|
|
|
Ok(Self::Index(val))
|
|
|
|
} else {
|
|
|
|
Ok(Self::Name(s.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 21:18:21 +00:00
|
|
|
impl Default for TagSelect {
|
2024-02-29 20:36:38 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::Index(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 21:18:21 +00:00
|
|
|
impl std::fmt::Display for TagSelect {
|
2024-02-29 20:36:38 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2024-03-02 21:18:21 +00:00
|
|
|
TagSelect::Index(i) => write!(f, "{i}"),
|
|
|
|
TagSelect::Name(name) => f.write_str(name),
|
2024-02-29 20:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
|
|
pub enum Index<N>
|
|
|
|
where
|
|
|
|
N: PartialEq,
|
|
|
|
{
|
|
|
|
Relative(N),
|
|
|
|
Absolute(N),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N> FromStr for Index<N>
|
|
|
|
where
|
|
|
|
N: PartialEq + FromStr,
|
|
|
|
{
|
|
|
|
type Err = StringParseError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let mut chars = s.chars();
|
|
|
|
let prefix = chars.next().ok_or(StringParseError::UnknownValue)?;
|
|
|
|
match prefix {
|
|
|
|
'+' => Ok(Self::Relative(
|
|
|
|
N::from_str(&chars.collect::<String>())
|
|
|
|
.map_err(|_| StringParseError::UnknownValue)?,
|
|
|
|
)),
|
|
|
|
'-' => Ok(Self::Relative(
|
|
|
|
N::from_str(s).map_err(|_| StringParseError::UnknownValue)?,
|
|
|
|
)),
|
|
|
|
_ => Ok(Self::Absolute(
|
|
|
|
N::from_str(s).map_err(|_| StringParseError::UnknownValue)?,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N> Default for Index<N>
|
|
|
|
where
|
|
|
|
N: PartialEq + Default,
|
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Absolute(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N> Display for Index<N>
|
|
|
|
where
|
|
|
|
N: Display + PartialOrd + Default + PartialEq,
|
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Index::Relative(rel) => {
|
|
|
|
if *rel > N::default() {
|
|
|
|
write!(f, "+{rel}")
|
|
|
|
} else {
|
|
|
|
write!(f, "{rel}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Index::Absolute(abs) => write!(f, "{abs}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, strum::EnumIter, PartialEq)]
|
|
|
|
pub enum Separator {
|
|
|
|
Comma,
|
|
|
|
Period,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Separator {
|
|
|
|
type Err = StringParseError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
Self::iter()
|
|
|
|
.into_iter()
|
|
|
|
.find(|i| i.to_string() == s)
|
|
|
|
.ok_or(StringParseError::UnknownValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Separator {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Comma
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Separator {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Separator::Comma => f.write_str(","),
|
|
|
|
Separator::Period => f.write_str("."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, strum::EnumIter, PartialEq)]
|
|
|
|
pub enum Operator {
|
|
|
|
/// =
|
|
|
|
Equal,
|
|
|
|
/// !=
|
|
|
|
NotEqual,
|
|
|
|
/// <=
|
|
|
|
LessThanEqual,
|
|
|
|
/// <
|
|
|
|
LessThan,
|
|
|
|
/// \>=
|
|
|
|
GreaterThanEqual,
|
|
|
|
/// \>
|
|
|
|
GreaterThan,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Operator {
|
|
|
|
type Err = StringParseError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
Self::iter()
|
|
|
|
.into_iter()
|
|
|
|
.find(|i| i.to_string() == s)
|
|
|
|
.ok_or(StringParseError::UnknownValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Operator {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Equal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Operator {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Operator::Equal => f.write_str("="),
|
|
|
|
Operator::NotEqual => f.write_str("!="),
|
|
|
|
Operator::LessThanEqual => f.write_str("<="),
|
|
|
|
Operator::LessThan => f.write_str("<"),
|
|
|
|
Operator::GreaterThanEqual => f.write_str(">="),
|
|
|
|
Operator::GreaterThan => f.write_str(">"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, strum::Display, strum::EnumIter, PartialEq)]
|
|
|
|
#[strum(serialize_all = "snake_case")]
|
|
|
|
pub enum Direction {
|
|
|
|
Up,
|
|
|
|
Down,
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Direction {
|
|
|
|
type Err = StringParseError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
Self::iter()
|
|
|
|
.into_iter()
|
|
|
|
.find(|dir| dir.to_string() == s)
|
|
|
|
.ok_or(StringParseError::UnknownValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Direction {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Up
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, strum::Display, strum::EnumIter, PartialEq)]
|
|
|
|
#[strum(serialize_all = "snake_case")]
|
|
|
|
pub enum Align {
|
|
|
|
Top(Option<f64>),
|
|
|
|
Left(Option<f64>),
|
|
|
|
Right(Option<f64>),
|
|
|
|
Bottom(Option<f64>),
|
|
|
|
Explode,
|
|
|
|
Auto,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Align {
|
|
|
|
type Err = StringParseError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let mut parts = s.split("\t");
|
|
|
|
let align = parts.next().ok_or(StringParseError::UnknownValue)?;
|
|
|
|
let fraction = match parts.next().map(|f| f64::from_str(f)) {
|
|
|
|
Some(val) => Some(val?),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
match align {
|
2024-02-29 20:40:21 +00:00
|
|
|
"bottom" | "vertical" | "vert" | "v" => Ok(Self::Bottom(fraction)),
|
2024-02-29 20:36:38 +00:00
|
|
|
"left" => Ok(Self::Left(fraction)),
|
|
|
|
"right" | "horizontal" | "horiz" | "h" => Ok(Self::Right(fraction)),
|
|
|
|
"top" => Ok(Self::Top(fraction)),
|
|
|
|
"explode" => Ok(Self::Explode),
|
|
|
|
"auto" => Ok(Self::Auto),
|
|
|
|
_ => Err(StringParseError::UnknownValue),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Align {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Top(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToCommandString for Align {
|
|
|
|
fn to_command_string(&self) -> String {
|
|
|
|
match self {
|
|
|
|
Align::Top(fraction)
|
|
|
|
| Align::Left(fraction)
|
|
|
|
| Align::Right(fraction)
|
|
|
|
| Align::Bottom(fraction) => match fraction {
|
|
|
|
Some(fraction) => vec![self.to_string(), fraction.to_string()],
|
|
|
|
None => vec![self.to_string()],
|
|
|
|
},
|
|
|
|
Align::Explode | Align::Auto => vec![self.to_string()],
|
|
|
|
}
|
|
|
|
.join("\t")
|
|
|
|
}
|
|
|
|
}
|