Skip to main content

rmux_client/commands/
scripting.rs

1use rmux_proto::{
2    IfShellRequest, Request, Response, RunShellDelaySeconds, RunShellRequest, Target, WaitForMode,
3    WaitForRequest,
4};
5use std::path::PathBuf;
6
7use crate::{connection::Connection, ClientError};
8
9impl Connection {
10    /// Sends a `run-shell` request over the detached RPC channel without a
11    /// response read timeout.
12    #[allow(clippy::too_many_arguments)]
13    pub fn run_shell(
14        &mut self,
15        command: String,
16        background: bool,
17        as_commands: bool,
18        show_stderr: bool,
19        delay_seconds: Option<f64>,
20        start_directory: Option<PathBuf>,
21        target: Option<rmux_proto::PaneTarget>,
22    ) -> Result<Response, ClientError> {
23        self.roundtrip_without_read_timeout(&Request::RunShell(RunShellRequest {
24            command,
25            background,
26            as_commands,
27            show_stderr,
28            delay_seconds: delay_seconds.map(RunShellDelaySeconds),
29            start_directory,
30            target,
31            source_depth: None,
32        }))
33    }
34
35    /// Sends an `if-shell` request over the detached RPC channel without a
36    /// response read timeout.
37    pub fn if_shell(
38        &mut self,
39        condition: String,
40        format_mode: bool,
41        then_command: String,
42        else_command: Option<String>,
43        target: Option<Target>,
44        background: bool,
45    ) -> Result<Response, ClientError> {
46        self.roundtrip_without_read_timeout(&Request::IfShell(IfShellRequest {
47            condition,
48            format_mode,
49            then_command,
50            else_command,
51            target,
52            caller_cwd: current_working_directory(),
53            background,
54        }))
55    }
56
57    /// Sends a `wait-for` request over the detached RPC channel without a
58    /// response read timeout.
59    pub fn wait_for(
60        &mut self,
61        channel: String,
62        mode: WaitForMode,
63    ) -> Result<Response, ClientError> {
64        self.roundtrip_without_read_timeout(&Request::WaitFor(WaitForRequest { channel, mode }))
65    }
66}
67
68fn current_working_directory() -> Option<PathBuf> {
69    std::env::current_dir().ok()
70}