bookmarks + history errors

pull/6/head
dvkt 5 years ago
parent 731199837c
commit cc9036b5ce

@ -1,11 +1,29 @@
use config;
use std::io::{Read, Result};
// Bookmarks only work if you've created a ~/.config/phetch/ manually.
const BOOKMARKS_FILE: &str = "bookmarks.gph";
macro_rules! dir_missing_fmt {
() => {
"i\r\ni\r
i\r\ni\x1b[91m{error}\x1b[0m\r
i\r\niRun this in your terminal to enable bookmarking:\r
i\r\nimkdir -p {dir}"
};
}
pub fn as_raw_menu() -> String {
let mut out = format!("i** bookmarks **\r\ni\r\n");
let path = config::path();
if let Err(e) = path {
return format!(dir_missing_fmt!(), error = e, dir = config::DIR);
}
let path = path.unwrap().join(BOOKMARKS_FILE);
if !path.exists() {
out.push_str("iNo bookmarks found.\r\ni\r\niUse <ctrl-s> to bookmark a page.\r\n");
return out;
}
config::load(BOOKMARKS_FILE)
.and_then(|mut reader| reader.read_to_string(&mut out))
@ -16,7 +34,7 @@ pub fn as_raw_menu() -> String {
out
}
// save a single history entry
// save a single bookmark entry
pub fn save(label: &str, url: &str) -> Result<()> {
config::append(BOOKMARKS_FILE, label, url)
}

@ -1,11 +1,42 @@
use config;
use std::io::{BufRead, Result};
// History only works if you've created ~/.config/phetch/history.gph manually.
const HISTORY_FILE: &str = "history.gph";
macro_rules! file_missing_fmt {
() => {
"i\r\ni\r
i\r\ni\x1b[91m{error}\x1b[0m\r
i\r\niHistory is only saved if {file} exists.\r
i\r\niRun this in your terminal to activate automatic history saving:\r
i\r\nimkdir -p {dir} && touch {file}"
};
}
pub fn as_raw_menu() -> String {
let mut out = vec![format!("i{}{}:\r\ni", config::DIR, HISTORY_FILE)];
let homepath = format!("{}{}", config::DIR, HISTORY_FILE);
let path = config::path();
if path.is_err() {
return format!(
file_missing_fmt!(),
file = homepath,
dir = config::DIR,
error = path.unwrap_err()
);
}
let path = path.unwrap();
let file = path.join(HISTORY_FILE);
if !file.exists() {
return format!(
file_missing_fmt!(),
file = homepath,
dir = config::DIR,
error = "No history file found."
);
}
let mut out = vec![format!("i{}:\r\ni", file.to_string_lossy())];
config::load(HISTORY_FILE)
.and_then(|reader| {
let mut lines = reader.lines();
@ -19,10 +50,18 @@ pub fn as_raw_menu() -> String {
e
});
if out.len() == 1 {
out.insert(1, "iNo history entries yet.".to_string());
}
out.join("\r\n")
}
// save a single history entry
// save a single history entry if the history file exists
pub fn save(label: &str, url: &str) -> Result<()> {
let file = format!("{}{}", config::DIR, HISTORY_FILE);
if !std::path::Path::new(&file).exists() {
return Err(error!("History file doesn't exist: {}", file));
}
config::append(HISTORY_FILE, label, url)
}

Loading…
Cancel
Save