1use std::collections::HashMap;
4use std::io::{self, stdout};
5
6use crossterm::cursor;
7use crossterm::execute;
8use crossterm::terminal::{self, DisableLineWrap, EnableLineWrap};
9use ratatui::Terminal;
10use ratatui::backend::CrosstermBackend;
11
12use crate::sidebar::context::ContextManager;
13use crate::sidebar::event::{self, Action};
14use crate::sidebar::state::{StateDetector, WindowState};
15use crate::sidebar::ui::SidebarWidget;
16use crate::tmux::{self, WindowInfo};
17
18struct SidebarApp {
21 windows: Vec<WindowInfo>,
22 states: HashMap<u32, WindowState>,
23 selected: usize,
24 tick: u64,
25 detector: StateDetector,
26 context_mgr: ContextManager,
27}
28
29const REFRESH_EVERY: u64 = 2;
32
33pub fn run() -> Result<(), String> {
36 let mut stdout = stdout();
38 execute!(stdout, cursor::Hide, DisableLineWrap).map_err(|e| format!("terminal: {e}"))?;
39 terminal::enable_raw_mode().map_err(|e| format!("terminal: {e}"))?;
40
41 let result = run_loop();
42
43 terminal::disable_raw_mode().ok();
45 execute!(stdout, cursor::Show, EnableLineWrap).ok();
46
47 result
48}
49
50fn run_loop() -> Result<(), String> {
53 let backend = CrosstermBackend::new(io::stdout());
54 let mut terminal = Terminal::new(backend).map_err(|e| format!("terminal: {e}"))?;
55
56 let mut app = SidebarApp {
57 windows: Vec::new(),
58 states: HashMap::new(),
59 selected: 0,
60 tick: 0,
61 detector: StateDetector::new(),
62 context_mgr: ContextManager::new(),
63 };
64
65 loop {
66 if app.tick % REFRESH_EVERY == 0 {
68 refresh_windows(&mut app);
69 }
70
71 app.states = app.detector.detect(&app.windows);
73
74 let detector = &app.detector;
76 app.context_mgr.tick(
77 &app.windows,
78 &app.states,
79 app.selected,
80 &|idx| detector.pane_id(idx).map(str::to_string),
81 &|idx| detector.cwd(idx).map(str::to_string),
82 );
83
84 let context = app
86 .windows
87 .get(app.selected)
88 .and_then(|win| app.context_mgr.get(&win.name));
89 let context_loading = app
90 .windows
91 .get(app.selected)
92 .is_some_and(|win| app.context_mgr.is_loading(&win.name));
93
94 terminal
96 .draw(|frame| {
97 let area = frame.area();
98 let widget = SidebarWidget {
99 windows: &app.windows,
100 states: &app.states,
101 selected: app.selected,
102 tick: app.tick,
103 context,
104 context_loading,
105 };
106 frame.render_widget(widget, area);
107 })
108 .map_err(|e| format!("render: {e}"))?;
109
110 let actions = event::poll();
112 let mut moved = false;
113
114 for action in actions {
115 match action {
116 Action::Up => {
117 if app.selected > 0 {
118 app.selected -= 1;
119 moved = true;
120 }
121 }
122 Action::Down => {
123 if app.selected + 1 < app.windows.len() {
124 app.selected += 1;
125 moved = true;
126 }
127 }
128 Action::Select => {
129 if let Some(win) = app.windows.get(app.selected) {
130 let _ = tmux::select_window(win.index);
131 refresh_windows(&mut app);
132 app.tick = 0;
133 continue;
134 }
135 }
136 Action::Quit => return Ok(()),
137 Action::Tick => {}
138 }
139 }
140
141 if moved {
143 if let Some(win) = app.windows.get(app.selected) {
144 let _ = tmux::select_window_sidebar(win.index);
145 }
146 app.tick = 1;
148 } else {
149 app.tick += 1;
150 }
151 }
152}
153
154fn refresh_windows(app: &mut SidebarApp) {
155 if let Ok(windows) = tmux::list_windows() {
156 let active_pos = windows.iter().position(|w| w.is_active).unwrap_or(0);
158
159 app.selected = active_pos;
160 app.windows = windows;
161
162 if app.selected >= app.windows.len() && !app.windows.is_empty() {
164 app.selected = app.windows.len() - 1;
165 }
166 }
167}