Run the monitor TUI. Blocks until Ctrl+C.
()
| 267 | |
| 268 | /// Run the monitor TUI. Blocks until Ctrl+C. |
| 269 | pub fn run() -> std::io::Result<()> { |
| 270 | use crossterm::{ |
| 271 | cursor, execute, terminal, |
| 272 | terminal::{EnterAlternateScreen, LeaveAlternateScreen}, |
| 273 | }; |
| 274 | let dir = global_tracedecay_dir().ok_or_else(|| { |
| 275 | std::io::Error::new( |
| 276 | std::io::ErrorKind::NotFound, |
| 277 | "cannot resolve home directory", |
| 278 | ) |
| 279 | })?; |
| 280 | std::fs::create_dir_all(&dir)?; |
| 281 | |
| 282 | // Single-instance lock. |
| 283 | let lock_path = dir.join(LOCK_FILENAME); |
| 284 | let lock_file = std::fs::OpenOptions::new() |
| 285 | .read(true) |
| 286 | .write(true) |
| 287 | .create(true) |
| 288 | .truncate(false) |
| 289 | .open(&lock_path)?; |
| 290 | |
| 291 | if lock_file.try_lock_exclusive().is_err() { |
| 292 | eprintln!("Monitor already running."); |
| 293 | return Ok(()); |
| 294 | } |
| 295 | |
| 296 | // Ensure mmap file exists. |
| 297 | let mmap_path = dir.join(MMAP_FILENAME); |
| 298 | if !mmap_path.exists() { |
| 299 | let f = std::fs::File::create(&mmap_path)?; |
| 300 | f.set_len(FILE_SIZE as u64)?; |
| 301 | } |
| 302 | |
| 303 | let mut reader = MmapReader::open()?; |
| 304 | let mut last_idx = reader.write_idx(); |
| 305 | let mut entries: Vec<MonitorEntry> = Vec::new(); |
| 306 | let mut recent_updates: Vec<(String, String)> = Vec::new(); |
| 307 | |
| 308 | // Populate with existing entries in the ring buffer (up to write_idx). |
| 309 | let populated = last_idx.min(RING_CAPACITY as u64) as usize; |
| 310 | if populated > 0 { |
| 311 | let start_slot = if last_idx > RING_CAPACITY as u64 { |
| 312 | (last_idx as usize) % RING_CAPACITY |
| 313 | } else { |
| 314 | 0 |
| 315 | }; |
| 316 | for i in 0..populated { |
| 317 | let slot = (start_slot + i) % RING_CAPACITY; |
| 318 | if let Some(e) = reader.entry(slot) { |
| 319 | if e.delta > 0 { |
| 320 | push_recent_update(&mut recent_updates, &e.project, &e.tool_name); |
| 321 | entries.push(e); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 |