helix-kanban 0.2.25

A terminal-based kanban board with file-based storage, multi-project support, Helix-style keybindings, and built-in MCP server for AI integration
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
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::time::Instant;
use tui_textarea::{CursorMove, TextArea};

// 导入用于渲染的类型
// 注意:tui-textarea 使用自己的 ratatui 版本,我们需要使用兼容的方式
use ratatui::{
    Frame,
    layout::{Alignment, Rect},
    style::{Color as RatatuiColor, Style as RatatuiStyle},
    widgets::Paragraph,
};

/// 编辑模式
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EditMode {
    /// 插入模式 - 直接输入文本
    Insert,
    /// 普通模式 - 导航和命令
    Normal,
    /// 命令模式 - 执行命令 (:w, :q 等)
    Command,
}

/// 输入动作
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputAction {
    /// 继续编辑
    Continue,
    /// 提交内容
    Submit,
    /// 取消对话框
    Cancel,
}

/// Helix 风格的文本输入区域
pub struct HelixTextArea {
    /// 底层 TextArea 组件
    textarea: TextArea<'static>,
    /// 当前编辑模式
    mode: EditMode,
    /// 命令缓冲区(用于 :w, :q 等命令)
    command_buffer: String,
    /// 按键序列缓冲区(用于 dd, yy, gg 等)
    key_sequence: Vec<char>,
    /// 上次按键时间(用于序列超时)
    last_key_time: Instant,
    /// 是否显示行号
    show_line_numbers: bool,
    /// 是否最大化
    is_maximized: bool,
}

impl HelixTextArea {
    /// 创建新的 HelixTextArea
    ///
    /// # 参数
    /// - `initial_value`: 初始文本内容
    /// - `show_line_numbers`: 是否显示行号
    /// - `start_in_normal_mode`: 是否从 Normal 模式开始(true 为 Normal,false 为 Insert)
    pub fn new(initial_value: String, show_line_numbers: bool, start_in_normal_mode: bool) -> Self {
        let mut textarea = if initial_value.is_empty() {
            TextArea::default()
        } else {
            TextArea::from(initial_value.lines().map(|s| s.to_string()))
        };

        // 配置 Nord 主题样式 - tui-textarea 使用 ratatui 的 Style
        textarea.set_style(
            RatatuiStyle::default()
                .fg(RatatuiColor::Rgb(236, 239, 244)) // Nord snow storm
                .bg(RatatuiColor::Rgb(46, 52, 64)),
        ); // Nord polar night

        // 光标样式 - 块状光标
        textarea.set_cursor_style(
            RatatuiStyle::default()
                .bg(RatatuiColor::Rgb(136, 192, 208)) // Nord frost (cyan)
                .fg(RatatuiColor::Rgb(46, 52, 64)),
        );

        // 行号样式
        if show_line_numbers {
            textarea
                .set_line_number_style(RatatuiStyle::default().fg(RatatuiColor::Rgb(76, 86, 106))); // Nord polar night (lighter)
        }

        // 当前行高亮
        textarea.set_cursor_line_style(RatatuiStyle::default().bg(RatatuiColor::Rgb(59, 66, 82))); // Nord polar night (slightly lighter)

        Self {
            textarea,
            mode: if start_in_normal_mode {
                EditMode::Normal // 从 Normal 模式开始
            } else {
                EditMode::Insert // 从 Insert 模式开始
            },
            command_buffer: String::new(),
            key_sequence: Vec::new(),
            last_key_time: Instant::now(),
            show_line_numbers,
            is_maximized: false, // 默认不最大化
        }
    }

    /// 获取当前模式
    #[allow(dead_code)]
    pub fn get_mode(&self) -> EditMode {
        self.mode
    }

    /// 获取是否最大化
    pub fn is_maximized(&self) -> bool {
        self.is_maximized
    }

    /// 切换最大化状态
    pub fn toggle_maximize(&mut self) {
        self.is_maximized = !self.is_maximized;
    }

    /// 获取内容
    pub fn get_content(&self) -> String {
        self.textarea.lines().join("\n")
    }

    /// 处理按键事件
    pub fn handle_key(&mut self, key: KeyEvent) -> InputAction {
        // Ctrl+S 在任何模式下都提交
        if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('s') {
            return InputAction::Submit;
        }

        match self.mode {
            EditMode::Insert => self.handle_insert_mode(key),
            EditMode::Normal => self.handle_normal_mode(key),
            EditMode::Command => self.handle_command_mode(key),
        }
    }

    /// 处理插入模式按键
    fn handle_insert_mode(&mut self, key: KeyEvent) -> InputAction {
        match key.code {
            KeyCode::Esc => {
                // Esc 切换到普通模式
                self.mode = EditMode::Normal;
                InputAction::Continue
            }
            KeyCode::Char(c) => {
                self.textarea.insert_char(c);
                InputAction::Continue
            }
            KeyCode::Enter => {
                self.textarea.insert_newline();
                InputAction::Continue
            }
            KeyCode::Backspace => {
                self.textarea.delete_char();
                InputAction::Continue
            }
            KeyCode::Delete => {
                self.textarea.delete_next_char();
                InputAction::Continue
            }
            KeyCode::Left => {
                self.textarea.move_cursor(CursorMove::Back);
                InputAction::Continue
            }
            KeyCode::Right => {
                self.textarea.move_cursor(CursorMove::Forward);
                InputAction::Continue
            }
            KeyCode::Up => {
                self.textarea.move_cursor(CursorMove::Up);
                InputAction::Continue
            }
            KeyCode::Down => {
                self.textarea.move_cursor(CursorMove::Down);
                InputAction::Continue
            }
            KeyCode::Home => {
                self.textarea.move_cursor(CursorMove::Head);
                InputAction::Continue
            }
            KeyCode::End => {
                self.textarea.move_cursor(CursorMove::End);
                InputAction::Continue
            }
            _ => InputAction::Continue,
        }
    }

    /// 处理普通模式按键
    fn handle_normal_mode(&mut self, key: KeyEvent) -> InputAction {
        // Ctrl+C 取消对话框
        if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') {
            return InputAction::Cancel;
        }

        // 检查按键序列超时(500ms)
        if self.last_key_time.elapsed().as_millis() > 500 {
            self.key_sequence.clear();
        }
        self.last_key_time = Instant::now();

        match key.code {
            // 进入插入模式
            KeyCode::Char('i') => {
                self.mode = EditMode::Insert;
                InputAction::Continue
            }
            KeyCode::Char('a') => {
                self.textarea.move_cursor(CursorMove::Forward);
                self.mode = EditMode::Insert;
                InputAction::Continue
            }
            KeyCode::Char('I') => {
                self.textarea.move_cursor(CursorMove::Head);
                self.mode = EditMode::Insert;
                InputAction::Continue
            }
            KeyCode::Char('A') => {
                self.textarea.move_cursor(CursorMove::End);
                self.mode = EditMode::Insert;
                InputAction::Continue
            }
            KeyCode::Char('o') => {
                self.textarea.move_cursor(CursorMove::End);
                self.textarea.insert_newline();
                self.mode = EditMode::Insert;
                InputAction::Continue
            }
            KeyCode::Char('O') => {
                self.textarea.move_cursor(CursorMove::Head);
                self.textarea.insert_newline();
                self.textarea.move_cursor(CursorMove::Up);
                self.mode = EditMode::Insert;
                InputAction::Continue
            }

            // 移动
            KeyCode::Char('h') | KeyCode::Left => {
                self.textarea.move_cursor(CursorMove::Back);
                InputAction::Continue
            }
            KeyCode::Char('j') | KeyCode::Down => {
                self.textarea.move_cursor(CursorMove::Down);
                InputAction::Continue
            }
            KeyCode::Char('k') | KeyCode::Up => {
                self.textarea.move_cursor(CursorMove::Up);
                InputAction::Continue
            }
            KeyCode::Char('l') | KeyCode::Right => {
                self.textarea.move_cursor(CursorMove::Forward);
                InputAction::Continue
            }
            KeyCode::Char('w') => {
                self.textarea.move_cursor(CursorMove::WordForward);
                InputAction::Continue
            }
            KeyCode::Char('b') => {
                self.textarea.move_cursor(CursorMove::WordBack);
                InputAction::Continue
            }
            KeyCode::Char('e') => {
                self.textarea.move_cursor(CursorMove::WordEnd);
                InputAction::Continue
            }
            KeyCode::Char('0') | KeyCode::Home => {
                self.textarea.move_cursor(CursorMove::Head);
                InputAction::Continue
            }
            KeyCode::Char('$') | KeyCode::End => {
                self.textarea.move_cursor(CursorMove::End);
                InputAction::Continue
            }
            KeyCode::Char('G') => {
                self.textarea.move_cursor(CursorMove::Bottom);
                InputAction::Continue
            }

            // Helix 风格:x 选择当前行,X 扩展到行边界
            // 注意:tui-textarea 不支持可视选择,所以我们用复制整行来模拟
            KeyCode::Char('x') => {
                // 选择当前行(复制整行)
                self.textarea.move_cursor(CursorMove::Head);
                self.textarea.start_selection();
                self.textarea.move_cursor(CursorMove::End);
                InputAction::Continue
            }
            KeyCode::Char('X') => {
                // 扩展选择到行边界(选择整行包括换行符)
                self.textarea.move_cursor(CursorMove::Head);
                self.textarea.start_selection();
                self.textarea.move_cursor(CursorMove::Down);
                self.textarea.move_cursor(CursorMove::Head);
                InputAction::Continue
            }
            KeyCode::Delete => {
                self.textarea.delete_next_char();
                InputAction::Continue
            }
            KeyCode::Backspace => {
                self.textarea.delete_char();
                InputAction::Continue
            }

            // 撤销/重做
            KeyCode::Char('u') => {
                self.textarea.undo();
                InputAction::Continue
            }
            KeyCode::Char('U') => {
                self.textarea.redo();
                InputAction::Continue
            }

            // 复制/粘贴
            KeyCode::Char('p') => {
                self.textarea.paste();
                InputAction::Continue
            }

            // 删除选中的文本
            KeyCode::Char('d') => {
                self.textarea.cut();
                InputAction::Continue
            }

            // 切换最大化(类似 status 的 m 键)
            KeyCode::Char('m') => {
                self.toggle_maximize();
                InputAction::Continue
            }

            // 进入命令模式
            KeyCode::Char(':') => {
                self.mode = EditMode::Command;
                self.command_buffer.clear();
                InputAction::Continue
            }

            // 按键序列处理
            KeyCode::Char(c) => {
                self.key_sequence.push(c);
                self.handle_key_sequence()
            }

            _ => InputAction::Continue,
        }
    }

    /// 处理按键序列(dd, yy, gg 等)
    fn handle_key_sequence(&mut self) -> InputAction {
        match self.key_sequence.as_slice() {
            ['g', 'g'] => {
                self.textarea.move_cursor(CursorMove::Top);
                self.key_sequence.clear();
                InputAction::Continue
            }
            ['g', 'e'] => {
                self.textarea.move_cursor(CursorMove::Bottom);
                self.key_sequence.clear();
                InputAction::Continue
            }
            ['d', 'd'] => {
                self.textarea.delete_line_by_head();
                self.key_sequence.clear();
                InputAction::Continue
            }
            ['c', 'c'] => {
                self.textarea.delete_line_by_head();
                self.mode = EditMode::Insert;
                self.key_sequence.clear();
                InputAction::Continue
            }
            ['y', 'y'] => {
                self.textarea.copy();
                self.key_sequence.clear();
                InputAction::Continue
            }
            _ => {
                // 如果序列不匹配,保持等待或清除
                if self.key_sequence.len() > 2 {
                    self.key_sequence.clear();
                }
                InputAction::Continue
            }
        }
    }

    /// 处理命令模式按键
    fn handle_command_mode(&mut self, key: KeyEvent) -> InputAction {
        match key.code {
            KeyCode::Esc => {
                // Esc 返回普通模式
                self.mode = EditMode::Normal;
                self.command_buffer.clear();
                InputAction::Continue
            }
            KeyCode::Enter => {
                // 执行命令
                let action = self.execute_command();
                self.command_buffer.clear();
                if action != InputAction::Continue {
                    action
                } else {
                    self.mode = EditMode::Normal;
                    InputAction::Continue
                }
            }
            KeyCode::Char(c) => {
                self.command_buffer.push(c);
                InputAction::Continue
            }
            KeyCode::Backspace => {
                self.command_buffer.pop();
                InputAction::Continue
            }
            _ => InputAction::Continue,
        }
    }

    /// 执行命令
    fn execute_command(&mut self) -> InputAction {
        match self.command_buffer.trim() {
            "w" | "write" => InputAction::Submit,
            "q" | "quit" => InputAction::Cancel,
            "wq" | "x" => InputAction::Submit,
            "q!" => InputAction::Cancel,
            _ => InputAction::Continue,
        }
    }

    /// 渲染文本区域
    pub fn render(&mut self, f: &mut Frame, area: Rect) {
        // 如果显示行号,启用行号
        if self.show_line_numbers {
            self.textarea
                .set_line_number_style(RatatuiStyle::default().fg(RatatuiColor::Rgb(76, 86, 106)));
        }

        // 渲染 TextArea - 直接传递引用
        f.render_widget(&self.textarea, area);
    }

    /// 渲染模式指示器
    pub fn render_mode_indicator(&self, f: &mut Frame, area: Rect) {
        let (mode_text, style) = match self.mode {
            EditMode::Insert => (
                "-- INSERT --".to_string(),
                RatatuiStyle::default().fg(RatatuiColor::Rgb(163, 190, 140)), // Nord green
            ),
            EditMode::Normal => (
                "-- NORMAL --".to_string(),
                RatatuiStyle::default().fg(RatatuiColor::Rgb(136, 192, 208)), // Nord cyan
            ),
            EditMode::Command => {
                let text = format!(":{}", self.command_buffer);
                (
                    text,
                    RatatuiStyle::default().fg(RatatuiColor::Rgb(235, 203, 139)), // Nord yellow
                )
            }
        };

        // 所有模式指示器都左对齐
        let paragraph = Paragraph::new(mode_text)
            .style(style)
            .alignment(Alignment::Left);
        f.render_widget(paragraph, area);
    }

    /// 渲染按键序列提示
    #[allow(dead_code)]
    pub fn render_key_sequence(&self, f: &mut Frame, area: Rect) {
        if !self.key_sequence.is_empty() && self.mode == EditMode::Normal {
            let text = self.key_sequence.iter().collect::<String>();
            let paragraph = Paragraph::new(text)
                .style(RatatuiStyle::default().fg(RatatuiColor::Rgb(235, 203, 139))) // Nord yellow
                .alignment(Alignment::Right);
            f.render_widget(paragraph, area);
        }
    }
}