diff --git a/src/commands/list.rs b/src/commands/list.rs index 55c83e9..3a46489 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,8 +1,38 @@ -use crate::config::get_config_dir; +use colored::Colorize; +use crate::config::{get_config_dir, get_device_name, get_device_peripherals, get_device_profiles}; -pub fn list_tablets(){ +pub fn list_tablets() { + // Get the config directory + let config_dir = get_config_dir().unwrap(); - let config_dir = get_config_dir(); + // Get all known tablets in the directory + let tablets = std::fs::read_dir(config_dir.join("devices")) + .unwrap() + .map(|entry| entry.unwrap()) + .collect::>(); -} \ No newline at end of file + // Iterate through the devices, grabbing additional info as needed and printing + for tablet in &tablets { + let tablet_dir_name = tablet.file_name(); + let tablet_dir_name = tablet_dir_name.to_str().unwrap(); + + // Get the tablet's name from its `name` file + let name = get_device_name(tablet_dir_name).unwrap(); + println!("{}: {}", tablet_dir_name, name.green()); + + // Get the tablet's peripheral profile from its `peripherals` file + let peripherals = get_device_peripherals(tablet_dir_name).unwrap(); + println!( + " - Peripherals:\n{}", + peripherals.iter().map(|p| format!(" - {}", p.to_string().cyan())).collect::>().join("\n") + ); + + // Get the tablet's profiles from its `profiles` directory + let profiles = get_device_profiles(tablet_dir_name).unwrap(); + println!( + " - Profiles:\n{}", + profiles.iter().map(|p| format!(" - {}", p.to_string().cyan())).collect::>().join("\n") + ); + } +} diff --git a/src/config.rs b/src/config.rs index c385d29..7a9f78f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,7 +3,8 @@ use std::path::PathBuf; use colored::Colorize; use directories::ProjectDirs; -pub fn get_config_dir() -> Result { +/// Gets the configuration directory for the app +pub fn get_config_dir() -> Result { // Construct the path to the config directory for this app let project_dir = ProjectDirs::from("com", "va3zza", "tabset"); @@ -14,13 +15,72 @@ pub fn get_config_dir() -> Result { if config_dir.exists() { Ok(config_dir.to_path_buf()) } else { - eprintln!("{}\nPlease create: {}\nConfiguration info can be found at: ", - "tabset requires configuration files to be placed in its config directory.\nThis does not exist.".red(), - config_dir.display() + eprintln!( + "{}\nPlease create: {}\nConfiguration info can be found at: {}", + "tabset not configured".red(), + config_dir.display().to_string().cyan(), + "https://github.com/Ewpratten/tabset#configuration".cyan() ); - Err(()) + Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "Config directory not found", + )) } } - None => Err(()), + None => Err(std::io::Error::new( + std::io::ErrorKind::NotFound, + "Config directory not found", + )), } } + +/// Gets the "friendly name" of a tablet from its config name +pub fn get_device_name(config_name: &str) -> Result { + let config_dir = get_config_dir()?; + return std::fs::read_to_string(config_dir.join("devices").join(config_name).join("name")); +} + +/// Gets a list of peripheral names for a tablet from its config name. +/// +/// These would be what XWacom will look for if we run a `check` command +pub fn get_device_peripherals(config_name: &str) -> Result, std::io::Error> { + let config_dir = get_config_dir()?; + return std::fs::read_to_string( + config_dir + .join("devices") + .join(config_name) + .join("peripherals"), + ) + .map(|peripherals| { + peripherals + .split('\n') + .map(|p| p.to_owned()) + .collect::>() + }); +} + +/// Gets a list of all profiles for a tablet from its config name. +pub fn get_device_profiles(config_name: &str) -> Result, std::io::Error> { + let config_dir = get_config_dir()?; + return std::fs::read_dir( + config_dir + .join("devices") + .join(config_name) + .join("profiles"), + ) + .map(|profiles| { + profiles + .map(|profile| { + profile + .unwrap() + .file_name() + .to_str() + .unwrap() + .to_owned() + .strip_suffix(".sh") + .unwrap() + .to_owned() + }) + .collect::>() + }); +}