use gformat

This commit is contained in:
yggverse 2024-09-27 01:16:54 +03:00
parent b408eaf4f2
commit fa95f7ffdd
4 changed files with 23 additions and 30 deletions

View File

@ -1,4 +1,4 @@
use gtk::glib::{markup_escape_text, GString, Regex, RegexCompileFlags, RegexMatchFlags};
use gtk::glib::{gformat, markup_escape_text, GString, Regex, RegexCompileFlags, RegexMatchFlags};
pub enum Level {
H1,
@ -42,18 +42,18 @@ impl Header {
// Init markup
let markup = match level {
Level::H1 => GString::from(format!(
Level::H1 => gformat!(
"<span size=\"xx-large\">{}</span>\n",
markup_escape_text(&text)
)),
Level::H2 => GString::from(format!(
),
Level::H2 => gformat!(
"<span size=\"x-large\">{}</span>\n",
markup_escape_text(&text)
)),
Level::H3 => GString::from(format!(
),
Level::H3 => gformat!(
"<span size=\"large\">{}</span>\n",
markup_escape_text(&text)
)),
),
_ => return None,
};

View File

@ -1,5 +1,5 @@
use gtk::glib::{
markup_escape_text, GString, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags,
gformat, markup_escape_text, GString, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags,
};
pub struct Link {
@ -83,12 +83,12 @@ impl Link {
}
// Markup
markup = GString::from(format!(
markup = gformat!(
"<a href=\"{}\" title=\"{}\"><span underline=\"none\">{}</span></a>\n",
markup_escape_text(&uri.to_str()), // use resolved address for href
markup_escape_text(&link), // show original address for title
markup_escape_text(&name.join(" ")),
));
);
Some(Self {
alt,

View File

@ -1,23 +1,17 @@
use gtk::glib::{markup_escape_text, GString};
use gtk::glib::{gformat, markup_escape_text, GString};
pub struct Plain {
markup: GString,
source: GString,
}
impl Plain {
pub fn from(line: &str) -> Plain {
Self {
markup: GString::from(format!("{}\n", markup_escape_text(line))),
source: GString::from(line),
markup: gformat!("{}\n", markup_escape_text(line)),
}
}
pub fn markup(&self) -> &GString {
&self.markup
}
pub fn source(&self) -> &GString {
&self.source
}
}

View File

@ -8,7 +8,7 @@ use navigation::Navigation;
use gtk::{
gio::{Cancellable, SocketClient, SocketProtocol, TlsCertificateFlags},
glib::{GString, Priority, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags},
glib::{gformat, GString, Priority, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags},
prelude::{
BoxExt, IOStreamExt, InputStreamExtManual, OutputStreamExtManual, SocketClientExt,
WidgetExt,
@ -170,7 +170,7 @@ impl Page {
},
_ => {
meta.borrow_mut().title = GString::from("Oops");
meta.borrow_mut().description = GString::from(format!("Content {mime} not supported"));
meta.borrow_mut().description = gformat!("Content {mime} not supported");
},
}
None => todo!(),
@ -179,7 +179,7 @@ impl Page {
},
_ => {
meta.borrow_mut().title = GString::from("Oops");
meta.borrow_mut().description = GString::from(format!("Status {code} not supported"));
meta.borrow_mut().description = gformat!("Status {code} not supported");
},
}
None => todo!(),
@ -193,7 +193,7 @@ impl Page {
}
Err(e) => {
meta.borrow_mut().title = GString::from("Oops");
meta.borrow_mut().description = GString::from(format!("Failed to read buffer data: {e}"));
meta.borrow_mut().description = gformat!("Failed to read buffer data: {e}");
meta.borrow_mut().progress_fraction = 1.0;
let _ = widget.activate_action(
@ -211,7 +211,7 @@ impl Page {
Err(e) => {
// Update
meta.borrow_mut().title = GString::from("Oops");
meta.borrow_mut().description = GString::from(format!("Failed to read response: {:?}", e));
meta.borrow_mut().description = gformat!("Failed to read response: {:?}", e);
meta.borrow_mut().progress_fraction = 1.0;
let _ = widget.activate_action(
@ -230,7 +230,7 @@ impl Page {
Err(e) => {
// Update
meta.borrow_mut().title = GString::from("Oops");
meta.borrow_mut().description = GString::from(format!("Failed to read request: {:?}", e));
meta.borrow_mut().description = gformat!("Failed to read request: {:?}", e);
meta.borrow_mut().progress_fraction = 1.0;
let _ = widget.activate_action(
@ -249,7 +249,7 @@ impl Page {
Err(e) => {
// Update
meta.borrow_mut().title = GString::from("Oops");
meta.borrow_mut().description = GString::from(format!("Failed to connect: {:?}", e));
meta.borrow_mut().description = gformat!("Failed to connect: {:?}", e);
meta.borrow_mut().progress_fraction = 1.0;
let _ = widget.activate_action(
@ -266,8 +266,7 @@ impl Page {
scheme => {
// Update
meta.borrow_mut().title = GString::from("Oops");
meta.borrow_mut().description =
GString::from(format!("Protocol {scheme} not supported"));
meta.borrow_mut().description = gformat!("Protocol {scheme} not supported");
meta.borrow_mut().progress_fraction = 1.0;
let _ = widget.activate_action("win.update", None);
@ -283,7 +282,7 @@ impl Page {
RegexMatchFlags::DEFAULT,
) {
// Seems request contain some host, try append default scheme
let request_text = GString::from(format!("gemini://{request_text}"));
let request_text = gformat!("gemini://{request_text}");
// Make sure new request conversible to valid URI
match Uri::parse(&request_text, UriFlags::NONE) {
Ok(_) => {
@ -299,10 +298,10 @@ impl Page {
} else {
// Plain text given, make search request to default provider
self.navigation.set_request_text(
&GString::from(format!(
&gformat!(
"gemini://tlgs.one/search?{}",
Uri::escape_string(&request_text, None, false)
)),
),
true, // activate (page reload)
);
}