Compare commits

...

3 Commits

Author SHA1 Message Date
af71cc3579 optional ipv6 address 2022-01-31 14:08:30 +01:00
322b3c0624 ipv6 address added 2022-01-31 14:08:18 +01:00
3805929479 volume 0 when muted 2022-01-31 14:08:02 +01:00
4 changed files with 46 additions and 17 deletions

View File

@ -5,6 +5,7 @@ use std::default::Default;
pub struct PubIp { pub struct PubIp {
pub icon: String, pub icon: String,
pub enabled: bool, pub enabled: bool,
pub show6: bool,
pub delay: f64, pub delay: f64,
} }
@ -13,6 +14,7 @@ impl Default for PubIp {
PubIp { PubIp {
icon: String::from(""), icon: String::from(""),
enabled: false, enabled: false,
show6: false,
delay: 120.0, delay: 120.0,
} }
} }

View File

@ -4,14 +4,24 @@ use crate::types::ThreadsData;
pub async fn get_local_ip() -> ThreadsData { pub async fn get_local_ip() -> ThreadsData {
let addrs = nix::ifaddrs::getifaddrs().unwrap(); let addrs = nix::ifaddrs::getifaddrs().unwrap();
let mut ip = String::new(); let mut ip = String::new();
let mut ip6 = String::new();
let mut found4 = false;
let mut found6 = false;
for ifaddr in addrs { for ifaddr in addrs {
match ifaddr.address { match ifaddr.address {
Some(address) => { Some(address) => {
if found4 && found6 { break; }
if ifaddr.interface_name == CONFIG.local_ip.interface { if ifaddr.interface_name == CONFIG.local_ip.interface {
match address.family() { match address.family() {
nix::sys::socket::AddressFamily::Inet => { nix::sys::socket::AddressFamily::Inet => {
if found4 { continue; }
ip = address.to_string().split(':').next().unwrap().to_string(); ip = address.to_string().split(':').next().unwrap().to_string();
break; found4 = true;
}
nix::sys::socket::AddressFamily::Inet6 => {
if found6 { continue; }
ip6 = address.to_string().rsplit_once(':').unwrap().0.to_string();
found6 = true;
} }
_ => continue, _ => continue,
}; };
@ -23,6 +33,9 @@ pub async fn get_local_ip() -> ThreadsData {
if ip.is_empty() { if ip.is_empty() {
ip = String::from("Error!") ip = String::from("Error!")
} }
let data = format!(" {} {} {}", CONFIG.local_ip.icon, ip, CONFIG.seperator); if !ip6.is_empty() {
ip6 = format!(" {}", ip6)
}
let data = format!(" {} {}{} {}", CONFIG.local_ip.icon, ip, ip6, CONFIG.seperator);
ThreadsData::LocalIp(data) ThreadsData::LocalIp(data)
} }

View File

@ -2,16 +2,28 @@ use crate::config::CONFIG;
use crate::types::ThreadsData; use crate::types::ThreadsData;
pub async fn get_pub_ip() -> ThreadsData { pub async fn get_pub_ip() -> ThreadsData {
let url = "http://api.ipify.org".to_string(); let url4 = "http://api.ipify.org".to_string();
let _err = String::from("Error"); let url6 = "http://api64.ipify.org".to_string();
let res = match minreq::get(url).send() { let _err4 = String::from("Error4");
let res4 = match minreq::get(url4).send() {
Ok(resp) => match resp.as_str() { Ok(resp) => match resp.as_str() {
Ok(res_str) => res_str.trim().to_string(), Ok(res_str) => res_str.trim().to_string(),
Err(_) => _err, Err(_) => _err4,
}, },
Err(_) => _err, Err(_) => _err4,
}; };
let _err6 = String::from("Error6");
let mut res6 = String::from("");
if CONFIG.pub_ip.show6 {
res6 = match minreq::get(url6).send() {
Ok(resp) => match resp.as_str() {
Ok(res_str) => format!("[{}]", res_str.trim().to_string()),
Err(_) => _err6,
},
Err(_) => _err6,
}
}
let data = format!(" {} {} {}", CONFIG.pub_ip.icon, res, CONFIG.seperator); let data = format!(" {} {} {} {}", CONFIG.pub_ip.icon, res4, res6, CONFIG.seperator);
ThreadsData::PubIp(data) ThreadsData::PubIp(data)
} }

View File

@ -20,23 +20,25 @@ pub async fn get_volume() -> ThreadsData {
.get_playback_volume(selem_chan_id) .get_playback_volume(selem_chan_id)
.expect("Failed to get raw_volume"); .expect("Failed to get raw_volume");
let muted = selem
.get_playback_switch(selem_chan_id)
.expect("Failed to get playback state")
== 0;
let range = max - min; let range = max - min;
let vol = if range == 0 { let vol = if muted || range == 0 {
0 0
} else { } else {
raw_volume -= min; raw_volume -= min;
((raw_volume as f64 / range as f64) * 100.) as u64 ((raw_volume as f64 / range as f64) * 100.) as u64
}; };
let muted = selem
.get_playback_switch(selem_chan_id)
.expect("Failed to get playback state");
let icon = match muted { let icon = match muted {
0 => &CONFIG.volume.icon_muted, true => &CONFIG.volume.icon_muted,
_ => if vol < 50 { &CONFIG.volume.icon_low } else { &CONFIG.volume.icon_high } false => if vol < 50 { &CONFIG.volume.icon_low } else { &CONFIG.volume.icon_high }
}; };
let data = format!(" {} {}% {}", icon, vol, CONFIG.seperator);
let data = format!(" {} {:>2}% {}", icon, vol, CONFIG.seperator);
ThreadsData::Sound(data) ThreadsData::Sound(data)
} }