Compare commits

..

2 Commits

Author SHA1 Message Date
imgurbot12
5fb9aa6863 feat: rustfmt, moved cache to xdg-cache dir 2024-04-02 10:45:51 -07:00
Andrew Scott
64fdbaafc4
Merge pull request #6 from LordGrimmauld/fix_xdg_css
allow searching css independent of config path
2024-04-02 10:35:08 -07:00
3 changed files with 26 additions and 24 deletions

View File

@ -3,15 +3,11 @@ use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
use once_cell::sync::Lazy;
use rmenu_plugin::Entry; use rmenu_plugin::Entry;
use thiserror::Error; use thiserror::Error;
use crate::config::{CacheSetting, PluginConfig}; use crate::config::{CacheSetting, PluginConfig};
use crate::CONFIG_DIR; use crate::XDG_PREFIX;
static CONFIG_PATH: Lazy<PathBuf> =
Lazy::new(|| PathBuf::from(shellexpand::tilde(CONFIG_DIR).to_string()));
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum CacheError { pub enum CacheError {
@ -29,7 +25,10 @@ pub enum CacheError {
#[inline] #[inline]
fn cache_file(name: &str) -> PathBuf { fn cache_file(name: &str) -> PathBuf {
CONFIG_PATH.join(format!("{name}.cache")) xdg::BaseDirectories::with_prefix(XDG_PREFIX)
.expect("Failed to read xdg base dirs")
.place_cache_file(format!("{name}.cache"))
.expect("Failed to write xdg cache dirs")
} }
/// Read Entries from Cache (if Valid and Available) /// Read Entries from Cache (if Valid and Available)

View File

@ -181,21 +181,24 @@ pub type Result<T> = std::result::Result<T, RMenuError>;
impl Args { impl Args {
/// Find a specifically named file across xdg config paths /// Find a specifically named file across xdg config paths
fn find_xdg_file(&self, name: &str, base: &Option<PathBuf>) -> Option<String> { fn find_xdg_file(&self, name: &str, base: &Option<PathBuf>) -> Option<String> {
return base.clone().or_else(|| { return base
xdg::BaseDirectories::with_prefix(XDG_PREFIX) .clone()
.expect("Failed to read xdg base dirs") .or_else(|| {
.find_config_file(name) xdg::BaseDirectories::with_prefix(XDG_PREFIX)
}).map(|f| { .expect("Failed to read xdg base dirs")
let f = f.to_string_lossy().to_string(); .find_config_file(name)
shellexpand::tilde(&f).to_string() })
}); .map(|f| {
let f = f.to_string_lossy().to_string();
shellexpand::tilde(&f).to_string()
});
} }
/// Load Configuration File /// Load Configuration File
pub fn get_config(&self) -> Result<Config> { pub fn get_config(&self) -> Result<Config> {
let config = self.find_xdg_file(DEFAULT_CONFIG, &self.config); let config = self.find_xdg_file(DEFAULT_CONFIG, &self.config);
if let Some (path) = config { if let Some(path) = config {
let config: Config = match read_to_string(path) { let config: Config = match read_to_string(path) {
Ok(content) => serde_yaml::from_str(&content), Ok(content) => serde_yaml::from_str(&content),
Err(err) => { Err(err) => {
@ -251,10 +254,12 @@ impl Args {
pub fn get_theme(&self) -> String { pub fn get_theme(&self) -> String {
self.find_xdg_file(DEFAULT_THEME, &self.theme) self.find_xdg_file(DEFAULT_THEME, &self.theme)
.map(read_to_string) .map(read_to_string)
.map(|f| f.unwrap_or_else(|err| { .map(|f| {
log::error!("Failed to load CSS: {err:?}"); f.unwrap_or_else(|err| {
String::new() log::error!("Failed to load CSS: {err:?}");
})) String::new()
})
})
.unwrap_or_else(String::new) .unwrap_or_else(String::new)
} }
@ -263,9 +268,9 @@ impl Args {
let css = self let css = self
.css .css
.clone() .clone()
.or(c.css.as_ref().map(|s| PathBuf::from(s))); .map(|s| s.to_string_lossy().to_string())
if let Some(css) = css { .or(c.css.clone());
let path = css.to_string_lossy().to_string(); if let Some(path) = css {
let path = shellexpand::tilde(&path).to_string(); let path = shellexpand::tilde(&path).to_string();
match read_to_string(&path) { match read_to_string(&path) {
Ok(css) => return css, Ok(css) => return css,

View File

@ -10,7 +10,6 @@ mod state;
use clap::Parser; use clap::Parser;
use rmenu_plugin::{self_exe, Entry}; use rmenu_plugin::{self_exe, Entry};
static CONFIG_DIR: &'static str = "~/.config/rmenu/";
static DEFAULT_THEME: &'static str = "style.css"; static DEFAULT_THEME: &'static str = "style.css";
static DEFAULT_CONFIG: &'static str = "config.yaml"; static DEFAULT_CONFIG: &'static str = "config.yaml";
static XDG_PREFIX: &'static str = "rmenu"; static XDG_PREFIX: &'static str = "rmenu";
@ -55,7 +54,6 @@ fn main() -> cli::Result<()> {
.any(|e| e.icon.is_some() || e.icon_alt.is_some()); .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()); config.use_comments = config.use_comments && entries.iter().any(|e| e.comment.is_some());
let theme = cli.get_theme(); let theme = cli.get_theme();
let css = cli.get_css(&config); let css = cli.get_css(&config);