Add cli options

This commit is contained in:
GHOSCHT 2024-12-20 22:20:57 +01:00
parent 99d764eeaf
commit 1aa195edcd
Signed by: ghoscht
GPG key ID: 2C2C1C62A5388E82
5 changed files with 91 additions and 17 deletions

61
Cargo.lock generated
View file

@ -49,9 +49,9 @@ dependencies = [
[[package]]
name = "anstyle"
version = "1.0.6"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc"
checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9"
[[package]]
name = "anstyle-parse"
@ -111,6 +111,46 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim 0.11.1",
]
[[package]]
name = "clap_derive"
version = "4.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab"
dependencies = [
"heck 0.5.0",
"proc-macro2",
"quote 1.0.35",
"syn 2.0.52",
]
[[package]]
name = "clap_lex"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"
[[package]]
name = "colorchoice"
version = "1.0.0"
@ -192,7 +232,7 @@ dependencies = [
"ident_case",
"proc-macro2",
"quote 1.0.35",
"strsim",
"strsim 0.10.0",
"syn 1.0.109",
]
@ -224,7 +264,7 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0ac8859845146979953797f03cc5b282fb4396891807cdb3d04929a88418197"
dependencies = [
"heck",
"heck 0.3.3",
"quote 0.3.15",
"syn 0.11.11",
]
@ -349,6 +389,12 @@ dependencies = [
"unicode-segmentation",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "humantime"
version = "2.1.0"
@ -543,6 +589,7 @@ checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
name = "picokontroller"
version = "0.1.0"
dependencies = [
"clap",
"crossbeam-channel",
"directories",
"env_logger",
@ -684,6 +731,12 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "0.11.11"

View file

@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
clap = { version = "4.5.23", features = ["derive"] }
crossbeam-channel = "0.5.8"
directories = "5.0.1"
env_logger = "0.11.3"

View file

@ -5,24 +5,42 @@ mod midi_client;
mod mpris_client;
mod pulse_controller;
use clap::Parser;
use crossbeam_channel::unbounded;
use directories::ProjectDirs;
use interaction_server::InteractionServer;
use midi_client::MidiClient;
use std::{path::PathBuf, sync::Arc};
use std::{path::PathBuf, process::exit, sync::Arc};
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
#[arg(long, help = "List all available MIDI devices.")]
list_midi: bool,
#[arg(
long,
help = "List all available MPRIS players."
)]
list_mpris: bool,
}
fn main() {
env_logger::init();
let cli = Cli::parse();
if cli.list_midi {
midi_client::list_ports();
exit(0);
} else if cli.list_mpris {
mpris_client::list_players();
exit(0);
}
let proj_dirs = ProjectDirs::from("com", "ghoscht", "picoKontroller");
let path: PathBuf = proj_dirs.unwrap().config_dir().join("config.toml");
let cfg: config::Config = config::init(path);
let arc_cfg = Arc::new(cfg);
midi_client::list_ports();
println!("\nMPRIS Clients:");
mpris_client::list_clients();
let (sender, receiver) = unbounded::<midi_client::KeyEvent>();
let midi = MidiClient::new(arc_cfg.general.midi_device.clone(), sender);

View file

@ -34,7 +34,7 @@ impl MidiClient {
fn exec(self) {
let port_filter = Regex::new(&self.device_name).expect("Creating RegEx failed");
let mut midi_in = MidiInput::new(CLIENT_NAME).expect("Creating Midi device failed");
let mut midi_in = MidiInput::new(CLIENT_NAME).expect("Creating MIDI device failed");
midi_in.ignore(Ignore::None);
let in_ports = midi_in.ports();
@ -63,9 +63,9 @@ impl MidiClient {
}
pub fn list_ports() -> () {
let midi_in = MidiInput::new(CLIENT_NAME).expect("Creating Midi device failed");
let midi_in = MidiInput::new(CLIENT_NAME).expect("Creating MIDI device failed");
let in_ports = midi_in.ports();
println!("All available midi devices:");
println!("All available MIDI devices:");
for (_, p) in in_ports.iter().enumerate() {
println!("{}", midi_in.port_name(p).unwrap());
}

View file

@ -1,17 +1,19 @@
use itertools::Itertools;
use log::warn;
use mpris::Player;
use mpris::PlayerFinder;
use regex::Regex;
pub fn list_clients() -> () {
pub fn list_players() -> () {
println!("All available MPRIS players:");
PlayerFinder::new()
.expect("Could not connect to D-Bus")
.find_all()
.unwrap_or_else(|_| Vec::new())
.into_iter()
.for_each(|player| {
println!("{}", player.identity());
});
.iter()
.map(|p| p.identity().to_owned())
.dedup()
.for_each(|i| {println!("{}",i)});
}
fn for_all_players<F>(player_id: &str, function: F)