added ability to save the content of the current view to a file path

pull/38/head
deadjakk 1 year ago
parent 3f2ae945db
commit dfaf10510f

@ -123,6 +123,7 @@ is save bookmark
ia show history ia show history
i i
ir view raw source ir view raw source
ic save current page to file
iw toggle wide mode iw toggle wide mode
ie toggle encoding ie toggle encoding
iq quit phetch iq quit phetch

@ -236,6 +236,23 @@ impl UI {
}) })
} }
/// Used to download the content of the current view
fn save_file(
&mut self,
file_name: &str,
content: &str,
)-> Result<()> {
let mut file = std::fs::OpenOptions::new()
.create_new(true)
.write(true)
.append(true)
.open(file_name)
.map_err(|e| error!("`open` error: {}", e))?;
file.write_all(content.as_bytes())?;
Ok(())
}
/// Download a binary file. Used by `open()` internally. /// Download a binary file. Used by `open()` internally.
fn download(&mut self, url: &str) -> Result<()> { fn download(&mut self, url: &str) -> Result<()> {
let url = url.to_string(); let url = url.to_string();
@ -659,6 +676,19 @@ impl UI {
Action::Keypress(Key::Char(key)) | Action::Keypress(Key::Ctrl(key)) => match key { Action::Keypress(Key::Char(key)) | Action::Keypress(Key::Ctrl(key)) => match key {
'a' => self.open("History", "gopher://phetch/1/history")?, 'a' => self.open("History", "gopher://phetch/1/history")?,
'b' => self.open("Bookmarks", "gopher://phetch/1/bookmarks")?, 'b' => self.open("Bookmarks", "gopher://phetch/1/bookmarks")?,
'c' => {
if let Ok(content) = self.render(){
if let Some(file_name) = self.prompt("Provide a filepath:", ""){
match self.save_file(file_name.as_str(), &content.as_str()){
Ok(()) => {
let msg = format!("Saved file: {}", file_name);
self.set_status(&msg);
}
Err(e) => return Err(error!("Save failed: {}", e)),
}
}
}
}
'g' => { 'g' => {
if let Some(url) = self.prompt("Go to URL: ", "") { if let Some(url) = self.prompt("Go to URL: ", "") {
self.open(&url, &url)?; self.open(&url, &url)?;

Loading…
Cancel
Save