|
|
|
@ -4,9 +4,11 @@ use std::{
|
|
|
|
|
io::{prelude::*, BufReader, Result, Write},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// The directory where phetch stores its files. Ex: bookmarks file
|
|
|
|
|
/// If you want the full, expanded path, use `path()`.
|
|
|
|
|
pub const DIR: &str = "~/.config/phetch/";
|
|
|
|
|
|
|
|
|
|
/// Loads a file in the config directory for reading.
|
|
|
|
|
/// Loads a file from the phetchdir for reading.
|
|
|
|
|
pub fn load(filename: &str) -> Result<BufReader<File>> {
|
|
|
|
|
path().and_then(|dotdir| {
|
|
|
|
|
let path = dotdir.join(filename);
|
|
|
|
@ -18,7 +20,7 @@ pub fn load(filename: &str) -> Result<BufReader<File>> {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Append a menu item as a line to a file in the config dir.
|
|
|
|
|
/// Append a menu item as a line to a file in the phetchdir.
|
|
|
|
|
pub fn append(filename: &str, label: &str, url: &str) -> Result<()> {
|
|
|
|
|
path().and_then(|dotdir| {
|
|
|
|
|
let path = dotdir.join(filename);
|
|
|
|
@ -42,7 +44,7 @@ pub fn append(filename: &str, label: &str, url: &str) -> Result<()> {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Add a menu item as the first line in a file in the config dir.
|
|
|
|
|
/// Add a menu item as the first line in a file in the phetchdir.
|
|
|
|
|
pub fn prepend(filename: &str, label: &str, url: &str) -> Result<()> {
|
|
|
|
|
path().and_then(|dotdir| {
|
|
|
|
|
let path = dotdir.join(filename);
|
|
|
|
@ -75,8 +77,10 @@ pub fn prepend(filename: &str, label: &str, url: &str) -> Result<()> {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// PathBuf to expanded config dir if it exists.
|
|
|
|
|
/// None if the config dir doesn't exist.
|
|
|
|
|
/// Returns the full, expanded PathBuf of the phetchdir only if it exists.
|
|
|
|
|
/// Returns None otherwise.
|
|
|
|
|
/// If you just want the phetchdir path whether or not it exists, use
|
|
|
|
|
/// the DIR constant directly.
|
|
|
|
|
pub fn path() -> Result<std::path::PathBuf> {
|
|
|
|
|
let homevar = std::env::var("HOME");
|
|
|
|
|
if homevar.is_err() {
|