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
//! Nika Intro Widget - ASCII art logo that explodes into matrix rain
//!
//! A startup animation showing "NIKA" in ASCII art, then exploding
//! into matrix rain drops for a dramatic effect.
//!
//! ```text
//! Phase 1: ASCII art appears (fade in)
//!    ███╗   ██╗██╗██╗  ██╗ █████╗
//!    ████╗  ██║██║██║ ██╔╝██╔══██╗
//!    ██╔██╗ ██║██║█████╔╝ ███████║
//!    ██║╚██╗██║██║██╔═██╗ ██╔══██║
//!    ██║ ╚████║██║██║  ██╗██║  ██║
//!    ╚═╝  ╚═══╝╚═╝╚═╝  ╚═╝╚═╝  ╚═╝
//!
//! Phase 2: Letters explode into particles
//! Phase 3: Particles become matrix rain
//! ```

use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Style},
    widgets::Widget,
};

use crate::tui::theme::solarized;

// ═══════════════════════════════════════════════════════════════════════════════
// ASCII ART
// ═══════════════════════════════════════════════════════════════════════════════

/// NIKA ASCII art logo (6 lines, ~40 chars wide)
const NIKA_ASCII: &[&str] = &[
    "███╗   ██╗██╗██╗  ██╗ █████╗ ",
    "████╗  ██║██║██║ ██╔╝██╔══██╗",
    "██╔██╗ ██║██║█████╔╝ ███████║",
    "██║╚██╗██║██║██╔═██╗ ██╔══██║",
    "██║ ╚████║██║██║  ██╗██║  ██║",
    "╚═╝  ╚═══╝╚═╝╚═╝  ╚═╝╚═╝  ╚═╝",
];

/// Alternative smaller logo for compact terminals
const NIKA_SMALL: &[&str] = &["╔╗╔ ╦ ╦╔═ ╔═╗", "║║║ ║ ╠╩╗ ╠═╣", "╝╚╝ ╩ ╩ ╩ ╩ ╩"];

/// Particle characters for explosion effect
const EXPLOSION_CHARS: &[char] = &[
    '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
    '', '', '', '', '', '', '', '',
];

/// Explosion colors (bright to faded)
const EXPLOSION_COLORS: &[Color] = &[
    solarized::CYAN,
    solarized::GREEN,
    solarized::BLUE,
    solarized::VIOLET,
    solarized::MAGENTA,
    solarized::YELLOW,
    solarized::ORANGE,
];

// ═══════════════════════════════════════════════════════════════════════════════
// PARTICLE
// ═══════════════════════════════════════════════════════════════════════════════

#[derive(Clone)]
struct Particle {
    x: f32,
    y: f32,
    vx: f32,
    vy: f32,
    char: char,
    color: Color,
    life: f32, // 1.0 = full, 0.0 = dead
}

// ═══════════════════════════════════════════════════════════════════════════════
// INTRO STATE
// ═══════════════════════════════════════════════════════════════════════════════

/// Animation phases
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum IntroPhase {
    /// Logo fading in
    FadeIn,
    /// Logo visible, waiting
    Hold,
    /// Logo exploding into particles
    Explode,
    /// Particles falling as rain
    Rain,
    /// Animation complete
    Done,
}

/// Nika intro animation state
pub struct NikaIntroState {
    pub phase: IntroPhase,
    pub frame: u16,
    pub opacity: f32,
    particles: Vec<Particle>,
    rng: SmallRng,
}

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

impl NikaIntroState {
    pub fn new() -> Self {
        Self {
            phase: IntroPhase::FadeIn,
            frame: 0,
            opacity: 0.0,
            particles: Vec::new(),
            rng: SmallRng::seed_from_u64(42),
        }
    }

    /// Check if intro animation is complete
    pub fn is_done(&self) -> bool {
        self.phase == IntroPhase::Done
    }

    /// Tick animation (call at 10Hz)
    pub fn tick(&mut self, area: Rect) {
        self.frame = self.frame.wrapping_add(1);

        match self.phase {
            IntroPhase::FadeIn => {
                self.opacity += 0.08; // ~1.2s fade in
                if self.opacity >= 1.0 {
                    self.opacity = 1.0;
                    self.phase = IntroPhase::Hold;
                    self.frame = 0; // Reset for hold timing
                }
            }
            IntroPhase::Hold => {
                // Hold for ~0.5s (5 frames at 10Hz)
                if self.frame > 5 {
                    self.phase = IntroPhase::Explode;
                    self.frame = 0; // Reset for explode timing
                    self.spawn_particles(area);
                }
            }
            IntroPhase::Explode => {
                self.update_particles();
                // Transition to rain after particles settle (~1.5s)
                if self.frame > 15 {
                    self.phase = IntroPhase::Rain;
                    self.frame = 0; // Reset for rain timing
                }
            }
            IntroPhase::Rain => {
                self.update_particles();
                self.opacity -= 0.04; // Fade out particles
                if self.opacity <= 0.0 || self.frame > 50 {
                    self.phase = IntroPhase::Done;
                }
            }
            IntroPhase::Done => {}
        }
    }

    /// Spawn explosion particles from logo position
    fn spawn_particles(&mut self, area: Rect) {
        let logo = NIKA_ASCII;
        let logo_width = logo[0].chars().count() as u16;
        let logo_height = logo.len() as u16;

        // Center position
        let start_x = (area.width.saturating_sub(logo_width)) / 2;
        let start_y = (area.height.saturating_sub(logo_height)) / 2;

        // Create particles from each character position
        for (row, line) in logo.iter().enumerate() {
            for (col, ch) in line.chars().enumerate() {
                if ch != ' ' && ch != '' && ch != '' && ch != '' {
                    let x = start_x as f32 + col as f32;
                    let y = start_y as f32 + row as f32;

                    // Random explosion velocity
                    let angle: f32 = self.rng.gen_range(0.0..std::f32::consts::TAU);
                    let speed: f32 = self.rng.gen_range(0.5..2.5);
                    let vx = angle.cos() * speed;
                    let vy = angle.sin() * speed + 0.5; // Slight downward bias

                    let color_idx = self.rng.gen_range(0..EXPLOSION_COLORS.len());
                    let char_idx = self.rng.gen_range(0..EXPLOSION_CHARS.len());

                    self.particles.push(Particle {
                        x,
                        y,
                        vx,
                        vy,
                        char: EXPLOSION_CHARS[char_idx],
                        color: EXPLOSION_COLORS[color_idx],
                        life: 1.0,
                    });
                }
            }
        }
    }

    /// Update particle physics
    fn update_particles(&mut self) {
        for p in &mut self.particles {
            p.x += p.vx;
            p.y += p.vy;
            p.vy += 0.15; // Gravity
            p.vx *= 0.98; // Air resistance
            p.life -= 0.02;

            // Randomly change character for glitchy effect
            if self.rng.gen::<f32>() < 0.1 {
                let idx = self.rng.gen_range(0..EXPLOSION_CHARS.len());
                p.char = EXPLOSION_CHARS[idx];
            }
        }

        // Remove dead particles
        self.particles.retain(|p| p.life > 0.0);
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// WIDGET
// ═══════════════════════════════════════════════════════════════════════════════

/// Nika intro animation widget
pub struct NikaIntro<'a> {
    state: &'a NikaIntroState,
}

impl<'a> NikaIntro<'a> {
    pub fn new(state: &'a NikaIntroState) -> Self {
        Self { state }
    }
}

impl Widget for NikaIntro<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        if area.width == 0 || area.height == 0 {
            return;
        }

        // Fill background first to ensure clean slate
        // Without this, random terminal artifacts could show through
        let bg_style = Style::default().bg(solarized::BASE03);
        buf.set_style(area, bg_style);

        match self.state.phase {
            IntroPhase::FadeIn | IntroPhase::Hold => {
                // Render ASCII logo centered
                let logo = if area.width >= 40 {
                    NIKA_ASCII
                } else {
                    NIKA_SMALL
                };
                let logo_width = logo[0].chars().count() as u16;
                let logo_height = logo.len() as u16;

                let start_x = area.x + (area.width.saturating_sub(logo_width)) / 2;
                let start_y = area.y + (area.height.saturating_sub(logo_height)) / 2;

                let opacity = self.state.opacity;
                let color = apply_opacity(solarized::CYAN, opacity);

                for (row, line) in logo.iter().enumerate() {
                    let y = start_y + row as u16;
                    if y < area.y + area.height {
                        buf.set_string(start_x, y, line, Style::default().fg(color));
                    }
                }

                // Add 🦋 decorations
                if opacity > 0.5 {
                    let butterfly = "🦋";
                    // Top left
                    if start_x > 4 && start_y > 1 {
                        buf.set_string(
                            start_x - 4,
                            start_y - 1,
                            butterfly,
                            Style::default().fg(solarized::MAGENTA),
                        );
                    }
                    // Top right
                    if start_x + logo_width + 2 < area.x + area.width {
                        buf.set_string(
                            start_x + logo_width + 2,
                            start_y - 1,
                            butterfly,
                            Style::default().fg(solarized::VIOLET),
                        );
                    }
                    // Bottom center
                    if start_y + logo_height < area.y + area.height {
                        buf.set_string(
                            start_x + logo_width / 2,
                            start_y + logo_height,
                            butterfly,
                            Style::default().fg(solarized::CYAN),
                        );
                    }
                }
            }
            IntroPhase::Explode | IntroPhase::Rain => {
                // Render particles
                for p in &self.state.particles {
                    let x = p.x as u16;
                    let y = p.y as u16;

                    if x >= area.x
                        && x < area.x + area.width
                        && y >= area.y
                        && y < area.y + area.height
                    {
                        let color = apply_opacity(p.color, p.life * self.state.opacity);
                        buf.set_string(x, y, p.char.to_string(), Style::default().fg(color));
                    }
                }
            }
            IntroPhase::Done => {}
        }
    }
}

/// Apply opacity to a color
fn apply_opacity(color: Color, opacity: f32) -> Color {
    match color {
        Color::Rgb(r, g, b) => Color::Rgb(
            (r as f32 * opacity) as u8,
            (g as f32 * opacity) as u8,
            (b as f32 * opacity) as u8,
        ),
        c => c,
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════════════════════

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

    #[test]
    fn test_intro_state_new() {
        let state = NikaIntroState::new();
        assert_eq!(state.phase, IntroPhase::FadeIn);
        assert_eq!(state.frame, 0);
        assert!((state.opacity - 0.0).abs() < 0.01);
    }

    #[test]
    fn test_intro_phase_progression() {
        let mut state = NikaIntroState::new();
        let area = Rect::new(0, 0, 80, 24);

        // Tick through fade in (~13 ticks at 0.08 opacity per tick)
        for _ in 0..13 {
            state.tick(area);
        }
        assert_eq!(state.phase, IntroPhase::Hold);

        // Tick through hold (>5 frames)
        for _ in 0..6 {
            state.tick(area);
        }
        assert_eq!(state.phase, IntroPhase::Explode);
    }

    #[test]
    fn test_intro_is_done() {
        let mut state = NikaIntroState::new();
        let area = Rect::new(0, 0, 80, 24);

        assert!(!state.is_done());

        // Fast forward to done
        for _ in 0..100 {
            state.tick(area);
        }
        assert!(state.is_done());
    }

    #[test]
    fn test_particle_spawning() {
        let mut state = NikaIntroState::new();
        let area = Rect::new(0, 0, 80, 24);

        // Get to explode phase
        state.phase = IntroPhase::Hold;
        state.frame = 13;
        state.tick(area);

        assert!(!state.particles.is_empty());
    }

    #[test]
    fn test_render_empty_area() {
        let state = NikaIntroState::new();
        let intro = NikaIntro::new(&state);
        let area = Rect::new(0, 0, 0, 0);
        let mut buf = Buffer::empty(area);
        intro.render(area, &mut buf);
        // Should not panic
    }
}