Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tests, docs, examples, clippy #309

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use hyprland::keyword::Keyword;
fn main() -> hyprland::Result<()> {
Keyword::set("submap", "example")?;
hyprland::bind!(SUPER, Key, "I" => ToggleFloating, None)?;
hyprland::bind!(l | CTRL ALT, Key, "Delete" => Exec, "sudo reboot"); // Reboot including from lock screen
hyprland::bind!(e | SUPER, Key, "C" => KillActiveWindow); // Kill all your windows
hyprland::bind!(l | CTRL ALT, Key, "Delete" => Exec, "sudo reboot")?; // Reboot including from lock screen
hyprland::bind!(e | SUPER, Key, "C" => KillActiveWindow)?; // Kill all your windows
Keyword::set("submap", "reset")?;

dispatch!(Custom, "submap", "example")?;
Expand All @@ -22,4 +22,4 @@ fn main() -> hyprland::Result<()> {
dispatch!(Custom, "submap", "reset")?;

Ok(())
}
}
4 changes: 2 additions & 2 deletions examples/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use hyprland::shared::{HyprData, HyprDataActive, HyprDataActiveOptional};
fn main() -> hyprland::Result<()>{
let args: Vec<_> = std::env::args().skip(1).collect();

if args.len() == 0 {
if args.is_empty(){
panic!("You have to specify client, workspace or monitor")
}

Expand All @@ -26,4 +26,4 @@ fn main() -> hyprland::Result<()>{
};

Ok(())
}
}
4 changes: 2 additions & 2 deletions examples/data_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use hyprland::shared::{HyprData, HyprDataActive, HyprDataActiveOptional};
async fn main() -> hyprland::Result<()>{
let args: Vec<_> = std::env::args().skip(1).collect();

if args.len() == 0 {
if args.is_empty(){
panic!("You have to specify client, workspace or monitor")
}

Expand All @@ -28,4 +28,4 @@ async fn main() -> hyprland::Result<()>{
};

Ok(())
}
}
17 changes: 8 additions & 9 deletions examples/events.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
/// Demostrats using hyprland-rs to listen for events
///
///
/// Usage: cargo run --example events

use hyprland::event_listener::EventListener;

fn main() -> hyprland::Result<()> {
// Create a event listener
let mut event_listener = EventListener::new();

event_listener.add_active_window_change_handler(|data| println!("{data:#?}"));
event_listener.add_fullscreen_state_change_handler(
|fstate| println!("Window {} fullscreen", if fstate { "is" } else { "is not" })
);
event_listener.add_active_monitor_change_handler(|state| println!("Monitor state: {state:#?}"));
event_listener.add_workspace_change_handler(|id| println!("workspace changed to {id:?}"));
event_listener.add_active_window_changed_handler(|data| println!("{data:#?}"));
event_listener.add_fullscreen_state_changed_handler(|fstate| {
println!("Window {} fullscreen", if fstate { "is" } else { "is not" })
});
event_listener.add_active_monitor_changed_handler(|state| println!("Monitor state: {state:#?}"));
event_listener.add_workspace_changed_handler(|id| println!("workspace changed to {id:?}"));

// and execute the function
// here we are using the blocking variant
// but there is a async version too
event_listener.start_listener()?;

Ok(())
}
}
10 changes: 5 additions & 5 deletions examples/events_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ async fn main() -> hyprland::Result<()> {
// Create a event listener
let mut event_listener = AsyncEventListener::new();

event_listener.add_active_window_change_handler(async_closure! {
event_listener.add_active_window_changed_handler(async_closure! {
|data| println!("{data:#?}")
});

event_listener.add_fullscreen_state_change_handler(async_closure! {
event_listener.add_fullscreen_state_changed_handler(async_closure! {
|fstate| println!("Window {} fullscreen", if fstate { "is" } else { "is not" })
});

event_listener.add_active_monitor_change_handler(async_closure! {
event_listener.add_active_monitor_changed_handler(async_closure! {
|state| println!("Monitor state: {state:#?}")
});

// add event, yes functions and closures both work!
event_listener.add_workspace_change_handler(async_closure! {
event_listener.add_workspace_changed_handler(async_closure! {
|id| println!("workspace changed to {id:?}")
});

Expand All @@ -33,4 +33,4 @@ async fn main() -> hyprland::Result<()> {
event_listener.start_listener_async().await?;

Ok(())
}
}
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,5 @@ fn test_binds() {
Ok(v) => v,
Err(e) => panic!("Error occured: {e}"), // Note to greppers: this is in a test!
};
assert_eq!(built_bind, "SUPER,v,/togglefloating");
assert_eq!(built_bind, "SUPER,v,togglefloating");
}
3 changes: 1 addition & 2 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
//! ```rust
//! use hyprland::data::*;
//! use hyprland::prelude::*;
//! use hyprland::shared::HResult;
//!
//! fn main() -> HResult<()> {
//! fn main() -> hyprland::Result<()> {
//! let monitors = Monitors::get()?.to_vec();
//! println!("{monitors:#?}");
//!
Expand Down
9 changes: 3 additions & 6 deletions src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
//! ## Usage
//!
//! ```rust
//! use hyprland::shared::HResult;
//! use hyprland::dispatch::{Dispatch, DispatchType};
//! fn main() -> HResult<()> {
//! fn main() -> hyprland::Result<()> {
//! Dispatch::call(DispatchType::Exec("kitty"))?;
//!
//! Ok(())
Expand Down Expand Up @@ -556,8 +555,7 @@ impl Dispatch {
/// This function calls a specified dispatcher (blocking)
///
/// ```rust
/// # use hyprland::shared::HResult;
/// # fn main() -> HResult<()> {
/// # fn main() -> hyprland::Result<()> {
/// use hyprland::dispatch::{DispatchType,Dispatch};
/// // This is an example of just one dispatcher, there are many more!
/// Dispatch::call(DispatchType::Exec("kitty"))
Expand All @@ -579,8 +577,7 @@ impl Dispatch {
/// This function calls a specified dispatcher (async)
///
/// ```rust
/// # use hyprland::shared::HResult;
/// # async fn function() -> HResult<()> {
/// # async fn function() -> hyprland::Result<()> {
/// use hyprland::dispatch::{DispatchType,Dispatch};
/// // This is an example of just one dispatcher, there are many more!
/// Dispatch::call_async(DispatchType::Exec("kitty")).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/event_listener/async_im.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::*;
/// use hyprland::event_listener::EventListener;
/// let mut listener = EventListener::new(); // creates a new listener
/// // add a event handler which will be ran when this event happens
/// listener.add_workspace_change_handler(|data| println!("{:#?}", data));
/// listener.add_workspace_changed_handler(|data| println!("{:#?}", data));
/// listener.start_listener(); // or `.start_listener_async().await` if async
/// ```
pub struct AsyncEventListener {
Expand Down Expand Up @@ -45,7 +45,7 @@ impl AsyncEventListener {
/// # async fn function() -> std::io::Result<()> {
/// use hyprland::event_listener::EventListener;
/// let mut listener = EventListener::new();
/// listener.add_workspace_change_handler(|id| println!("changed workspace to {id:?}"));
/// listener.add_workspace_changed_handler(|id| println!("changed workspace to {id:?}"));
/// listener.start_listener_async().await;
/// # Ok(())
/// # }
Expand Down
6 changes: 3 additions & 3 deletions src/event_listener/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::io;
/// use hyprland::event_listener::EventListener;
/// let mut listener = EventListener::new(); // creates a new listener
/// // add a event handler which will be ran when this event happens
/// listener.add_workspace_change_handler(|data| println!("{:#?}", data));
/// listener.add_workspace_changed_handler(|data| println!("{:#?}", data));
/// listener.start_listener(); // or `.start_listener_async().await` if async
/// ```
pub struct EventListener {
Expand Down Expand Up @@ -45,7 +45,7 @@ impl EventListener {
/// # async fn function() -> std::io::Result<()> {
/// use hyprland::event_listener::EventListener;
/// let mut listener = EventListener::new();
/// listener.add_workspace_change_handler(|id| println!("changed workspace to {id:?}"));
/// listener.add_workspace_changed_handler(|id| println!("changed workspace to {id:?}"));
/// listener.start_listener_async().await;
/// # Ok(())
/// # }
Expand Down Expand Up @@ -82,7 +82,7 @@ impl EventListener {
/// ```rust, no_run
/// use hyprland::event_listener::EventListener;
/// let mut listener = EventListener::new();
/// listener.add_workspace_change_handler(&|id| println!("changed workspace to {id:?}"));
/// listener.add_workspace_changed_handler(&|id| println!("changed workspace to {id:?}"));
/// listener.start_listener();
/// ```
pub fn start_listener(&mut self) -> crate::Result<()> {
Expand Down
34 changes: 32 additions & 2 deletions src/event_listener/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ macro_rules! add_listener_reg_raw {
```rust, no_run
use hyprland::event_listener::EventListener;
let mut listener = EventListener::new();
listener.add_"#, stringify!($name), r#"_handler(|"#, stringify!($id), r#"| println!(""#, $c2, ": {", stringify!($id), r#":#?}"));
listener.add_"#, stringify!($name), r#"_handler("#, handler_example_closure! { $f, $c2, $id }, r#");
listener.start_listener();"#)]
pub fn [<add_ $name _handler>](&mut self, f: $f) {
self.events.[<$list_name _events>].push(Box::new(f));
Expand All @@ -128,7 +128,7 @@ macro_rules! add_async_listener_raw {
```rust, no_run
use hyprland::event_listener::EventListener;
let mut listener = EventListener::new();
listener.add_"#, stringify!($name), r#"_handler(|"#, stringify!($id), r#"| println!(""#, $c2, ": {", stringify!($id), r#":#?}"));
listener.add_"#, stringify!($name), r#"_handler("#, handler_example_async_closure! { $f, $c2, $id }, r#");
listener.start_listener();"#)]
pub fn [<add_ $name _handler>](&mut self, f: $f) {
self.events.[<$list_name _events>].push(Box::pin(f));
Expand All @@ -137,6 +137,36 @@ listener.start_listener();"#)]
}
};
}
/// Expands to an example closure for documenting event listeners.
macro_rules! handler_example_closure {
($f:ty, $c2:expr, $id:ident) => {
type_if! {
impl Fn() + 'static ,
$f,
concat!(
r#"|| println!(""#, $c2, r#"")"#,
),
concat!(
r#"|"#, stringify!($id), r#"| println!(""#, $c2, ": {", stringify!($id), r#":#?}")"#,
)
}
}
}
/// Expands to an example closure for documenting async event listeners.
macro_rules! handler_example_async_closure {
($f:ty, $c2:expr, $id:ident) => {
type_if! {
impl Fn() -> VoidFuture + Send + Sync + 'static ,
$f,
concat!(
r#"|| println!(""#, $c2, r#"")"#,
),
concat!(
r#"|"#, stringify!($id), r#"| println!(""#, $c2, ": {", stringify!($id), r#":#?}")"#,
)
}
}
}

macro_rules! arm {
($val:expr,$nam:ident,$se:ident) => {{
Expand Down
18 changes: 7 additions & 11 deletions src/event_listener/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub(crate) trait HasExecutor {
}
}

pub(crate) fn event_primer_noexec<'a>(
pub(crate) fn event_primer_noexec(
event: Event,
abuf: &mut Vec<ActiveWindowState>,
) -> crate::Result<Vec<Event>> {
Expand Down Expand Up @@ -675,9 +675,10 @@ pub(crate) static EVENTS: phf::Map<&'static str, (usize, ParsedEventType)> = phf

use either::Either;

fn new_event_parser(
input: &str,
) -> crate::Result<Either<(ParsedEventType, Vec<String>), (String, String)>> {
type KnownEvent = (ParsedEventType, Vec<String>);
type UnknownEvent = (String, String);

fn new_event_parser(input: &str) -> crate::Result<Either<KnownEvent, UnknownEvent>> {
input
.to_string()
.split_once(">>")
Expand All @@ -688,9 +689,7 @@ fn new_event_parser(
if let Some(event) = EVENTS.get(name) {
Either::Left((
event.1,
x.splitn(event.0 as usize, ",")
.map(|y| y.to_string())
.collect(),
x.splitn(event.0, ",").map(|y| y.to_string()).collect(),
))
} else {
Either::Right((name.to_string(), x.to_string()))
Expand Down Expand Up @@ -877,10 +876,7 @@ pub(crate) fn event_parser(event: String) -> crate::Result<Vec<Event>> {
})),
ParsedEventType::ToggleGroup => Ok(Event::GroupToggled(GroupToggledEventData {
toggled: get![ref args;0] == "1",
window_addresses: get![ref args;1]
.split(",")
.map(|x| Address::new(x))
.collect(),
window_addresses: get![ref args;1].split(",").map(Address::new).collect(),
})),
ParsedEventType::MoveIntoGroup => {
Ok(Event::WindowMovedIntoGroup(Address::new(get![ref args;0])))
Expand Down
8 changes: 4 additions & 4 deletions src/event_listener/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ use futures_lite::{Stream, StreamExt};
/// as its more idiomatic, and allows for more efficient memory management
///
/// # Examples
/// ```rust
/// ```rust, no_run
/// use hyprland::prelude::*;
/// use hyprland::event_listener::EventStream;
/// use hyprland::Result as HResult;
/// use futures_lite::StreamExt;
///
/// #[tokio::main]
/// async fn main() -> HResult<()> {
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() {
/// let mut stream = EventStream::new();
/// while let Some(Ok(event)) = stream.next().await {
/// println!("{event:?}");
Expand All @@ -32,6 +31,7 @@ pub struct EventStream {
}
impl EventStream {
/// Creates a new [EventStream]
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
use crate::unix_async::*;
let stream = try_stream! {
Expand Down
3 changes: 1 addition & 2 deletions src/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
//! ## Usage
//!
//! ```rust, no_run
//! use hyprland::shared::HResult;
//! use hyprland::keyword::Keyword;
//! fn main() -> HResult<()> {
//! fn main() -> hyprland::Result<()> {
//! Keyword::get("some_keyword")?;
//! Keyword::set("another_keyword", "the value to set it to")?;
//!
Expand Down