fewer Strings, more refs

pull/14/head
chris west 4 years ago
parent ae280b4a61
commit 3c321c53da

@ -89,8 +89,8 @@ impl View for Menu {
self.tor
}
fn raw(&self) -> String {
self.raw.to_string()
fn raw(&self) -> &str {
self.raw.as_ref()
}
fn render(&mut self, cfg: &Config) -> String {
@ -105,15 +105,15 @@ impl View for Menu {
self.size = (cols, rows);
}
fn url(&self) -> String {
self.url.to_string()
fn url(&self) -> &str {
self.url.as_ref()
}
}
impl Menu {
/// Create a representation of a Gopher Menu from a raw Gopher
/// response and a few options.
pub fn from(url: String, response: String, tls: bool, tor: bool) -> Menu {
pub fn from(url: &str, response: &str, tls: bool, tor: bool) -> Menu {
Menu {
tls,
tor,
@ -760,7 +760,7 @@ impl Menu {
}
/// Parse gopher response into a Menu object.
pub fn parse(url: String, raw: String) -> Menu {
pub fn parse(url: &str, raw: &str) -> Menu {
let mut lines = vec![];
let mut links = vec![];
let mut longest = 0;
@ -850,11 +850,11 @@ impl Menu {
}
Menu {
url,
url: url.into(),
lines,
links,
longest,
raw,
raw: url.into(),
input: String::new(),
link: 0,
mode: Default::default(),
@ -874,7 +874,7 @@ mod tests {
macro_rules! parse {
($s:literal) => {
Menu::parse("test".to_string(), $s.to_string());
Menu::parse("test", $s);
};
}

@ -47,12 +47,12 @@ impl View for Text {
self.tor
}
fn url(&self) -> String {
self.url.to_string()
fn url(&self) -> &str {
self.url.as_ref()
}
fn raw(&self) -> String {
self.raw_response.to_string()
fn raw(&self) -> &str {
self.raw_response.as_ref()
}
fn term_size(&mut self, cols: usize, rows: usize) {
@ -161,7 +161,7 @@ impl View for Text {
impl Text {
/// Create a Text View from a raw Gopher response and a few options.
pub fn from(url: String, response: String, tls: bool, tor: bool) -> Text {
pub fn from(url: &str, response: &str, tls: bool, tor: bool) -> Text {
let mut lines = 0;
let mut longest = 0;
for line in response.split_terminator('\n') {
@ -172,8 +172,8 @@ impl Text {
}
Text {
url,
raw_response: response,
url: url.into(),
raw_response: response.into(),
scroll: 0,
lines,
longest,

@ -159,7 +159,7 @@ impl UI {
self.status.clear();
}
if let Err(e) = self.process_action(action) {
self.set_status(format!("{}{}{}", color::Red, e, termion::cursor::Hide));
self.set_status(&format!("{}{}{}", color::Red, e, termion::cursor::Hide));
}
}
@ -213,11 +213,14 @@ impl UI {
})
.and_then(|res| res)
.and_then(|(path, bytes)| {
self.set_status(format!(
"Download complete! {} saved to {}",
utils::human_bytes(bytes),
path
));
self.set_status(
format!(
"Download complete! {} saved to {}",
utils::human_bytes(bytes),
path
)
.as_ref(),
);
Ok(())
})
}
@ -243,8 +246,8 @@ impl UI {
};
let (typ, _, _, _) = gopher::parse_url(&url);
match typ {
Type::Menu | Type::Search => Ok(Box::new(Menu::from(url.to_string(), res, tls, tor))),
Type::Text | Type::HTML => Ok(Box::new(Text::from(url.to_string(), res, tls, tor))),
Type::Menu | Type::Search => Ok(Box::new(Menu::from(url, &res, tls, tor))),
Type::Text | Type::HTML => Ok(Box::new(Text::from(url, &res, tls, tor))),
_ => Err(error!("Unsupported Gopher Response: {:?}", typ)),
}
}
@ -255,7 +258,7 @@ impl UI {
&url.trim_start_matches("gopher://phetch/")
.trim_start_matches("1/"),
) {
Ok(Box::new(Menu::from(url.to_string(), source, false, false)))
Ok(Box::new(Menu::from(url, &source, false, false)))
} else {
Err(error!("phetch URL not found: {}", url))
}
@ -338,7 +341,7 @@ impl UI {
}
/// Set the status line's content.
fn set_status(&mut self, status: String) {
fn set_status(&mut self, status: &str) {
self.status = status.replace('\n', "\\n").replace('\r', "\\r");
}
@ -550,7 +553,7 @@ impl UI {
out.write_all(s.as_ref());
out.flush();
}
Action::Status(s) => self.set_status(s),
Action::Status(s) => self.set_status(&s),
Action::Open(title, url) => self.open(&title, &url)?,
Action::Prompt(query, fun) => {
if let Some(response) = self.prompt(&query, "") {
@ -591,7 +594,10 @@ impl UI {
if let Some(page) = self.views.get(self.focused) {
let url = page.url();
match bookmarks::save(&url, &url) {
Ok(()) => self.set_status(format!("Saved bookmark: {}", url)),
Ok(()) => {
let msg = format!("Saved bookmark: {}", url);
self.set_status(&msg);
}
Err(e) => return Err(error!("Save failed: {}", e)),
}
}
@ -610,7 +616,8 @@ impl UI {
if let Some(page) = self.views.get(self.focused) {
let url = page.url();
copy_to_clipboard(&url)?;
self.set_status(format!("Copied {} to clipboard.", url));
let msg = format!("Copied {} to clipboard.", url);
self.set_status(&msg);
}
}
'w' => {

@ -14,9 +14,9 @@ pub trait View: fmt::Display {
/// Was this View's content fetched over Tor?
fn is_tor(&self) -> bool;
/// The Gopher URL this View represents.
fn url(&self) -> String;
fn url(&self) -> &str;
/// The raw Gopher representation of this View.
fn raw(&self) -> String;
fn raw(&self) -> &str;
/// Set the current screen size.
fn term_size(&mut self, cols: usize, rows: usize);
}

Loading…
Cancel
Save