Skip to content

Commit

Permalink
initial gosub:// protocols are created
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Jul 1, 2024
1 parent be1ad44 commit d3734a6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 16 deletions.
1 change: 1 addition & 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 @@ -22,3 +22,4 @@ uuid = { version = "1.7.0", features = ["v4", "serde"] }
chrono = "0.4.35"
once_cell = "1.19.0"
ureq = "2.9.1"
url = "2.5.0"
54 changes: 46 additions & 8 deletions src/dive/tab_manager.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anyhow::Error;
use crate::dive::widgets::status_bar::TabInfo;
use ureq;
use url::Url;

pub struct Tab {
pub name: String,
Expand Down Expand Up @@ -103,22 +105,58 @@ impl TabManager {
}

fn load_content(url: &str) -> Result<String, anyhow::Error> {
if url.starts_with("file://") {
return Ok("File content is not supported".into());
let parts = match Url::parse(url) {
Ok(parts) => parts,
Err(e) => return Err(anyhow::Error::msg(format!("Invalid URL: {}", e))),
};

if parts.scheme() == "file" {
return Ok("File content is not yet supported".into());
}
if parts.scheme() == "data" {
return Ok("Data content is not yet supported".into());
}
if parts.scheme() == "gosub" {
return process_gosub_protocol(parts);
}

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

return Ok(content);
}

if url.starts_with("https://") {
// TLS
if parts.scheme() == "" || parts.scheme() == "http" {
log::warn!("Opening insecure connection to {}", url);
let content = ureq::get(url).call()?.into_string()?;

return Ok(content);
}

// Always assume no protocol defaults to HTTPS://

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

fn process_gosub_protocol(url: Url) -> Result<String, Error> {
match url.host_str() {
Some("blank") => return Ok("This page is left intentionally blank".into()),
Some("help") => return Ok(gosub_help()),
Some("credits") => return Ok("Here be credits for the gosub engine".into()),
Some("settings") => return Ok("Here you can tinker with all kinds of dive and gosub settings".into()),
_ => return Ok("Unknown gosub protocol".into()),
}
}

fn gosub_help() -> String {
return r#"<h1>gosub://help</h1>
<p>This is the help page for the gosub engine</p>
<p>The following special gosub pages are supported:</p>
<table>
<tr><td><a target="_blank"href="gosub://blank">gosub://blank</td><td>Opens a blank page</td></tr>
<tr><td><a target="_blank"href="gosub://help">gosub://help</td><td>Displays this help page</td></tr>
<tr><td><a target="_blank" href="gosub://credits">gosub://credits</td><td>Displays credits of the Dive Browser and the Gosub Engine</td></tr>
<tr><td><a target="_blank" href="gosub://settings">gosub://settings</td><td>Displays the settings page</td></tr>
</table>
"#.into();
}
20 changes: 18 additions & 2 deletions src/dive/widgets/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@ use crate::dive::tab_manager::TabManager;
use crate::dive::ui::get_layout_chunks;
use crate::dive::widget_manager::Drawable;
use crossterm::event::KeyEvent;
use ratatui::layout::Layout;
use ratatui::layout::{Layout, Rect};
use ratatui::prelude::{Constraint, Direction, Stylize};
use ratatui::widgets::{Block, Borders, Clear, ListState, Paragraph, Tabs, Wrap};
use ratatui::widgets::{Block, Borders, Clear, ListState, Paragraph, Tabs, Widget, Wrap};
use ratatui::Frame;
use std::cell::RefCell;
use std::rc::Rc;
use ratatui::buffer::Buffer;

#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
pub struct BackgroundClear;

impl Widget for BackgroundClear {
fn render(self, area: Rect, buf: &mut Buffer) {
for x in area.left()..area.right() {
for y in area.top()..area.bottom() {
buf.get_mut(x, y).reset();
buf.get_mut(x, y).set_char('░');
}
}
}
}

pub struct TabsWidget {
pub tab_manager: Rc<RefCell<TabManager>>,
Expand Down Expand Up @@ -60,6 +75,7 @@ impl Drawable for TabsWidget {

let content = self.tab_manager.borrow().current().content.clone();
let block = Block::default().borders(Borders::NONE).on_dark_gray();

let paragraph = Paragraph::new(content)
.block(block)
.wrap(Wrap { trim: true });
Expand Down
6 changes: 0 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ fn main() -> Result<()> {
app.tab_manager
.borrow_mut()
.open("New Tab", "gosub://blank");
app.tab_manager
.borrow_mut()
.open("Second Tab", "https://gosub.io");
app.tab_manager
.borrow_mut()
.open("Third Tab", "https://news.ycombinator.com");

let w1 = Widget::new(
"splash",
Expand Down

0 comments on commit d3734a6

Please sign in to comment.