diff options
| author | salmonik <salmonik@noreply.codeberg.org> | 2026-06-06 19:07:09 +0200 |
|---|---|---|
| committer | salmonik <salmonik@noreply.codeberg.org> | 2026-06-06 19:07:09 +0200 |
| commit | 385c2a38eba689154b46d515f6a3a010c34d3119 (patch) | |
| tree | 8916681bafc9822ef371f949a176cf22307d2a2e /src/cli.rs | |
| parent | add verbosity to `rsp scan` (diff) | |
| parent | delete not needed md file (diff) | |
| download | rsplayer-385c2a38eba689154b46d515f6a3a010c34d3119.tar.gz rsplayer-385c2a38eba689154b46d515f6a3a010c34d3119.zip | |
Merge pull request 'autocompletion' (#1) from autocompletion into main
Reviewed-on: https://codeberg.org/salmonik/RSPlayer/pulls/1
Diffstat (limited to 'src/cli.rs')
| -rw-r--r-- | src/cli.rs | 41 |
1 files changed, 40 insertions, 1 deletions
| @@ -92,6 +92,13 @@ pub enum Commands { | |||
| 92 | #[arg(value_name = "SHELL")] | 92 | #[arg(value_name = "SHELL")] |
| 93 | shell: Option<String>, | 93 | shell: Option<String>, |
| 94 | }, | 94 | }, |
| 95 | #[clap(hide = true)] | ||
| 96 | /// Complete artists, albums, or tracks from database | ||
| 97 | DbComplete { | ||
| 98 | /// Query string (e.g. \"J\", \"Artist/\", \"Artist/Album/\") | ||
| 99 | #[arg(value_name = "QUERY")] | ||
| 100 | query: String, | ||
| 101 | }, | ||
| 95 | } | 102 | } |
| 96 | 103 | ||
| 97 | #[derive(Subcommand)] | 104 | #[derive(Subcommand)] |
| @@ -235,11 +242,43 @@ impl Cli { | |||
| 235 | } | 242 | } |
| 236 | } | 243 | } |
| 237 | } | 244 | } |
| 245 | Commands::DbComplete { query } => { | ||
| 246 | // Unescape shell escapes in the input query | ||
| 247 | let query = { | ||
| 248 | let mut unescaped = String::with_capacity(query.len()); | ||
| 249 | let mut chars = query.chars(); | ||
| 250 | while let Some(c) = chars.next() { | ||
| 251 | if c == '\\' { | ||
| 252 | if let Some(next) = chars.next() { | ||
| 253 | unescaped.push(next); | ||
| 254 | } | ||
| 255 | } else { | ||
| 256 | unescaped.push(c); | ||
| 257 | } | ||
| 258 | } | ||
| 259 | unescaped | ||
| 260 | }; | ||
| 261 | |||
| 262 | let config = match Config::load() { | ||
| 263 | Ok(c) => c, | ||
| 264 | Err(_) => return Ok(()), | ||
| 265 | }; | ||
| 266 | let db = match Database::open(&config) { | ||
| 267 | Ok(d) => d, | ||
| 268 | Err(_) => return Ok(()), | ||
| 269 | }; | ||
| 270 | |||
| 271 | let results = db.complete(&query).unwrap_or_default(); | ||
| 272 | |||
| 273 | for r in &results { | ||
| 274 | println!("{r}"); | ||
| 275 | } | ||
| 276 | } | ||
| 238 | Commands::Completions { shell } => { | 277 | Commands::Completions { shell } => { |
| 239 | let full = Cli::command(); | 278 | let full = Cli::command(); |
| 240 | let subcommands: Vec<Command> = full | 279 | let subcommands: Vec<Command> = full |
| 241 | .get_subcommands() | 280 | .get_subcommands() |
| 242 | .filter(|s| s.get_name() != "completions") | 281 | .filter(|s| s.get_name() != "completions" && s.get_name() != "db-complete") |
| 243 | .cloned() | 282 | .cloned() |
| 244 | .collect(); | 283 | .collect(); |
| 245 | let cmd_name = full.get_name().to_string(); | 284 | let cmd_name = full.get_name().to_string(); |
