Skip to content

Commit f550b35

Browse files
authored
Merge pull request #2193 from Urgau/gh-range-diff-legend-title
Improve and simplify a bit our range-diff UI
2 parents 24d215a + ffc08f2 commit f550b35

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

src/gh_range_diff.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use axum::{
88
http::HeaderValue,
99
response::IntoResponse,
1010
};
11-
use axum_extra::extract::Host;
1211
use hyper::header::CACHE_CONTROL;
1312
use hyper::{
1413
HeaderMap, StatusCode,
@@ -34,7 +33,6 @@ static MARKER_RE: LazyLock<Regex> =
3433
pub async fn gh_range_diff(
3534
Path((owner, repo, basehead)): Path<(String, String, String)>,
3635
State(ctx): State<Arc<Context>>,
37-
host: Host,
3836
) -> axum::response::Result<impl IntoResponse, AppError> {
3937
let Some((oldhead, newhead)) = basehead.split_once("..") else {
4038
return Ok((
@@ -122,7 +120,6 @@ pub async fn gh_range_diff(
122120
let ((oldbase, old), (newbase, new)) = futures::try_join!(old, new)?;
123121

124122
process_old_new(
125-
host,
126123
(&owner, &repo),
127124
(&oldbase, oldhead, old),
128125
(&newbase, newhead, new),
@@ -136,7 +133,6 @@ pub async fn gh_range_diff(
136133
pub async fn gh_ranges_diff(
137134
Path((owner, repo, oldbasehead, newbasehead)): Path<(String, String, String, String)>,
138135
State(ctx): State<Arc<Context>>,
139-
host: Host,
140136
) -> axum::response::Result<impl IntoResponse, AppError> {
141137
let Some((oldbase, oldhead)) = oldbasehead.split_once("..") else {
142138
return Ok((
@@ -191,15 +187,13 @@ pub async fn gh_ranges_diff(
191187
let (old, new) = futures::try_join!(old, new)?;
192188

193189
process_old_new(
194-
host,
195190
(&owner, &repo),
196191
(&oldbase, oldhead, old),
197192
(&newbase, newhead, new),
198193
)
199194
}
200195

201196
fn process_old_new(
202-
Host(host): Host,
203197
(owner, repo): (&str, &str),
204198
(oldbase, oldhead, mut old): (&str, &str, GithubCompare),
205199
(newbase, newhead, mut new): (&str, &str, GithubCompare),
@@ -216,8 +210,10 @@ fn process_old_new(
216210
// Create the HTML buffer with a very rough approximation for the capacity
217211
let mut html: String = String::with_capacity(800 + old.files.len() * 100);
218212

219-
// Compute the bookmarklet for the current host
220-
let bookmarklet = bookmarklet(&host);
213+
let a_oldbase = a_github_commit(owner, repo, oldbase);
214+
let a_oldhead = a_github_commit(owner, repo, oldhead);
215+
let a_newbase = a_github_commit(owner, repo, newbase);
216+
let a_newhead = a_github_commit(owner, repo, newhead);
221217

222218
// Write HTML header, style, ...
223219
writeln!(
@@ -228,7 +224,7 @@ fn process_old_new(
228224
<meta charset="UTF-8">
229225
<meta name="viewport" content="width=device-width, initial-scale=1.0">
230226
<link rel="icon" sizes="32x32" type="image/png" href="https://rust-lang.org/static/images/favicon-32x32.png">
231-
<title>range-diff of {oldbase}...{oldhead} {newbase}...{newhead}</title>
227+
<title>range-diff of {oldbase}..{oldhead} {newbase}..{newhead}</title>
232228
<style>
233229
body {{
234230
font: 14px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
@@ -241,6 +237,10 @@ fn process_old_new(
241237
overflow-wrap: break-word;
242238
white-space: normal;
243239
}}
240+
.commit {{
241+
text-decoration: none;
242+
color: unset;
243+
}}
244244
.diff-content {{
245245
overflow-x: auto;
246246
}}
@@ -256,7 +256,7 @@ fn process_old_new(
256256
color: rgb(220, 0, 0)
257257
}}
258258
.line-added-after {{
259-
color: rgb(0, 73, 0)
259+
color: rgb(0, 221, 0)
260260
}}
261261
.line-removed-before {{
262262
color: rgb(192, 78, 76)
@@ -328,8 +328,8 @@ fn process_old_new(
328328
</style>
329329
</head>
330330
<body>
331-
<h3>range-diff of {oldbase}<wbr>...{oldhead} {newbase}<wbr>...{newhead}</h3>
332-
<p>Bookmarklet: <a href="{bookmarklet}" title="Drag-and-drop me on the bookmarks bar, and use me on GitHub compare page.">range-diff</a> <span title="This javascript bookmark can be used to access this page with the right URL. To use it drag-on-drop the range-diff link to your bookmarks bar and click on it when you are on GitHub's compare page to use range-diff compare.">&#128712;</span> | {REMOVED_BLOCK_SIGN}&nbsp;before | {ADDED_BLOCK_SIGN}&nbsp;after</p>
331+
<h3>range-diff of {a_oldbase}..{a_oldhead} {a_newbase}..{a_newhead} in {owner}/{repo}</h3>
332+
<p>Legend: {REMOVED_BLOCK_SIGN}&nbsp;before | {ADDED_BLOCK_SIGN}&nbsp;after</p>
333333
"#
334334
)?;
335335

@@ -609,24 +609,6 @@ impl UnifiedDiffPrinter for HtmlDiffPrinter<'_> {
609609
}
610610
}
611611

612-
// Create the javascript bookmarklet based on the host
613-
fn bookmarklet(host: &str) -> String {
614-
let protocol = if host.starts_with("localhost:") {
615-
"http"
616-
} else {
617-
"https"
618-
};
619-
620-
format!(
621-
r"javascript:(() => {{
622-
const githubUrlPattern = /^https:\/\/github\.com\/([^\/]+)\/([^\/]+)\/compare\/([^\/]+[.]{{2}}[^\/]+)$/;
623-
const match = window.location.href.match(githubUrlPattern);
624-
if (!match) {{alert('Invalid GitHub Compare URL format.\nExpected: https://github.com/ORG_NAME/REPO_NAME/compare/BASESHA..HEADSHA'); return;}}
625-
const [, orgName, repoName, basehead] = match; window.location = `{protocol}://{host}/gh-range-diff/${{orgName}}/${{repoName}}/${{basehead}}`;
626-
}})();"
627-
)
628-
}
629-
630612
// Simple abstraction over `unicode_segmentation::split_word_bounds` for `imara_diff::TokenSource`
631613
struct SplitWordBoundaries<'a>(&'a str);
632614

@@ -643,3 +625,10 @@ impl<'a> imara_diff::TokenSource for SplitWordBoundaries<'a> {
643625
(self.0.len() as f32 / 4.7f32) as u32
644626
}
645627
}
628+
629+
fn a_github_commit(owner: &str, repo: &str, ref_: &str) -> String {
630+
format!(
631+
r#"<a href="https://github.com/{owner}/{repo}/commit/{ref_}" class="commit">{}</a>"#,
632+
&ref_[..=6]
633+
)
634+
}

0 commit comments

Comments
 (0)