tazuna 0.1.0

TUI tool for managing multiple Claude Code sessions in parallel
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
//! Popup state types.

use std::path::PathBuf;

use ratatui::widgets::ListState;
use throbber_widgets_tui::ThrobberState;

/// Unified loading state for async operations
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum LoadingOperation {
    /// No operation in progress
    #[default]
    Idle,
    /// Fetching from remotes (git fetch)
    Fetching,
    /// Pulling changes for specific worktree
    Pulling { path: PathBuf },
    /// Deleting a worktree
    Deleting { path: PathBuf },
    /// Fetching GitHub issues list
    FetchingIssues,
    /// Fetching single issue details
    FetchingIssue { issue_number: u32 },
    /// Generating action choices for an issue
    GeneratingActions { issue_number: u32 },
}

impl LoadingOperation {
    /// Returns display message for this loading operation
    #[must_use]
    pub const fn message(&self) -> &'static str {
        match self {
            Self::Idle => "",
            Self::Fetching => "Fetching...",
            Self::Pulling { .. } => "Pulling...",
            Self::Deleting { .. } => "Deleting...",
            Self::FetchingIssues => "Loading issues...",
            Self::FetchingIssue { .. } => "Fetching issue...",
            Self::GeneratingActions { .. } => "Generating actions...",
        }
    }

    /// Check if loading operation is in progress
    #[must_use]
    pub const fn is_loading(&self) -> bool {
        !matches!(self, Self::Idle)
    }
}

/// Section focus within combined workspace popup
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PopupSection {
    /// Branch name input field
    #[default]
    BranchInput,
    /// Active sessions list
    Sessions,
    /// Available worktrees list
    Worktrees,
    /// GitHub issues list
    Issues,
}

impl PopupSection {
    /// Move to next section
    #[must_use]
    pub fn next(self) -> Self {
        match self {
            Self::BranchInput => Self::Sessions,
            Self::Sessions => Self::Worktrees,
            Self::Worktrees => Self::Issues,
            Self::Issues => Self::BranchInput,
        }
    }

    /// Move to previous section
    #[must_use]
    pub fn prev(self) -> Self {
        match self {
            Self::BranchInput => Self::Issues,
            Self::Sessions => Self::BranchInput,
            Self::Worktrees => Self::Sessions,
            Self::Issues => Self::Worktrees,
        }
    }
}

/// Combined workspace popup state
#[derive(Debug, Default)]
pub struct WorkspacePopupState {
    /// Current focus section
    pub section: PopupSection,
    /// Branch input buffer
    pub input: String,
    /// Cursor position in input
    pub cursor: usize,
    /// Session list selection
    pub session_list: ListState,
    /// Worktree list selection
    pub worktree_list: ListState,
    /// Issue list selection
    pub issue_list: ListState,
    /// Loading state for async operations
    pub loading: LoadingOperation,
    /// Spinner animation state
    pub throbber_state: ThrobberState,
}

impl WorkspacePopupState {
    /// Create new workspace popup state
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Move to next section
    pub fn next_section(&mut self) {
        self.section = self.section.next();
    }

    /// Move to previous section
    pub fn prev_section(&mut self) {
        self.section = self.section.prev();
    }

    /// Insert character at cursor position
    pub fn input_char(&mut self, c: char) {
        self.input.insert(self.cursor, c);
        self.cursor += c.len_utf8();
    }

    /// Delete character before cursor (backspace)
    pub fn input_backspace(&mut self) {
        if self.cursor > 0 {
            let prev_char_boundary = self
                .input
                .char_indices()
                .take_while(|(i, _)| *i < self.cursor)
                .last()
                .map_or(0, |(i, _)| i);
            self.input.remove(prev_char_boundary);
            self.cursor = prev_char_boundary;
        }
    }

    /// Move cursor left
    pub fn cursor_left(&mut self) {
        if self.cursor > 0 {
            self.cursor = self
                .input
                .char_indices()
                .take_while(|(i, _)| *i < self.cursor)
                .last()
                .map_or(0, |(i, _)| i);
        }
    }

    /// Move cursor right
    pub fn cursor_right(&mut self) {
        if self.cursor < self.input.len() {
            self.cursor = self
                .input
                .char_indices()
                .find(|(i, _)| *i > self.cursor)
                .map_or(self.input.len(), |(i, _)| i);
        }
    }

    /// Clear input and reset cursor
    pub fn clear_input(&mut self) {
        self.input.clear();
        self.cursor = 0;
    }

    /// Advance spinner animation (call on each frame when loading)
    pub fn tick_spinner(&mut self) {
        if self.loading.is_loading() {
            self.throbber_state.calc_next();
        }
    }

    /// Check if loading operation is in progress
    #[must_use]
    pub fn is_loading(&self) -> bool {
        self.loading.is_loading()
    }

    /// Get loading message for spinner
    #[must_use]
    pub fn loading_message(&self) -> &'static str {
        self.loading.message()
    }
}

/// State for action selection popup
#[derive(Debug, Default)]
pub struct ActionSelectPopupState {
    /// List selection state
    pub list_state: ListState,
}

impl ActionSelectPopupState {
    /// Create new action select popup state
    #[must_use]
    pub fn new() -> Self {
        let mut state = Self::default();
        state.list_state.select(Some(0));
        state
    }

    /// Select next item
    pub fn select_next(&mut self, len: usize) {
        if len == 0 {
            return;
        }
        let i = match self.list_state.selected() {
            Some(i) => (i + 1) % len,
            None => 0,
        };
        self.list_state.select(Some(i));
    }

    /// Select previous item
    pub fn select_prev(&mut self, len: usize) {
        if len == 0 {
            return;
        }
        let i = match self.list_state.selected() {
            Some(i) => {
                if i == 0 {
                    len - 1
                } else {
                    i - 1
                }
            }
            None => 0,
        };
        self.list_state.select(Some(i));
    }

    /// Get currently selected index
    #[must_use]
    pub fn selected(&self) -> Option<usize> {
        self.list_state.selected()
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    // PopupSection tests
    #[test]
    fn popup_section_default_is_branch_input() {
        assert_eq!(PopupSection::default(), PopupSection::BranchInput);
    }

    #[test]
    fn popup_section_next_cycles() {
        assert_eq!(PopupSection::BranchInput.next(), PopupSection::Sessions);
        assert_eq!(PopupSection::Sessions.next(), PopupSection::Worktrees);
        assert_eq!(PopupSection::Worktrees.next(), PopupSection::Issues);
        assert_eq!(PopupSection::Issues.next(), PopupSection::BranchInput);
    }

    #[test]
    fn popup_section_prev_cycles() {
        assert_eq!(PopupSection::BranchInput.prev(), PopupSection::Issues);
        assert_eq!(PopupSection::Sessions.prev(), PopupSection::BranchInput);
        assert_eq!(PopupSection::Worktrees.prev(), PopupSection::Sessions);
        assert_eq!(PopupSection::Issues.prev(), PopupSection::Worktrees);
    }

    // WorkspacePopupState tests
    #[test]
    fn workspace_popup_state_default() {
        let state = WorkspacePopupState::new();
        assert_eq!(state.section, PopupSection::BranchInput);
        assert!(state.input.is_empty());
        assert_eq!(state.cursor, 0);
    }

    #[test]
    fn workspace_popup_state_section_navigation() {
        let mut state = WorkspacePopupState::new();
        assert_eq!(state.section, PopupSection::BranchInput);

        state.next_section();
        assert_eq!(state.section, PopupSection::Sessions);

        state.next_section();
        assert_eq!(state.section, PopupSection::Worktrees);

        state.next_section();
        assert_eq!(state.section, PopupSection::Issues);

        state.next_section();
        assert_eq!(state.section, PopupSection::BranchInput);

        state.prev_section();
        assert_eq!(state.section, PopupSection::Issues);

        state.prev_section();
        assert_eq!(state.section, PopupSection::Worktrees);
    }

    #[test]
    fn workspace_popup_state_input_char() {
        let mut state = WorkspacePopupState::new();
        state.input_char('f');
        state.input_char('o');
        state.input_char('o');
        assert_eq!(state.input, "foo");
        assert_eq!(state.cursor, 3);
    }

    #[test]
    fn workspace_popup_state_input_backspace() {
        let mut state = WorkspacePopupState::new();
        state.input_char('a');
        state.input_char('b');
        state.input_char('c');
        assert_eq!(state.input, "abc");

        state.input_backspace();
        assert_eq!(state.input, "ab");
        assert_eq!(state.cursor, 2);

        state.input_backspace();
        assert_eq!(state.input, "a");
        assert_eq!(state.cursor, 1);
    }

    #[test]
    fn workspace_popup_state_input_backspace_empty() {
        let mut state = WorkspacePopupState::new();
        state.input_backspace(); // should not panic
        assert!(state.input.is_empty());
        assert_eq!(state.cursor, 0);
    }

    #[test]
    fn workspace_popup_state_cursor_movement() {
        let mut state = WorkspacePopupState::new();
        state.input_char('a');
        state.input_char('b');
        state.input_char('c');
        assert_eq!(state.cursor, 3);

        state.cursor_left();
        assert_eq!(state.cursor, 2);

        state.cursor_left();
        assert_eq!(state.cursor, 1);

        state.cursor_right();
        assert_eq!(state.cursor, 2);

        state.cursor_right();
        assert_eq!(state.cursor, 3);

        // At end, should stay
        state.cursor_right();
        assert_eq!(state.cursor, 3);

        // At start, should stay
        state.cursor = 0;
        state.cursor_left();
        assert_eq!(state.cursor, 0);
    }

    #[test]
    fn workspace_popup_state_clear_input() {
        let mut state = WorkspacePopupState::new();
        state.input_char('t');
        state.input_char('e');
        state.input_char('s');
        state.input_char('t');

        state.clear_input();
        assert!(state.input.is_empty());
        assert_eq!(state.cursor, 0);
    }

    #[test]
    fn workspace_popup_state_unicode_input() {
        let mut state = WorkspacePopupState::new();
        state.input_char('æ—¥');
        state.input_char('本');
        assert_eq!(state.input, "日本");
        assert_eq!(state.cursor, 6); // 3 bytes each

        state.input_backspace();
        assert_eq!(state.input, "æ—¥");
        assert_eq!(state.cursor, 3);
    }

    // LoadingOperation tests
    use rstest::rstest;

    #[test]
    fn loading_operation_default_is_idle() {
        assert_eq!(LoadingOperation::default(), LoadingOperation::Idle);
    }

    #[rstest]
    #[case(LoadingOperation::Idle, false, "")]
    #[case(LoadingOperation::Fetching, true, "Fetching...")]
    #[case(LoadingOperation::Pulling { path: PathBuf::from("/test") }, true, "Pulling...")]
    #[case(LoadingOperation::Deleting { path: PathBuf::from("/test") }, true, "Deleting...")]
    #[case(LoadingOperation::FetchingIssues, true, "Loading issues...")]
    #[case(LoadingOperation::FetchingIssue { issue_number: 1 }, true, "Fetching issue...")]
    #[case(LoadingOperation::GeneratingActions { issue_number: 42 }, true, "Generating actions...")]
    fn loading_operation_behavior(
        #[case] op: LoadingOperation,
        #[case] expected_loading: bool,
        #[case] expected_message: &str,
    ) {
        assert_eq!(op.is_loading(), expected_loading);
        assert_eq!(op.message(), expected_message);
    }

    #[test]
    fn workspace_popup_state_is_loading() {
        let mut state = WorkspacePopupState::new();
        assert!(!state.is_loading());

        state.loading = LoadingOperation::Fetching;
        assert!(state.is_loading());

        state.loading = LoadingOperation::Idle;
        assert!(!state.is_loading());
    }

    #[test]
    fn workspace_popup_state_tick_spinner_when_loading() {
        let mut state = WorkspacePopupState::new();

        // When idle, tick should not change state
        state.tick_spinner();

        // When loading, tick should advance throbber
        state.loading = LoadingOperation::Fetching;
        state.tick_spinner();
        // ThrobberState has internal state that advances
    }

    #[test]
    fn loading_operation_eq() {
        assert_eq!(LoadingOperation::Idle, LoadingOperation::Idle);
        assert_eq!(LoadingOperation::Fetching, LoadingOperation::Fetching);
        assert_eq!(
            LoadingOperation::Pulling {
                path: PathBuf::from("/a")
            },
            LoadingOperation::Pulling {
                path: PathBuf::from("/a")
            }
        );
        assert_eq!(
            LoadingOperation::Deleting {
                path: PathBuf::from("/b")
            },
            LoadingOperation::Deleting {
                path: PathBuf::from("/b")
            }
        );
        assert_eq!(
            LoadingOperation::FetchingIssues,
            LoadingOperation::FetchingIssues
        );
        assert_eq!(
            LoadingOperation::FetchingIssue { issue_number: 1 },
            LoadingOperation::FetchingIssue { issue_number: 1 }
        );
        assert_eq!(
            LoadingOperation::GeneratingActions { issue_number: 42 },
            LoadingOperation::GeneratingActions { issue_number: 42 }
        );
    }

    // ActionSelectPopupState tests
    #[test]
    fn action_select_popup_state_new() {
        let state = ActionSelectPopupState::new();
        assert_eq!(state.selected(), Some(0));
    }

    #[test]
    fn action_select_popup_state_select_next() {
        let mut state = ActionSelectPopupState::new();
        state.select_next(3);
        assert_eq!(state.selected(), Some(1));
        state.select_next(3);
        assert_eq!(state.selected(), Some(2));
        state.select_next(3);
        assert_eq!(state.selected(), Some(0)); // wraps
    }

    #[test]
    fn action_select_popup_state_select_prev() {
        let mut state = ActionSelectPopupState::new();
        state.select_prev(3);
        assert_eq!(state.selected(), Some(2)); // wraps to end
        state.select_prev(3);
        assert_eq!(state.selected(), Some(1));
    }

    #[test]
    fn action_select_popup_state_empty_list() {
        let mut state = ActionSelectPopupState::new();
        state.select_next(0); // should not panic
        state.select_prev(0); // should not panic
    }

    #[test]
    fn action_select_popup_state_select_next_no_initial_selection() {
        let mut state = ActionSelectPopupState::new();
        // Clear the initial selection
        state.list_state = ListState::default();
        assert_eq!(state.selected(), None);

        state.select_next(3);
        assert_eq!(state.selected(), Some(0));
    }

    #[test]
    fn action_select_popup_state_select_prev_no_initial_selection() {
        let mut state = ActionSelectPopupState::new();
        // Clear the initial selection
        state.list_state = ListState::default();
        assert_eq!(state.selected(), None);

        state.select_prev(3);
        assert_eq!(state.selected(), Some(0));
    }
}