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"
hidapi = "0.4.1"
users = "0.7"
colored = "1.6.1"

View File

@ -1,17 +1,23 @@
//! Footswitch-RS
//!
//! `footswitch-rs` enables you to use footswitches of <xxx>
//!
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 <dennis@dennispotter.eu>");
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,8 +121,7 @@ fn main() {
"append_str" => {
}
_ => {
eprintln!("Error: Unkonwn command!");
process::exit(0);
error!("Unkonwn command!");
}
}
}
@ -122,17 +129,18 @@ fn main() {
// 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]);
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");
}
goodbye();
},
None => {
eprintln!("Error: You did not specify any command. Run './footswitch-rs --help' for more information.");
process::exit(0);
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!");
}
}

View File

@ -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,19 +75,15 @@ 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
@ -93,9 +92,6 @@ impl Pedals {
println!("\n{}", "".repeat(total_width));
// Print space before value row
print!(" ");
// Read and print keys
for i in peds.iter() {
// Read value from pedal and directly translate it to a key
@ -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);
}
}