mirror of
https://github.com/imgurbot12/rmenu.git
synced 2025-01-12 23:36:29 +01:00
Merge pull request #6 from LordGrimmauld/fix_xdg_css
allow searching css independent of config path
This commit is contained in:
commit
64fdbaafc4
2 changed files with 37 additions and 36 deletions
|
@ -10,7 +10,7 @@ use rmenu_plugin::{Entry, Message};
|
|||
use thiserror::Error;
|
||||
|
||||
use crate::config::{cfg_replace, Config, Keybind};
|
||||
use crate::{DEFAULT_CONFIG, DEFAULT_THEME};
|
||||
use crate::{DEFAULT_CONFIG, DEFAULT_THEME, XDG_PREFIX};
|
||||
|
||||
/// Allowed Formats for Entry Ingestion
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -179,28 +179,34 @@ pub enum RMenuError {
|
|||
pub type Result<T> = std::result::Result<T, RMenuError>;
|
||||
|
||||
impl Args {
|
||||
/// Find Configuration Path
|
||||
pub fn find_config(&self) -> PathBuf {
|
||||
self.config.clone().unwrap_or_else(|| {
|
||||
xdg::BaseDirectories::with_prefix("rmenu")
|
||||
/// Find a specifically named file across xdg config paths
|
||||
fn find_xdg_file(&self, name: &str, base: &Option<PathBuf>) -> Option<String> {
|
||||
return base.clone().or_else(|| {
|
||||
xdg::BaseDirectories::with_prefix(XDG_PREFIX)
|
||||
.expect("Failed to read xdg base dirs")
|
||||
.find_config_file(DEFAULT_CONFIG)
|
||||
.unwrap_or_else(PathBuf::new)
|
||||
})
|
||||
.find_config_file(name)
|
||||
}).map(|f| {
|
||||
let f = f.to_string_lossy().to_string();
|
||||
shellexpand::tilde(&f).to_string()
|
||||
});
|
||||
}
|
||||
|
||||
/// Load Configuration File
|
||||
pub fn get_config(&self, path: &PathBuf) -> Result<Config> {
|
||||
let path = path.to_string_lossy().to_string();
|
||||
let path = shellexpand::tilde(&path).to_string();
|
||||
let config: Config = match read_to_string(path) {
|
||||
Ok(content) => serde_yaml::from_str(&content),
|
||||
Err(err) => {
|
||||
log::error!("Failed to Load Config: {err:?}");
|
||||
Ok(Config::default())
|
||||
}
|
||||
}?;
|
||||
Ok(config)
|
||||
pub fn get_config(&self) -> Result<Config> {
|
||||
let config = self.find_xdg_file(DEFAULT_CONFIG, &self.config);
|
||||
|
||||
if let Some (path) = config {
|
||||
let config: Config = match read_to_string(path) {
|
||||
Ok(content) => serde_yaml::from_str(&content),
|
||||
Err(err) => {
|
||||
log::error!("Failed to Load Config: {err:?}");
|
||||
Ok(Config::default())
|
||||
}
|
||||
}?;
|
||||
return Ok(config);
|
||||
}
|
||||
log::error!("Failed to Load Config: no file found in xdg config paths");
|
||||
Ok(Config::default())
|
||||
}
|
||||
|
||||
/// Update Configuration w/ CLI Specified Settings
|
||||
|
@ -242,17 +248,14 @@ impl Args {
|
|||
}
|
||||
|
||||
/// Load CSS Theme or Default
|
||||
pub fn get_theme(&self, cfgdir: &PathBuf) -> String {
|
||||
let theme = self.theme.clone().or(Some(cfgdir.join(DEFAULT_THEME)));
|
||||
if let Some(theme) = theme {
|
||||
let path = theme.to_string_lossy().to_string();
|
||||
let path = shellexpand::tilde(&path).to_string();
|
||||
match read_to_string(&path) {
|
||||
Ok(css) => return css,
|
||||
Err(err) => log::error!("Failed to load CSS: {err:?}"),
|
||||
}
|
||||
}
|
||||
String::new()
|
||||
pub fn get_theme(&self) -> String {
|
||||
self.find_xdg_file(DEFAULT_THEME, &self.theme)
|
||||
.map(read_to_string)
|
||||
.map(|f| f.unwrap_or_else(|err| {
|
||||
log::error!("Failed to load CSS: {err:?}");
|
||||
String::new()
|
||||
}))
|
||||
.unwrap_or_else(String::new)
|
||||
}
|
||||
|
||||
/// Load Additional CSS or Default
|
||||
|
|
|
@ -13,6 +13,7 @@ use rmenu_plugin::{self_exe, Entry};
|
|||
static CONFIG_DIR: &'static str = "~/.config/rmenu/";
|
||||
static DEFAULT_THEME: &'static str = "style.css";
|
||||
static DEFAULT_CONFIG: &'static str = "config.yaml";
|
||||
static XDG_PREFIX: &'static str = "rmenu";
|
||||
static DEFAULT_CSS_CONTENT: &'static str = include_str!("../public/default.css");
|
||||
|
||||
/// Application State for GUI
|
||||
|
@ -43,8 +44,7 @@ fn main() -> cli::Result<()> {
|
|||
|
||||
// parse cli and retrieve values for app
|
||||
let mut cli = cli::Args::parse();
|
||||
let mut cfgpath = cli.find_config();
|
||||
let mut config = cli.get_config(&cfgpath)?;
|
||||
let mut config = cli.get_config()?;
|
||||
let entries = cli.get_entries(&mut config)?;
|
||||
|
||||
// update config based on cli-settings and entries
|
||||
|
@ -55,10 +55,8 @@ fn main() -> cli::Result<()> {
|
|||
.any(|e| e.icon.is_some() || e.icon_alt.is_some());
|
||||
config.use_comments = config.use_comments && entries.iter().any(|e| e.comment.is_some());
|
||||
|
||||
// retrieve cfgdir and get theme/css
|
||||
cfgpath.pop();
|
||||
let cfgdir = cfgpath;
|
||||
let theme = cli.get_theme(&cfgdir);
|
||||
|
||||
let theme = cli.get_theme();
|
||||
let css = cli.get_css(&config);
|
||||
|
||||
// genrate app context and run gui
|
||||
|
|
Loading…
Reference in a new issue