Added a module which nicely formats output to console. Fixes #4

This commit is contained in:
Dennis Potter 2018-07-26 17:15:39 +02:00
parent 551a97f359
commit 3558a4869a
4 changed files with 42 additions and 39 deletions

View File

@ -7,4 +7,5 @@ authors = ["Dennis <dennis@dennispotter.eu>"]
structopt = "0.2.10" structopt = "0.2.10"
hidapi = "0.4.1" hidapi = "0.4.1"
users = "0.7" users = "0.7"
colored = "1.6.1"

View File

@ -325,7 +325,7 @@ pub fn print_key(response: &[u8]) -> Option<String> {
} }
pub fn print_key_map(rows: usize) { pub fn print_key_map(rows: usize) {
print!("{}", " ‖ Key Name ¦ Value ".repeat(rows)); print!("{}", " ‖ Key Name ¦ Value ".repeat(rows));
println!(""); println!("");
println!(" {}", "-".repeat(rows*36)); println!(" {}", "-".repeat(rows*36));

View File

@ -1,17 +1,23 @@
//! Footswitch-RS //! Footswitch-RS
//! //!
//! `footswitch-rs` enables you to use footswitches of <xxx> //! `footswitch-rs` enables you to use footswitches of <xxx>
//!
mod key_operations; mod key_operations;
mod pedal_operations; mod pedal_operations;
#[macro_use]
mod messages;
#[macro_use] #[macro_use]
extern crate structopt; extern crate structopt;
extern crate hidapi; extern crate hidapi;
extern crate users; extern crate users;
extern crate colored;
use std::process; use std::process;
use structopt::StructOpt; use structopt::StructOpt;
use messages::*;
use colored::*;
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug)]
#[structopt(name = "rust-footswitch")] #[structopt(name = "rust-footswitch")]
@ -56,6 +62,8 @@ enum Command {
fn main() { fn main() {
let mut pedals = pedal_operations::Pedals::new(); let mut pedals = pedal_operations::Pedals::new();
welcome("footswitch-rs, Dennis Potter <dennis@dennispotter.eu>");
check_sudo(); check_sudo();
let opt = Opt::from_args(); let opt = Opt::from_args();
@ -64,7 +72,7 @@ fn main() {
// Print all keys and exit application // Print all keys and exit application
if let Some(x) = opt.listkeys { if let Some(x) = opt.listkeys {
key_operations::print_key_map(x); key_operations::print_key_map(x);
process::exit(0); goodbye();
} }
// Open device // Open device
@ -76,29 +84,29 @@ fn main() {
(0x413d , 0x2107) (0x413d , 0x2107)
]; ];
info!("Initializing HidApi. This can take a moment.");
let api = hidapi::HidApi::new().expect("Hidapi init failed!"); let api = hidapi::HidApi::new().expect("Hidapi init failed!");
let mut dev_path = String::new(); let mut dev_path = String::new();
for device in &api.devices() { for device in &api.devices() {
//println!("{}:{}, {}, {}", device.vendor_id, device.product_id, device.interface_number, device.path);
for val in vld_dev.iter() { for val in vld_dev.iter() {
if *val == (device.vendor_id, device.product_id) && device.interface_number == 1 { 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(); dev_path = device.path.clone();
} }
} }
} }
let dev = api.open_path(dev_path.as_str()).unwrap(); 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 // All options that need the device to be open
match opt.cmd { match opt.cmd {
Some(Command::Write {pedal: ped_list, command: cmd_list, value: val_list}) => { 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() { 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!"); error!("You must define as much pedals as you define commands and as you define values!");
process::exit(0);
} }
for (i, cmd) in cmd_list.iter().enumerate() { for (i, cmd) in cmd_list.iter().enumerate() {
@ -113,26 +121,26 @@ fn main() {
"append_str" => { "append_str" => {
} }
_ => { _ => {
eprintln!("Error: Unkonwn command!"); error!("Unkonwn command!");
process::exit(0);
} }
} }
} }
// Since we ran the Write command without any errors, we are now writing everything // Since we ran the Write command without any errors, we are now writing everything
pedals.write_pedals(& dev); pedals.write_pedals(& dev);
println!("Succesfully wrote everything to footpedal!"); info!("Succesfully wrote everything to footpedal!");
println!("The current state of the device is shown below.\n"); info!("The current state of the device is shown below.\n");
// Show user current state of pedal // 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}) => { Some(Command::Read {all: all_var, pedals: ped_list}) => {
if ped_list.len() > 3 { if ped_list.len() > 3 {
eprintln!("Error: Number of pedals may not be bigger than 3!"); error!("Number of pedals may not be bigger than 3!");
process::exit(0);
} }
if all_var { if all_var {
@ -142,13 +150,13 @@ fn main() {
pedals.read_pedals(&dev, ped_list) pedals.read_pedals(&dev, ped_list)
} }
else { else {
eprintln!("Error: You did not specify any command. Run './footswitch-rs read --help' for more information"); error!("You did not specify any command. Run './footswitch-rs read --help' for more information");
process::exit(0);
} }
},
None => { goodbye();
eprintln!("Error: You did not specify any command. Run './footswitch-rs --help' for more information."); },
process::exit(0); 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 /// Checks if user is super user
fn check_sudo() { fn check_sudo() {
if users::get_current_uid() != 0 { if users::get_current_uid() != 0 {
eprintln!("Error: Please execute this application as super user!"); error!("Please execute this application as super user!");
process::exit(0);
} }
} }

View File

@ -1,7 +1,10 @@
#[macro_use]
#[path = "messages.rs"] mod messages;
#[path = "key_operations.rs"] mod key_operations; #[path = "key_operations.rs"] mod key_operations;
extern crate hidapi; extern crate hidapi;
use std::process; use std::process;
use colored::*;
pub struct PedalsData { pub struct PedalsData {
header: [u8; 8], header: [u8; 8],
@ -72,29 +75,22 @@ impl Pedals {
let column_width = 60 / peds.len() + (3 - peds.len()); let column_width = 60 / peds.len() + (3 - peds.len());
// Print header // Print header
println!("{}", "".repeat(total_width)); println!("{}", "".repeat(total_width));
println!("{name:^width$}", name = "Programmed Keys", width = total_width); println!("{name:^width$}", name = "Programmed Keys", width = total_width);
println!("{}", "".repeat(total_width)); println!("{}", "".repeat(total_width));
// Print space before pedal number row
print!(" ");
// Print pedal numbers // Print pedal numbers
for i in peds.iter() { for i in peds.iter() {
// Check if passed pedal number is valid // Check if passed pedal number is valid
if *i > 2 { if *i > 2 {
eprintln!("Error: Pedal value {} is larger than 2 and thus not valid!", i); error!("Pedal value {} is larger than 2 and thus not valid!", i);
process::exit(0);
} }
// Print pedal numbers // Print pedal numbers
print!("{ped_nr:^-width$}", ped_nr = i, width = column_width); print!("{ped_nr:^-width$}", ped_nr = i, width = column_width);
} }
println!("\n{}", "".repeat(total_width)); println!("\n{}", "".repeat(total_width));
// Print space before value row
print!(" ");
// Read and print keys // Read and print keys
for i in peds.iter() { for i in peds.iter() {
@ -108,7 +104,7 @@ impl Pedals {
} }
// Print simple footer // Print simple footer
println!("\n{}", "".repeat(total_width)); println!("\n{}", "".repeat(total_width));
} }
fn write_pedal(&self, dev: & hidapi::HidDevice, ped:usize) { fn write_pedal(&self, dev: & hidapi::HidDevice, ped:usize) {
@ -149,8 +145,7 @@ impl Pedals {
self.ped_data[ped].data[3] = encoded_key; self.ped_data[ped].data[3] = encoded_key;
} }
else { else {
eprintln!("Error: Key '{}' is not recognized! Please provide a valid key, listed in './footswitch-rs --listkeys 4'", key); error!("Key '{}' is not recognized! Please provide a valid key, listed in './footswitch-rs --listkeys 4'", key);
process::exit(0);
} }
} }