Use clap to parse arguments from the CLI

This commit is contained in:
charlotte ✨ 2021-07-21 22:48:20 +01:00
parent 211b6a74b1
commit a19588a19e
2 changed files with 34 additions and 7 deletions

View File

@ -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"

View File

@ -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 <agatharose@wantscuddl.es>")
(@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<Mutex<MidiOutputConnection>>, msg: &Message) {
fn midi_hook(conf: &BwowwwConfig, midi_out: Arc<Mutex<MidiOutputConnection>>, msg: &Message) {
let args = msg.get_items();
// do nothing if args are empty
@ -69,8 +96,7 @@ fn midi_hook(midi_out: Arc<Mutex<MidiOutputConnection>>, 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(),