Skip to content

Commit

Permalink
feat(cli): Add status command
Browse files Browse the repository at this point in the history
Fixes #99.
  • Loading branch information
danyspin97 committed Nov 8, 2024
1 parent 945a93e commit c2feec3
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ wpaperd-ipc = { path = "../ipc", version = "1.0.0" }
clap = { version = "4.5.20", features = ["derive", "wrap_help"] }
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.131"
humantime-serde = "1.1.1"
humantime = "2.1.0"

[build-dependencies]
clap = { version = "4.5.20", features = ["derive", "cargo"] }
Expand Down
57 changes: 56 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::{
io::{Read, Write},
os::unix::net::UnixStream,
path::PathBuf,
time::Duration,
};

use clap::Parser;
use serde::Serialize;
use serde_json::to_string;
use wpaperd_ipc::{socket_path, IpcError, IpcMessage, IpcResponse};

use crate::opts::{Opts, SubCmd};
Expand Down Expand Up @@ -53,7 +53,14 @@ fn main() {
SubCmd::TogglePauseWallpaper { monitors } => IpcMessage::TogglePauseWallpaper {
monitors: monitors.into_iter().map(unquote).collect(),
},
SubCmd::GetStatus { json, monitors } => {
json_resp = json;
IpcMessage::GetStatus {
monitors: monitors.into_iter().map(unquote).collect(),
}
}
};

conn.write_all(&serde_json::to_vec(&msg).unwrap()).unwrap();
let mut buf = String::new();
conn.read_to_string(&mut buf).unwrap();
Expand Down Expand Up @@ -86,6 +93,54 @@ fn main() {
}
}
}
IpcResponse::DisplaysStatus { entries } => {
/// Clean up the duration for human readability
/// remove the milliseconds and the leading 0s
fn clean_duration(duration: Duration) -> Duration {
let duration = duration.as_secs();
Duration::from_secs(if duration < 60 {
duration
} else if duration < 60 * 60 {
// if the duration is in minutes, remove the seconds
duration - duration % 60
// duration is in hours, remove the minutes and seconds
} else {
duration - duration % (60 * 60)
})
}
if json_resp {
#[derive(Serialize)]
struct Item {
display: String,
status: String,
#[serde(rename = "duration_left", with = "humantime_serde")]
duration_left: Option<Duration>,
}
let val = entries
.into_iter()
.map(|(display, status, duration_left)| Item {
display,
status,
duration_left: duration_left.map(clean_duration),
})
.collect::<Vec<_>>();
println!(
"{}",
serde_json::to_string(&val).expect("json encoding to work")
);
} else {
for (monitor, status, duration_left) in entries {
println!(
"{monitor}: {status}{}",
if let Some(d) = duration_left {
format!(" ({} left)", humantime::format_duration(clean_duration(d)))
} else {
"".to_string()
}
);
}
}
}
IpcResponse::Ok => (),
},
Err(err) => match err {
Expand Down
6 changes: 6 additions & 0 deletions cli/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ pub enum SubCmd {
ResumeWallpaper { monitors: Vec<String> },
#[clap(visible_alias = "toggle-pause")]
TogglePauseWallpaper { monitors: Vec<String> },
#[clap(visible_alias = "status")]
GetStatus {
#[clap(short, long)]
json: bool,
monitors: Vec<String>,
},
}
15 changes: 15 additions & 0 deletions daemon/src/ipc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ pub fn handle_message(
IpcResponse::Ok
})
}

IpcMessage::GetStatus { monitors } => {
check_monitors(wpaperd, &monitors).map(|_| IpcResponse::DisplaysStatus {
entries: collect_surfaces(wpaperd, monitors)
.iter()
.map(|surface| {
(
surface.name().to_string(),
surface.status().to_string(),
surface.get_remaining_duration(),
)
})
.collect(),
})
}
};

let mut stream = BufWriter::new(ustream);
Expand Down
14 changes: 11 additions & 3 deletions ipc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::{path::PathBuf, time::Duration};

use serde::{Deserialize, Serialize};
use xdg::{BaseDirectories, BaseDirectoriesError};
Expand All @@ -13,12 +13,20 @@ pub enum IpcMessage {
TogglePauseWallpaper { monitors: Vec<String> },
AllWallpapers,
ReloadWallpaper { monitors: Vec<String> },
GetStatus { monitors: Vec<String> },
}

#[derive(Serialize, Deserialize)]
pub enum IpcResponse {
CurrentWallpaper { path: PathBuf },
AllWallpapers { entries: Vec<(String, PathBuf)> },
CurrentWallpaper {
path: PathBuf,
},
AllWallpapers {
entries: Vec<(String, PathBuf)>,
},
DisplaysStatus {
entries: Vec<(String, String, Option<Duration>)>,
},
Ok,
}

Expand Down

0 comments on commit c2feec3

Please sign in to comment.