Files
zen-widget/src/tmux.rs
T
2026-04-30 19:44:14 +02:00

64 lines
1.8 KiB
Rust

use std::process::Command;
/// Get the content from a tmux pane
pub fn get_tmux_content(session_name: &str) -> String {
// First check if session exists
if !session_exists(session_name) {
// Create the session if it doesn't exist
let create_output = Command::new("tmux")
.args(&["new-session", "-d", "-s", session_name, "-x", "80", "-y", "24"])
.output();
if create_output.map(|o| !o.status.success()).unwrap_or(true) {
return String::new();
}
}
// Capture the pane content
let output = Command::new("tmux")
.args(&["capture-pane", "-t", session_name, "-p"])
.output();
match output {
Ok(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).to_string(),
_ => String::new(),
}
}
/// Check if a tmux session exists
fn session_exists(session_name: &str) -> bool {
Command::new("tmux")
.args(&["has-session", "-t", session_name])
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
/// Send a message to a tmux session (useful for testing)
#[allow(dead_code)]
pub fn send_tmux_message(session_name: &str, message: &str) -> bool {
Command::new("tmux")
.args(&["send-keys", "-t", session_name, message, "Enter"])
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_tmux_content() {
// This will create the session if it doesn't exist
let content = get_tmux_content("zen-widget-test");
// Should return something (possibly empty)
assert!(content.len() >= 0);
// Clean up
let _ = Command::new("tmux")
.args(&["kill-session", "-t", "zen-widget-test"])
.output();
}
}