nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM 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
//! Tree widget renderer with icons, colors, and animations.
//!
//! Provides a full tree view widget that renders TreeNodes with:
//! - Proper indentation and tree lines
//! - NodeKind-based icons and colors
//! - Selection highlighting
//! - Glow animation for ecosystem files
//! - Git status indicators

use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, StatefulWidget, Widget},
};

use super::{
    animation::AnimationTicker, colors::TreeColors, filter::FilterConfig, NodeKind, TreeNode,
    TreeState,
};

/// Tree branch characters (VS Code-style box-drawing)
pub mod chars {
    /// Vertical line (indent guide)
    pub const VERTICAL: &str = "";
    /// Branch (tee) - middle child
    pub const BRANCH: &str = "";
    /// Last branch (corner) - last child
    pub const LAST: &str = "";
    /// Horizontal line (connector)
    pub const HORIZONTAL: &str = "";
    /// Collapsed folder arrow (Nerd Font)
    pub const COLLAPSED: &str = "";
    /// Expanded folder arrow (Nerd Font)
    pub const EXPANDED: &str = "";
    /// Collapsed folder arrow (fallback)
    pub const COLLAPSED_FALLBACK: &str = "";
    /// Expanded folder arrow (fallback)
    pub const EXPANDED_FALLBACK: &str = "";
    /// Spacer (empty indent)
    #[allow(dead_code)]
    pub const SPACER: &str = "  ";
}

/// Tree widget for rendering file trees
pub struct TreeWidget<'a> {
    /// Root node to render
    root: &'a TreeNode,
    /// Block wrapper
    block: Option<Block<'a>>,
    /// Color palette
    colors: TreeColors,
    /// Animation ticker for glow effects
    ticker: Option<&'a AnimationTicker>,
    /// Filter configuration
    filter: FilterConfig,
    /// Highlight style for selected items
    highlight_style: Style,
    /// Whether to use NerdFont icons
    use_nerd_icons: bool,
}

impl<'a> TreeWidget<'a> {
    /// Create a new tree widget
    pub fn new(root: &'a TreeNode) -> Self {
        Self {
            root,
            block: None,
            colors: TreeColors::default(),
            ticker: None,
            filter: FilterConfig::default(),
            highlight_style: Style::default()
                .add_modifier(Modifier::BOLD)
                .add_modifier(Modifier::REVERSED),
            use_nerd_icons: false,
        }
    }

    /// Set the block wrapper
    pub fn block(mut self, block: Block<'a>) -> Self {
        self.block = Some(block);
        self
    }

    /// Set the color palette
    pub fn colors(mut self, colors: TreeColors) -> Self {
        self.colors = colors;
        self
    }

    /// Set the animation ticker for glow effects
    pub fn ticker(mut self, ticker: &'a AnimationTicker) -> Self {
        self.ticker = Some(ticker);
        self
    }

    /// Set the filter configuration
    pub fn filter(mut self, filter: FilterConfig) -> Self {
        self.filter = filter;
        self
    }

    /// Set the highlight style
    pub fn highlight_style(mut self, style: Style) -> Self {
        self.highlight_style = style;
        self
    }

    /// Enable NerdFont icons
    pub fn nerd_icons(mut self, enabled: bool) -> Self {
        self.use_nerd_icons = enabled;
        self
    }

    /// Flatten the tree into visible lines
    fn flatten_visible(&self, node: &TreeNode, state: &TreeState) -> Vec<FlatNode> {
        let mut result = Vec::new();
        self.flatten_recursive(node, state, &mut result, &[], true);
        result
    }

    fn flatten_recursive(
        &self,
        node: &TreeNode,
        state: &TreeState,
        result: &mut Vec<FlatNode>,
        ancestors: &[bool], // true = last child at each level
        is_visible: bool,
    ) {
        // Check filter
        let is_hidden = node.is_hidden();
        if !self.filter.is_visible(&node.kind, is_hidden) {
            return;
        }

        if is_visible {
            result.push(FlatNode {
                id: node.id,
                name: node.name.clone(),
                kind: node.kind,
                depth: node.depth,
                is_directory: node.is_directory(),
                is_expanded: state.is_expanded(node.id),
                is_last: ancestors.last().copied().unwrap_or(true),
                ancestors: ancestors.to_vec(),
                git_status: node.git_status,
            });
        }

        // Recurse into children if expanded
        if node.is_directory() && state.is_expanded(node.id) {
            let child_count = node.children.len();
            for (i, child) in node.children.iter().enumerate() {
                let is_last_child = i == child_count - 1;
                let mut child_ancestors = ancestors.to_vec();
                child_ancestors.push(is_last_child);
                self.flatten_recursive(child, state, result, &child_ancestors, true);
            }
        }
    }

    /// Render a single node line
    fn render_line(&self, node: &FlatNode, is_selected: bool, width: u16) -> Line<'static> {
        let mut spans = Vec::new();

        // Build indent with tree lines
        for (i, &is_last_at_level) in node.ancestors.iter().enumerate() {
            if i == node.ancestors.len() - 1 {
                // Last level shows branch/corner
                if is_last_at_level {
                    spans.push(Span::styled(
                        chars::LAST,
                        Style::default().fg(self.colors.branch_lines),
                    ));
                } else {
                    spans.push(Span::styled(
                        chars::BRANCH,
                        Style::default().fg(self.colors.branch_lines),
                    ));
                }
                spans.push(Span::styled(
                    chars::HORIZONTAL,
                    Style::default().fg(self.colors.branch_lines),
                ));
            } else {
                // Inner levels show vertical lines or space
                if is_last_at_level {
                    spans.push(Span::raw("  "));
                } else {
                    spans.push(Span::styled(
                        chars::VERTICAL,
                        Style::default().fg(self.colors.indent_guide),
                    ));
                    spans.push(Span::raw(" "));
                }
            }
        }

        // Extra padding after tree lines for cleaner VS Code-like spacing
        spans.push(Span::raw(" "));

        // Folder expand/collapse indicator
        if node.is_directory {
            let indicator = if node.is_expanded {
                if self.use_nerd_icons {
                    chars::EXPANDED
                } else {
                    chars::EXPANDED_FALLBACK
                }
            } else if self.use_nerd_icons {
                chars::COLLAPSED
            } else {
                chars::COLLAPSED_FALLBACK
            };
            spans.push(Span::styled(
                indicator,
                Style::default().fg(self.colors.directory),
            ));
            spans.push(Span::raw(" "));
        } else {
            // File - add spacing to align with folder indicators (chevron width)
            spans.push(Span::raw("  "));
        }

        // Icon
        let icon = if self.use_nerd_icons {
            node.kind.nerd_icon()
        } else {
            node.kind.icon()
        };

        // Get base color
        let is_hidden = node.kind.is_hidden();
        let base_color = self.colors.node_color(node.kind, is_hidden);

        // Apply glow for ecosystem files on selection
        let icon_style = if node.kind.is_ecosystem() && is_selected {
            if let Some(ticker) = self.ticker {
                let glow = ticker.glow_factor();
                // Modulate color brightness
                let icon_color = self.colors.icon_color(node.kind);
                // Create glowing style
                Style::default()
                    .fg(interpolate_color(icon_color, Color::White, glow - 0.7))
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default()
                    .fg(self.colors.icon_color(node.kind))
                    .add_modifier(Modifier::BOLD)
            }
        } else {
            Style::default().fg(self.colors.icon_color(node.kind))
        };

        spans.push(Span::styled(format!("{} ", icon), icon_style));

        // Name
        let name_style = if is_selected {
            self.highlight_style.fg(base_color)
        } else {
            Style::default().fg(base_color)
        };

        // Truncate name if needed (UTF-8 safe)
        let max_name_len = width
            .saturating_sub(spans.iter().map(|s| s.width()).sum::<usize>() as u16 + 2)
            as usize;
        let name = if node.name.chars().count() > max_name_len {
            // Use chars() for UTF-8 safe truncation
            let truncate_at = max_name_len.saturating_sub(3);
            let truncated: String = node.name.chars().take(truncate_at).collect();
            format!("{}...", truncated)
        } else {
            node.name.clone()
        };

        spans.push(Span::styled(name, name_style));

        // Git status badge
        if let Some(git_status) = node.git_status {
            let (badge, color) = match git_status {
                super::GitStatus::Modified => ("M", self.colors.git_modified),
                super::GitStatus::Added => ("A", self.colors.git_added),
                super::GitStatus::Deleted => ("D", self.colors.git_deleted),
                super::GitStatus::Untracked => ("?", self.colors.git_untracked),
                super::GitStatus::Conflict => ("!", Color::Red),
                super::GitStatus::Ignored => ("", self.colors.hidden),
                super::GitStatus::Clean => ("", self.colors.hidden),
            };
            if !badge.is_empty() {
                spans.push(Span::raw(" "));
                spans.push(Span::styled(badge, Style::default().fg(color)));
            }
        }

        Line::from(spans)
    }
}

/// Flattened node for rendering
#[derive(Debug, Clone)]
struct FlatNode {
    id: u64,
    name: String,
    kind: NodeKind,
    #[allow(dead_code)]
    depth: usize,
    is_directory: bool,
    is_expanded: bool,
    #[allow(dead_code)]
    is_last: bool,
    ancestors: Vec<bool>,
    git_status: Option<super::GitStatus>,
}

/// Interpolate between two colors
fn interpolate_color(from: Color, to: Color, factor: f32) -> Color {
    let factor = factor.clamp(0.0, 1.0);

    match (from, to) {
        (Color::Rgb(r1, g1, b1), Color::Rgb(r2, g2, b2)) => {
            let r = (r1 as f32 + (r2 as f32 - r1 as f32) * factor) as u8;
            let g = (g1 as f32 + (g2 as f32 - g1 as f32) * factor) as u8;
            let b = (b1 as f32 + (b2 as f32 - b1 as f32) * factor) as u8;
            Color::Rgb(r, g, b)
        }
        _ => from, // Fallback for non-RGB colors
    }
}

impl StatefulWidget for TreeWidget<'_> {
    type State = TreeState;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        // Render block if present
        let inner = if let Some(ref block) = self.block {
            let inner = block.inner(area);
            block.clone().render(area, buf);
            inner
        } else {
            area
        };

        // Update viewport height
        state.set_viewport_height(inner.height as usize);

        // Flatten visible nodes
        let flat_nodes = self.flatten_visible(self.root, state);

        // Get scroll offset
        let scroll_offset = state.scroll_offset();

        // Render visible lines
        for (i, node) in flat_nodes
            .iter()
            .skip(scroll_offset)
            .take(inner.height as usize)
            .enumerate()
        {
            let y = inner.y + i as u16;
            let is_selected = state.is_selected(node.id);
            let line = self.render_line(node, is_selected, inner.width);

            // Render line
            buf.set_line(inner.x, y, &line, inner.width);

            // Full row highlight for selected item
            if is_selected {
                for x in inner.x..(inner.x + inner.width) {
                    if let Some(cell) = buf.cell_mut((x, y)) {
                        cell.set_style(cell.style().add_modifier(Modifier::REVERSED));
                    }
                }
            }
        }

        // Show scroll indicator if there are more items
        let total = flat_nodes.len();
        let visible = inner.height as usize;
        if total > visible && scroll_offset > 0 {
            // Top scroll indicator
            if let Some(cell) = buf.cell_mut((inner.x + inner.width - 1, inner.y)) {
                cell.set_char('');
                cell.set_fg(self.colors.fg);
            }
        }
        if total > visible && scroll_offset + visible < total {
            // Bottom scroll indicator
            if let Some(cell) =
                buf.cell_mut((inner.x + inner.width - 1, inner.y + inner.height - 1))
            {
                cell.set_char('');
                cell.set_fg(self.colors.fg);
            }
        }
    }
}

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

    fn create_test_tree() -> TreeNode {
        TreeNode {
            id: 1,
            name: "root".to_string(),
            path: Utf8PathBuf::from("."),
            kind: NodeKind::Directory,
            git_status: None,
            children: vec![
                TreeNode {
                    id: 2,
                    name: "workflow.nika.yaml".to_string(),
                    path: Utf8PathBuf::from("./workflow.nika.yaml"),
                    kind: NodeKind::NikaWorkflow,
                    git_status: Some(super::super::GitStatus::Modified),
                    children: vec![],
                    expanded: false,
                    depth: 1,
                },
                TreeNode {
                    id: 3,
                    name: "src".to_string(),
                    path: Utf8PathBuf::from("./src"),
                    kind: NodeKind::SrcFolder,
                    git_status: None,
                    children: vec![TreeNode {
                        id: 4,
                        name: "main.rs".to_string(),
                        path: Utf8PathBuf::from("./src/main.rs"),
                        kind: NodeKind::Rust,
                        git_status: None,
                        children: vec![],
                        expanded: false,
                        depth: 2,
                    }],
                    expanded: false,
                    depth: 1,
                },
            ],
            expanded: true,
            depth: 0,
        }
    }

    #[test]
    fn test_tree_widget_new() {
        let root = create_test_tree();
        let widget = TreeWidget::new(&root);
        assert!(!widget.use_nerd_icons);
    }

    #[test]
    fn test_tree_widget_with_options() {
        let root = create_test_tree();
        let colors = TreeColors::solarized_light();
        let widget = TreeWidget::new(&root).colors(colors).nerd_icons(true);
        assert!(widget.use_nerd_icons);
    }

    #[test]
    fn test_flatten_visible() {
        let root = create_test_tree();
        let mut state = TreeState::new();
        state.expand(1); // Expand root

        let widget = TreeWidget::new(&root);
        let flat = widget.flatten_visible(&root, &state);

        // root + workflow + src
        assert_eq!(flat.len(), 3);
        assert_eq!(flat[0].name, "root");
        assert_eq!(flat[1].name, "workflow.nika.yaml");
        assert_eq!(flat[2].name, "src");
    }

    #[test]
    fn test_flatten_with_nested_expand() {
        let root = create_test_tree();
        let mut state = TreeState::new();
        state.expand(1); // Expand root
        state.expand(3); // Expand src

        let widget = TreeWidget::new(&root);
        let flat = widget.flatten_visible(&root, &state);

        // root + workflow + src + main.rs
        assert_eq!(flat.len(), 4);
        assert_eq!(flat[3].name, "main.rs");
    }

    #[test]
    fn test_interpolate_color() {
        let white = Color::Rgb(255, 255, 255);
        let black = Color::Rgb(0, 0, 0);

        // Midpoint should be gray
        let mid = interpolate_color(black, white, 0.5);
        if let Color::Rgb(r, g, b) = mid {
            assert!(r > 100 && r < 150);
            assert!(g > 100 && g < 150);
            assert!(b > 100 && b < 150);
        }

        // Factor 0 should return from
        let zero = interpolate_color(black, white, 0.0);
        assert_eq!(zero, black);

        // Factor 1 should return to
        let one = interpolate_color(black, white, 1.0);
        assert_eq!(one, white);
    }

    #[test]
    fn test_filter_affects_visibility() {
        let root = create_test_tree();
        let mut state = TreeState::new();
        state.expand(1);

        // With workflow filter
        let mut filter = FilterConfig::new();
        filter.filter = super::super::filter::TreeFilter::WorkflowsOnly;

        let widget = TreeWidget::new(&root).filter(filter);
        let flat = widget.flatten_visible(&root, &state);

        // Only directories and workflow files pass
        // root (dir) + workflow.nika.yaml + src (dir)
        assert!(flat.iter().any(|n| n.name == "workflow.nika.yaml"));
    }
}