mirror of
https://github.com/xvxx/phetch
synced 2024-11-15 00:12:50 +00:00
67 lines
1.8 KiB
Rust
67 lines
1.8 KiB
Rust
use crate::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 homepath = format!("{}{}", config::DIR, HISTORY_FILE);
|
|
let path = config::path();
|
|
if let Err(error) = path {
|
|
return format!(
|
|
file_missing_fmt!(),
|
|
file = homepath,
|
|
dir = config::DIR,
|
|
error = error
|
|
);
|
|
}
|
|
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", homepath)];
|
|
config::load(HISTORY_FILE)
|
|
.and_then(|reader| {
|
|
let mut lines = reader.lines();
|
|
while let Some(Ok(line)) = lines.next() {
|
|
out.insert(1, line);
|
|
}
|
|
Ok(())
|
|
})
|
|
.map_err(|e| {
|
|
out.push(format!("3{}", e));
|
|
e
|
|
});
|
|
|
|
if out.len() == 1 {
|
|
out.insert(1, "iNo history entries yet.".to_string());
|
|
}
|
|
out.join("\r\n")
|
|
}
|
|
|
|
// save a single history entry if the history file exists
|
|
pub fn save(label: &str, url: &str) -> Result<()> {
|
|
if let Err(e) = config::path() {
|
|
return Err(error!("History file doesn't exist: {}", e));
|
|
}
|
|
|
|
config::append(HISTORY_FILE, label, url)
|
|
}
|