helix-kanban 0.2.2

A terminal-based kanban board with file-based storage, multi-project support, and Helix-style keybindings
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
use ratatui::{
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Wrap},
    Frame,
};

/// 确认操作类型
#[derive(Debug, Clone, PartialEq)]
pub enum ConfirmAction {
    DeleteTask,
    DeleteProject,
    HideProject,
}

/// 对话框类型
#[derive(Debug, Clone, PartialEq)]
pub enum DialogType {
    /// 输入对话框(用于创建项目、任务等)
    Input {
        title: String,
        prompt: String,
        value: String,
        cursor_pos: usize,
    },
    /// 选择对话框(用于选择项目等)
    Select {
        title: String,
        items: Vec<String>,
        selected: usize,
        filter: String,
    },
    /// 确认对话框
    Confirm {
        title: String,
        message: String,
        yes_selected: bool,
        action: ConfirmAction,  // 添加操作类型
    },
}

/// 渲染居中的对话框
pub fn render_dialog(f: &mut Frame, dialog: &DialogType) {
    // 渲染半透明背景遮罩
    render_backdrop(f, f.area());

    let area = centered_rect(60, 50, f.area());

    // 清空对话框区域
    f.render_widget(Clear, area);

    match dialog {
        DialogType::Input {
            title,
            prompt,
            value,
            cursor_pos,
        } => render_input_dialog(f, area, title, prompt, value, *cursor_pos),
        DialogType::Select {
            title,
            items,
            selected,
            filter,
        } => render_select_dialog(f, area, title, items, *selected, filter),
        DialogType::Confirm {
            title,
            message,
            yes_selected,
            ..
        } => render_confirm_dialog(f, area, title, message, *yes_selected),
    }
}

/// 渲染半透明背景遮罩
fn render_backdrop(f: &mut Frame, area: Rect) {
    let block = Block::default().style(Style::default().bg(Color::Rgb(0, 0, 0))); // 黑色背景
    f.render_widget(block, area);
}

/// 渲染输入对话框
fn render_input_dialog(
    f: &mut Frame,
    area: Rect,
    title: &str,
    prompt: &str,
    value: &str,
    cursor_pos: usize,
) {
    // 判断是否是任务输入(需要更大的输入框)
    let is_task_input = title.contains("任务");

    let block = Block::default()
        .title(format!("  {}  ", title))
        .title_alignment(Alignment::Left)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Rgb(76, 86, 106)))  // Nord border color
        .border_type(ratatui::widgets::BorderType::Rounded)
        .style(Style::default().bg(Color::Rgb(46, 52, 64)));  // Nord background

    let inner = block.inner(area);
    f.render_widget(block, area);

    // 分割内部区域 - 任务输入使用更大的输入框
    let chunks = if is_task_input {
        Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(2),  // 提示文本
                Constraint::Min(10),    // 大输入框(多行)
                Constraint::Length(3),  // 帮助文本
            ])
            .split(inner)
    } else {
        Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(2), // 提示文本
                Constraint::Length(3), // 普通输入框
                Constraint::Length(2), // 帮助文本
            ])
            .split(inner)
    };

    // 提示文本
    let prompt_text = if is_task_input {
        Paragraph::new(format!("{}\n(支持多行输入,包含任务的所有内容)", prompt))
            .style(Style::default().fg(Color::Rgb(129, 161, 193)))  // Nord frost color
    } else {
        Paragraph::new(prompt).style(Style::default().fg(Color::Rgb(129, 161, 193)))
    };
    f.render_widget(prompt_text, chunks[0]);

    // 输入框
    let input_block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Rgb(136, 192, 208)))  // Nord cyan
        .border_type(ratatui::widgets::BorderType::Rounded);

    let input_inner = input_block.inner(chunks[1]);
    f.render_widget(input_block, chunks[1]);

    // 渲染输入内容和光标(支持多行)
    let chars: Vec<char> = value.chars().collect();
    let input_with_cursor = if cursor_pos >= chars.len() {
        // 光标在末尾
        format!("{}_", value)
    } else {
        // 光标在中间
        let mut display_chars = chars.clone();
        display_chars.insert(cursor_pos, '|');
        display_chars.into_iter().collect()
    };

    let input_text = Paragraph::new(input_with_cursor)
        .wrap(Wrap { trim: false });  // 支持自动换行
    f.render_widget(input_text, input_inner);

    // 帮助文本
    let help_text = if is_task_input {
        "Ctrl+J: 换行 | Enter: 确认 | ESC: 取消"
    } else {
        "Enter: 确认 | ESC: 取消"
    };

    let help = Paragraph::new(help_text)
        .style(Style::default().fg(Color::DarkGray))
        .alignment(Alignment::Center);
    f.render_widget(help, chunks[2]);
}

/// 渲染选择对话框
fn render_select_dialog(
    f: &mut Frame,
    area: Rect,
    title: &str,
    items: &[String],
    selected: usize,
    filter: &str,
) {
    let block = Block::default()
        .title(format!("  {}  ", title))
        .title_alignment(Alignment::Left)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Rgb(76, 86, 106)))  // Nord border color
        .border_type(ratatui::widgets::BorderType::Rounded)
        .style(Style::default().bg(Color::Rgb(46, 52, 64)));  // Nord background

    let inner = block.inner(area);
    f.render_widget(block, area);

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // 搜索框
            Constraint::Min(0),    // 列表
            Constraint::Length(1), // 帮助
        ])
        .split(inner);

    // 渲染搜索框
    let search_text = if filter.is_empty() {
        "🔍 输入搜索...".to_string()
    } else {
        format!("🔍 {}", filter)
    };

    let search_style = if filter.is_empty() {
        Style::default().fg(Color::Rgb(129, 161, 193))  // 灰色提示
    } else {
        Style::default().fg(Color::Rgb(136, 192, 208))  // 高亮搜索文本
    };

    let search_block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Rgb(136, 192, 208)))
        .border_type(ratatui::widgets::BorderType::Rounded);

    let search_inner = search_block.inner(chunks[0]);
    f.render_widget(search_block, chunks[0]);

    let search_paragraph = Paragraph::new(search_text)
        .style(search_style);
    f.render_widget(search_paragraph, search_inner);

    // 过滤项目列表
    let filtered_items: Vec<_> = if filter.is_empty() {
        items.iter().enumerate().collect()
    } else {
        items
            .iter()
            .enumerate()
            .filter(|(_, item)| item.to_lowercase().contains(&filter.to_lowercase()))
            .collect()
    };

    // 列表项 - 支持多行显示,添加分隔线
    let list_items: Vec<ListItem> = filtered_items
        .iter()
        .flat_map(|(idx, item)| {
            let is_selected = *idx == selected;

            // 分割成多行
            let lines: Vec<&str> = item.lines().collect();
            let main_line = lines.get(0).unwrap_or(&"");
            let sub_line = lines.get(1);

            let mut content_lines = vec![];

            // 第一项上方添加空行(顶部间距)
            if *idx == 0 {
                content_lines.push(Line::from(""));
            }

            if is_selected {
                // 选中项:蓝色背景,带序号
                let sequence_num = format!("{}", *idx + 1);

                content_lines.push(Line::from(vec![
                    Span::raw("  "),
                    Span::styled(
                        sequence_num,
                        Style::default()
                            .fg(Color::White)
                            .bg(Color::Rgb(94, 129, 172))  // 蓝色序号标记
                            .add_modifier(Modifier::BOLD),
                    ),
                    Span::raw("  "),
                    Span::styled(
                        *main_line,
                        Style::default().fg(Color::White).add_modifier(Modifier::BOLD),
                    ),
                    Span::raw("  "),
                    Span::styled("", Style::default().fg(Color::Rgb(163, 190, 140))),  // 绿色勾
                    Span::raw("  "),
                    Span::styled(
                        "Enter",
                        Style::default()
                            .fg(Color::Black)
                            .bg(Color::Rgb(136, 192, 208)),
                    ),
                ]));
            } else {
                // 未选中项:正常显示
                content_lines.push(Line::from(format!("      {}", main_line)));
            }

            // 添加子行(路径)
            if let Some(sub) = sub_line {
                let sub_style = if is_selected {
                    Style::default().fg(Color::Rgb(216, 222, 233))
                } else {
                    Style::default().fg(Color::Rgb(129, 161, 193))
                };
                content_lines.push(Line::from(vec![
                    Span::styled(*sub, sub_style),
                ]));
            }

            // 添加分隔线(除了最后一项)
            if *idx < filtered_items.len() - 1 {
                content_lines.push(Line::from(vec![
                    Span::styled(
                        "  ────────────────────────────────────────────────────────",
                        Style::default().fg(Color::Rgb(76, 86, 106)),  // 灰色分隔线
                    ),
                ]));
            }

            let style = if is_selected {
                Style::default()
                    .bg(Color::Rgb(59, 66, 82))  // Nord 深蓝背景
            } else {
                Style::default()
            };

            vec![ListItem::new(content_lines).style(style)]
        })
        .collect();

    let list = List::new(list_items);

    // 创建 ListState 以支持滚动
    let mut list_state = ratatui::widgets::ListState::default();

    // 在过滤后的项目中找到当前选中项的索引
    let filtered_selected = filtered_items.iter()
        .position(|(idx, _)| *idx == selected)
        .unwrap_or(0);

    list_state.select(Some(filtered_selected));

    f.render_stateful_widget(list, chunks[1], &mut list_state);

    // 帮助文本 - 简化提示(搜索框已经在顶部显示)
    let help_text = format!("↑↓ 导航  Enter 确认  Esc 取消  [{}/{}]", filtered_items.len(), items.len());
    let help_paragraph = Paragraph::new(help_text)
        .style(Style::default().fg(Color::Rgb(129, 161, 193)))  // Nord frost color
        .alignment(Alignment::Center);
    f.render_widget(help_paragraph, chunks[2]);

    // 右上角显示计数
    let count_text = format!("{}/{}", filtered_items.len(), items.len());
    let count_area = Rect {
        x: area.x + area.width.saturating_sub(count_text.len() as u16 + 3),
        y: area.y,
        width: count_text.len() as u16 + 2,
        height: 1,
    };
    let count_paragraph = Paragraph::new(count_text)
        .style(Style::default().fg(Color::Rgb(129, 161, 193)));
    f.render_widget(count_paragraph, count_area);
}

/// 渲染确认对话框
fn render_confirm_dialog(
    f: &mut Frame,
    area: Rect,
    title: &str,
    message: &str,
    yes_selected: bool,
) {
    let block = Block::default()
        .title(format!("  {}  ", title))
        .title_alignment(Alignment::Left)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Rgb(235, 203, 139)))  // Nord yellow for warnings
        .border_type(ratatui::widgets::BorderType::Rounded)
        .style(Style::default().bg(Color::Rgb(46, 52, 64)));  // Nord background

    let inner = block.inner(area);
    f.render_widget(block, area);

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Min(0),    // 消息
            Constraint::Length(3), // 按钮
        ])
        .split(inner);

    // 消息文本
    let message_text = Paragraph::new(message)
        .wrap(Wrap { trim: true })
        .alignment(Alignment::Center)
        .style(Style::default().fg(Color::Rgb(216, 222, 233)));  // Nord snow storm
    f.render_widget(message_text, chunks[0]);

    // 按钮区域 - 添加快捷键提示
    let button_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage(25),
            Constraint::Percentage(25),
            Constraint::Percentage(25),
            Constraint::Percentage(25),
        ])
        .split(chunks[1]);

    // 否按钮 (n) - 放在左侧
    let no_style = if !yes_selected {
        Style::default()
            .bg(Color::Rgb(191, 97, 106))   // Nord 柔和红色
            .fg(Color::Rgb(46, 52, 64))      // Nord 深色背景
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default()
            .fg(Color::Rgb(191, 97, 106))
            .add_modifier(Modifier::DIM)
    };
    let no_button = Paragraph::new("[ n ] 否")
        .style(no_style)
        .alignment(Alignment::Center);
    f.render_widget(no_button, button_chunks[1]);

    // 是按钮 (y) - 放在右侧
    let yes_style = if yes_selected {
        Style::default()
            .bg(Color::Rgb(163, 190, 140))  // Nord 柔和绿色
            .fg(Color::Rgb(46, 52, 64))      // Nord 深色背景
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default()
            .fg(Color::Rgb(163, 190, 140))
            .add_modifier(Modifier::DIM)
    };
    let yes_button = Paragraph::new("[ y ] 是")
        .style(yes_style)
        .alignment(Alignment::Center);
    f.render_widget(yes_button, button_chunks[2]);
}

/// 创建一个居中的矩形区域
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
    let popup_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage((100 - percent_y) / 2),
            Constraint::Percentage(percent_y),
            Constraint::Percentage((100 - percent_y) / 2),
        ])
        .split(r);

    Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage((100 - percent_x) / 2),
            Constraint::Percentage(percent_x),
            Constraint::Percentage((100 - percent_x) / 2),
        ])
        .split(popup_layout[1])[1]
}