Skip to main content

linkmarks_cli/cmd/
tui.rs

1//! `linkmarks tui` — launch the interactive terminal browser.
2
3use crate::Paths;
4use anyhow::Result;
5use clap::Args;
6use linkmarks_tui::{run as run_tui, AppConfig, SourceRegistry, SourceSelection};
7use std::path::PathBuf;
8
9#[derive(Args, Debug)]
10pub struct TuiArgs {
11    /// Source to read from. `all` (default) merges from every
12    /// available source. `chrome`, `netscape`, and `firefox` filter
13    /// to that one source.
14    #[arg(long, env = "LINKMARKS_TUI_DEFAULT_SOURCE")]
15    pub source: Option<String>,
16    /// Optional path override for the Netscape HTML file.
17    #[arg(long)]
18    pub netscape_path: Option<PathBuf>,
19    /// Optional path override for the Chromium `Bookmarks` JSON file.
20    #[arg(long)]
21    pub chromium_path: Option<PathBuf>,
22}
23
24pub fn execute(args: TuiArgs, paths: Paths) -> Result<i32> {
25    let selection = parse_selection(&args.source)?;
26
27    let registry = SourceRegistry::resolve(
28        selection,
29        args.netscape_path.clone(),
30        args.chromium_path.clone(),
31        Some(paths.store.clone()),
32    );
33
34    let config = AppConfig::from_args(
35        selection,
36        args.netscape_path,
37        args.chromium_path,
38        Some(paths.store),
39    );
40
41    let code = run_tui(registry, config).map_err(|e| anyhow::anyhow!("tui: {e}"))?;
42    Ok(code)
43}
44
45fn parse_selection(raw: &Option<String>) -> Result<SourceSelection> {
46    let s = raw.clone().unwrap_or_else(|| "all".to_string());
47    SourceSelection::parse(&s).ok_or_else(|| {
48        anyhow::anyhow!("unsupported --source={s} (try: all, chrome, netscape, firefox)")
49    })
50}