radio-cli 0.12.0

A lightweight terminal radio and podcast player
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
use super::state::{Focus, Source};
use crate::{
    cli::{Action, Cli},
    model::{Filter, Station},
};
use anyhow::{Result, bail};
use clap::{CommandFactory, Parser};
use std::{path::PathBuf, time::Duration};

use std::sync::mpsc::{self, Receiver, Sender, SyncSender};
use std::time::Instant;

pub(super) struct CommandSender {
    normal: SyncSender<Command>,
    control: Sender<Command>,
}
pub(super) struct CommandReceiver {
    normal: Receiver<Command>,
    control: Receiver<Command>,
}
fn priority(command: &Command) -> bool {
    matches!(
        command,
        Command::Podcast(
            crate::podcast::Action::Seek { .. } | crate::podcast::Action::Speed { .. }
        ) | Command::Pause
            | Command::Mute
            | Command::Stop
            | Command::RecordStop
            | Command::Volume(_)
            | Command::AdjustVolume(_)
            | Command::Cancel
    )
}
pub(super) fn channel(capacity: usize) -> (CommandSender, CommandReceiver) {
    let (normal, normal_rx) = mpsc::sync_channel(capacity);
    let (control, control_rx) = mpsc::channel();
    (
        CommandSender { normal, control },
        CommandReceiver {
            normal: normal_rx,
            control: control_rx,
        },
    )
}
impl CommandSender {
    pub fn try_send(&self, command: Command) -> Result<(), String> {
        if priority(&command) {
            self.control.send(command).map_err(|e| e.to_string())
        } else {
            self.normal.try_send(command).map_err(|e| e.to_string())
        }
    }
    #[cfg(test)]
    pub fn send(&self, command: Command) -> Result<(), String> {
        if priority(&command) {
            self.control.send(command).map_err(|e| e.to_string())
        } else {
            self.normal.send(command).map_err(|e| e.to_string())
        }
    }
}
impl CommandReceiver {
    #[cfg(test)]
    pub fn try_recv(&self) -> Result<Command, mpsc::TryRecvError> {
        self.control.try_recv().or_else(|_| self.normal.try_recv())
    }
    pub fn recv_timeout(&self, timeout: Duration) -> Result<Command, mpsc::RecvTimeoutError> {
        let deadline = Instant::now() + timeout;
        loop {
            if let Ok(command) = self.control.try_recv() {
                return Ok(command);
            }
            let remaining = deadline.saturating_duration_since(Instant::now());
            match self
                .normal
                .recv_timeout(remaining.min(Duration::from_millis(10)))
            {
                Ok(command) => return Ok(command),
                Err(mpsc::RecvTimeoutError::Disconnected) => {
                    return self
                        .control
                        .try_recv()
                        .map_err(|_| mpsc::RecvTimeoutError::Disconnected);
                }
                Err(_) if Instant::now() >= deadline => {
                    return Err(mpsc::RecvTimeoutError::Timeout);
                }
                Err(_) => {}
            }
        }
    }
    #[cfg(test)]
    pub fn recv(&self) -> Result<Command, mpsc::RecvTimeoutError> {
        self.recv_timeout(Duration::from_secs(2))
    }
    #[cfg(test)]
    pub fn try_iter(&self) -> impl Iterator<Item = Command> + '_ {
        std::iter::from_fn(|| self.try_recv().ok())
    }
}

#[derive(Clone)]
pub(super) enum Command {
    Podcast(crate::podcast::Action),
    PodcastRequest {
        action: crate::podcast::Action,
        request: u64,
    },
    Subtitles(String),
    SubtitleModel(String),
    LocatePlaying {
        uuid: String,
        request: u64,
        limit: usize,
    },
    Mute,
    SetLanguage(String),
    Facets {
        selected: usize,
        prefix: String,
    },
    Backups(String),
    Station(crate::service::StationCommand),
    Search(Filter),
    Play(String),
    PlayRecording(String),
    DeleteRecording {
        id: String,
        index_only: bool,
    },
    OpenRecordings,
    Random(Filter),
    Favorite(String, bool),
    ToggleFavorite(String),
    Pause,
    Stop,
    Volume(u8),
    AdjustVolume(i32),
    RecordToggle,
    RecordStop,
    Record {
        uuid: Option<String>,
        duration: Option<Duration>,
        dir: Option<PathBuf>,
    },
    Sync(bool),
    Cancel,
    Recordings(Filter),
    Info(String, u64),
    Refresh,
    Doctor,
}

pub(super) enum Parsed {
    SubtitleHistory,
    Mouse(bool),
    HelpText(String),
    Worker(Command),
    Search(Filter),
    Source(Source, crate::cli::Page),
    Focus(Focus),
    Help,
    Quit,
}

pub(super) const COMPLETIONS: &[&str] = &[
    "subtitles on",
    "subtitles off",
    "subtitles toggle",
    "subtitles history",
    "subtitles clear",
    "subtitles retry",
    "set subtitles.model sensevoice-small-int8",
    "set subtitles.model sensevoice-small-yue-int8",
    "set subtitles.model qwen3-asr-0.6b-int8",
    "sync",
    "refresh",
    "sync-cancel",
    "search",
    "play",
    "random",
    "pause",
    "mute",
    "set language en",
    "set language zh-CN",
    "set mouse true",
    "set mouse false",
    "stop",
    "volume",
    "record",
    "record stop",
    "podcast list",
    "podcast episodes",
    "podcast add",
    "podcast preview",
    "podcast search",
    "podcast discover",
    "podcast refresh",
    "podcast unsubscribe",
    "podcast play",
    "podcast mark",
    "podcast download",
    "podcast downloads list",
    "podcast downloads pause",
    "podcast downloads resume",
    "podcast downloads cancel",
    "podcast downloads remove",
    "podcast queue list",
    "podcast queue add",
    "podcast queue remove",
    "podcast queue move",
    "podcast queue clear",
    "podcast queue play",
    "podcast import",
    "podcast export",
    "podcast seek",
    "podcast speed",
    "podcast auto-next",
    "recordings list",
    "recordings play",
    "recordings remove",
    "open recordings",
    "favorites list",
    "favorites add",
    "favorites remove",
    "history",
    "explore",
    "info",
    "current",
    "station add",
    "station list",
    "station edit",
    "station remove",
    "station backups",
    "doctor",
    "help",
    "quit",
    "focus map",
    "focus sources",
    "focus list",
    "focus details",
    "focus player",
    "focus activity",
];

pub(super) fn parse(
    line: &str,
    selected: Option<&Station>,
    current: Option<&Station>,
    filter: &Filter,
) -> Result<Parsed> {
    let mut words = shell_words::split(line)?;
    let Some(name) = words.first().cloned() else {
        bail!(crate::t!("command_empty"));
    };
    let selected_id = || {
        selected
            .map(|s| s.stationuuid.clone())
            .ok_or_else(|| anyhow::anyhow!(crate::t!("command_need_station")))
    };
    match name.as_str() {
        "subtitles" => {
            return match words.get(1).map(String::as_str) {
                Some("history") if words.len() == 2 => Ok(Parsed::SubtitleHistory),
                Some("on" | "off" | "toggle" | "clear" | "retry") if words.len() == 2 => {
                    Ok(Parsed::Worker(Command::Subtitles(words[1].clone())))
                }
                _ => bail!(crate::t!("subtitle_usage")),
            };
        }
        "refresh" if words.len() == 1 => return Ok(Parsed::Worker(Command::Refresh)),
        "open" if words.get(1).map(String::as_str) == Some("recordings") && words.len() == 2 => {
            return Ok(Parsed::Worker(Command::OpenRecordings));
        }
        "current" if words.len() == 1 => {
            return Ok(Parsed::Worker(Command::Info(
                current
                    .ok_or_else(|| anyhow::anyhow!(crate::t!("command_nothing_playing")))?
                    .stationuuid
                    .clone(),
                0,
            )));
        }
        "q" | "quit" if words.len() == 1 => return Ok(Parsed::Quit),
        "help" if words.len() == 1 => return Ok(Parsed::Help),
        "help" => {
            let mut root = Cli::command();
            let mut selected = &mut root;
            for word in &words[1..] {
                selected = selected.find_subcommand_mut(word).ok_or_else(|| {
                    anyhow::anyhow!(crate::t!("command_unknown_help", "word" => word))
                })?;
            }
            return Ok(Parsed::HelpText(selected.render_long_help().to_string()));
        }
        "set" if words.len() == 3 => match words[1].as_str() {
            "subtitles.model" => {
                return Ok(Parsed::Worker(Command::SubtitleModel(words[2].clone())));
            }
            "language" if matches!(words[2].as_str(), "en" | "zh-CN") => {
                return Ok(Parsed::Worker(Command::SetLanguage(words[2].clone())));
            }
            "mouse" if matches!(words[2].as_str(), "true" | "false") => {
                return Ok(Parsed::Mouse(words[2] == "true"));
            }
            _ => bail!(crate::t!("command_usage_set")),
        },
        "sync-cancel" if words.len() == 1 => return Ok(Parsed::Worker(Command::Cancel)),
        "pause" if words.len() == 1 => return Ok(Parsed::Worker(Command::Pause)),
        "mute" if words.len() == 1 => return Ok(Parsed::Worker(Command::Mute)),
        "stop" if words.len() == 1 => return Ok(Parsed::Worker(Command::Stop)),
        "volume" => {
            let value = words
                .get(1)
                .filter(|_| words.len() == 2)
                .and_then(|s| s.parse::<u8>().ok())
                .filter(|n| *n <= 100)
                .ok_or_else(|| anyhow::anyhow!(crate::t!("command_usage_volume")))?;
            return Ok(Parsed::Worker(Command::Volume(value)));
        }
        "explore" if words.len() == 1 => {
            return Ok(Parsed::Source(
                Source::Explore,
                crate::cli::Page {
                    limit: 100,
                    offset: 0,
                    all: false,
                },
            ));
        }
        "focus" => {
            let panel = match words
                .get(1)
                .map(String::as_str)
                .filter(|_| words.len() == 2)
            {
                Some("sources" | "1") => Focus::Sources,
                Some("list" | "2") => Focus::List,
                Some("details" | "3") => Focus::Details,
                Some("player" | "4") => Focus::Player,
                Some("activity" | "5") => Focus::Activity,
                Some("map" | "6") => Focus::Map,
                _ => bail!(crate::t!("command_usage_focus")),
            };
            return Ok(Parsed::Focus(panel));
        }
        "record" if words.get(1).is_some_and(|s| s == "stop") && words.len() == 2 => {
            return Ok(Parsed::Worker(Command::RecordStop));
        }
        "random" if words.len() == 1 => return Ok(Parsed::Worker(Command::Random(filter.clone()))),
        "play" | "info" if words.len() == 1 => words.push(selected_id()?),
        "favorites" if words.len() == 2 && matches!(words[1].as_str(), "add" | "remove") => {
            words.push(selected_id()?)
        }
        _ => {}
    }
    // Record without UUID targets the actual playing source, including an active backup.
    let current_record =
        name == "record" && (words.len() == 1 || words.get(1).is_some_and(|s| s.starts_with('-')));
    if current_record {
        words.insert(
            1,
            current
                .map(|s| s.stationuuid.clone())
                .ok_or_else(|| anyhow::anyhow!(crate::t!("command_nothing_playing_uuid")))?,
        );
    }
    let cli = Cli::try_parse_from(std::iter::once("radio".to_owned()).chain(words))
        .map_err(|e| anyhow::anyhow!("{e}"))?;
    // Global process/config flags have no meaning inside an existing session.
    if cli.config.is_some()
        || cli.lang.is_some()
        || cli.data_dir.is_some()
        || cli.api_url.is_some()
        || cli.json
        || cli.json_v2
    {
        bail!(crate::t!("command_process_options"));
    }
    let parsed = match cli.command {
        Some(Action::Podcast {
            action: crate::podcast::Action::Add { url },
        }) => Parsed::Worker(Command::Podcast(crate::podcast::Action::Preview { url })),
        Some(Action::Podcast { action }) => Parsed::Worker(Command::Podcast(action)),
        Some(Action::Sync { accept_shrink }) => Parsed::Worker(Command::Sync(accept_shrink)),
        Some(Action::Search(search)) => Parsed::Search(search.filter()),
        Some(Action::Play { uuid }) => Parsed::Worker(Command::Play(uuid)),
        Some(Action::Random(search)) => Parsed::Worker(Command::Random(search.filter())),
        Some(Action::Info { uuid }) => Parsed::Worker(Command::Info(uuid, 0)),
        Some(Action::History(page)) => Parsed::Source(Source::History, page),
        Some(Action::Favorites { action }) => match action {
            crate::cli::Favorite::List(page) => Parsed::Source(Source::Favorites, page),
            crate::cli::Favorite::Add { uuid } => Parsed::Worker(Command::Favorite(uuid, true)),
            crate::cli::Favorite::Remove { uuid } => Parsed::Worker(Command::Favorite(uuid, false)),
        },
        Some(Action::Recordings {
            action: crate::cli::RecordingAction::List(page),
        }) => Parsed::Source(Source::Recordings, page),
        Some(Action::Recordings {
            action: crate::cli::RecordingAction::Play { id },
        }) => Parsed::Worker(Command::PlayRecording(id)),
        Some(Action::Recordings {
            action: crate::cli::RecordingAction::Remove { id, index_only },
        }) => Parsed::Worker(Command::DeleteRecording { id, index_only }),
        Some(Action::Recordings {
            action: crate::cli::RecordingAction::Open,
        }) => Parsed::Worker(Command::OpenRecordings),
        Some(Action::Record {
            uuid,
            duration,
            output_dir,
        }) => Parsed::Worker(Command::Record {
            uuid: (!current_record).then_some(uuid),
            duration,
            dir: output_dir,
        }),
        Some(Action::Doctor) => Parsed::Worker(Command::Doctor),
        Some(Action::Station { action }) => match action.into_request() {
            crate::cli::StationRequest::List(page) => {
                if page.all {
                    bail!(crate::t!("command_paging"));
                }
                Parsed::Search(Filter {
                    custom: true,
                    limit: usize::from(page.limit),
                    offset: page.offset,
                    ..Filter::default()
                })
            }
            crate::cli::StationRequest::Change(command) => {
                Parsed::Worker(Command::Station(command))
            }
        },
        _ => bail!(crate::t!("command_unknown")),
    };
    if matches!(&parsed, Parsed::Source(_, page) if page.all) {
        bail!(crate::t!("command_paging"));
    }
    Ok(parsed)
}

pub(super) fn completion_candidates(
    text: &str,
    stations: &[String],
    recordings: &[String],
) -> Vec<String> {
    let mut result: Vec<String> = COMPLETIONS
        .iter()
        .filter(|s| s.starts_with(text))
        .map(|s| (*s).into())
        .collect();
    let Ok(words) = shell_words::split(text) else {
        return result;
    };
    let Some(command) = words.first().map(String::as_str) else {
        return result;
    };
    let start = text.rfind(char::is_whitespace).map_or(0, |i| i + 1);
    let base = &text[..start];
    let prefix = &text[start..];
    let mut values: Vec<String> = vec![];
    let preceding = if prefix.is_empty() {
        words.last()
    } else {
        words.iter().rev().nth(1)
    }
    .map(String::as_str);
    if preceding == Some("--duration") {
        values.extend(["10m", "30m", "1h"].map(String::from));
    } else if preceding == Some("--sort") {
        values.extend(["name", "bitrate", "votes", "clickcount", "random"].map(String::from));
    } else {
        let options: &[&str] = match command {
            "record" => &["--duration", "--output-dir"],
            "search" | "random" => &[
                "--country",
                "--language",
                "--tag",
                "--min-bitrate",
                "--limit",
                "--offset",
                "--sort",
            ],
            "station" if words.get(1).map(String::as_str) == Some("add") => &["--backup-url"],
            "station" if words.get(1).map(String::as_str) == Some("edit") => {
                &["--name", "--url", "--backup-url"]
            }
            "station" if words.get(1).map(String::as_str) == Some("remove") => {
                &["--keep-references"]
            }
            "station" if words.get(1).map(String::as_str) == Some("backups") => {
                &["--add", "--remove"]
            }
            "recordings" if words.get(1).map(String::as_str) == Some("remove") => &["--index-only"],
            "history" | "favorites" | "recordings" => &["--limit", "--offset"],
            _ => &[],
        };
        values.extend(options.iter().map(|s| (*s).into()));
        if matches!(command, "record" | "play" | "info")
            && words.len() <= if prefix.is_empty() { 1 } else { 2 }
        {
            values.extend_from_slice(stations);
        }
        if command == "recordings"
            && matches!(words.get(1).map(String::as_str), Some("play" | "remove"))
        {
            values.extend_from_slice(recordings);
        }
        if text == "station add " {
            values.push("'Station name' https://example.com/live".into());
        }
    }
    result.extend(
        values
            .into_iter()
            .filter(|value| value.starts_with(prefix))
            .map(|value| format!("{base}{value}")),
    );
    let mut unique = std::collections::HashSet::new();
    result.retain(|candidate| unique.insert(candidate.clone()));
    result
}