err when trying to save bookmark

pull/6/head
dvkt 5 years ago
parent 76d9396c90
commit 5f875d9920

@ -1,6 +1,6 @@
use config;
use std::io::Read;
use std::io::{Read, Result};
const BOOKMARKS_FILE: &str = "bookmarks.gph";
@ -17,6 +17,6 @@ pub fn as_raw_menu() -> String {
}
// save a single history entry
pub fn save(label: &str, url: &str) {
config::append(BOOKMARKS_FILE, label, url);
pub fn save(label: &str, url: &str) -> Result<()> {
config::append(BOOKMARKS_FILE, label, url)
}

@ -6,26 +6,24 @@ pub const DIR: &str = "~/.config/phetch/";
// Loads a file in the config directory for reading.
pub fn load(filename: &str) -> Result<BufReader<File>> {
if let Some(dotdir) = path() {
path().and_then(|dotdir| {
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))
}
} else {
Err(error!("{} directory doesn't exist", DIR))
}
})
}
// Append a menu item as a line to a file in the config dir.
pub fn append(filename: &str, label: &str, url: &str) {
if let Some(dotdir) = path() {
let filename = dotdir.join(filename);
pub fn append(filename: &str, label: &str, url: &str) -> Result<()> {
path().and_then(|dotdir| {
let path = dotdir.join(filename);
if let Ok(mut file) = std::fs::OpenOptions::new()
.append(true)
.create(true)
.open(filename)
.open(path)
{
let (t, host, port, sel) = gopher::parse_url(&url);
file.write_all(
@ -39,23 +37,26 @@ pub fn append(filename: &str, label: &str, url: &str) {
)
.as_ref(),
);
Ok(())
} else {
Err(error!("Can't open file for writing: {:?}", filename))
}
}
})
}
// PathBuf to config dir if it exists.
// None if the config dir doesn't exist.
pub fn path() -> Option<std::path::PathBuf> {
pub fn path() -> Result<std::path::PathBuf> {
let homevar = std::env::var("HOME");
if homevar.is_err() {
return None;
return Err(error!("$HOME not set, can't decode `~`"));
}
let dotdir = DIR.replace('~', &homevar.unwrap());
let dotdir = std::path::Path::new(&dotdir);
if dotdir.exists() {
Some(std::path::PathBuf::from(dotdir))
Ok(std::path::PathBuf::from(dotdir))
} else {
None
Err(error!("Config dir not found: {}", DIR))
}
}

@ -1,5 +1,5 @@
use config;
use std::io::BufRead;
use std::io::{BufRead, Result};
const HISTORY_FILE: &str = "history.gph";
@ -23,6 +23,6 @@ pub fn as_raw_menu() -> String {
}
// save a single history entry
pub fn save(label: &str, url: &str) {
config::append(HISTORY_FILE, label, url);
pub fn save(label: &str, url: &str) -> Result<()> {
config::append(HISTORY_FILE, label, url)
}

@ -470,7 +470,7 @@ impl Menu {
self.input.clear();
if let Some(line) = self.link(self.link) {
let url = line.url.to_string();
let (typ, _, _, _) = gopher::parse_url(&url);
let (typ, host, _, _) = gopher::parse_url(&url);
match typ {
Type::Search => {
if let Some(query) = ui::prompt(&format!("{}> ", line.name)) {
@ -482,10 +482,15 @@ impl Menu {
Type::Error => Action::Error(line.name.to_string()),
Type::Telnet => Action::Error("Telnet support coming soon".into()),
_ => {
let hurl = url.to_string();
let hname = line.name.clone();
thread::spawn(move || history::save(&hurl, &hname));
Action::Open(url)
// don't record internal urls
if host != "help" {
let hurl = url.to_string();
let hname = line.name.clone();
thread::spawn(move || history::save(&hurl, &hname));
Action::Open(url)
} else {
Action::None
}
}
}
} else {

@ -328,8 +328,10 @@ impl UI {
Action::Keypress(Key::Ctrl('s')) => {
if let Some(page) = self.views.get(self.focused) {
let url = page.url();
self.set_status(format!("Saved bookmark: {}", url));
bookmarks::save(&url, &url);
match bookmarks::save(&url, &url) {
Ok(()) => self.set_status(format!("Saved bookmark: {}", url)),
Err(e) => return Err(error!("Save failed: {}", e)),
}
}
}
Action::Keypress(Key::Ctrl('u')) => {

Loading…
Cancel
Save