Add device listing
This commit is contained in:
parent
ea7cd5cfa9
commit
e822aa55ad
@ -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::<Vec<_>>();
|
||||||
|
|
||||||
}
|
// 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::<Vec<_>>().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::<Vec<_>>().join("\n")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -3,7 +3,8 @@ use std::path::PathBuf;
|
|||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use directories::ProjectDirs;
|
use directories::ProjectDirs;
|
||||||
|
|
||||||
pub fn get_config_dir() -> Result<PathBuf, ()> {
|
/// Gets the configuration directory for the app
|
||||||
|
pub fn get_config_dir() -> Result<PathBuf, std::io::Error> {
|
||||||
// Construct the path to the config directory for this app
|
// Construct the path to the config directory for this app
|
||||||
let project_dir = ProjectDirs::from("com", "va3zza", "tabset");
|
let project_dir = ProjectDirs::from("com", "va3zza", "tabset");
|
||||||
|
|
||||||
@ -14,13 +15,72 @@ pub fn get_config_dir() -> Result<PathBuf, ()> {
|
|||||||
if config_dir.exists() {
|
if config_dir.exists() {
|
||||||
Ok(config_dir.to_path_buf())
|
Ok(config_dir.to_path_buf())
|
||||||
} else {
|
} else {
|
||||||
eprintln!("{}\nPlease create: {}\nConfiguration info can be found at: ",
|
eprintln!(
|
||||||
"tabset requires configuration files to be placed in its config directory.\nThis does not exist.".red(),
|
"{}\nPlease create: {}\nConfiguration info can be found at: {}",
|
||||||
config_dir.display()
|
"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<String, std::io::Error> {
|
||||||
|
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<Vec<String>, 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::<Vec<_>>()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets a list of all profiles for a tablet from its config name.
|
||||||
|
pub fn get_device_profiles(config_name: &str) -> Result<Vec<String>, 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::<Vec<_>>()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user