skim 4.0.0

Fuzzy Finder in rust!
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
use std::ops::{Deref, DerefMut};
use std::time::Instant;

use ansi_to_tui::IntoText;
use ratatui::{prelude::*, widgets::Widget};
use unicode_display_width::width as display_width;

use crate::helper::item::strip_ansi;
use crate::tui::BorderType;
use crate::tui::options::TuiLayout;
use crate::tui::statusline::InfoDisplay;
use crate::tui::util::style_line;
use crate::tui::widget::{SkimRender, SkimWidget};
use crate::{SkimOptions, theme::ColorTheme};
use std::sync::Arc;

const SPINNER_DURATION: u32 = 200;
const SPINNERS_UNICODE: [char; 10] = ['', '', '', '', '', '', '', '', '', ''];

/// Status information to display in the input widget's title
#[derive(Clone, Default)]
pub struct StatusInfo {
    /// Total number of items
    pub total: usize,
    /// Number of matched items
    pub matched: usize,
    /// Number of processed items
    pub processed: usize,
    /// Whether the spinner should be shown (controlled by App with debouncing)
    pub show_spinner: bool,
    /// Current matcher mode (e.g., "RE" for regex)
    pub matcher_mode: String,
    /// Whether multi-selection mode is enabled
    pub multi_selection: bool,
    /// Number of selected items
    pub selected: usize,
    /// Index of the current item
    pub current_item_idx: usize,
    /// Horizontal scroll offset
    pub hscroll_offset: i64,
    /// Start time for calculating spinner animation
    pub start: Option<Instant>,
}

impl StatusInfo {
    /// Build the left-aligned title string (spinner, matched/total, mode, progress, selection)
    /// Used for Default info display mode (separate line)
    pub fn left_title(&self) -> String {
        let mut parts = String::new();

        // Spinner
        if self.show_spinner
            && let Some(start) = self.start
        {
            let spinner_elapsed_ms = start.elapsed().as_millis();
            let index = ((spinner_elapsed_ms / (SPINNER_DURATION as u128)) % (SPINNERS_UNICODE.len() as u128)) as usize;
            parts.push(SPINNERS_UNICODE[index]);
            parts.push(' ');
        } else {
            parts.push_str("  ");
        }

        // Matched/total
        parts.push_str(&format!("{}/{}", self.matched, self.total));

        // Matcher mode
        if !self.matcher_mode.is_empty() {
            parts.push_str(&format!("/{}", &self.matcher_mode));
        }

        // Progress percentage
        if self.show_spinner && self.total > 0 && self.processed != self.total {
            let pct = self.processed.saturating_mul(100) / self.total;
            parts.push_str(&format!(" ({}%)", pct));
        }

        // Selection count
        if self.multi_selection && self.selected > 0 {
            parts.push_str(&format!(" [{}]", self.selected));
        }

        parts
    }

    /// Get the inline separator character: spinner when active, '<' otherwise
    /// Used for Inline info display mode
    pub fn inline_separator(&self) -> char {
        if self.show_spinner
            && let Some(start) = self.start
        {
            let spinner_elapsed_ms = start.elapsed().as_millis();
            let index = ((spinner_elapsed_ms / (SPINNER_DURATION as u128)) % (SPINNERS_UNICODE.len() as u128)) as usize;
            SPINNERS_UNICODE[index]
        } else {
            '<'
        }
    }

    /// Build the inline status string (matched/total, mode, progress, selection)
    /// Used for Inline info display mode - does NOT include spinner prefix
    pub fn inline_status(&self) -> String {
        let mut parts = String::new();

        // Matched/total
        parts.push_str(&format!("{}/{}", self.matched, self.total));

        // Matcher mode
        if !self.matcher_mode.is_empty() {
            parts.push_str(&format!("/{}", &self.matcher_mode));
        }

        // Progress percentage
        if self.show_spinner && self.total > 0 {
            let pct = self.processed.saturating_mul(100) / self.total;
            parts.push_str(&format!(" ({}%)", pct));
        }

        // Selection count
        if self.multi_selection && self.selected > 0 {
            parts.push_str(&format!(" [{}]", self.selected));
        }

        parts
    }

    /// Build the right-aligned title string (current index / hscroll)
    pub fn right_title(&self) -> String {
        format!("{}/{}", self.current_item_idx, self.hscroll_offset)
    }
}

pub struct Input {
    pub prompt: String,
    /// see alternate_value
    alternate_prompt: String,
    pub value: String,
    /// cmd query when in normal mode, query when in interactive mode
    alternate_value: String,
    pub cursor_pos: u16,
    pub alternate_cursor_pos: u16,
    pub theme: Arc<ColorTheme>,
    /// Border type, if borders are enabled
    pub border: Option<BorderType>,
    /// Status information to display as the input's title
    pub status_info: Option<StatusInfo>,
    /// How to display the info/status (default, inline, or hidden)
    pub info_display: InfoDisplay,
    /// Whether layout is reversed (status goes below input instead of above)
    pub reverse: bool,
}

impl Default for Input {
    fn default() -> Self {
        Self {
            prompt: String::from(">"),
            alternate_prompt: String::from("c>"),
            value: String::default(),
            alternate_value: String::default(),
            cursor_pos: 0,
            alternate_cursor_pos: 0,
            theme: Arc::new(ColorTheme::default()),
            border: None,
            status_info: None,
            info_display: InfoDisplay::Default,
            reverse: false,
        }
    }
}

impl Input {
    pub fn insert(&mut self, c: char) {
        self.value.insert(self.cursor_pos.into(), c);
        // unwrap: len_utf8 < 4
        self.move_cursor(c.len_utf8().try_into().unwrap());
    }
    pub fn insert_str(&mut self, s: &str) {
        self.value.insert_str(self.cursor_pos as usize, s);
        self.move_cursor(
            s.chars()
                .count()
                .try_into()
                .expect("Failed to fit inserted str len into an i32"),
        );
    }
    fn nchars(&self) -> usize {
        self.value.chars().count()
    }
    pub fn delete(&mut self, offset: i32) -> Option<char> {
        if self.value.is_empty() {
            return None;
        }
        let new_pos = self.cursor_pos as i32 + offset;
        if new_pos < 0 || new_pos as usize >= self.value.len() {
            return None;
        }
        let pos = self.value.floor_char_boundary(new_pos as usize);
        let ch = self.value.remove(pos);
        // Only move cursor if deleting backwards
        if offset < 0 {
            self.move_cursor(-1);
        }
        Some(ch)
    }
    pub fn move_cursor(&mut self, offset: i32) {
        if offset == 0 {
            return;
        }
        if offset < 0 {
            self.move_cursor_to(
                self.value
                    .floor_char_boundary((self.cursor_pos as i32 + offset) as usize) as u16,
            );
        } else {
            self.move_cursor_to(
                self.value
                    .ceil_char_boundary((self.cursor_pos as i32 + offset) as usize) as u16,
            );
        }
    }
    pub fn move_cursor_to(&mut self, pos: u16) {
        if self.value.is_char_boundary(pos as usize) {
            self.cursor_pos = u16::clamp(pos, 0, self.value.len() as u16);
        } else {
            warn!("Invalid cursor pos");
        }
    }
    pub fn move_to_end(&mut self) {
        self.move_cursor_to(
            self.value
                .len()
                .try_into()
                .expect("Failed to fit input len into an u16"),
        )
    }

    /// Check if a character is a word character (alphanumeric only)
    fn is_word_char(ch: char) -> bool {
        ch.is_alphanumeric()
    }

    /// Find the position of the end of the next word (alphanumeric boundaries for deletion)
    fn find_next_word_end(&self, start_pos: usize) -> usize {
        let mut pos = start_pos;

        // Skip any non-word characters
        while pos < self.nchars() {
            let ch = self.value.chars().nth(pos).unwrap();
            if Self::is_word_char(ch) {
                break;
            }
            pos += 1;
        }

        // Skip to the end of the word
        while pos < self.nchars() {
            let ch = self.value.chars().nth(pos).unwrap();
            if !Self::is_word_char(ch) {
                break;
            }
            pos += 1;
        }

        pos
    }

    /// Find the end of compound word (whitespace boundaries for cursor movement)
    fn find_compound_word_end(&self, start_pos: usize) -> usize {
        let mut pos = start_pos;

        // Skip any whitespace
        while pos < self.nchars() {
            let ch = self.value.chars().nth(pos).unwrap();
            if !ch.is_whitespace() {
                break;
            }
            pos += 1;
        }

        // Skip to the end of the non-whitespace sequence (includes punctuation)
        while pos < self.nchars() {
            let ch = self.value.chars().nth(pos).unwrap();
            if ch.is_whitespace() {
                break;
            }
            pos += 1;
        }

        pos
    }

    /// Find the position of the start of the previous word (alphanumeric word boundaries)
    fn find_prev_word_start(&self, start_pos: usize) -> usize {
        if start_pos == 0 {
            return 0;
        }

        let mut pos = start_pos;

        // Move back at least one position
        pos = pos.saturating_sub(1);

        // Skip any non-word characters
        while pos > 0 && !Self::is_word_char(self.value.chars().nth(pos).unwrap()) {
            pos -= 1;
        }

        // Skip to the beginning of the word
        while pos > 0 && Self::is_word_char(self.value.chars().nth(pos - 1).unwrap()) {
            pos -= 1;
        }

        pos
    }

    /// Find the position to delete backward to (stops at non-word characters)
    fn find_delete_backward_pos(&self, start_pos: usize) -> usize {
        if start_pos == 0 {
            return 0;
        }

        let mut pos = start_pos;

        // Skip any non-word characters (whitespace, punctuation, etc.)
        while pos > 0 {
            let ch = self.value.chars().nth(pos - 1).unwrap();
            if Self::is_word_char(ch) {
                break;
            }
            pos -= 1;
        }

        // Skip back through word characters
        while pos > 0 {
            let ch = self.value.chars().nth(pos - 1).unwrap();
            if !Self::is_word_char(ch) {
                break;
            }
            pos -= 1;
        }

        pos
    }

    pub fn delete_backward_word(&mut self) -> String {
        if self.cursor_pos == 0 {
            return String::new();
        }
        // Delete back by alphanumeric word boundaries (for Alt+Backspace)
        let start_pos = self.find_delete_backward_pos(self.cursor_pos as usize);
        let deleted = self.value[start_pos..self.cursor_pos as usize].to_string();
        self.value = format!(
            "{}{}",
            &self.value[..start_pos],
            &self.value[self.cursor_pos as usize..]
        );
        self.cursor_pos = start_pos as u16;
        deleted
    }

    pub fn delete_backward_to_whitespace(&mut self) -> String {
        if self.cursor_pos == 0 {
            return String::new();
        }
        // Unix word rubout: delete back to whitespace (for Ctrl+W)
        let mut pos = self.cursor_pos as usize;

        // Skip any trailing whitespace
        while pos > 0 && self.value.chars().nth(pos - 1).unwrap_or_default().is_whitespace() {
            pos -= 1;
        }

        // Delete back to next whitespace or start
        while pos > 0 && !self.value.chars().nth(pos - 1).unwrap_or_default().is_whitespace() {
            pos -= 1;
        }

        let deleted = self.value[pos..self.cursor_pos as usize].to_string();
        self.value = format!("{}{}", &self.value[..pos], &self.value[self.cursor_pos as usize..]);
        self.cursor_pos = pos as u16;
        deleted
    }

    pub fn delete_forward_word(&mut self) -> String {
        if self.cursor_pos as usize >= self.value.len() {
            return String::new();
        }
        let end_pos = self.find_next_word_end(self.cursor_pos as usize);
        let deleted = self.value[self.cursor_pos as usize..end_pos].to_string();
        self.value = format!("{}{}", &self.value[..self.cursor_pos as usize], &self.value[end_pos..]);
        deleted
    }
    pub fn move_cursor_forward_word(&mut self) {
        let new_pos = self.find_compound_word_end(self.cursor_pos as usize);
        self.cursor_pos = new_pos as u16;
    }

    pub fn move_cursor_backward_word(&mut self) {
        let new_pos = self.find_prev_word_start(self.cursor_pos as usize);
        self.cursor_pos = new_pos as u16;
    }
    pub fn delete_to_beginning(&mut self) -> String {
        let deleted = self.value[..self.cursor_pos as usize].to_string();
        self.value = self.value[self.cursor_pos as usize..].to_string();
        self.cursor_pos = 0;
        deleted
    }
    pub fn cursor_pos(&self) -> u16 {
        (display_width(&self.value[..(self.cursor_pos as usize)]) + display_width(&strip_ansi(&self.prompt).0))
            .try_into()
            .expect("Failed to fit cursor char into an u16")
    }
    pub fn switch_mode(&mut self) {
        std::mem::swap(&mut self.prompt, &mut self.alternate_prompt);
        std::mem::swap(&mut self.value, &mut self.alternate_value);
        std::mem::swap(&mut self.cursor_pos, &mut self.alternate_cursor_pos);
    }
}

impl SkimWidget for Input {
    fn from_options(options: &SkimOptions, theme: Arc<ColorTheme>) -> Self {
        let mut res = Self {
            theme,
            border: options.border,
            info_display: options.info.clone(),
            reverse: options.layout == TuiLayout::Reverse,
            ..Default::default()
        };
        if options.interactive {
            res.prompt = options.cmd_prompt.clone();
            res.alternate_prompt = options.prompt.clone();
            res.value = options.cmd_query.clone().unwrap_or_default();
            res.alternate_value = options.query.clone().unwrap_or_default();
        } else {
            res.prompt = options.prompt.clone();
            res.alternate_prompt = options.cmd_prompt.clone();
            res.value = options.query.clone().unwrap_or_default();
            res.alternate_value = options.cmd_query.clone().unwrap_or_default();
        }
        res.cursor_pos = res.value.len() as u16;
        res.alternate_cursor_pos = res.alternate_value.len() as u16;
        res
    }

    fn render(&mut self, area: Rect, buf: &mut Buffer) -> SkimRender {
        use ratatui::layout::Alignment;
        use ratatui::text::{Line, Span};
        use ratatui::widgets::Paragraph;
        use ratatui::widgets::{Block, Borders};

        let mut line = self
            .prompt
            .into_text()
            .map(|t| t.lines.into_iter().next().unwrap_or_default())
            .unwrap_or_else(|_| Line::from(self.prompt.as_str()));
        style_line(&mut line, self.theme.prompt);
        line.push_span(Span::styled(&self.value, self.theme.query));

        let mut block = Block::default();

        // Add borders if enabled
        if let Some(border_type) = self.border {
            block = block
                .borders(Borders::ALL)
                .border_type(border_type.into())
                .border_style(self.theme.border);
        }

        // Handle different info display modes
        match self.info_display {
            InfoDisplay::Inline => {
                // Inline mode: render status on the same line as input
                // Format: prompt + value + " " + separator_char + " " + status + padding + right_status
                // separator_char is spinner when active, '<' otherwise
                if let Some(ref status) = self.status_info {
                    let separator = status.inline_separator();
                    let inline_status = status.inline_status();
                    let right_status = status.right_title();

                    // Calculate available width for padding
                    // Format: " X " where X is separator (3 chars total)
                    let prompt_width = display_width(&self.prompt);
                    let value_width = display_width(&self.value);
                    let separator_width = 4; // "  X " (2xspace + separator + space)
                    let inline_status_width = display_width(&inline_status);
                    let right_status_width = display_width(&right_status);

                    let used_width =
                        prompt_width + value_width + separator_width + inline_status_width + right_status_width;
                    let available_width = area.width as u64;
                    let padding_width = available_width.saturating_sub(used_width);

                    line.push_span(Span::styled(format!("  {} ", separator), self.theme.info));
                    line.push_span(Span::styled(inline_status, self.theme.info));
                    line.push_span(Span::raw(" ".repeat(padding_width.try_into().unwrap())));
                    line.push_span(Span::styled(right_status, self.theme.info));

                    Paragraph::new(line)
                        .block(block)
                        .style(self.theme.normal)
                        .render(area, buf);
                } else {
                    // No status info, just render input
                    Paragraph::new(line)
                        .block(block)
                        .style(self.theme.normal)
                        .render(area, buf);
                }
            }
            InfoDisplay::Default => {
                // Default mode: render status as block title (separate line)
                // In normal layout: status above input (title_top)
                // In reverse layout: status below input (title_bottom)
                if let Some(ref status) = self.status_info {
                    let left_title = status.left_title();
                    let right_title = status.right_title();

                    if self.reverse {
                        block = block
                            .title_bottom(Line::from(left_title).style(self.theme.info).alignment(Alignment::Left))
                            .title_bottom(
                                Line::from(right_title)
                                    .style(self.theme.info)
                                    .alignment(Alignment::Right),
                            );
                    } else {
                        block = block
                            .title_top(Line::from(left_title).style(self.theme.info).alignment(Alignment::Left))
                            .title_top(
                                Line::from(right_title)
                                    .style(self.theme.info)
                                    .alignment(Alignment::Right),
                            );
                    }
                }

                Paragraph::new(line)
                    .block(block)
                    .style(self.theme.normal)
                    .render(area, buf);
            }
            InfoDisplay::Hidden => {
                // Hidden mode: no status displayed
                Paragraph::new(line)
                    .block(block)
                    .style(self.theme.normal)
                    .render(area, buf);
            }
        }

        SkimRender::default()
    }
}

impl Deref for Input {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl DerefMut for Input {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.value
    }
}