Skip to main content

flow_tmux/
window.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum WindowError {
5    #[error("Tmux error: {0}")]
6    Tmux(String),
7    #[error("IO error: {0}")]
8    Io(#[from] std::io::Error),
9}
10
11/// Create a new tmux window.
12///
13/// # Errors
14///
15/// Returns an error if the window cannot be created.
16pub fn create(name: &str, command: Option<&str>) -> Result<(), WindowError> {
17    let mut args = vec!["new-window", "-n", name];
18
19    if let Some(cmd) = command {
20        args.push(cmd);
21    }
22
23    let output = std::process::Command::new("tmux").args(&args).output()?;
24
25    if !output.status.success() {
26        return Err(WindowError::Tmux(
27            String::from_utf8_lossy(&output.stderr).to_string(),
28        ));
29    }
30
31    Ok(())
32}
33
34/// Split the current pane horizontally.
35///
36/// # Errors
37///
38/// Returns an error if the pane cannot be split.
39pub fn split_horizontal(command: Option<&str>) -> Result<(), WindowError> {
40    let mut args = vec!["split-window", "-h"];
41
42    if let Some(cmd) = command {
43        args.push(cmd);
44    }
45
46    let output = std::process::Command::new("tmux").args(&args).output()?;
47
48    if !output.status.success() {
49        return Err(WindowError::Tmux(
50            String::from_utf8_lossy(&output.stderr).to_string(),
51        ));
52    }
53
54    Ok(())
55}
56
57/// Split the current pane vertically.
58///
59/// # Errors
60///
61/// Returns an error if the pane cannot be split.
62pub fn split_vertical(command: Option<&str>) -> Result<(), WindowError> {
63    let mut args = vec!["split-window", "-v"];
64
65    if let Some(cmd) = command {
66        args.push(cmd);
67    }
68
69    let output = std::process::Command::new("tmux").args(&args).output()?;
70
71    if !output.status.success() {
72        return Err(WindowError::Tmux(
73            String::from_utf8_lossy(&output.stderr).to_string(),
74        ));
75    }
76
77    Ok(())
78}