-
-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathstatus_bar.rs
More file actions
265 lines (227 loc) · 8.72 KB
/
status_bar.rs
File metadata and controls
265 lines (227 loc) · 8.72 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! tmux status bar configuration for aoe sessions
use anyhow::Result;
use ratatui::style::Color;
use std::process::Command;
use crate::tui::styles::Theme;
/// Information about a sandboxed session for status bar display.
pub struct SandboxDisplay {
pub container_name: String,
}
/// Convert a ratatui Color to a tmux-compatible hex color string (e.g. "#0f172a").
fn color_to_tmux(color: Color) -> String {
match color {
Color::Rgb(r, g, b) => format!("#{:02x}{:02x}{:02x}", r, g, b),
_ => "default".to_string(),
}
}
/// Apply aoe-styled status bar configuration to a tmux session.
///
/// Sets tmux user options (@aoe_title, @aoe_branch, @aoe_sandbox) and configures
/// the status-right to display session information using theme colors.
pub fn apply_status_bar(
session_name: &str,
title: &str,
branch: Option<&str>,
sandbox: Option<&SandboxDisplay>,
theme: &Theme,
) -> Result<()> {
// Set the session title as a tmux user option
set_session_option(session_name, "@aoe_title", title)?;
// Set branch if provided (for worktree sessions)
if let Some(branch_name) = branch {
set_session_option(session_name, "@aoe_branch", branch_name)?;
}
// Set sandbox info if running in docker container
if let Some(sandbox_info) = sandbox {
set_session_option(session_name, "@aoe_sandbox", &sandbox_info.container_name)?;
}
let accent = color_to_tmux(theme.accent);
let fg = color_to_tmux(theme.text);
let bg = color_to_tmux(theme.background);
let branch_color = color_to_tmux(theme.branch);
let sandbox_color = color_to_tmux(theme.sandbox);
let hint = color_to_tmux(theme.dimmed);
// Format: "aoe: Title | branch | [container] | 14:30"
let status_format = format!(
" #[fg={accent},bold]aoe#[fg={fg},nobold]: \
#{{@aoe_title}}\
#{{?#{{@aoe_branch}}, #[fg={branch_color}]| #{{@aoe_branch}}#[fg={fg}],}}\
#{{?#{{@aoe_sandbox}}, #[fg={sandbox_color}]\u{2b21} #{{@aoe_sandbox}}#[fg={fg}],}}\
| %H:%M ",
);
set_session_option(session_name, "status-right", &status_format)?;
set_session_option(session_name, "status-right-length", "80")?;
set_session_option(session_name, "status-style", &format!("bg={bg},fg={fg}"))?;
let prefix = crate::tmux::utils::tmux_prefix_display();
set_session_option(
session_name,
"status-left",
&format!(
" #[fg={accent},bold]#S#[fg={fg},nobold] \u{2502} #[fg={hint}]{prefix} d#[fg={hint}] to detach ",
),
)?;
set_session_option(session_name, "status-left-length", "50")?;
Ok(())
}
/// Set a tmux option for a specific session.
fn set_session_option(session_name: &str, option: &str, value: &str) -> Result<()> {
let output = Command::new("tmux")
.args(["set-option", "-t", session_name, option, value])
.output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
// Don't fail on option errors - status bar is non-critical
tracing::debug!("Failed to set tmux option {}: {}", option, stderr);
}
Ok(())
}
/// Apply mouse support option to a tmux session.
/// When enabled, scrolling with the mouse wheel enters copy mode.
pub fn apply_mouse_option(session_name: &str, enabled: bool) -> Result<()> {
let value = if enabled { "on" } else { "off" };
set_session_option(session_name, "mouse", value)
}
/// Apply all configured tmux options to a session.
/// This is a unified entry point that applies status bar styling and mouse settings.
pub fn apply_all_tmux_options(
session_name: &str,
title: &str,
branch: Option<&str>,
sandbox: Option<&SandboxDisplay>,
) {
use crate::session::config::{should_apply_tmux_mouse, should_apply_tmux_status_bar};
use crate::tui::styles::load_theme;
if should_apply_tmux_status_bar() {
let config = crate::session::config::Config::load_or_warn();
let theme_name = if config.theme.name.is_empty() {
"empire"
} else {
&config.theme.name
};
// Always use truecolor here: tmux receives hex color values (#rrggbb)
// and manages its own escape-sequence rendering via TERM/terminfo.
// Palette mode only affects the TUI's direct terminal output.
let theme = load_theme(theme_name);
if let Err(e) = apply_status_bar(session_name, title, branch, sandbox, &theme) {
tracing::debug!("Failed to apply tmux status bar: {}", e);
}
}
if let Some(mouse_enabled) = should_apply_tmux_mouse() {
if let Err(e) = apply_mouse_option(session_name, mouse_enabled) {
tracing::debug!("Failed to apply tmux mouse option: {}", e);
}
}
}
/// Session info retrieved from tmux user options.
pub struct SessionInfo {
pub title: String,
pub branch: Option<String>,
pub sandbox: Option<String>,
}
/// Get session info for the current tmux session (used by `aoe tmux-status` command).
/// Returns structured session info for use in user's custom tmux status bar.
pub fn get_session_info_for_current() -> Option<SessionInfo> {
let session_name = crate::tmux::get_current_session_name()?;
// Check if this is an aoe session
if !session_name.starts_with(crate::tmux::SESSION_PREFIX) {
return None;
}
// Try to get the aoe title from tmux user option
let title = get_session_option(&session_name, "@aoe_title").unwrap_or_else(|| {
// Fallback: extract title from session name
// Session names are: aoe_<title>_<id>
let name_without_prefix = session_name
.strip_prefix(crate::tmux::SESSION_PREFIX)
.unwrap_or(&session_name);
if let Some(last_underscore) = name_without_prefix.rfind('_') {
name_without_prefix[..last_underscore].to_string()
} else {
name_without_prefix.to_string()
}
});
let branch = get_session_option(&session_name, "@aoe_branch");
let sandbox = get_session_option(&session_name, "@aoe_sandbox");
Some(SessionInfo {
title,
branch,
sandbox,
})
}
/// Get formatted status string for the current tmux session.
/// Returns a plain text string like "aoe: Title | branch | [container]"
pub fn get_status_for_current_session() -> Option<String> {
let info = get_session_info_for_current()?;
let mut result = format!("aoe: {}", info.title);
if let Some(b) = &info.branch {
result.push_str(" | ");
result.push_str(b);
}
if let Some(s) = &info.sandbox {
result.push_str(" [");
result.push_str(s);
result.push(']');
}
Some(result)
}
/// Get a tmux option value for a session.
fn get_session_option(session_name: &str, option: &str) -> Option<String> {
let output = Command::new("tmux")
.args(["show-options", "-t", session_name, "-v", option])
.output()
.ok()?;
if output.status.success() {
let value = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !value.is_empty() {
return Some(value);
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tui::styles::load_theme;
#[test]
fn test_get_status_returns_none_for_non_tmux() {
// When not in tmux, get_current_session_name returns None
// so get_status_for_current_session should also return None
// This test just verifies the function doesn't panic
let _ = get_status_for_current_session();
}
#[test]
fn test_color_to_tmux_rgb() {
assert_eq!(color_to_tmux(Color::Rgb(15, 23, 42)), "#0f172a");
assert_eq!(color_to_tmux(Color::Rgb(255, 255, 255)), "#ffffff");
assert_eq!(color_to_tmux(Color::Rgb(0, 0, 0)), "#000000");
}
#[test]
fn test_color_to_tmux_non_rgb_fallback() {
assert_eq!(color_to_tmux(Color::Red), "default");
}
#[test]
fn test_all_themes_produce_valid_status_bar_colors() {
for theme_name in &[
"empire",
"phosphor",
"tokyo-night-storm",
"catppuccin-latte",
"dracula",
] {
let theme = load_theme(theme_name);
let colors = [
("background", color_to_tmux(theme.background)),
("text", color_to_tmux(theme.text)),
("accent", color_to_tmux(theme.accent)),
("branch", color_to_tmux(theme.branch)),
("sandbox", color_to_tmux(theme.sandbox)),
("dimmed", color_to_tmux(theme.dimmed)),
];
for (field, hex) in &colors {
assert!(
hex.starts_with('#'),
"{theme_name}: {field} should be hex, got {hex}"
);
}
}
}
}