Skip to content

Commit

Permalink
Merge pull request #14 from gosub-browser/content-in-tab
Browse files Browse the repository at this point in the history
Added content to tabs
  • Loading branch information
jaytaph authored Jan 11, 2024
2 parents d0c5260 + 6548d91 commit 007ad0f
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 10 deletions.
172 changes: 172 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ filetreelist = "0.5.0"
uuid = { version = "1.6.1", features = ["v4", "serde"] }
chrono = "0.4.31"
once_cell = "1.19.0"
ureq = "2.9.1"
16 changes: 13 additions & 3 deletions src/dive/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ impl App {
self.status_bar
.borrow_mut()
.status(format!("Switched to tab {}", digit).as_str());
self.status_bar
.borrow_mut()
.tab_info(Some(self.tab_manager.borrow().current().info()));
}
}
// Show help
Expand Down Expand Up @@ -155,13 +158,19 @@ impl App {
self.status_bar
.borrow_mut()
.status(format!("Switched to tab {}", idx).as_str());
self.status_bar
.borrow_mut()
.tab_info(Some(self.tab_manager.borrow().current().info()));
}
// switch to next tab
KeyCode::Tab => {
let idx = self.tab_manager.borrow_mut().next();
self.status_bar
.borrow_mut()
.status(format!("Switched to tab {}", idx).as_str());
self.status_bar
.borrow_mut()
.tab_info(Some(self.tab_manager.borrow().current().info()));
}
// Asks and opens URL in new tab
Char('g') if key.modifiers.contains(KeyModifiers::CONTROL) => {
Expand Down Expand Up @@ -268,6 +277,9 @@ impl App {
self.status_bar
.borrow_mut()
.status(format!("Opened new tab {}", idx).as_str());
self.status_bar
.borrow_mut()
.tab_info(Some(self.tab_manager.borrow().current().info()));
}
Some(Command::CloseTab { idx }) => {
if self.tab_manager.borrow().len() == 1 {
Expand All @@ -279,11 +291,9 @@ impl App {
self.status_bar
.borrow_mut()
.status(format!("Closed tab {}", idx).as_str());

self.tab_manager.borrow_mut().close(idx);
self.status_bar
.borrow_mut()
.status(format!("Closed tab {}", idx).as_str());
.tab_info(Some(self.tab_manager.borrow().current().info()));
}
}
}
Expand Down
41 changes: 40 additions & 1 deletion src/dive/tab_manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
use crate::dive::widgets::status_bar::TabInfo;
use ureq;

pub struct Tab {
pub name: String,
pub url: String,
pub content: String,
pub secure: bool,
}

impl Tab {
pub fn info(&self) -> TabInfo {
TabInfo {
name: self.name.clone(),
url: self.url.clone(),
secure: self.secure,
}
}
}

pub struct TabManager {
Expand All @@ -28,12 +42,16 @@ impl TabManager {
}

pub fn open(&mut self, name: &str, url: &str) -> usize {
let tab = Tab {
let mut tab = Tab {
name: name.into(),
url: url.into(),
content: String::new(),
secure: false,
};

tab.secure = url.starts_with("https://");
tab.content = load_content(url).expect("Failed to load content").clone();

self.tabs.push(tab);
log::debug!("Opening new tab: {}", url);

Expand Down Expand Up @@ -83,3 +101,24 @@ impl TabManager {
self.tabs.len()
}
}

fn load_content(url: &str) -> Result<String, anyhow::Error> {
if url.starts_with("file://") {
return Ok("File content is not supported".into());
}

if url.starts_with("http://") {
let content = ureq::get(url).call()?.into_string()?;

return Ok(content);
}

if url.starts_with("https://") {
// TLS
let content = ureq::get(url).call()?.into_string()?;

return Ok(content);
}

Ok("Unknown protocol".into())
}
Loading

0 comments on commit 007ad0f

Please sign in to comment.