You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
phetch/src/phetchdir.rs

98 lines
3.0 KiB
Rust

use crate::gopher;
use std::{
fs::{File, OpenOptions},
io::{prelude::*, BufReader, Result, Write},
};
5 years ago
/// The directory where phetch stores its files. Ex: bookmarks file
/// If you want the full, expanded path, use `path()`.
5 years ago
pub const DIR: &str = "~/.config/phetch/";
/// Loads a file from the phetchdir for reading.
5 years ago
pub fn load(filename: &str) -> Result<BufReader<File>> {
path().and_then(|dotdir| {
5 years ago
let path = dotdir.join(filename);
if let Ok(file) = OpenOptions::new().read(true).open(&path) {
Ok(BufReader::new(file))
} else {
Err(error!("Couldn't open {:?}", path))
}
})
5 years ago
}
/// 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);
if let Ok(mut file) = OpenOptions::new().append(true).create(true).open(path) {
let (t, host, port, sel) = gopher::parse_url(&url);
file.write_all(
format!(
"{}{}\t{}\t{}\t{}\r\n",
t.to_char().unwrap_or('i'),
label,
sel,
host,
port
)
.as_ref(),
);
Ok(())
} else {
Err(error!("Can't open file for writing: {:?}", filename))
}
})
}
/// 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);
if let Ok(mut file) = OpenOptions::new()
.write(true)
.read(true)
5 years ago
.create(true)
.open(path)
{
let (t, host, port, sel) = gopher::parse_url(&url);
let mut buf = vec![];
file.read_to_end(&mut buf);
file.seek(std::io::SeekFrom::Start(0));
file.write_all(
format!(
"{}{}\t{}\t{}\t{}\r\n",
5 years ago
t.to_char().unwrap_or('i'),
label,
sel,
host,
port
)
.as_ref(),
);
file.write_all(&buf);
Ok(())
} else {
Err(error!("Can't open file for writing: {:?}", filename))
}
})
5 years ago
}
/// 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> {
5 years ago
let homevar = std::env::var("HOME");
if homevar.is_err() {
return Err(error!("$HOME not set, can't decode `~`"));
5 years ago
}
let dotdir = DIR.replace('~', &homevar.unwrap());
let dotdir = std::path::Path::new(&dotdir);
if dotdir.exists() {
Ok(std::path::PathBuf::from(dotdir))
5 years ago
} else {
Err(error!("Config dir not found: {}", DIR))
5 years ago
}
}