1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Desktop related functions.

use std::process::Output;

use super::wmctrl;

/// List desktops. The current desktop is marked with an asterisk
///
/// This function is the equivalent of `wmctrl -d`.
///
/// # Examples
///
/// ```
/// use wmctrl::desktop;
///
/// println!("{}", String::from_utf8(desktop::list_desktops().stdout).unwrap());
/// ```
pub fn list_desktops() -> Output {
    wmctrl("-d")
}

/// Switch to the specified desktop
///
/// This function is the equivalent of `wmctrl -s <DESK>`.
///
/// # Examples
///
/// ```
/// use wmctrl::desktop;
///
/// desktop::switch_desktop("1");
/// ```
pub fn switch_desktop(desktop: &str) -> Output {
    wmctrl(&format!("-s {}", desktop))
}

/// Change the number of desktops
///
/// This function is the equivalent of `wmctrl -n <NUM>`.
pub fn set_desktop_count(count: u8) -> Output {
    wmctrl(&format!("-n {}", count))
}

/// Get the currently active Desktop
pub fn get_current_desktop() -> String {
    let output = String::from_utf8(list_desktops().stdout).unwrap();

    let columns = output
        .lines()
        .find(|line| line.contains('*'))
        .unwrap()
        .split(' ')
        .filter(|column| !column.is_empty())
        .collect::<Vec<&str>>();

    String::from(columns[0])
}