Skip to content

Commit

Permalink
Extra arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Nov 26, 2023
1 parent 0f98ce2 commit 85636ad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ use midir::{MidiOutput, MidiOutputConnection};

#[derive(Parser)]
pub struct Args {
/// The location of the config file.
/// Defaults to `config.toml` in the current directory.
#[clap(short, long)]
pub config: Option<PathBuf>,

/// Logs each key event to stdout.
#[clap(short, long)]
pub debug: bool,

#[clap(subcommand)]
pub midi: Midi,
}
Expand All @@ -30,6 +36,7 @@ pub enum Midi {
},

/// Lists all available MIDI devices.
/// For use with the `connect` subcommand.
List,
}

Expand Down
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashSet, fs};
use std::{collections::HashSet, fs, path::PathBuf};

use anyhow::Result;
use clap::Parser;
Expand All @@ -11,6 +11,7 @@ mod args;
mod config;

struct MakeyMidi {
debug: bool,
config: Config,
output: MidiOutputConnection,
pressed: HashSet<Key>,
Expand All @@ -19,10 +20,12 @@ struct MakeyMidi {
fn main() -> Result<()> {
let args = Args::parse();

let raw_config = fs::read_to_string("config.toml")?;
let raw_config =
fs::read_to_string(args.config.unwrap_or_else(|| PathBuf::from("config.toml")))?;
let config = toml::from_str::<Config>(&raw_config)?;

let mut app = MakeyMidi {
debug: args.debug,
config,
output: args.midi.midi_device()?,
pressed: HashSet::new(),
Expand All @@ -42,6 +45,10 @@ fn callback(app: &mut MakeyMidi, event: Event) {
}

let key = app.config.get_key(e);
if key.is_some() && app.debug {
println!("Key pressed: {:?}", e);
}

key.map(|x| MidiMessage::NoteOn {
key: x.into(),
vel: u7::max_value(),
Expand Down

0 comments on commit 85636ad

Please sign in to comment.