flow-tui 0.1.2

Terminal UI for Flow with Kanban board, agent status, and dependency graph views
Documentation
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use flow_core::{Feature, Theme};
use flow_db::Database;
use ratatui::Frame;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};

use crate::theme::TuiTheme;
use crate::views;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum View {
    Kanban,
    Agents,
    Logs,
    Graph,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LayoutMode {
    Full,
    Compact,
    Mobile,
}

pub struct App {
    pub current_view: View,
    pub layout_mode: LayoutMode,
    pub theme: Theme,
    pub tui_theme: TuiTheme,
    pub features: Vec<Feature>,
    pub selected_index: usize,
    pub show_help: bool,
    #[allow(dead_code)]
    pub should_quit: bool,
    pub terminal_width: u16,
    pub terminal_height: u16,
    #[allow(dead_code)]
    pub scroll_offset: usize,
    pub log_messages: Vec<String>,
    pub db: Option<Arc<Database>>,
    pub db_path: Option<PathBuf>,
    pub db_error: Option<String>,
    pub last_refresh: Instant,
    pub refresh_interval: Duration,
    pub status_message: Option<(String, Instant)>,
    pub feature_stats: Option<flow_core::FeatureStats>,
}

impl Default for App {
    fn default() -> Self {
        Self::new()
    }
}

impl App {
    pub fn new() -> Self {
        Self::with_db_path(None)
    }

    pub fn with_db_path(db_path: Option<PathBuf>) -> Self {
        let theme = Theme::default();
        let tui_theme = TuiTheme::from(&theme.colors());

        let mut app = Self {
            current_view: View::Kanban,
            layout_mode: LayoutMode::Full,
            theme,
            tui_theme,
            features: Vec::new(),
            selected_index: 0,
            show_help: false,
            should_quit: false,
            terminal_width: 120,
            terminal_height: 30,
            scroll_offset: 0,
            log_messages: Vec::new(),
            db: None,
            db_path,
            db_error: None,
            last_refresh: Instant::now(),
            refresh_interval: Duration::from_secs(2),
            status_message: None,
            feature_stats: None,
        };

        app.load_from_database();
        app
    }

    pub fn render(&self, frame: &mut Frame) {
        if self.show_help {
            views::help::render(frame, self);
        } else {
            match self.current_view {
                View::Kanban => views::kanban::render(frame, self),
                View::Agents => views::agents::render(frame, self),
                View::Logs => views::logs::render(frame, self),
                View::Graph => views::graph::render(frame, self),
            }
        }
    }

    pub fn handle_key(&mut self, key: KeyEvent) -> bool {
        // Quit commands
        if key.code == KeyCode::Char('q')
            || (key.code == KeyCode::Char('c') && key.modifiers.contains(KeyModifiers::CONTROL))
        {
            return true;
        }

        // Help toggle
        if key.code == KeyCode::Char('?') {
            self.show_help = !self.show_help;
            return false;
        }

        // If help is showing, only toggle it off
        if self.show_help {
            self.show_help = false;
            return false;
        }

        // View switching
        match key.code {
            KeyCode::Char('1') => self.current_view = View::Kanban,
            KeyCode::Char('2') => self.current_view = View::Agents,
            KeyCode::Char('3') => self.current_view = View::Logs,
            KeyCode::Char('4') => self.current_view = View::Graph,
            KeyCode::Tab => self.next_view(),
            _ => {}
        }

        // Navigation
        match key.code {
            KeyCode::Char('j') | KeyCode::Down => self.next_item(),
            KeyCode::Char('k') | KeyCode::Up => self.prev_item(),
            _ => {}
        }

        // Theme cycling
        if key.code == KeyCode::Char('t') {
            self.cycle_theme();
        }

        // Database actions
        match key.code {
            KeyCode::Enter | KeyCode::Char(' ') => self.claim_selected_feature(),
            KeyCode::Char('p') => self.mark_selected_passing(),
            KeyCode::Char('f') => self.mark_selected_failing(),
            KeyCode::Char('c') if !key.modifiers.contains(KeyModifiers::CONTROL) => {
                self.clear_selected_in_progress();
            }
            KeyCode::Char('r') => self.manual_refresh(),
            _ => {}
        }

        false
    }

    pub fn resize(&mut self, width: u16, height: u16) {
        self.terminal_width = width;
        self.terminal_height = height;

        // Update layout mode based on width
        self.layout_mode = if width >= 120 {
            LayoutMode::Full
        } else if width >= 80 {
            LayoutMode::Compact
        } else {
            LayoutMode::Mobile
        };
    }

    fn next_view(&mut self) {
        self.current_view = match self.current_view {
            View::Kanban => View::Agents,
            View::Agents => View::Logs,
            View::Logs => View::Graph,
            View::Graph => View::Kanban,
        };
    }

    fn next_item(&mut self) {
        if !self.features.is_empty() {
            self.selected_index = (self.selected_index + 1) % self.features.len();
        }
    }

    fn prev_item(&mut self) {
        if !self.features.is_empty() {
            self.selected_index = if self.selected_index == 0 {
                self.features.len() - 1
            } else {
                self.selected_index - 1
            };
        }
    }

    fn cycle_theme(&mut self) {
        self.theme = self.theme.next();
        self.tui_theme = TuiTheme::from(&self.theme.colors());
        self.add_log(format!("Theme changed to: {}", self.theme.name()));
    }

    pub fn add_log(&mut self, message: String) {
        self.log_messages.push(message);
        // Keep last 100 messages
        if self.log_messages.len() > 100 {
            self.log_messages.remove(0);
        }
    }

    pub fn set_status(&mut self, message: String) {
        self.status_message = Some((message, Instant::now()));
    }

    pub fn get_status_message(&self) -> Option<&str> {
        if let Some((msg, timestamp)) = &self.status_message {
            // Show status for 3 seconds
            if timestamp.elapsed() < Duration::from_secs(3) {
                return Some(msg.as_str());
            }
        }
        None
    }

    pub fn should_refresh(&self) -> bool {
        self.last_refresh.elapsed() >= self.refresh_interval
    }

    pub fn auto_refresh(&mut self) {
        if self.should_refresh() {
            self.load_from_database();
        }
    }

    pub fn load_from_database(&mut self) {
        self.last_refresh = Instant::now();

        // Determine database path
        let db_path = if let Some(ref path) = self.db_path {
            path.clone()
        } else {
            // Try default path: ~/.agent-flow/flow.db
            let home = match std::env::var("HOME") {
                Ok(h) => PathBuf::from(h),
                Err(_) => {
                    self.db_error = Some("HOME environment variable not set".to_string());
                    self.load_demo_data();
                    return;
                }
            };
            home.join(".agent-flow").join("flow.db")
        };

        // Try to open database
        match Database::open(&db_path) {
            Ok(db) => {
                let db = Arc::new(db);
                self.db = Some(Arc::clone(&db));
                self.db_path = Some(db_path.clone());
                self.db_error = None;

                // Load features
                if let Ok(conn) = db.writer().lock() {
                    match flow_db::FeatureStore::get_all(&conn) {
                        Ok(features) => {
                            self.features = features;
                            if self.selected_index >= self.features.len()
                                && !self.features.is_empty()
                            {
                                self.selected_index = self.features.len() - 1;
                            }
                        }
                        Err(e) => {
                            self.db_error = Some(format!("Failed to load features: {e}"));
                            self.add_log(format!("Error loading features: {e}"));
                        }
                    }

                    // Load stats
                    match flow_db::FeatureStore::get_stats(&conn) {
                        Ok(stats) => {
                            self.feature_stats = Some(stats);
                        }
                        Err(e) => {
                            self.add_log(format!("Error loading stats: {e}"));
                        }
                    }
                } else {
                    self.db_error = Some("Failed to acquire database lock".to_string());
                }

                self.add_log(format!(
                    "Loaded {} features from database",
                    self.features.len()
                ));
            }
            Err(e) => {
                self.db_error = Some(format!("Failed to open database: {e}"));
                self.add_log("No database found. Run 'flow init' to create one, or 'flow features add' to add features.".to_string());
                self.load_demo_data();
            }
        }
    }

    fn manual_refresh(&mut self) {
        self.load_from_database();
        self.set_status("✓ Refreshed from database".to_string());
    }

    fn claim_selected_feature(&mut self) {
        if self.features.is_empty() {
            return;
        }

        let feature_id = self.features[self.selected_index].id;
        let feature_name = self.features[self.selected_index].name.clone();

        let db = match &self.db {
            Some(db) => Arc::clone(db),
            None => return,
        };

        let result = {
            if let Ok(conn) = db.writer().lock() {
                flow_db::FeatureStore::claim_and_get(&conn, feature_id)
            } else {
                return;
            }
        };

        match result {
            Ok(_) => {
                self.set_status(format!("✓ Claimed \"{}\" (#{feature_id})", feature_name));
                self.add_log(format!("Claimed feature #{feature_id}: {feature_name}"));
                self.load_from_database();
            }
            Err(e) => {
                self.set_status(format!("✗ Failed to claim: {e}"));
                self.add_log(format!("Failed to claim feature #{feature_id}: {e}"));
            }
        }
    }

    fn mark_selected_passing(&mut self) {
        if self.features.is_empty() {
            return;
        }

        let feature_id = self.features[self.selected_index].id;
        let feature_name = self.features[self.selected_index].name.clone();

        let db = match &self.db {
            Some(db) => Arc::clone(db),
            None => return,
        };

        let result = {
            if let Ok(conn) = db.writer().lock() {
                flow_db::FeatureStore::mark_passing(&conn, feature_id)
            } else {
                return;
            }
        };

        match result {
            Ok(()) => {
                self.set_status(format!("✓ Marked \"{}\" as passing", feature_name));
                self.add_log(format!("Marked feature #{feature_id} as passing"));
                self.load_from_database();
            }
            Err(e) => {
                self.set_status(format!("✗ Failed to mark passing: {e}"));
                self.add_log(format!(
                    "Failed to mark feature #{feature_id} as passing: {e}"
                ));
            }
        }
    }

    fn mark_selected_failing(&mut self) {
        if self.features.is_empty() {
            return;
        }

        let feature_id = self.features[self.selected_index].id;
        let feature_name = self.features[self.selected_index].name.clone();

        let db = match &self.db {
            Some(db) => Arc::clone(db),
            None => return,
        };

        let result = {
            if let Ok(conn) = db.writer().lock() {
                flow_db::FeatureStore::mark_failing(&conn, feature_id)
            } else {
                return;
            }
        };

        match result {
            Ok(()) => {
                self.set_status(format!("✓ Marked \"{}\" as failing", feature_name));
                self.add_log(format!("Marked feature #{feature_id} as failing"));
                self.load_from_database();
            }
            Err(e) => {
                self.set_status(format!("✗ Failed to mark failing: {e}"));
                self.add_log(format!(
                    "Failed to mark feature #{feature_id} as failing: {e}"
                ));
            }
        }
    }

    fn clear_selected_in_progress(&mut self) {
        if self.features.is_empty() {
            return;
        }

        let feature_id = self.features[self.selected_index].id;
        let feature_name = self.features[self.selected_index].name.clone();

        let db = match &self.db {
            Some(db) => Arc::clone(db),
            None => return,
        };

        let result = {
            if let Ok(conn) = db.writer().lock() {
                flow_db::FeatureStore::clear_in_progress(&conn, feature_id)
            } else {
                return;
            }
        };

        match result {
            Ok(()) => {
                self.set_status(format!("✓ Cleared in-progress for \"{}\"", feature_name));
                self.add_log(format!("Cleared in-progress for feature #{feature_id}"));
                self.load_from_database();
            }
            Err(e) => {
                self.set_status(format!("✗ Failed to clear in-progress: {e}"));
                self.add_log(format!(
                    "Failed to clear in-progress for feature #{feature_id}: {e}"
                ));
            }
        }
    }

    #[allow(clippy::too_many_lines)]
    pub fn load_demo_data(&mut self) {
        // Create demo features for testing
        self.features = vec![
            Feature {
                id: 1,
                priority: 100,
                category: "Backend".to_string(),
                name: "User Authentication".to_string(),
                description: "Implement JWT-based authentication system".to_string(),
                steps: vec![
                    "Create user model".to_string(),
                    "Implement JWT tokens".to_string(),
                    "Add login/logout endpoints".to_string(),
                ],
                passes: false,
                in_progress: false,
                dependencies: vec![],
                created_at: Some("2024-01-15T10:00:00Z".to_string()),
                updated_at: Some("2024-01-15T10:00:00Z".to_string()),
            },
            Feature {
                id: 2,
                priority: 90,
                category: "Frontend".to_string(),
                name: "Login Page UI".to_string(),
                description: "Create responsive login page".to_string(),
                steps: vec![
                    "Design mockup".to_string(),
                    "Implement form".to_string(),
                    "Add validation".to_string(),
                ],
                passes: false,
                in_progress: false,
                dependencies: vec![],
                created_at: Some("2024-01-15T11:00:00Z".to_string()),
                updated_at: Some("2024-01-15T11:00:00Z".to_string()),
            },
            Feature {
                id: 3,
                priority: 80,
                category: "Backend".to_string(),
                name: "API Rate Limiting".to_string(),
                description: "Add rate limiting middleware".to_string(),
                steps: vec![
                    "Research solutions".to_string(),
                    "Implement middleware".to_string(),
                ],
                passes: false,
                in_progress: true,
                dependencies: vec![1],
                created_at: Some("2024-01-16T09:00:00Z".to_string()),
                updated_at: Some("2024-01-16T14:30:00Z".to_string()),
            },
            Feature {
                id: 4,
                priority: 70,
                category: "Testing".to_string(),
                name: "Integration Tests".to_string(),
                description: "Write comprehensive test suite".to_string(),
                steps: vec![
                    "Setup test framework".to_string(),
                    "Write API tests".to_string(),
                    "Add CI/CD integration".to_string(),
                ],
                passes: false,
                in_progress: true,
                dependencies: vec![1, 3],
                created_at: Some("2024-01-16T10:00:00Z".to_string()),
                updated_at: Some("2024-01-16T15:00:00Z".to_string()),
            },
            Feature {
                id: 5,
                priority: 60,
                category: "Frontend".to_string(),
                name: "Dashboard".to_string(),
                description: "User dashboard with stats".to_string(),
                steps: vec![
                    "Create layout".to_string(),
                    "Add charts".to_string(),
                    "Implement data fetching".to_string(),
                ],
                passes: true,
                in_progress: false,
                dependencies: vec![1, 2],
                created_at: Some("2024-01-14T08:00:00Z".to_string()),
                updated_at: Some("2024-01-15T17:00:00Z".to_string()),
            },
            Feature {
                id: 6,
                priority: 50,
                category: "Backend".to_string(),
                name: "Email Notifications".to_string(),
                description: "Send email notifications for events".to_string(),
                steps: vec![
                    "Setup email service".to_string(),
                    "Create templates".to_string(),
                ],
                passes: true,
                in_progress: false,
                dependencies: vec![1],
                created_at: Some("2024-01-13T14:00:00Z".to_string()),
                updated_at: Some("2024-01-14T16:00:00Z".to_string()),
            },
            Feature {
                id: 7,
                priority: 40,
                category: "DevOps".to_string(),
                name: "Docker Setup".to_string(),
                description: "Containerize application".to_string(),
                steps: vec![
                    "Create Dockerfile".to_string(),
                    "Setup docker-compose".to_string(),
                ],
                passes: true,
                in_progress: false,
                dependencies: vec![],
                created_at: Some("2024-01-12T09:00:00Z".to_string()),
                updated_at: Some("2024-01-13T11:00:00Z".to_string()),
            },
            Feature {
                id: 8,
                priority: 30,
                category: "Frontend".to_string(),
                name: "Settings Page".to_string(),
                description: "User settings and preferences".to_string(),
                steps: vec![
                    "Design UI".to_string(),
                    "Implement form handling".to_string(),
                ],
                passes: true,
                in_progress: false,
                dependencies: vec![1, 2],
                created_at: Some("2024-01-11T10:00:00Z".to_string()),
                updated_at: Some("2024-01-12T15:00:00Z".to_string()),
            },
            Feature {
                id: 9,
                priority: 20,
                category: "Documentation".to_string(),
                name: "API Documentation".to_string(),
                description: "Complete OpenAPI specs".to_string(),
                steps: vec!["Write specs".to_string(), "Generate docs".to_string()],
                passes: true,
                in_progress: false,
                dependencies: vec![1, 3],
                created_at: Some("2024-01-10T13:00:00Z".to_string()),
                updated_at: Some("2024-01-11T16:00:00Z".to_string()),
            },
            Feature {
                id: 10,
                priority: 10,
                category: "Security".to_string(),
                name: "Security Audit".to_string(),
                description: "Comprehensive security review".to_string(),
                steps: vec![
                    "Run vulnerability scan".to_string(),
                    "Review dependencies".to_string(),
                    "Fix issues".to_string(),
                ],
                passes: true,
                in_progress: false,
                dependencies: vec![1, 3, 6],
                created_at: Some("2024-01-09T08:00:00Z".to_string()),
                updated_at: Some("2024-01-10T18:00:00Z".to_string()),
            },
        ];

        self.add_log("Loaded 10 demo features".to_string());
        self.add_log(format!("Current theme: {}", self.theme.name()));
        self.add_log("Press ? for help".to_string());
    }
}