Expand truncated numbers on mouseover. Close #156

This commit is contained in:
spikecodes 2021-03-20 15:42:47 -07:00
parent ab886d1e67
commit 966e0ce921
No known key found for this signature in database
GPG key ID: 004CECFF9B463BCB
10 changed files with 26 additions and 70 deletions

View file

@ -206,7 +206,7 @@ async fn main() {
app.at("/").get(|r| subreddit::community(r).boxed());
// View Reddit wiki
app.at("/w").get(|_| async move { Ok(redirect("/wiki".to_string())) }.boxed());
app.at("/w").get(|_| async { Ok(redirect("/wiki".to_string())) }.boxed());
app
.at("/w/:page")
.get(|r| async move { Ok(redirect(format!("/wiki/{}", r.param("page").unwrap_or_default()))) }.boxed());

View file

@ -199,7 +199,7 @@ async fn parse_comments(json: &serde_json::Value, post_link: &str, post_author:
distinguished: val(&comment, "distinguished"),
},
score: if data["score_hidden"].as_bool().unwrap_or_default() {
"\u{2022}".to_string()
("\u{2022}".to_string(), "Hidden".to_string())
} else {
format_num(score)
},

View file

@ -20,7 +20,7 @@ struct Subreddit {
url: String,
icon: String,
description: String,
subscribers: String,
subscribers: (String, String),
}
#[derive(Template)]

View file

@ -181,7 +181,7 @@ pub struct Post {
pub body: String,
pub author: Author,
pub permalink: String,
pub score: String,
pub score: (String, String),
pub upvote_ratio: i64,
pub post_type: String,
pub flair: Flair,
@ -191,7 +191,7 @@ pub struct Post {
pub domain: String,
pub rel_time: String,
pub created: String,
pub comments: String,
pub comments: (String, String),
pub gallery: Vec<GalleryMedia>,
}
@ -251,7 +251,7 @@ impl Post {
distinguished: val(post, "distinguished"),
},
score: if data["hide_score"].as_bool().unwrap_or_default() {
"\u{2022}".to_string()
("\u{2022}".to_string(), "Hidden".to_string())
} else {
format_num(score)
},
@ -307,7 +307,7 @@ pub struct Comment {
pub post_author: String,
pub body: String,
pub author: Author,
pub score: String,
pub score: (String, String),
pub rel_time: String,
pub created: String,
pub edited: (String, String),
@ -342,8 +342,8 @@ pub struct Subreddit {
pub description: String,
pub info: String,
pub icon: String,
pub members: String,
pub active: String,
pub members: (String, String),
pub active: (String, String),
pub wiki: bool,
}
@ -414,8 +414,8 @@ pub fn format_url(url: &str) -> String {
Regex::new(regex)
.map(|re| match re.captures(url) {
Some(caps) => match segments {
1 => [format, &caps[1], "/"].join(""),
2 => [format, &caps[1], "/", &caps[2], "/"].join(""),
1 => [format, &caps[1]].join(""),
2 => [format, &caps[1], "/", &caps[2]].join(""),
_ => String::new(),
},
None => String::new(),
@ -443,21 +443,23 @@ pub fn format_url(url: &str) -> String {
// Rewrite Reddit links to Libreddit in body of text
pub fn rewrite_urls(text: &str) -> String {
match Regex::new(r#"href="(https|http|)://(www.|old.|np.|)(reddit).(com)/"#) {
match Regex::new(r#"href="(https|http|)://(www.|old.|np.|amp.|)(reddit).(com)/"#) {
Ok(re) => re.replace_all(text, r#"href="/"#).to_string(),
Err(_) => String::new(),
}
}
// Append `m` and `k` for millions and thousands respectively
pub fn format_num(num: i64) -> String {
if num >= 1_000_000 || num <= -1_000_000 {
pub fn format_num(num: i64) -> (String, String) {
let truncated = if num >= 1_000_000 || num <= -1_000_000 {
format!("{}m", num / 1_000_000)
} else if num >= 1000 || num <= -1000 {
format!("{}k", num / 1_000)
} else {
num.to_string()
}
};
(truncated, num.to_string())
}
// Parse a relative and absolute time from a UNIX timestamp
@ -539,49 +541,3 @@ pub async fn error(req: Request<Body>, msg: String) -> Result<Response<Body>, St
Ok(Response::builder().status(404).header("content-type", "text/html").body(body.into()).unwrap_or_default())
}
// #[async_recursion]
// async fn connect(path: String) -> io::Result<String> {
// // Construct an HTTP request body
// let req = format!(
// "GET {} HTTP/1.1\r\nHost: www.reddit.com\r\nAccept: */*\r\nConnection: close\r\nUser-Agent: {}\r\n\r\n",
// path, user_agent
// );
// // Open a TCP connection
// let tcp_stream = TcpStream::connect("www.reddit.com:443").await?;
// // Initialize TLS connector for requests
// let connector = TlsConnector::default();
// // Use the connector to start the handshake process
// let mut tls_stream = connector.connect("www.reddit.com", tcp_stream).await?;
// // Write the crafted HTTP request to the stream
// tls_stream.write_all(req.as_bytes()).await?;
// // And read the response
// let mut writer = Vec::new();
// io::copy(&mut tls_stream, &mut writer).await?;
// let response = String::from_utf8_lossy(&writer).to_string();
// let split = response.split("\r\n\r\n").collect::<Vec<&str>>();
// let headers = split[0].split("\r\n").collect::<Vec<&str>>();
// let status: i16 = headers[0].split(' ').collect::<Vec<&str>>()[1].parse().unwrap_or(200);
// let body = split[1].to_string();
// if (300..400).contains(&status) {
// let location = headers
// .iter()
// .find(|header| header.starts_with("location:"))
// .map(|f| f.to_owned())
// .unwrap_or_default()
// .split(": ")
// .collect::<Vec<&str>>()[1];
// connect(location.replace("https://www.reddit.com", "")).await
// } else {
// Ok(body)
// }
// }

View file

@ -5,7 +5,7 @@
{% else if kind == "t1" %}
<div id="{{ id }}" class="comment">
<div class="comment_left">
<p class="comment_score">{{ score }}</p>
<p class="comment_score" title="{{ score.1 }}">{{ score.0 }}</p>
<div class="line"></div>
</div>
<details class="comment_right" open>

View file

@ -89,7 +89,7 @@
<!-- POST BODY -->
<div class="post_body">{{ post.body }}</div>
<div class="post_score">{{ post.score }}<span class="label"> Upvotes</span></div>
<div class="post_score" title="{{ post.score.1 }}">{{ post.score.0 }}<span class="label"> Upvotes</span></div>
<div class="post_footer">
<ul id="post_links">
<li><a href="/{{ post.id }}">permalink</a></li>

View file

@ -39,7 +39,7 @@
<p class="search_subreddit_header">
<span class="search_subreddit_name">{{ subreddit.name }}</span>
<span class="dot">&bull;</span>
<span class="search_subreddit_members">{{ subreddit.subscribers }} Members</span>
<span class="search_subreddit_members" title="{{ subreddit.subscribers.1 }} Members">{{ subreddit.subscribers.0 }} Members</span>
</p>
<p class="search_subreddit_description">{{ subreddit.description }}</p>
</div>
@ -55,7 +55,7 @@
{% else %}
<div class="comment">
<div class="comment_left">
<p class="comment_score">{{ post.score }}</p>
<p class="comment_score" title="{{ post.score.1 }}">{{ post.score.0 }}</p>
<div class="line"></div>
</div>
<details class="comment_right" open>

View file

@ -81,8 +81,8 @@
<div id="sub_details">
<label>Members</label>
<label>Active</label>
<div>{{ sub.members }}</div>
<div>{{ sub.active }}</div>
<div title="{{ sub.members.1 }}">{{ sub.members.0 }}</div>
<div title="{{ sub.active.1 }}">{{ sub.active.0 }}</div>
</div>
<div id="sub_subscription">
{% if prefs.subscriptions.contains(sub.name) %}

View file

@ -37,7 +37,7 @@
{% else %}
<div class="comment">
<div class="comment_left">
<p class="comment_score">{{ post.score }}</p>
<p class="comment_score" title="{{ post.score.1 }}">{{ post.score.0 }}</p>
<div class="line"></div>
</div>
<details class="comment_right" open>

View file

@ -108,9 +108,9 @@
</a>
{% endif %}
<div class="post_score">{{ post.score }}<span class="label"> Upvotes</span></div>
<div class="post_score" title="{{ post.score.1 }}">{{ post.score.0 }}<span class="label"> Upvotes</span></div>
<div class="post_footer">
<a href="{{ post.permalink }}" class="post_comments">{{ post.comments }} comments</a>
<a href="{{ post.permalink }}" class="post_comments" title="{{ post.comments.1 }} comments">{{ post.comments.0 }} comments</a>
</div>
</div>
{%- endmacro %}