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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::collections::HashMap;
use async_std::process::Command;
use crate::{
error::{check_empty_process_output, Error},
Result,
};
pub async fn start(initial_session_name: &str) -> Result<()> {
let args = vec!["new-session", "-d", "-s", initial_session_name];
let output = Command::new("tmux").args(&args).output().await?;
check_empty_process_output(&output, "new-session")
}
pub async fn kill_session(name: &str) -> Result<()> {
let exact_name = format!("={name}");
let args = vec!["kill-session", "-t", &exact_name];
let output = Command::new("tmux").args(&args).output().await?;
check_empty_process_output(&output, "kill-session")
}
pub async fn show_option(option_name: &str, global: bool) -> Result<Option<String>> {
let mut args = vec!["show-options", "-w", "-q"];
if global {
args.push("-g");
}
args.push(option_name);
let output = Command::new("tmux").args(&args).output().await?;
let buffer = String::from_utf8(output.stdout)?;
let buffer = buffer.trim_end();
if buffer.is_empty() {
return Ok(None);
}
Ok(Some(buffer.to_string()))
}
pub async fn show_options(global: bool) -> Result<HashMap<String, String>> {
let args = if global {
vec!["show-options", "-g"]
} else {
vec!["show-options"]
};
let output = Command::new("tmux").args(&args).output().await?;
let buffer = String::from_utf8(output.stdout)?;
let pairs: HashMap<String, String> = buffer
.trim_end()
.split('\n')
.into_iter()
.map(|s| s.split_at(s.find(' ').unwrap()))
.map(|(k, v)| (k.to_string(), v[1..].to_string()))
.collect();
Ok(pairs)
}
pub async fn default_command() -> Result<String> {
let all_options = show_options(true).await?;
let default_shell = all_options
.get("default-shell")
.ok_or(Error::TmuxConfig("no default-shell"))
.map(|cmd| cmd.to_owned())
.map(|cmd| {
if cmd.ends_with("bash") {
format!("-l {cmd}")
} else {
cmd
}
})?;
all_options
.get("default-command")
.or(Some(&default_shell))
.ok_or(Error::TmuxConfig("no default-command nor default-shell"))
.map(|cmd| cmd.to_owned())
}