summaryrefslogtreecommitdiffstats
path: root/src/commands/preview/var_stdin.rs
blob: 78cc8efa6e054ef281b80a36185452892d11f98a (plain)
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
use clap::Args;

use super::var;
use crate::common::shell::{self, ShellSpawnError, EOF};
use crate::prelude::*;
use std::io::{self, Read};

#[derive(Debug, Clone, Args)]
pub struct Input {}

impl Runnable for Input {
    fn run(&self) -> Result<()> {
        let mut text = String::new();
        io::stdin().read_to_string(&mut text)?;

        let mut parts = text.split(EOF);
        let selection = parts.next().expect("Unable to get selection").to_owned();
        let query = parts.next().expect("Unable to get query").to_owned();
        let variable = parts.next().expect("Unable to get variable").trim().to_owned();

        let input = var::Input {
            selection,
            query,
            variable,
        };

        input.run()?;

        if let Some(extra) = parts.next() {
            if !extra.is_empty() {
                print!("");

                let mut cmd = shell::out();
                cmd.arg(extra);
                debug!(?cmd);
                cmd.spawn().map_err(|e| ShellSpawnError::new(extra, e))?.wait()?;
            }
        }

        Ok(())
    }
}