Skip to content
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
2 changes: 1 addition & 1 deletion MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Steps (This order is recommended so lsp doesn't scream at you lol)
1. Add the prelude! This new release includes traits! `use hyprland::prelude::*;`
2. Switch to the `HResult` Result type (`hyprland::shared::HResult`)
2. Switch to the `Result` type — it moved to the crate root: `use hyprland::Result;` (it was `hyprland::shared::HResult` in 0.2.5)
3. [Update your data fetcher functions to structs](#update-data-fetchers)
4. [Update your dispatch functions to structs](#update-dispatchers)
5. Switch all `String`s to `&str`s in your dispatchers
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ hyprland = { git = "https://github.com/hyprland-community/hyprland-rs", branch =

### What this crate provides

This crate provides 6 modules (+1 for shared things)
This crate provides 8 modules (+1 for shared things)

- `data` for getting information on the compositor
- `event_listener` which provides the `EventListener` struct for listening for events
- `dispatch` for calling dispatchers
- `keyword` for dealing with config option (aka keywords)
- `config::binds` for changing binds (in future `config` might have config generation)
- `ctl` for calling hyprctl commands
- `hyprpaper` for talking to hyprpaper
- `instance` for targeting a specific running Hyprland instance

## Example Usage

Expand Down
27 changes: 17 additions & 10 deletions src/ctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ pub mod output {
backend: OutputBackends,
name: Option<&str>,
) -> crate::Result<()> {
let name = name.unwrap_or_default();
// Hyprland's syntax is `output create <backend> name=<name>` - the
// `name=` prefix is part of the name, not a key we strip.
let name = name.map(|n| format!("name={n}")).unwrap_or_default();
instance.write_to_socket(command!(Empty, "output create {backend} {name}"))?;
Ok(())
}
Expand All @@ -166,7 +168,9 @@ pub mod output {
backend: OutputBackends,
name: Option<&str>,
) -> crate::Result<()> {
let name = name.unwrap_or_default();
// Hyprland's syntax is `output create <backend> name=<name>` - the
// `name=` prefix is part of the name, not a key we strip.
let name = name.map(|n| format!("name={n}")).unwrap_or_default();
instance
.write_to_socket_async(command!(Empty, "output create {backend} {name}"))
.await?;
Expand Down Expand Up @@ -249,28 +253,28 @@ pub mod switch_xkb_layout {
}
}

/// Creates a error that Hyprland will display
/// Stuff related to setting an error message Hyprland displays
pub mod set_error {
use super::*;

/// Creates a error that Hyprland will display
/// Creates an error that Hyprland will display
pub fn call(color: Color, msg: String) -> crate::Result<()> {
instance_call(default_instance()?, color, msg)
}

/// Creates a error that Hyprland will display
/// Creates an error that Hyprland will display
pub fn instance_call(instance: &Instance, color: Color, msg: String) -> crate::Result<()> {
instance.write_to_socket(command!(Empty, "seterror {color} {msg}"))?;
Ok(())
}

/// Creates a error that Hyprland will display (async)
/// Creates an error that Hyprland will display (async)
#[cfg(any(feature = "async-lite", feature = "tokio"))]
pub async fn call_async(color: Color, msg: String) -> crate::Result<()> {
instance_call_async(default_instance()?, color, msg).await
}

/// Creates a error that Hyprland will display (async)
/// Creates an error that Hyprland will display (async)
#[cfg(any(feature = "async-lite", feature = "tokio"))]
pub async fn instance_call_async(
instance: &Instance,
Expand Down Expand Up @@ -328,7 +332,7 @@ pub mod notify {
Ok(())
}

/// Creates a error that Hyprland will display (async)
/// Sends a notification to the user (async)
#[cfg(any(feature = "async-lite", feature = "tokio"))]
pub async fn call_async(
icon: Icon,
Expand All @@ -339,7 +343,7 @@ pub mod notify {
instance_call_async(default_instance()?, icon, time, color, msg).await
}

/// Creates a error that Hyprland will display (async)
/// Sends a notification to the user (async)
#[cfg(any(feature = "async-lite", feature = "tokio"))]
pub async fn instance_call_async(
instance: &Instance,
Expand Down Expand Up @@ -735,7 +739,7 @@ pub mod instance {
pub struct Instance {
/// instance name (9958d29...) in /run/user/$UID/hypr/$instance
pub instance: String,
/// ???
/// unix timestamp of when the instance started
pub time: u64,
/// pid of hyprland process
pub pid: u32,
Expand All @@ -744,6 +748,9 @@ pub mod instance {
}

/// Returns a list of running instances
///
/// Named `instance_list` rather than `list` to avoid colliding with the
/// `list` functions in sibling modules when glob-imported.
pub fn instance_list() -> crate::Result<Vec<Instance>> {
let buf = get_hypr_path()?;
let entries = std::fs::read_dir(buf)?;
Expand Down
2 changes: 1 addition & 1 deletion src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! ## Usage
//!
//! here is a example of every function in use! (blocking)
//! here is an example of every function in use! (blocking)
//! ```rust
//! use hyprland::data::*;
//! use hyprland::prelude::*;
Expand Down
51 changes: 45 additions & 6 deletions src/data/regular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,12 +622,14 @@ impl From<String> for AnimationStyle {
if value.starts_with("popin") {
let mut iter = value.split(' ');
iter.next();
AnimationStyle::PopIn({
let mut str = iter.next().unwrap_or("100%").to_string();
str.remove(str.len() - 1);

str.parse().unwrap_or(100_u8)
})
// Hyprland may emit a bare "popin " — an empty token here would make
// `remove(len - 1)` panic on an empty string.
let raw = iter.next().unwrap_or("100%");
let str = raw
.strip_suffix('%')
.filter(|s| !s.is_empty())
.unwrap_or("100");
AnimationStyle::PopIn(str.parse().unwrap_or(100_u8))
} else {
match value.as_str() {
"slide" => AnimationStyle::Slide,
Expand Down Expand Up @@ -819,3 +821,40 @@ create_data_struct!(
holding_type: WorkspaceRuleset,
doc: "This struct holds a vector of workspace rules per workspace"
);

#[cfg(test)]
mod animation_tests {
use super::*;

/// `"popin "` used to panic: the second token is empty and the old code did
/// `str.remove(str.len() - 1)` on it.
#[test]
fn popin_with_trailing_space_does_not_panic() {
let style = AnimationStyle::from("popin ".to_string());
assert!(matches!(style, AnimationStyle::PopIn(100)), "got {style:?}");
}

#[test]
fn popin_bare_falls_back_to_full_scale() {
assert!(matches!(
AnimationStyle::from("popin".to_string()),
AnimationStyle::PopIn(100)
));
}

#[test]
fn popin_parses_the_percentage() {
assert!(matches!(
AnimationStyle::from("popin 87%".to_string()),
AnimationStyle::PopIn(87)
));
}

#[test]
fn empty_string_maps_to_none() {
assert!(matches!(
AnimationStyle::from("".to_string()),
AnimationStyle::None
));
}
}
Loading
Loading