use directories;
use std::fs;
use std::path::PathBuf;
use std::env::current_dir;
pub mod log {
pub use urs::log;
pub use urs::log::LogType::*;
}
pub enum ConfigDir {
RootDir,
KnifeDir,
LocalDir
}
pub fn get_config_dir(d: ConfigDir) -> Option<PathBuf> {
let base_dir = directories::BaseDirs::new()?;
let root = base_dir.config_dir().join("autosway");
match d {
ConfigDir::RootDir => Some(root),
ConfigDir::KnifeDir => Some(root.join("knifes")),
ConfigDir::LocalDir => Some(if let Ok(p) = current_dir() {
p.join(".autosway")
} else {
log::log!(log::Error, "Failed to get local config directory");
std::process::exit(1)
})
}
}
pub struct AutoswayLocalConfig {
pub knife_dir: PathBuf
}
impl Default for AutoswayLocalConfig {
fn default() -> Self {
let local_dir = if let Some(p) = get_config_dir(ConfigDir::LocalDir) {
p
} else {
log::log!(log::Error, "Failed to local autosway directory");
std::process::exit(1)
};
let mut knife_dir = local_dir.join("knifes");
if let Ok(mut f) = std::fs::File::open(local_dir.join("config")) {
if let Ok(c) = urs::misc::config::parse_config_file(&mut f) {
if let Some(dir) = c.get(&"knife_dir".to_string()) {
knife_dir = if let Ok(p) = current_dir() {
p.join(PathBuf::from(dir))
} else {
log::log!(log::Error, "");
std::process::exit(1)
}
}
}
}
Self {
knife_dir
}
}
}
pub struct Knife {
pub path: PathBuf,
pub name: String,
pub init: String,
}
pub fn get_knifes() -> Vec<Knife> {
let mut knifes = vec![];
let push_knife = |path: PathBuf, knifes: &mut Vec<Knife>| {
let name = if let Some(name) = path.file_name() {
match name.to_owned().to_str() {
Some(s) => s.to_owned(),
None => return,
}
} else {
return;
};
let init = path.join("init.lua");
if init.exists() {
let init_str = match init.to_str() {
Some(s) => s.to_owned(),
None => return,
};
knifes.push(Knife {
path,
name,
init: init_str,
});
}
};
if let Some(knife_dir) = get_config_dir(ConfigDir::KnifeDir) {
let read = match fs::read_dir(knife_dir) {
Ok(read_dir) => read_dir,
Err(_) => return knifes,
};
for entry in read {
let entry = if let Ok(e) = entry { e } else { continue };
if entry.path().is_dir() {
push_knife(entry.path(), &mut knifes);
}
}
}
let local_dir = AutoswayLocalConfig::default().knife_dir;
let read = match fs::read_dir(local_dir) {
Ok(read_dir) => read_dir,
Err(_) => return knifes,
};
for entry in read {
let entry = if let Ok(e) = entry { e } else { continue };
if entry.path().is_dir() {
push_knife(entry.path(), &mut knifes);
}
}
knifes
}
#[derive(Default)]
pub struct StateOptions {
pub verbose: bool,
}
pub struct State {
pub lua: mlua::Lua,
pub knifes: Vec<Knife>,
pub options: StateOptions,
}
impl State {
pub fn new() -> Self {
let mut state = Self {
lua: mlua::Lua::new(),
knifes: get_knifes(),
options: StateOptions::default(),
};
match state.setup_lua() {
Ok(()) => {}
Err(e) => {
log::log!(log::Error, "{e}");
std::process::exit(1);
}
}
state
}
}
impl Default for State {
fn default() -> Self {
Self::new()
}
}