feat: previous / next instructions
This commit is contained in:
47
src/utils.rs
47
src/utils.rs
@@ -1,6 +1,16 @@
|
||||
use anyhow::anyhow;
|
||||
use std::process::Command;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use pulse::{context::introspect::SinkInputInfo, volume};
|
||||
|
||||
#[link(name = "c")]
|
||||
extern "C" {
|
||||
/// Gets the current user's ID
|
||||
pub fn getuid() -> u32;
|
||||
}
|
||||
|
||||
const NOTIFY_SEND_REPLACE_ID: u32 = 1448531;
|
||||
const NOTIFICATION_DURATION_MILLIS: u32 = 1000;
|
||||
const FULL_VOLUME: u32 = 1 << 16;
|
||||
|
||||
pub fn volume_to_percentage(volume: volume::ChannelVolumes) -> u8 {
|
||||
@@ -22,7 +32,38 @@ pub fn get_sink_input_name(sink_input: &SinkInputInfo) -> anyhow::Result<String>
|
||||
return Err(anyhow!("Invalid sink input name"));
|
||||
};
|
||||
|
||||
Ok(String::from_utf8(
|
||||
Ok(capitalize_string(&String::from_utf8(
|
||||
name_bytes[..name_bytes.len() - 1].to_vec(),
|
||||
)?)
|
||||
)?))
|
||||
}
|
||||
|
||||
fn capitalize_string(s: &str) -> String {
|
||||
let mut c = s.chars();
|
||||
match c.next() {
|
||||
None => String::new(),
|
||||
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_notification(message: &str) -> Result<()> {
|
||||
let user_id = unsafe { getuid() };
|
||||
|
||||
Command::new("notify-send")
|
||||
.args(vec![
|
||||
"Mixrs",
|
||||
message,
|
||||
"-t",
|
||||
&NOTIFICATION_DURATION_MILLIS.to_string(),
|
||||
"-r",
|
||||
&NOTIFY_SEND_REPLACE_ID.to_string(),
|
||||
"-i",
|
||||
"/",
|
||||
])
|
||||
.env(
|
||||
"DBUS_SESSION_BUS_ADDRESS",
|
||||
format!("unix:path=/run/user/{user_id}/bus"),
|
||||
)
|
||||
.spawn()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user