Skip to content

Commit

Permalink
Upgrade nom
Browse files Browse the repository at this point in the history
  • Loading branch information
Soft committed Sep 7, 2021
1 parent a424dff commit f40d2b1
Show file tree
Hide file tree
Showing 6 changed files with 455 additions and 245 deletions.
50 changes: 14 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ repository = "Soft/run-or-raise"
encoding = "0.2"
anyhow = "1"
lazy_static = "1.4"
nom = "3.2.1"
nom = "7"
regex = "1.5"
termion = "1.5"
xcb = "0.9"

[profile.release]
Expand Down
36 changes: 18 additions & 18 deletions src/conditions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use anyhow::{anyhow, Result};
use regex::Regex;
use xcb::{self, Connection, Window};
use anyhow::{Result, anyhow};

use crate::windows::{get_string_property, get_atom};
use crate::windows::{get_atom, get_string_property};

#[derive(Debug,PartialEq)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Property {
Class,
Name,
Expand Down Expand Up @@ -35,36 +35,36 @@ impl Property {
pub fn from_window(&self, conn: &Connection, win: Window) -> Result<Option<String>> {
match *self {
Property::Class => {
get_string_property(conn, win, xcb::ATOM_WM_CLASS)?
.map_or(Ok(None), |p| p.split('\u{0}')
get_string_property(conn, win, xcb::ATOM_WM_CLASS)?.map_or(Ok(None), |p| {
p.split('\u{0}')
.nth(1)
.ok_or_else(|| anyhow!("Invalid class defintion"))
.map(|s| Some(s.to_owned())))
.map(|s| Some(s.to_owned()))
})
}
Property::Name => {
get_string_property(conn, win, get_atom(conn, "_NET_WM_NAME")?)?
.map_or_else(|| get_string_property(conn, win, xcb::ATOM_WM_NAME), |v| Ok(Some(v)))
}
Property::Role => get_string_property(conn, win, get_atom(conn, "WM_WINDOW_ROLE")?)
Property::Name => get_string_property(conn, win, get_atom(conn, "_NET_WM_NAME")?)?
.map_or_else(
|| get_string_property(conn, win, xcb::ATOM_WM_NAME),
|v| Ok(Some(v)),
),
Property::Role => get_string_property(conn, win, get_atom(conn, "WM_WINDOW_ROLE")?),
}
}
}

impl Match {
pub fn matches(&self, conn: &Connection, win: Window) -> Result<bool> {
Ok(self.prop
Ok(self
.prop
.from_window(conn, win)?
.map(|p| {
match self.op {
Operator::Regex(ref pattern) => pattern.is_match(&p),
Operator::Equal(ref value) => value == &p,
}
.map(|p| match self.op {
Operator::Regex(ref pattern) => pattern.is_match(&p),
Operator::Equal(ref value) => value == &p,
})
.unwrap_or(false))
}
}

// TODO: Avoid multiple lookups
impl Condition {
pub fn matches(&self, conn: &Connection, win: Window) -> Result<bool> {
Ok(match *self {
Expand Down
26 changes: 10 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
mod conditions;
mod parsing;
mod windows;
mod conditions;

use anyhow::{bail, Error, Result};
use std::env;
use std::os::unix::process::CommandExt;
use std::process;
use std::process::Command;
use std::os::unix::process::CommandExt;
use xcb::Connection;
use anyhow::{Result, Error, anyhow};

fn exec_program(prog: &str, args: &[String]) -> Error {
let error = Command::new(prog).args(args).exec();
anyhow!("Could not execute program \"{}\": {}", prog, error)
Error::new(error).context("Executing program failed")
}

fn run() -> Result<()> {
Expand All @@ -21,11 +21,10 @@ fn run() -> Result<()> {
let (condition, prog, prog_args) = if args.len() >= 3 {
(&args[1], &args[2], &args[3..])
} else {
return Err(anyhow!("{} CONDITION PROGRAM [ARGS...]", app));
bail!("{} CONDITION PROGRAM [ARGS...]", app);
};

let cond = condition.parse()
.map_err(|_| anyhow!("Invalid condition"))?;
let cond = condition.parse()?;

let (conn, screen_num) = Connection::connect(None)?;
let screen = conn.get_setup().roots().nth(screen_num as usize).unwrap();
Expand All @@ -40,16 +39,11 @@ fn run() -> Result<()> {
}

fn main() {
use termion::{color, style};

if let Err(err) = run() {
let message = format!("{}{}error:{} {}",
style::Bold,
color::Fg(color::Red),
style::Reset,
err);
eprintln!("{}", message);
eprintln!("{}: {}", env!("CARGO_BIN_NAME"), err);
err.chain()
.skip(1)
.for_each(|cause| eprintln!("caused by:\n{}", cause));
process::exit(1);
}

}
Loading

0 comments on commit f40d2b1

Please sign in to comment.