diff --git a/Cargo.toml b/Cargo.toml index fe26e16..4adef75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ authors = ["Dennis "] structopt = "0.2.10" hidapi = "0.4.1" users = "0.7" +colored = "1.6.1" diff --git a/src/key_operations.rs b/src/key_operations.rs index f3db5ba..9ee3058 100644 --- a/src/key_operations.rs +++ b/src/key_operations.rs @@ -325,7 +325,7 @@ pub fn print_key(response: &[u8]) -> Option { } pub fn print_key_map(rows: usize) { -print!("{}", " ‖ Key Name ¦ Value ".repeat(rows)); + print!("{}", " ‖ Key Name ¦ Value ".repeat(rows)); println!(" ‖"); println!(" {}", "-".repeat(rows*36)); diff --git a/src/main.rs b/src/main.rs index 848d29a..da0b4ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,23 @@ //! Footswitch-RS //! //! `footswitch-rs` enables you to use footswitches of - +//! mod key_operations; mod pedal_operations; +#[macro_use] +mod messages; + #[macro_use] extern crate structopt; extern crate hidapi; extern crate users; +extern crate colored; use std::process; use structopt::StructOpt; +use messages::*; +use colored::*; #[derive(StructOpt, Debug)] #[structopt(name = "rust-footswitch")] @@ -56,6 +62,8 @@ enum Command { fn main() { let mut pedals = pedal_operations::Pedals::new(); + + welcome("footswitch-rs, Dennis Potter "); check_sudo(); let opt = Opt::from_args(); @@ -64,7 +72,7 @@ fn main() { // Print all keys and exit application if let Some(x) = opt.listkeys { key_operations::print_key_map(x); - process::exit(0); + goodbye(); } // Open device @@ -76,29 +84,29 @@ fn main() { (0x413d , 0x2107) ]; + info!("Initializing HidApi. This can take a moment."); + let api = hidapi::HidApi::new().expect("Hidapi init failed!"); let mut dev_path = String::new(); for device in &api.devices() { - //println!("{}:{}, {}, {}", device.vendor_id, device.product_id, device.interface_number, device.path); for val in vld_dev.iter() { if *val == (device.vendor_id, device.product_id) && device.interface_number == 1 { - println!("Found device {:x}:{:x} ({})", device.vendor_id, device.product_id, device.path); + info!("Found device {:x}:{:x} ({})", device.vendor_id, device.product_id, device.path); dev_path = device.path.clone(); } } } let dev = api.open_path(dev_path.as_str()).unwrap(); - println!("Succesfully opened device."); + info!("Succesfully opened device."); // All options that need the device to be open match opt.cmd { Some(Command::Write {pedal: ped_list, command: cmd_list, value: val_list}) => { if ped_list.len() != cmd_list.len() && ped_list.len() != val_list.len() { - eprintln!("Error: You must define as much pedals as you define commands and as you define values!"); - process::exit(0); + error!("You must define as much pedals as you define commands and as you define values!"); } for (i, cmd) in cmd_list.iter().enumerate() { @@ -113,26 +121,26 @@ fn main() { "append_str" => { } _ => { - eprintln!("Error: Unkonwn command!"); - process::exit(0); + error!("Unkonwn command!"); } } } - + // Since we ran the Write command without any errors, we are now writing everything pedals.write_pedals(& dev); - println!("Succesfully wrote everything to footpedal!"); - println!("The current state of the device is shown below.\n"); + info!("Succesfully wrote everything to footpedal!"); + info!("The current state of the device is shown below.\n"); // Show user current state of pedal - pedals.read_pedals(&dev, vec![0,1,2]); + pedals.read_pedals(&dev, vec![0,1,2]); + + goodbye(); }, Some(Command::Read {all: all_var, pedals: ped_list}) => { if ped_list.len() > 3 { - eprintln!("Error: Number of pedals may not be bigger than 3!"); - process::exit(0); + error!("Number of pedals may not be bigger than 3!"); } if all_var { @@ -142,13 +150,13 @@ fn main() { pedals.read_pedals(&dev, ped_list) } else { - eprintln!("Error: You did not specify any command. Run './footswitch-rs read --help' for more information"); - process::exit(0); + error!("You did not specify any command. Run './footswitch-rs read --help' for more information"); } - }, - None => { - eprintln!("Error: You did not specify any command. Run './footswitch-rs --help' for more information."); - process::exit(0); + + goodbye(); + }, + None => { + error!("You did not specify any command. Run './footswitch-rs --help' for more information."); } } } @@ -156,7 +164,6 @@ fn main() { /// Checks if user is super user fn check_sudo() { if users::get_current_uid() != 0 { - eprintln!("Error: Please execute this application as super user!"); - process::exit(0); + error!("Please execute this application as super user!"); } } diff --git a/src/pedal_operations.rs b/src/pedal_operations.rs index 27758ff..d2a7404 100644 --- a/src/pedal_operations.rs +++ b/src/pedal_operations.rs @@ -1,7 +1,10 @@ +#[macro_use] +#[path = "messages.rs"] mod messages; #[path = "key_operations.rs"] mod key_operations; extern crate hidapi; use std::process; +use colored::*; pub struct PedalsData { header: [u8; 8], @@ -72,29 +75,22 @@ impl Pedals { let column_width = 60 / peds.len() + (3 - peds.len()); // Print header - println!(" ╔{}╗", "═".repeat(total_width)); - println!(" ║{name:^width$}║", name = "Programmed Keys", width = total_width); - println!(" ╠{}╣", "═".repeat(total_width)); - - // Print space before pedal number row - print!(" "); + println!("├{}┐", "─".repeat(total_width)); + println!("│{name:^width$}│", name = "Programmed Keys", width = total_width); + println!("╞{}╡", "═".repeat(total_width)); // Print pedal numbers for i in peds.iter() { // Check if passed pedal number is valid if *i > 2 { - eprintln!("Error: Pedal value {} is larger than 2 and thus not valid!", i); - process::exit(0); + error!("Pedal value {} is larger than 2 and thus not valid!", i); } // Print pedal numbers print!("│{ped_nr:^-width$}", ped_nr = i, width = column_width); } - println!("│\n ├{}┤", "─".repeat(total_width)); - - // Print space before value row - print!(" "); + println!("│\n├{}┤", "─".repeat(total_width)); // Read and print keys for i in peds.iter() { @@ -108,7 +104,7 @@ impl Pedals { } // Print simple footer - println!("│\n └{}┘", "─".repeat(total_width)); + println!("│\n├{}┘", "─".repeat(total_width)); } fn write_pedal(&self, dev: & hidapi::HidDevice, ped:usize) { @@ -149,8 +145,7 @@ impl Pedals { self.ped_data[ped].data[3] = encoded_key; } else { - eprintln!("Error: Key '{}' is not recognized! Please provide a valid key, listed in './footswitch-rs --listkeys 4'", key); - process::exit(0); + error!("Key '{}' is not recognized! Please provide a valid key, listed in './footswitch-rs --listkeys 4'", key); } }