about summary refs log tree commit diff stats
path: root/pkgs/by-name/ts/tskm/src/rofi/mod.rs
blob: 37c2eafa9ab12ddeddfaa55831762e123ba6a4c6 (plain) (blame)
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
// nixos-config - My current NixOS configuration
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of my nixos-config.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

use std::{
    io::Write,
    process::{Command, Stdio},
};

use anyhow::{Context, Result};

pub fn select(options: &[String]) -> Result<String> {
    let mut child = Command::new("rofi")
        .args(["-sep", "\n", "-dmenu"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .context("Failed to spawn rofi")?;

    let mut stdin = child
        .stdin
        .take()
        .expect("We piped this, so should be available");

    stdin
        .write_all(options.join("\n").as_bytes())
        .context("Failed to write to rofi's stdin")?;

    let output = child
        .wait_with_output()
        .context("Failed to wait for rofi's output")?;

    let selected = String::from_utf8(output.stdout.clone()).with_context(|| {
        format!(
            "Failed to decode '{}' as utf8",
            String::from_utf8_lossy(&output.stdout)
        )
    })?;

    Ok(selected.trim_end().to_owned())
}