Shell completions

This commit is contained in:
Nox Sluijtman 2025-07-02 23:07:43 +02:00
parent c21072727c
commit 49f7746f57
Signed by: Egg
SSH key fingerprint: SHA256:2sG9X3C7Xvq2svGumz1/k7cm8l4G9+qAtAeugqB4J9M
6 changed files with 219 additions and 86 deletions

94
src/cli.rs Normal file
View file

@ -0,0 +1,94 @@
use clap::{Parser, Subcommand, ValueHint};
use clap_complete::Shell;
#[derive(Parser, Debug, PartialEq)]
#[command(
author = "Nox Sluijtman",
version,
about,
long_about = "A small, opinionated MPD client",
name = "noise"
)]
#[command(propagate_version = true)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Commands>,
/// Enables verbose output
#[arg(short, long, global = true)]
pub verbose: bool,
/// Hostname where MPD listens at
#[arg(short = 'H', long, global = true)]
pub host: Option<String>,
/// Generate shell completions
#[arg(long = "generate-completions", value_enum)]
pub completions: Option<Shell>,
// /// Generate manpage
// #[arg(long = "generate-manpage", value_enum)]
// pub manpage: bool,
}
#[derive(Subcommand, Debug, PartialEq)]
pub enum Commands {
/// Toggle MPD stream
Toggle,
/// Skip to the next track
Next,
/// Revert to the previous track
Prev,
/// Stops playing
Stop,
/// Play queueueu
Play {
#[arg(short, long, value_hint = ValueHint::Other)]
track: Option<u32>,
},
/// Set or get crossfade
Crossfade {
#[arg(short, long, value_hint = ValueHint::Other)]
seconds: Option<i64>,
},
///Update still needs some work
Update,
/// Return currently playing song
Current,
/// Clear current queueueu
Clear,
/// Query database
Search {
///Search query
#[arg(trailing_var_arg = true, value_hint = ValueHint::Other)]
query: Vec<String>,
///Only return the first n results
#[arg(short, long, value_hint = ValueHint::Other)]
max: Option<u32>,
// #[arg(short, long)]
// append: bool,
// #[arg(short, long)]
// insert: Option<u32>,
},
/// Query database differently
Find {
///Search query
#[arg(trailing_var_arg = true, value_hint = ValueHint::Other)]
query: Vec<String>,
///Only return the first n results
#[arg(short, long, value_hint = ValueHint::Other)]
max: Option<u32>,
#[arg(short, long)]
append: bool,
},
/// List items in the current queueueu
List {
#[arg(short, long)]
file: bool,
},
// Add {
// #[arg(short, long)]
// position: Option<u32>,
// },
/// Shuffles the current queueue
Shuffle,
}

View file

@ -1,88 +1,37 @@
extern crate libnotify;
extern crate mpd;
use clap::{Parser, Subcommand};
mod cli;
use cli::*;
use clap::{Command, CommandFactory, Parser};
use clap_complete::{generate, Generator};
use libnotify::Notification;
use mpd::{Client, Query, Song, State, Term};
use std::time::Duration;
use std::{io, time::Duration};
#[derive(Parser)]
#[command(author, version, about, long_about = "Banaan")]
#[command(propagate_version = true)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
#[arg(short, long, global = true)]
verbose: bool,
///hostname where MPD listens at
#[arg(short = 'H', long, global = true)]
host: Option<String>,
}
#[derive(Subcommand)]
enum Commands {
/// Toggle MPD stream
Toggle,
/// Skip to the next track
Next,
/// Revert to the previous track
Prev,
/// Stops playing
Stop,
/// Play queueueu
Play { track: Option<u32> },
/// Set or get crossfade
Crossfade { seconds: Option<i64> },
///Update still needs some work
Update,
/// Return currently playing song
Current,
/// Clear current queueueu
Clear,
/// Query database
Search {
///Search query
#[arg(trailing_var_arg = true)]
query: Vec<String>,
///Only return the first n results
#[arg(short, long)]
max: Option<u32>,
// #[arg(short, long)]
// append: bool,
// #[arg(short, long)]
// insert: Option<u32>,
},
/// Query database differently
Find {
///Search query
#[arg(trailing_var_arg = true)]
query: Vec<String>,
///Only return the first n results
#[arg(short, long)]
max: Option<u32>,
#[arg(short, long)]
append: bool,
},
/// List items in the current queueueu
List {
#[arg(short, long)]
file: bool,
},
// Add {
// #[arg(short, long)]
// position: Option<u32>,
// },
/// Shuffles the current queueue
Shuffle,
fn print_completions<G: Generator>(generator: G, cmd: &mut Command) {
generate(
generator,
cmd,
cmd.get_name().to_string(),
&mut io::stdout(),
);
}
fn main() {
let cli = Cli::parse();
if let Some(completions) = cli.completions {
let mut cmd = Cli::command();
eprintln!("Generating completion file for {completions:?}...");
print_completions(completions, &mut cmd);
return;
}
libnotify::init("noise").unwrap();
let n = libnotify::Notification::new("Noise", None, None);
let cli = Cli::parse();
let host = cli.host.unwrap_or("localhost:6600".into());
@ -109,17 +58,20 @@ fn main() {
}
Commands::Next => conn.next().unwrap(),
Commands::Prev => conn.prev().unwrap(),
Commands::List { file } => conn.queue().unwrap().iter().for_each(|x| {
println!(
"{:<02} {}",
x.place.unwrap().pos,
if *file {
x.file.clone()
} else {
format_song(x.clone())
}
)
}),
Commands::List { file } => {
let _current_song = conn.currentsong().unwrap();
conn.queue().unwrap().iter().for_each(|x| {
println!(
"{:<02} {}",
x.place.unwrap().pos,
if *file {
x.file.clone()
} else {
format_song(x.clone())
}
)
});
}
Commands::Update => {
let thing = conn.update().unwrap();
println!("{thing}")