rnk 0.15.31

A React-like declarative terminal UI framework for Rust, inspired by Ink
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
//! Cursor component for blinking text cursor
//!
//! Provides a customizable blinking cursor for text input fields.

use crate::components::Text;
use crate::core::{Color, Element};

/// Cursor shape variants
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CursorShape {
    /// Block cursor (█)
    #[default]
    Block,
    /// Underline cursor (_)
    Underline,
    /// Bar/Line cursor (|)
    Bar,
    /// Custom character
    Custom(char),
}

impl CursorShape {
    /// Get the character representation of the cursor
    pub fn char(&self) -> char {
        match self {
            CursorShape::Block => '',
            CursorShape::Underline => '_',
            CursorShape::Bar => '',
            CursorShape::Custom(c) => *c,
        }
    }
}

/// Cursor style configuration
#[derive(Debug, Clone)]
pub struct CursorStyle {
    /// Cursor shape
    pub shape: CursorShape,
    /// Cursor color
    pub color: Option<Color>,
    /// Whether cursor blinks
    pub blink: bool,
    /// Blink interval in milliseconds
    pub blink_interval_ms: u64,
}

impl Default for CursorStyle {
    fn default() -> Self {
        Self {
            shape: CursorShape::Block,
            color: None,
            blink: true,
            blink_interval_ms: 530,
        }
    }
}

impl CursorStyle {
    /// Create a new cursor style
    pub fn new() -> Self {
        Self::default()
    }

    /// Set cursor shape
    pub fn shape(mut self, shape: CursorShape) -> Self {
        self.shape = shape;
        self
    }

    /// Set cursor color
    pub fn color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    /// Set whether cursor blinks
    pub fn blink(mut self, blink: bool) -> Self {
        self.blink = blink;
        self
    }

    /// Set blink interval in milliseconds
    pub fn blink_interval(mut self, ms: u64) -> Self {
        self.blink_interval_ms = ms;
        self
    }

    /// Block cursor style
    pub fn block() -> Self {
        Self::default().shape(CursorShape::Block)
    }

    /// Underline cursor style
    pub fn underline() -> Self {
        Self::default().shape(CursorShape::Underline)
    }

    /// Bar cursor style
    pub fn bar() -> Self {
        Self::default().shape(CursorShape::Bar)
    }

    /// Smooth blinking cursor style (faster blink for fade effect)
    ///
    /// Creates a cursor optimized for smooth fade animations.
    /// Use with `CursorState::opacity()` for smooth transitions.
    pub fn smooth() -> Self {
        Self::new()
            .shape(CursorShape::Block)
            .blink(true)
            .blink_interval(400)
    }
}

/// Cursor state for managing blink animation
#[derive(Debug, Clone)]
pub struct CursorState {
    /// Whether cursor is currently visible (for blink animation)
    visible: bool,
    /// Whether cursor is active/focused
    active: bool,
    /// Style configuration
    style: CursorStyle,
    /// Last toggle timestamp (for blink timing)
    last_toggle_ms: u64,
}

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

impl CursorState {
    /// Create a new cursor state
    pub fn new() -> Self {
        Self {
            visible: true,
            active: true,
            style: CursorStyle::default(),
            last_toggle_ms: 0,
        }
    }

    /// Create cursor state with custom style
    pub fn with_style(style: CursorStyle) -> Self {
        Self {
            visible: true,
            active: true,
            style,
            last_toggle_ms: 0,
        }
    }

    /// Check if cursor is visible
    pub fn is_visible(&self) -> bool {
        self.visible && self.active
    }

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

    /// Set cursor active state
    pub fn set_active(&mut self, active: bool) {
        self.active = active;
        if active {
            self.visible = true;
        }
    }

    /// Toggle visibility (for blink animation)
    pub fn toggle_visibility(&mut self) {
        if self.style.blink {
            self.visible = !self.visible;
        }
    }

    /// Update blink state based on elapsed time
    pub fn update(&mut self, current_time_ms: u64) {
        if !self.style.blink || !self.active {
            return;
        }

        let elapsed = current_time_ms.saturating_sub(self.last_toggle_ms);
        if elapsed >= self.style.blink_interval_ms {
            self.visible = !self.visible;
            self.last_toggle_ms = current_time_ms;
        }
    }

    /// Reset cursor to visible state
    pub fn reset(&mut self) {
        self.visible = true;
        self.last_toggle_ms = 0;
    }

    /// Get the cursor style
    pub fn style(&self) -> &CursorStyle {
        &self.style
    }

    /// Set the cursor style
    pub fn set_style(&mut self, style: CursorStyle) {
        self.style = style;
    }

    /// Get cursor character
    pub fn char(&self) -> char {
        self.style.shape.char()
    }

    /// Get opacity for smooth animation (0.0 - 1.0)
    ///
    /// Returns a value between 0.0 and 1.0 based on the current blink cycle.
    /// Use this for smooth fade animations instead of binary on/off blinking.
    ///
    /// # Example
    ///
    /// ```
    /// use rnk::components::{CursorState, CursorStyle};
    ///
    /// let mut state = CursorState::with_style(CursorStyle::smooth());
    /// state.update(200); // Update with current time
    /// let opacity = state.opacity(200);
    /// // opacity will be between 0.0 and 1.0
    /// ```
    pub fn opacity(&self, current_time_ms: u64) -> f32 {
        if !self.style.blink || !self.active {
            return 1.0;
        }

        let interval = self.style.blink_interval_ms as f32;
        let full_cycle = interval * 2.0;
        let cycle_position = (current_time_ms % (full_cycle as u64)) as f32;

        // Smooth sine wave for fade effect
        // First half: fade out (1.0 -> 0.0)
        // Second half: fade in (0.0 -> 1.0)
        if cycle_position < interval {
            // Fade out
            1.0 - (cycle_position / interval)
        } else {
            // Fade in
            (cycle_position - interval) / interval
        }
    }

    /// Get smooth opacity using cosine interpolation
    ///
    /// Similar to `opacity()` but uses cosine for smoother transitions.
    pub fn smooth_opacity(&self, current_time_ms: u64) -> f32 {
        if !self.style.blink || !self.active {
            return 1.0;
        }

        let interval = self.style.blink_interval_ms as f32;
        let full_cycle = interval * 2.0;
        let cycle_position = (current_time_ms % (full_cycle as u64)) as f32;

        // Use cosine for smooth easing
        let t = cycle_position / full_cycle;
        (1.0 + (t * std::f32::consts::PI * 2.0).cos()) / 2.0
    }
}

/// Cursor component for rendering
#[derive(Debug, Clone)]
pub struct Cursor<'a> {
    state: &'a CursorState,
    /// Override character to show when cursor is hidden
    placeholder: Option<char>,
}

impl<'a> Cursor<'a> {
    /// Create a new cursor component
    pub fn new(state: &'a CursorState) -> Self {
        Self {
            state,
            placeholder: None,
        }
    }

    /// Set placeholder character when cursor is hidden
    pub fn placeholder(mut self, ch: char) -> Self {
        self.placeholder = Some(ch);
        self
    }

    /// Render the cursor to a string
    pub fn render(&self) -> String {
        if self.state.is_visible() {
            let ch = self.state.char();
            if let Some(color) = &self.state.style.color {
                format!("{}{}\x1b[0m", color.to_ansi_fg(), ch)
            } else {
                ch.to_string()
            }
        } else if let Some(placeholder) = self.placeholder {
            placeholder.to_string()
        } else {
            " ".to_string()
        }
    }

    /// Convert to Element
    pub fn into_element(self) -> Element {
        let ch = if self.state.is_visible() {
            self.state.char().to_string()
        } else if let Some(placeholder) = self.placeholder {
            placeholder.to_string()
        } else {
            " ".to_string()
        };

        let mut text = Text::new(ch);
        if let Some(color) = &self.state.style.color {
            text = text.color(*color);
        }
        text.into_element()
    }
}

/// Create a simple cursor with default settings
pub fn cursor(state: &CursorState) -> Cursor<'_> {
    Cursor::new(state)
}

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

    #[test]
    fn test_cursor_shape() {
        assert_eq!(CursorShape::Block.char(), '');
        assert_eq!(CursorShape::Underline.char(), '_');
        assert_eq!(CursorShape::Bar.char(), '');
        assert_eq!(CursorShape::Custom('').char(), '');
    }

    #[test]
    fn test_cursor_style_builder() {
        let style = CursorStyle::new()
            .shape(CursorShape::Bar)
            .color(Color::Cyan)
            .blink(false)
            .blink_interval(1000);

        assert_eq!(style.shape, CursorShape::Bar);
        assert_eq!(style.color, Some(Color::Cyan));
        assert!(!style.blink);
        assert_eq!(style.blink_interval_ms, 1000);
    }

    #[test]
    fn test_cursor_style_presets() {
        let block = CursorStyle::block();
        assert_eq!(block.shape, CursorShape::Block);

        let underline = CursorStyle::underline();
        assert_eq!(underline.shape, CursorShape::Underline);

        let bar = CursorStyle::bar();
        assert_eq!(bar.shape, CursorShape::Bar);
    }

    #[test]
    fn test_cursor_state_visibility() {
        let mut state = CursorState::new();
        assert!(state.is_visible());

        state.toggle_visibility();
        assert!(!state.is_visible());

        state.toggle_visibility();
        assert!(state.is_visible());
    }

    #[test]
    fn test_cursor_state_active() {
        let mut state = CursorState::new();
        assert!(state.is_active());

        state.set_active(false);
        assert!(!state.is_active());
        assert!(!state.is_visible());

        state.set_active(true);
        assert!(state.is_active());
        assert!(state.is_visible());
    }

    #[test]
    fn test_cursor_state_update() {
        let style = CursorStyle::new().blink_interval(100);
        let mut state = CursorState::with_style(style);

        assert!(state.is_visible());

        // Simulate time passing
        state.update(150);
        assert!(!state.is_visible());

        state.update(300);
        assert!(state.is_visible());
    }

    #[test]
    fn test_cursor_state_no_blink() {
        let style = CursorStyle::new().blink(false);
        let mut state = CursorState::with_style(style);

        assert!(state.is_visible());
        state.update(1000);
        assert!(state.is_visible()); // Should stay visible
    }

    #[test]
    fn test_cursor_render() {
        let state = CursorState::new();
        let cursor = Cursor::new(&state);
        assert_eq!(cursor.render(), "");
    }

    #[test]
    fn test_cursor_render_hidden() {
        let mut state = CursorState::new();
        state.toggle_visibility();

        let cursor = Cursor::new(&state);
        assert_eq!(cursor.render(), " ");

        let cursor_with_placeholder = Cursor::new(&state).placeholder('_');
        assert_eq!(cursor_with_placeholder.render(), "_");
    }

    #[test]
    fn test_cursor_render_with_color() {
        let style = CursorStyle::new().color(Color::Cyan);
        let state = CursorState::with_style(style);
        let cursor = Cursor::new(&state);
        let rendered = cursor.render();
        assert!(rendered.contains(""));
        assert!(rendered.contains("\x1b["));
    }

    #[test]
    fn test_cursor_reset() {
        let mut state = CursorState::new();
        state.toggle_visibility();
        assert!(!state.is_visible());

        state.reset();
        assert!(state.is_visible());
    }

    #[test]
    fn test_cursor_style_smooth() {
        let style = CursorStyle::smooth();
        assert_eq!(style.shape, CursorShape::Block);
        assert!(style.blink);
        assert_eq!(style.blink_interval_ms, 400);
    }

    #[test]
    fn test_cursor_opacity() {
        let style = CursorStyle::new().blink(true).blink_interval(100);
        let state = CursorState::with_style(style);

        // At t=0, opacity should be 1.0 (start of fade out)
        let opacity = state.opacity(0);
        assert!((opacity - 1.0).abs() < 0.01);

        // At t=50, opacity should be around 0.5 (middle of fade out)
        let opacity = state.opacity(50);
        assert!((opacity - 0.5).abs() < 0.01);

        // At t=100, opacity should be 0.0 (end of fade out)
        let opacity = state.opacity(100);
        assert!(opacity < 0.01);

        // At t=150, opacity should be around 0.5 (middle of fade in)
        let opacity = state.opacity(150);
        assert!((opacity - 0.5).abs() < 0.01);
    }

    #[test]
    fn test_cursor_opacity_no_blink() {
        let style = CursorStyle::new().blink(false);
        let state = CursorState::with_style(style);

        // Should always return 1.0 when blink is disabled
        assert_eq!(state.opacity(0), 1.0);
        assert_eq!(state.opacity(500), 1.0);
        assert_eq!(state.opacity(1000), 1.0);
    }

    #[test]
    fn test_cursor_smooth_opacity() {
        let style = CursorStyle::smooth();
        let state = CursorState::with_style(style);

        // Smooth opacity should be between 0.0 and 1.0
        let opacity = state.smooth_opacity(200);
        assert!(opacity >= 0.0 && opacity <= 1.0);
    }
}