diff --git a/src/help.rs b/src/help.rs index 0239ce8..d77dd31 100644 --- a/src/help.rs +++ b/src/help.rs @@ -123,6 +123,7 @@ is save bookmark ia show history i ir view raw source +ic save current page to file iw toggle wide mode ie toggle encoding iq quit phetch diff --git a/src/ui.rs b/src/ui.rs index b5bb1f2..19263f1 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -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. fn download(&mut self, url: &str) -> Result<()> { let url = url.to_string(); @@ -659,6 +676,19 @@ impl UI { Action::Keypress(Key::Char(key)) | Action::Keypress(Key::Ctrl(key)) => match key { 'a' => self.open("History", "gopher://phetch/1/history")?, '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' => { if let Some(url) = self.prompt("Go to URL: ", "") { self.open(&url, &url)?;