mkui 0.1.0

A minimalist, typography-driven TUI library with Kitty graphics support
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
//! Command palette component - Vim-style command line
//!
//! Provides a command input interface for:
//! - Ex commands (`:`)
//! - Forward search (`/`)
//! - Backward search (`?`)
//! - Shell commands (`!`)
//!
//! Features:
//! - Command history with navigation
//! - Tab completion support
//! - Prompt indicator based on mode

use crate::component::Component;
use crate::components::text_input::TextInput;
use crate::context::RenderContext;
use crate::event::{Event, EventHandler, Key};
use crate::layout::Rect;
use crate::render::Renderer;
use anyhow::Result;

/// Command mode determines the prompt character and behavior
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommandMode {
    /// Ex command mode (`:`)
    Ex,
    /// Forward search (`/`)
    Search,
    /// Backward search (`?`)
    SearchBack,
    /// Shell command (`!`)
    Shell,
}

impl CommandMode {
    /// Get the prompt character for this mode
    pub fn prompt(&self) -> &'static str {
        match self {
            CommandMode::Ex => ":",
            CommandMode::Search => "/",
            CommandMode::SearchBack => "?",
            CommandMode::Shell => "!",
        }
    }

    /// Get the mode name
    pub fn name(&self) -> &'static str {
        match self {
            CommandMode::Ex => "Ex",
            CommandMode::Search => "Search",
            CommandMode::SearchBack => "SearchBack",
            CommandMode::Shell => "Shell",
        }
    }
}

/// Result of command execution
#[derive(Debug, Clone)]
pub enum CommandResult {
    /// Command executed successfully
    Success(Option<String>),
    /// Command execution failed
    Error(String),
    /// Command not found
    NotFound,
    /// Empty command (no-op)
    Empty,
}

/// Trait for command execution
///
/// Implement this trait to handle commands from the palette.
pub trait CommandExecutor {
    /// Execute a command string
    ///
    /// Returns the result of execution.
    fn execute(&mut self, command: &str, mode: CommandMode) -> CommandResult;

    /// Get completions for a partial command
    ///
    /// Returns a list of possible completions.
    fn complete(&self, partial: &str, mode: CommandMode) -> Vec<String>;
}

/// Command palette component
///
/// A Vim-style command line that sits at the bottom of the screen.
pub struct CommandPalette {
    /// Text input for command entry
    input: TextInput,
    /// Current command mode
    mode: CommandMode,
    /// Command history
    history: Vec<String>,
    /// Current position in history (None = new command)
    history_index: Option<usize>,
    /// Maximum history size
    max_history: usize,
    /// Current completions
    completions: Vec<String>,
    /// Current completion index
    completion_index: Option<usize>,
    /// Whether the palette is active/visible
    active: bool,
    /// Last error message
    last_error: Option<String>,
    /// Last message (success feedback)
    last_message: Option<String>,
    /// Component dirty flag
    dirty: bool,
    /// Saved input before history navigation
    saved_input: Option<String>,
}

impl CommandPalette {
    /// Create a new command palette
    pub fn new() -> Self {
        CommandPalette {
            input: TextInput::new(":"),
            mode: CommandMode::Ex,
            history: Vec::new(),
            history_index: None,
            max_history: 100,
            completions: Vec::new(),
            completion_index: None,
            active: false,
            last_error: None,
            last_message: None,
            dirty: true,
            saved_input: None,
        }
    }

    /// Activate the command palette with the given mode
    pub fn activate(&mut self, mode: CommandMode) {
        self.mode = mode;
        self.input = TextInput::new(mode.prompt());
        self.input.on_focus();
        self.active = true;
        self.history_index = None;
        self.completions.clear();
        self.completion_index = None;
        self.last_error = None;
        self.saved_input = None;
        self.dirty = true;
    }

    /// Deactivate the command palette
    pub fn deactivate(&mut self) {
        self.active = false;
        self.input.on_blur();
        self.input.clear();
        self.completions.clear();
        self.completion_index = None;
        self.saved_input = None;
        self.dirty = true;
    }

    /// Check if the palette is active
    pub fn is_active(&self) -> bool {
        self.active
    }

    /// Get current command mode
    pub fn mode(&self) -> CommandMode {
        self.mode
    }

    /// Get current input value
    pub fn value(&self) -> &str {
        self.input.value()
    }

    /// Get last error message
    pub fn last_error(&self) -> Option<&str> {
        self.last_error.as_deref()
    }

    /// Get last success message
    pub fn last_message(&self) -> Option<&str> {
        self.last_message.as_deref()
    }

    /// Clear last error
    pub fn clear_error(&mut self) {
        self.last_error = None;
        self.dirty = true;
    }

    /// Clear last message
    pub fn clear_message(&mut self) {
        self.last_message = None;
        self.dirty = true;
    }

    /// Set error message
    pub fn set_error(&mut self, error: impl Into<String>) {
        self.last_error = Some(error.into());
        self.dirty = true;
    }

    /// Set success message
    pub fn set_message(&mut self, message: impl Into<String>) {
        self.last_message = Some(message.into());
        self.dirty = true;
    }

    /// Execute the current command
    ///
    /// Returns the command string that should be executed.
    /// The actual execution is handled by the parent component.
    pub fn submit(&mut self) -> Option<String> {
        let command = self.input.value().to_string();

        if command.is_empty() {
            self.deactivate();
            return None;
        }

        // Add to history if different from last entry
        if self.history.last().map(|s| s.as_str()) != Some(&command) {
            self.history.push(command.clone());
            if self.history.len() > self.max_history {
                self.history.remove(0);
            }
        }

        self.deactivate();
        Some(command)
    }

    /// Cancel input and deactivate
    pub fn cancel(&mut self) {
        self.deactivate();
    }

    /// Navigate history up (older)
    fn history_prev(&mut self) {
        if self.history.is_empty() {
            return;
        }

        // Save current input when starting history navigation
        if self.history_index.is_none() {
            self.saved_input = Some(self.input.value().to_string());
        }

        match self.history_index {
            None => {
                self.history_index = Some(self.history.len() - 1);
                self.input.set_value(&self.history[self.history.len() - 1]);
            }
            Some(0) => {
                // Already at oldest entry
            }
            Some(idx) => {
                self.history_index = Some(idx - 1);
                self.input.set_value(&self.history[idx - 1]);
            }
        }
        self.dirty = true;
    }

    /// Navigate history down (newer)
    fn history_next(&mut self) {
        match self.history_index {
            None => {
                // Already at newest (current input)
            }
            Some(idx) if idx >= self.history.len() - 1 => {
                // Restore saved input
                self.history_index = None;
                if let Some(saved) = self.saved_input.take() {
                    self.input.set_value(&saved);
                } else {
                    self.input.clear();
                }
            }
            Some(idx) => {
                self.history_index = Some(idx + 1);
                self.input.set_value(&self.history[idx + 1]);
            }
        }
        self.dirty = true;
    }

    /// Update completions based on current input
    pub fn update_completions<E: CommandExecutor>(&mut self, executor: &E) {
        let partial = self.input.value();
        self.completions = executor.complete(partial, self.mode);
        self.completion_index = None;
        self.dirty = true;
    }

    /// Cycle to next completion
    fn complete_next(&mut self) {
        if self.completions.is_empty() {
            return;
        }

        match self.completion_index {
            None => {
                self.completion_index = Some(0);
                self.input.set_value(&self.completions[0]);
            }
            Some(idx) => {
                let next = (idx + 1) % self.completions.len();
                self.completion_index = Some(next);
                self.input.set_value(&self.completions[next]);
            }
        }
        self.dirty = true;
    }

    /// Cycle to previous completion
    fn complete_prev(&mut self) {
        if self.completions.is_empty() {
            return;
        }

        match self.completion_index {
            None => {
                let last = self.completions.len() - 1;
                self.completion_index = Some(last);
                self.input.set_value(&self.completions[last]);
            }
            Some(0) => {
                let last = self.completions.len() - 1;
                self.completion_index = Some(last);
                self.input.set_value(&self.completions[last]);
            }
            Some(idx) => {
                self.completion_index = Some(idx - 1);
                self.input.set_value(&self.completions[idx - 1]);
            }
        }
        self.dirty = true;
    }

    /// Get number of completions available
    pub fn completion_count(&self) -> usize {
        self.completions.len()
    }

    /// Get current completions
    pub fn completions(&self) -> &[String] {
        &self.completions
    }
}

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

impl EventHandler for CommandPalette {
    fn handle_event(&mut self, event: &Event) -> bool {
        if !self.active {
            return false;
        }

        match event {
            Event::Key(key) => match key {
                // Submit
                Key::Enter => {
                    // Mark as consumed - parent should call submit() to get the command
                    true
                }

                // Cancel
                Key::Esc => {
                    self.cancel();
                    true
                }

                // History navigation
                Key::Up => {
                    self.history_prev();
                    true
                }
                Key::Down => {
                    self.history_next();
                    true
                }

                // Completion
                Key::Tab => {
                    self.complete_next();
                    true
                }
                Key::BackTab => {
                    self.complete_prev();
                    true
                }

                // Ctrl+P/N for history (vi-style)
                Key::Ctrl('p') => {
                    self.history_prev();
                    true
                }
                Key::Ctrl('n') => {
                    self.history_next();
                    true
                }

                // Delegate to text input
                _ => {
                    let handled = self.input.handle_event(event);
                    if handled {
                        // Clear completions when input changes
                        self.completions.clear();
                        self.completion_index = None;
                    }
                    handled
                }
            },

            // Delegate paste to input
            Event::Paste(_) => self.input.handle_event(event),

            _ => false,
        }
    }

    fn on_focus(&mut self) {
        self.input.on_focus();
    }

    fn on_blur(&mut self) {
        self.input.on_blur();
    }
}

impl Component for CommandPalette {
    fn render(&mut self, renderer: &mut Renderer, bounds: Rect, ctx: &RenderContext) -> Result<()> {
        if !self.active {
            // When inactive, show last message or error if any
            if let Some(error) = &self.last_error {
                renderer.move_cursor(bounds.x, bounds.y)?;
                renderer.write_styled(error, "\x1b[31m")?; // Red
            } else if let Some(msg) = &self.last_message {
                renderer.move_cursor(bounds.x, bounds.y)?;
                renderer.write_text(msg)?;
            }
            self.dirty = false;
            return Ok(());
        }

        // Render the text input
        self.input.render(renderer, bounds, ctx)?;

        self.dirty = false;
        Ok(())
    }

    fn min_size(&self) -> (u16, u16) {
        (20, 1)
    }

    fn mark_dirty(&mut self) {
        self.dirty = true;
        self.input.mark_dirty();
    }

    fn is_dirty(&self) -> bool {
        self.dirty || self.input.is_dirty()
    }

    fn name(&self) -> &str {
        "CommandPalette"
    }
}

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

    struct MockExecutor;

    impl CommandExecutor for MockExecutor {
        fn execute(&mut self, _command: &str, _mode: CommandMode) -> CommandResult {
            CommandResult::Success(None)
        }

        fn complete(&self, partial: &str, _mode: CommandMode) -> Vec<String> {
            let commands = vec!["write", "quit", "wq", "help", "set"];
            commands
                .into_iter()
                .filter(|c| c.starts_with(partial))
                .map(|s| s.to_string())
                .collect()
        }
    }

    #[test]
    fn test_command_palette_creation() {
        let palette = CommandPalette::new();
        assert!(!palette.is_active());
        assert_eq!(palette.mode(), CommandMode::Ex);
    }

    #[test]
    fn test_activate_deactivate() {
        let mut palette = CommandPalette::new();

        palette.activate(CommandMode::Search);
        assert!(palette.is_active());
        assert_eq!(palette.mode(), CommandMode::Search);

        palette.deactivate();
        assert!(!palette.is_active());
    }

    #[test]
    fn test_mode_prompts() {
        assert_eq!(CommandMode::Ex.prompt(), ":");
        assert_eq!(CommandMode::Search.prompt(), "/");
        assert_eq!(CommandMode::SearchBack.prompt(), "?");
        assert_eq!(CommandMode::Shell.prompt(), "!");
    }

    #[test]
    fn test_history_navigation() {
        let mut palette = CommandPalette::new();

        // Add some history
        palette.activate(CommandMode::Ex);
        palette.input.set_value("cmd1");
        palette.submit();

        palette.activate(CommandMode::Ex);
        palette.input.set_value("cmd2");
        palette.submit();

        palette.activate(CommandMode::Ex);
        palette.input.set_value("cmd3");
        palette.submit();

        // Navigate history
        palette.activate(CommandMode::Ex);
        palette.history_prev();
        assert_eq!(palette.value(), "cmd3");

        palette.history_prev();
        assert_eq!(palette.value(), "cmd2");

        palette.history_next();
        assert_eq!(palette.value(), "cmd3");
    }

    #[test]
    fn test_completion() {
        let mut palette = CommandPalette::new();
        let executor = MockExecutor;

        palette.activate(CommandMode::Ex);
        palette.input.set_value("w");
        palette.update_completions(&executor);

        assert_eq!(palette.completions(), &["write", "wq"]);

        palette.complete_next();
        assert_eq!(palette.value(), "write");

        palette.complete_next();
        assert_eq!(palette.value(), "wq");

        palette.complete_prev();
        assert_eq!(palette.value(), "write");
    }
}