Skip to content

Commit

Permalink
fix: truncate long urls in table
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Oct 25, 2024
1 parent e50c4f0 commit 5fbce64
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ use crate::{
types::Res,
};

pub fn truncate_middle(text: &str, max_len: usize) -> String {
if text.len() <= max_len {
return text.to_string();
}

let part_len = (max_len - 3) / 2;
let start = &text[..part_len];
let end = &text[text.len() - part_len..];

format!("{}...{}", start, end)
}

pub fn get_header<'a>(req: &'a Request, name: &'a str) -> Option<&'a str> {
match req.headers().get(name) {
Some(x) => Some(x.to_str().unwrap_or_default()),
Expand Down
3 changes: 2 additions & 1 deletion src/routes/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use thousands::Separable;
use crate::db_client::{
DbClient, Direction, PopularFilter, PopularKind, PopularSort, RepoFilter, RepoSort, RepoTotals,
};
use crate::helpers::truncate_middle;
use crate::types::{AppError, HtmlRes};
use crate::AppState;

Expand All @@ -25,7 +26,7 @@ fn maybe_url(item: &(String, Option<String>)) -> Markup {
let (name, url) = item;

match url {
Some(url) => html!(a href=(url) { (name) }),
Some(url) => html!(a href=(url) { (truncate_middle(name, 40)) }),
None => html!(span { (name) }),
}
}
Expand Down

0 comments on commit 5fbce64

Please sign in to comment.