launcher: Cache desktop files
This commit is contained in:
+8
-4
@@ -26,6 +26,7 @@ use fonts::{FontMap, MaybeFontMap};
|
||||
use keyboard::{KeyRepeatSource, RepeatMessage};
|
||||
use state::State;
|
||||
use widgets::{Geometry, Widget};
|
||||
use utils::{xdg, desktop::{load_desktop_files, write_desktop_cache}};
|
||||
|
||||
use std::{env, rc::Rc, thread};
|
||||
|
||||
@@ -44,13 +45,17 @@ fn main() {
|
||||
None => Config::generate_v2(false),
|
||||
Some(s) => panic!("unknown argument: {}", s),
|
||||
};
|
||||
let home = env::var_os("HOME").unwrap().into_string().unwrap();
|
||||
match File::create(format!("{}/.config/wldash/config.yml", home)) {
|
||||
match File::create(format!("{}/wldash/config.yml", xdg::config_folder())) {
|
||||
Ok(f) => serde_yaml::to_writer(f, &config).unwrap(),
|
||||
Err(_) => panic!("uh"),
|
||||
}
|
||||
std::process::exit(0);
|
||||
}
|
||||
Some(ref s) if s == "desktop-refresh" => {
|
||||
let v = load_desktop_files();
|
||||
write_desktop_cache(&v).unwrap();
|
||||
std::process::exit(0);
|
||||
}
|
||||
Some(_) => panic!("unknown argument"),
|
||||
None => break,
|
||||
}
|
||||
@@ -126,8 +131,7 @@ fn main() {
|
||||
|
||||
let mut fm = FontMap::new();
|
||||
|
||||
let home = env::var_os("HOME").unwrap().into_string().unwrap();
|
||||
let config: Config = match File::open(format!("{}/.config/wldash/config.yml", home)) {
|
||||
let config: Config = match File::open(format!("{}/wldash/config.yml", xdg::config_folder())) {
|
||||
Ok(f) => serde_yaml::from_reader(f).unwrap(),
|
||||
Err(_) => panic!("configuration file missing"),
|
||||
};
|
||||
|
||||
+21
-16
@@ -1,17 +1,19 @@
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
collections::HashSet,
|
||||
env,
|
||||
error::Error,
|
||||
fs::read_to_string,
|
||||
fs::{read_to_string, File},
|
||||
io::{Error as io_error, ErrorKind},
|
||||
};
|
||||
|
||||
use serde::{self, Deserialize, Serialize};
|
||||
use simd_json;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use crate::utils::inish;
|
||||
use crate::utils::{inish, xdg};
|
||||
|
||||
#[derive(Clone, Debug, Eq, Hash)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, Eq, Hash)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub struct Desktop {
|
||||
pub entry_type: String,
|
||||
pub name: String,
|
||||
@@ -72,19 +74,22 @@ impl PartialEq for Desktop {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_desktop_cache() -> Result<Vec<Desktop>, Box<dyn Error>> {
|
||||
match File::open(format!("{}/wldash/desktop.json", xdg::cache_folder())) {
|
||||
Ok(f) => simd_json::from_reader(f).map_err(|e| e.into()),
|
||||
Err(e) => Err(e.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_desktop_cache(v: &Vec<Desktop>) -> Result<(), Box<dyn Error>> {
|
||||
match File::create(format!("{}/wldash/desktop.json", xdg::cache_folder())) {
|
||||
Ok(f) => simd_json::to_writer(f, v).map_err(|e| e.into()),
|
||||
Err(e) => Err(e.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_desktop_files() -> Vec<Desktop> {
|
||||
let home = env::var_os("HOME").unwrap().into_string().unwrap();
|
||||
|
||||
let xdg_data_home = match env::var_os("XDG_DATA_HOME") {
|
||||
Some(s) => s.into_string().unwrap(),
|
||||
None => format!("{}/.local/share", home),
|
||||
};
|
||||
let xdg_data_dirs = match env::var_os("XDG_DATA_DIRS") {
|
||||
Some(s) => s.into_string().unwrap(),
|
||||
None => "/usr/local/share:/usr/share".to_string(),
|
||||
};
|
||||
|
||||
let dirs = std::iter::once(xdg_data_home.as_str()).chain(xdg_data_dirs.split(':'));
|
||||
let dirs = xdg::data_folders();
|
||||
|
||||
let mut desktop: Vec<Desktop> = Vec::new();
|
||||
let mut seen: HashSet<String> = HashSet::new();
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod desktop;
|
||||
pub mod xdg;
|
||||
mod inish;
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
use std::env;
|
||||
|
||||
pub fn cache_folder() -> String {
|
||||
match env::var_os("XDG_CACHE_HOME") {
|
||||
Some(s) => s.into_string().unwrap(),
|
||||
None => {
|
||||
let home = env::var_os("HOME").unwrap().into_string().unwrap();
|
||||
format!("{}/.cache", home)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn config_folder() -> String {
|
||||
match env::var_os("XDG_CONFIG_HOME") {
|
||||
Some(s) => s.into_string().unwrap(),
|
||||
None => {
|
||||
let home = env::var_os("HOME").unwrap().into_string().unwrap();
|
||||
format!("{}/.config", home)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn data_folders() -> Vec<String> {
|
||||
let xdg_data_home = match env::var_os("XDG_DATA_HOME") {
|
||||
Some(s) => s.into_string().unwrap(),
|
||||
None => {
|
||||
let home = env::var_os("HOME").unwrap().into_string().unwrap();
|
||||
format!("{}/.local/share", home)
|
||||
}
|
||||
};
|
||||
let xdg_data_dirs = match env::var_os("XDG_DATA_DIRS") {
|
||||
Some(s) => s.into_string().unwrap(),
|
||||
None => "/usr/local/share:/usr/share".to_string(),
|
||||
};
|
||||
|
||||
// Eww.
|
||||
std::iter::once(xdg_data_home.to_string()).chain(xdg_data_dirs.split(':').map(|x| x.to_string())).collect()
|
||||
}
|
||||
+16
-11
@@ -2,7 +2,6 @@ use std::{
|
||||
cmp::{max, min, Ordering},
|
||||
collections::HashMap,
|
||||
default::Default,
|
||||
env,
|
||||
fs::{read_to_string, File},
|
||||
io::Write,
|
||||
process::{exit, Command},
|
||||
@@ -16,7 +15,10 @@ use crate::{
|
||||
event::{Event, Events, PointerButton, PointerEvent},
|
||||
fonts::FontMap,
|
||||
keyboard::{keysyms, KeyEvent},
|
||||
utils::desktop::{load_desktop_files, Desktop},
|
||||
utils::{
|
||||
desktop::{load_desktop_files, load_desktop_cache, write_desktop_cache, Desktop},
|
||||
xdg,
|
||||
},
|
||||
widgets::{Geometry, Widget},
|
||||
};
|
||||
|
||||
@@ -221,7 +223,14 @@ impl Launcher {
|
||||
.name("desktopini".to_string())
|
||||
.spawn(move || {
|
||||
let mut options = options.lock().unwrap();
|
||||
*options = load_desktop_files();
|
||||
*options = match load_desktop_cache() {
|
||||
Ok(v) => v,
|
||||
Err(_) => {
|
||||
let v = load_desktop_files();
|
||||
write_desktop_cache(&v).unwrap();
|
||||
v
|
||||
}
|
||||
};
|
||||
drop(options);
|
||||
let mut events = events.lock().unwrap();
|
||||
events.add_event(Event::LauncherUpdate);
|
||||
@@ -432,8 +441,7 @@ impl Calc {
|
||||
thread::Builder::new()
|
||||
.name("calc".to_string())
|
||||
.spawn(move || {
|
||||
let home = env::var_os("HOME").unwrap().into_string().unwrap();
|
||||
let s = match read_to_string(format!("{}/.cache/wldash/calc", home)) {
|
||||
let s = match read_to_string(format!("{}/wldash/calc", xdg::cache_folder())) {
|
||||
Ok(s) => s,
|
||||
_ => return,
|
||||
};
|
||||
@@ -455,8 +463,7 @@ impl Calc {
|
||||
}
|
||||
|
||||
fn sync(&self) {
|
||||
let home = env::var_os("HOME").unwrap().into_string().unwrap();
|
||||
let mut f = match File::create(format!("{}/.cache/wldash/calc", home)) {
|
||||
let mut f = match File::create(format!("{}/wldash/calc", xdg::cache_folder())) {
|
||||
Ok(f) => f,
|
||||
_ => return,
|
||||
};
|
||||
@@ -634,8 +641,7 @@ impl Interface {
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
let home = env::var_os("HOME").unwrap().into_string().unwrap();
|
||||
if let Ok(mut f) = File::create(format!("{}/.cache/wldash/prompt", home)) {
|
||||
if let Ok(mut f) = File::create(format!("{}/wldash/prompt", xdg::cache_folder())) {
|
||||
write!(f, "{}", self.inner.prompt.input).unwrap();
|
||||
f.sync_data().unwrap();
|
||||
}
|
||||
@@ -666,8 +672,7 @@ impl Interface {
|
||||
widget.update(&self.inner);
|
||||
}
|
||||
keysyms::XKB_KEY_r if event.modifiers.ctrl => {
|
||||
let home = env::var_os("HOME").unwrap().into_string().unwrap();
|
||||
if let Ok(s) = read_to_string(format!("{}/.cache/wldash/prompt", home)) {
|
||||
if let Ok(s) = read_to_string(format!("{}/wldash/prompt", xdg::cache_folder())) {
|
||||
self.inner.prompt.set(&s);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user