tunein-cli 0.7.1

Browse and listen to thousands of radio stations across the globe right from your terminal 🌎 📻 🎵✨
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
//! `/` fuzzy finder: a centered, fzf-style modal for searching stations.
//!
//! The popup owns only view state — the query, the candidate corpus it was
//! handed, and the fuzzy-ranked selection. The host ([`crate::interactive`])
//! feeds it fresh candidates from the provider as the query changes and acts
//! on whatever the user submits with `enter`.

use std::collections::HashSet;

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::{
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph},
    Frame,
};

use crate::theme;
use crate::types::Station;

/// Braille spinner frames shown while a provider search is in flight.
const SPINNER: [char; 8] = ['', '', '', '', '', '', '', ''];

/// What the host should do after the popup has handled a key.
pub enum FzfOutcome {
    /// The popup was not open; the key belongs to the host.
    Ignored,
    /// The key was handled; nothing else to do.
    Consumed,
    /// The query text changed — the host should (re)run its search.
    QueryChanged,
    /// The user picked a station with `enter`.
    Submit(Station),
    /// The user dismissed the popup with `esc`.
    Close,
}

pub struct FzfPopup {
    visible: bool,
    /// The text typed after the `❯` prompt.
    query: String,
    /// The candidate corpus most recently handed to the popup.
    results: Vec<Station>,
    /// `(index into results, matched char positions in the name)`, best first.
    matches: Vec<(usize, Vec<usize>)>,
    state: ListState,
    /// A provider search is in flight for the current query.
    searching: bool,
    /// Advances every frame the spinner is drawn.
    spinner: usize,
}

impl FzfPopup {
    pub fn new() -> Self {
        Self {
            visible: false,
            query: String::new(),
            results: Vec::new(),
            matches: Vec::new(),
            state: ListState::default(),
            searching: false,
            spinner: 0,
        }
    }

    pub fn is_visible(&self) -> bool {
        self.visible
    }

    /// The current query, trimmed of surrounding whitespace.
    pub fn query(&self) -> &str {
        &self.query
    }

    /// Open the finder seeded with an initial candidate list (e.g. the
    /// favourites or the list currently on screen) so it is useful before
    /// the first keystroke.
    pub fn open(&mut self, seed: Vec<Station>) {
        self.visible = true;
        self.query.clear();
        self.searching = false;
        self.results = seed;
        self.recompute();
    }

    pub fn close(&mut self) {
        self.visible = false;
        self.searching = false;
    }

    /// Replace the candidate corpus (typically fresh provider results for the
    /// current query) and re-rank.
    pub fn set_results(&mut self, results: Vec<Station>) {
        self.results = results;
        self.recompute();
    }

    pub fn set_searching(&mut self, searching: bool) {
        self.searching = searching;
    }

    /// Handle a key while the popup is open.
    pub fn handle_key(&mut self, key: KeyEvent) -> FzfOutcome {
        if !self.visible {
            return FzfOutcome::Ignored;
        }
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        match key.code {
            KeyCode::Esc => {
                self.close();
                FzfOutcome::Close
            }
            KeyCode::Enter => match self.selected_station() {
                Some(station) => {
                    self.close();
                    FzfOutcome::Submit(station)
                }
                None => FzfOutcome::Consumed,
            },
            KeyCode::Down => {
                self.move_selection(1);
                FzfOutcome::Consumed
            }
            KeyCode::Up => {
                self.move_selection(-1);
                FzfOutcome::Consumed
            }
            // Emacs-style navigation, familiar from fzf itself.
            KeyCode::Char('n') if ctrl => {
                self.move_selection(1);
                FzfOutcome::Consumed
            }
            KeyCode::Char('p') if ctrl => {
                self.move_selection(-1);
                FzfOutcome::Consumed
            }
            KeyCode::Char('u') if ctrl => {
                self.query.clear();
                self.recompute();
                FzfOutcome::QueryChanged
            }
            KeyCode::Backspace => {
                self.query.pop();
                self.recompute();
                FzfOutcome::QueryChanged
            }
            KeyCode::Char(c) if !ctrl => {
                self.query.push(c);
                self.recompute();
                FzfOutcome::QueryChanged
            }
            _ => FzfOutcome::Consumed,
        }
    }

    /// The station under the selection cursor, if any.
    fn selected_station(&self) -> Option<Station> {
        let selected = self.state.selected()?;
        let (index, _) = self.matches.get(selected)?;
        self.results.get(*index).cloned()
    }

    fn move_selection(&mut self, delta: i32) {
        if self.matches.is_empty() {
            self.state.select(None);
            return;
        }
        let len = self.matches.len() as i32;
        let current = self.state.selected().unwrap_or(0) as i32;
        let next = (current + delta).clamp(0, len - 1) as usize;
        self.state.select(Some(next));
    }

    /// Fuzzy-rank the corpus against the query. An empty query keeps the
    /// corpus in its original order. Selection jumps back to the top match,
    /// like fzf does after every edit.
    fn recompute(&mut self) {
        let mut scored: Vec<(usize, i32, Vec<usize>)> = self
            .results
            .iter()
            .enumerate()
            .filter_map(|(i, station)| {
                fuzzy_match(&self.query, &station.name)
                    .map(|(score, positions)| (i, score, positions))
            })
            .collect();
        // Higher score first; ties keep the corpus order for stability.
        scored.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
        self.matches = scored.into_iter().map(|(i, _, pos)| (i, pos)).collect();

        if self.matches.is_empty() {
            self.state.select(None);
        } else {
            self.state.select(Some(0));
        }
    }

    /// Draw the popup centered over `frame`. Call last so it sits on top.
    pub fn render(&mut self, frame: &mut Frame) {
        if !self.visible {
            return;
        }

        let size = frame.size();
        let width = ((size.width as u32 * 80) / 100) as u16;
        let height = ((size.height as u32 * 75) / 100) as u16;
        let area = centered_rect(
            size,
            width.clamp(24, size.width),
            height.clamp(6, size.height),
        );
        frame.render_widget(Clear, area);

        let block = Block::new()
            .borders(Borders::ALL)
            .title(" 🔍 Fuzzy Finder ")
            .title_alignment(Alignment::Center)
            .border_style(Style::default().fg(theme::PRIMARY));
        let inner = block.inner(area);
        frame.render_widget(block, area);
        if inner.height < 3 || inner.width < 4 {
            return;
        }

        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(1), // prompt
                Constraint::Length(1), // counter
                Constraint::Min(1),    // results
            ])
            .split(inner);

        // Prompt line: `❯ query▌`, with a placeholder before the first keystroke.
        let mut prompt = vec![Span::styled(
            "",
            Style::default()
                .fg(theme::PRIMARY)
                .add_modifier(Modifier::BOLD),
        )];
        if self.query.is_empty() {
            prompt.push(Span::styled(
                "Type to search stations…",
                Style::default().fg(Color::DarkGray),
            ));
        } else {
            prompt.push(Span::raw(self.query.clone()));
            prompt.push(Span::styled(
                " ",
                Style::default().add_modifier(Modifier::REVERSED),
            ));
        }
        frame.render_widget(Paragraph::new(Line::from(prompt)), chunks[0]);

        // Counter line: `⣾ 12/240` — spinner (while searching) and match count.
        let spinner = if self.searching {
            self.spinner = self.spinner.wrapping_add(1);
            SPINNER[self.spinner % SPINNER.len()]
        } else {
            ' '
        };
        let counter = format!(
            "  {} {}/{}",
            spinner,
            self.matches.len(),
            self.results.len()
        );
        frame.render_widget(
            Paragraph::new(Line::from(Span::styled(
                counter,
                Style::default().fg(Color::DarkGray),
            ))),
            chunks[1],
        );

        // Results, with matched characters highlighted.
        let items: Vec<ListItem> = if self.matches.is_empty() {
            let message = if self.searching {
                "Searching…"
            } else if self.query.is_empty() {
                "No stations yet — start typing"
            } else {
                "No matches"
            };
            vec![ListItem::new(Span::styled(
                message,
                Style::default().fg(Color::DarkGray),
            ))]
        } else {
            self.matches
                .iter()
                .map(|(index, positions)| {
                    ListItem::new(highlight_line(&self.results[*index], positions))
                })
                .collect()
        };
        let list = List::new(items).highlight_symbol("").highlight_style(
            Style::default()
                .fg(theme::ACCENT)
                .add_modifier(Modifier::BOLD),
        );
        frame.render_stateful_widget(list, chunks[2], &mut self.state);
    }
}

/// Build the display line for one result, painting the fuzzy-matched
/// characters of the name and appending its "now playing" subtext.
fn highlight_line(station: &Station, positions: &[usize]) -> Line<'static> {
    let matched: HashSet<usize> = positions.iter().copied().collect();
    let mut spans = Vec::new();
    let mut run = String::new();
    let mut run_matched = false;

    for (i, ch) in station.name.chars().enumerate() {
        let is_match = matched.contains(&i);
        if !run.is_empty() && is_match != run_matched {
            spans.push(make_span(&run, run_matched));
            run.clear();
        }
        run.push(ch);
        run_matched = is_match;
    }
    if !run.is_empty() {
        spans.push(make_span(&run, run_matched));
    }

    if let Some(playing) = &station.playing {
        let playing = playing.trim();
        if !playing.is_empty() {
            spans.push(Span::styled(
                format!("{}", playing),
                Style::default().fg(Color::DarkGray),
            ));
        }
    }

    Line::from(spans)
}

fn make_span(text: &str, matched: bool) -> Span<'static> {
    if matched {
        Span::styled(
            text.to_string(),
            Style::default()
                .fg(theme::PRIMARY)
                .add_modifier(Modifier::BOLD),
        )
    } else {
        Span::raw(text.to_string())
    }
}

/// Fuzzy subsequence match of `query` against `text`, case-insensitive.
///
/// Returns `None` when `query` is not a subsequence of `text`; otherwise a
/// `(score, matched_char_positions)` pair. The scoring rewards consecutive
/// matches, matches at word boundaries and matches near the start, so the
/// most fzf-like candidate sorts to the top. An empty query matches
/// everything with a neutral score.
pub fn fuzzy_match(query: &str, text: &str) -> Option<(i32, Vec<usize>)> {
    if query.is_empty() {
        return Some((0, Vec::new()));
    }

    let text: Vec<char> = text.chars().collect();
    let mut positions = Vec::new();
    let mut score = 0i32;
    let mut cursor = 0usize;
    let mut previous: Option<usize> = None;

    for qc in query.chars() {
        let needle = qc.to_ascii_lowercase();
        let found = loop {
            if cursor >= text.len() {
                return None;
            }
            let matches = text[cursor].to_ascii_lowercase() == needle;
            cursor += 1;
            if matches {
                break cursor - 1;
            }
        };

        let mut char_score = 1;
        match previous {
            Some(prev) if found == prev + 1 => char_score += 5, // adjacent
            Some(prev) => char_score -= ((found - prev - 1).min(10)) as i32, // gap penalty
            None => char_score += 10 - (found.min(10) as i32),  // reward an early first hit
        }
        // Word-boundary bonus: start of string or after a separator.
        let boundary = found == 0
            || matches!(
                text.get(found - 1),
                Some(' ') | Some('-') | Some('_') | Some('/') | Some('.') | Some('(')
            );
        if boundary {
            char_score += 3;
        }

        score += char_score;
        positions.push(found);
        previous = Some(found);
    }

    Some((score, positions))
}

/// A rect of at most `width`×`height`, centered in `outer`.
fn centered_rect(outer: Rect, width: u16, height: u16) -> Rect {
    let w = width.min(outer.width);
    let h = height.min(outer.height);
    Rect {
        x: outer.x + (outer.width - w) / 2,
        y: outer.y + (outer.height - h) / 2,
        width: w,
        height: h,
    }
}