wmctrl/desktop.rs
1//! Desktop related functions.
2
3use std::process::Output;
4
5use super::wmctrl;
6
7/// List desktops. The current desktop is marked with an asterisk
8///
9/// This function is the equivalent of `wmctrl -d`.
10///
11/// # Examples
12///
13/// ```
14/// use wmctrl::desktop;
15///
16/// println!("{}", String::from_utf8(desktop::list_desktops().stdout).unwrap());
17/// ```
18pub fn list_desktops() -> Output {
19 wmctrl("-d")
20}
21
22/// Switch to the specified desktop
23///
24/// This function is the equivalent of `wmctrl -s <DESK>`.
25///
26/// # Examples
27///
28/// ```
29/// use wmctrl::desktop;
30///
31/// desktop::switch_desktop("1");
32/// ```
33pub fn switch_desktop(desktop: &str) -> Output {
34 wmctrl(&format!("-s {}", desktop))
35}
36
37/// Change the number of desktops
38///
39/// This function is the equivalent of `wmctrl -n <NUM>`.
40pub fn set_desktop_count(count: u8) -> Output {
41 wmctrl(&format!("-n {}", count))
42}
43
44/// Get the currently active Desktop
45pub fn get_current_desktop() -> String {
46 let output = String::from_utf8(list_desktops().stdout).unwrap();
47
48 let columns = output
49 .lines()
50 .find(|line| line.contains('*'))
51 .unwrap()
52 .split(' ')
53 .filter(|column| !column.is_empty())
54 .collect::<Vec<&str>>();
55
56 String::from(columns[0])
57}