From a19588a19e0277ed1176907291ddb31f1d1bd08d Mon Sep 17 00:00:00 2001 From: videogame hacker Date: Wed, 21 Jul 2021 22:48:20 +0100 Subject: [PATCH] Use clap to parse arguments from the CLI --- Cargo.toml | 1 + src/main.rs | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2df4e79..8a1ae49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = "2.33.3" dbus = "0.9.3" midi-control = "0.2.0" midir = "0.7.0" diff --git a/src/main.rs b/src/main.rs index cded31d..4b4445e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::{ time::Duration, }; +use clap::clap_app; use dbus::blocking::Connection; use dbus::message::MatchRule; use dbus::Message; @@ -10,13 +11,39 @@ use dbus::{arg::messageitem::MessageItem, channel::MatchingReceiver}; use midi_control::*; use midir::*; +struct BwowwwConfig { + browser: String, + port: usize, +} + +fn parse_cli_opts() -> BwowwwConfig { + let matches = clap_app!(bwowww => + (version: "1.0") + (author: "Agatha Rose ") + (@arg BROWSER: -b --browser +takes_value "Set a custom browser") + (@arg PORT: -p --port +takes_value "Select a MIDI port") + ) + .get_matches(); + + let browser = matches.value_of("BROWSER").unwrap_or("Nightly"); + let browser = browser.to_string(); + + let port = matches + .value_of("PORT") + .and_then(|s| s.parse().ok()) + .unwrap_or(1); + + BwowwwConfig { browser, port } +} + fn main() { + let conf = parse_cli_opts(); + let midi_device = MidiOutput::new("synth output meow").expect("could not find device"); let ports = midi_device.ports(); // assumes that the device is connected on port 1 - // TODO: add port selection let midi_out = midi_device - .connect(&ports[1], "meow port") + .connect(&ports[conf.port], "meow port") .expect("oh dear"); let midi_out = Arc::new(Mutex::new(midi_out)); @@ -40,7 +67,7 @@ fn main() { conn.start_receive( rule, Box::new(move |msg, _| { - midi_hook(midi_out.clone(), &msg); + midi_hook(&conf, midi_out.clone(), &msg); true }), ); @@ -48,7 +75,7 @@ fn main() { // Start matching using old scheme rule.eavesdrop = true; conn.add_match(rule, move |_: (), _, msg| { - midi_hook(midi_out.clone(), &msg); + midi_hook(&conf, midi_out.clone(), &msg); true }) .expect("add_match failed"); @@ -60,7 +87,7 @@ fn main() { // TODO: use the ctrlc crate to handle the midi connection closing } -fn midi_hook(midi_out: Arc>, msg: &Message) { +fn midi_hook(conf: &BwowwwConfig, midi_out: Arc>, msg: &Message) { let args = msg.get_items(); // do nothing if args are empty @@ -69,8 +96,7 @@ fn midi_hook(midi_out: Arc>, msg: &Message) { } // only apply to firefox notifications - // TODO: make browser name configurable - if args[0] == "Nightly".into() { + if args[0] == conf.browser.as_str().into() { // get the dict of values inside the message's args let dict_values = match &args[6] { MessageItem::Dict(v) => v.clone().into_vec(),