mirror of https://github.com/xvxx/phetch
config, bookmarks
parent
06b5745bd5
commit
05b8e6ef56
@ -0,0 +1,22 @@
|
||||
use config;
|
||||
use std::io::{BufRead, BufReader, Result, Write};
|
||||
|
||||
const BOOKMARKS_FILE: &str = "bookmarks.gph";
|
||||
|
||||
pub fn as_raw_menu() -> String {
|
||||
let mut out = vec![format!("i** bookmarks **\r\ni")];
|
||||
|
||||
config::load(BOOKMARKS_FILE)
|
||||
.and_then(|reader| reader.read(&mut out))
|
||||
.map_err(|e| {
|
||||
out.push(format!("3{}", e));
|
||||
e
|
||||
});
|
||||
|
||||
out.join("\r\n")
|
||||
}
|
||||
|
||||
// save a single history entry
|
||||
pub fn save(label: &str, url: &str) {
|
||||
config::append(BOOKMARKS_FILE, label, url);
|
||||
}
|
@ -1,85 +1,28 @@
|
||||
use gopher;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader, Result, Write};
|
||||
use config;
|
||||
use std::io::BufRead;
|
||||
|
||||
const CONFIG_DIR: &str = "~/.config/phetch/";
|
||||
const HISTORY_FILE: &str = "history.gph";
|
||||
|
||||
/// History file is saved in ~/.config/phetch/history.gph if the phetch
|
||||
/// config directory exists.
|
||||
pub fn load_as_raw_menu() -> String {
|
||||
let mut out = vec![];
|
||||
pub fn as_raw_menu() -> String {
|
||||
let mut out = vec![format!("i{}{}:\r\ni", config::DIR, HISTORY_FILE)];
|
||||
|
||||
match load() {
|
||||
Ok(reader) => {
|
||||
let mut lines = reader.lines();
|
||||
config::load(HISTORY_FILE)
|
||||
.and_then(|reader| {
|
||||
let lines = reader.lines();
|
||||
while let Some(Ok(line)) = lines.next() {
|
||||
out.insert(0, line); // reverse order
|
||||
out.insert(1, line);
|
||||
}
|
||||
}
|
||||
Err(e) => out.push(format!("3{}", e)),
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.map_err(|e| {
|
||||
out.push(format!("3{}", e));
|
||||
e
|
||||
});
|
||||
|
||||
out.insert(0, format!("i{}{}:\r\ni", CONFIG_DIR, HISTORY_FILE));
|
||||
out.join("\r\n")
|
||||
}
|
||||
|
||||
pub fn load() -> Result<BufReader<File>> {
|
||||
let dotdir = config_dir_path();
|
||||
if dotdir.is_none() {
|
||||
return Err(error!("{} directory doesn't exist", CONFIG_DIR));
|
||||
}
|
||||
let history = dotdir.unwrap().join(HISTORY_FILE);
|
||||
if let Ok(file) = std::fs::OpenOptions::new().read(true).open(&history) {
|
||||
return Ok(BufReader::new(file));
|
||||
}
|
||||
Err(error!("Couldn't open {:?}", history))
|
||||
}
|
||||
|
||||
// save a single history entry
|
||||
pub fn save(url: &str, label: &str) {
|
||||
let dotdir = config_dir_path();
|
||||
if dotdir.is_none() {
|
||||
return;
|
||||
}
|
||||
let dotdir = dotdir.unwrap();
|
||||
let history = dotdir.join(HISTORY_FILE);
|
||||
if let Ok(mut file) = std::fs::OpenOptions::new()
|
||||
.append(true)
|
||||
.create(true)
|
||||
.open(history)
|
||||
{
|
||||
let (t, host, port, sel) = gopher::parse_url(&url);
|
||||
// ignore internal URLs
|
||||
if host == "help" {
|
||||
return;
|
||||
}
|
||||
file.write_all(
|
||||
format!(
|
||||
"{}{}\t{}\t{}\t{}\r\n",
|
||||
gopher::char_for_type(t).unwrap_or('i'),
|
||||
label,
|
||||
sel,
|
||||
host,
|
||||
port
|
||||
)
|
||||
.as_ref(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Returns None if the config dir doesn't exist.
|
||||
pub fn config_dir_path() -> Option<std::path::PathBuf> {
|
||||
let homevar = std::env::var("HOME");
|
||||
if homevar.is_err() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let dotdir = CONFIG_DIR.replace('~', &homevar.unwrap());
|
||||
let dotdir = std::path::Path::new(&dotdir);
|
||||
if dotdir.exists() {
|
||||
Some(std::path::PathBuf::from(dotdir))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
pub fn save(label: &str, url: &str) {
|
||||
config::append(HISTORY_FILE, label, url);
|
||||
}
|
||||
|
Loading…
Reference in New Issue