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 { pub enum Level {
H1, H1,
@ -42,18 +42,18 @@ impl Header {
// Init markup // Init markup
let markup = match level { let markup = match level {
Level::H1 => GString::from(format!( Level::H1 => gformat!(
"<span size=\"xx-large\">{}</span>\n", "<span size=\"xx-large\">{}</span>\n",
markup_escape_text(&text) markup_escape_text(&text)
)), ),
Level::H2 => GString::from(format!( Level::H2 => gformat!(
"<span size=\"x-large\">{}</span>\n", "<span size=\"x-large\">{}</span>\n",
markup_escape_text(&text) markup_escape_text(&text)
)), ),
Level::H3 => GString::from(format!( Level::H3 => gformat!(
"<span size=\"large\">{}</span>\n", "<span size=\"large\">{}</span>\n",
markup_escape_text(&text) markup_escape_text(&text)
)), ),
_ => return None, _ => return None,
}; };

View File

@ -1,5 +1,5 @@
use gtk::glib::{ 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 { pub struct Link {
@ -83,12 +83,12 @@ impl Link {
} }
// Markup // Markup
markup = GString::from(format!( markup = gformat!(
"<a href=\"{}\" title=\"{}\"><span underline=\"none\">{}</span></a>\n", "<a href=\"{}\" title=\"{}\"><span underline=\"none\">{}</span></a>\n",
markup_escape_text(&uri.to_str()), // use resolved address for href markup_escape_text(&uri.to_str()), // use resolved address for href
markup_escape_text(&link), // show original address for title markup_escape_text(&link), // show original address for title
markup_escape_text(&name.join(" ")), markup_escape_text(&name.join(" ")),
)); );
Some(Self { Some(Self {
alt, 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 { pub struct Plain {
markup: GString, markup: GString,
source: GString,
} }
impl Plain { impl Plain {
pub fn from(line: &str) -> Plain { pub fn from(line: &str) -> Plain {
Self { Self {
markup: GString::from(format!("{}\n", markup_escape_text(line))), markup: gformat!("{}\n", markup_escape_text(line)),
source: GString::from(line),
} }
} }
pub fn markup(&self) -> &GString { pub fn markup(&self) -> &GString {
&self.markup &self.markup
} }
pub fn source(&self) -> &GString {
&self.source
}
} }

View File

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