From c3c597897c8f6ffb53868fd43d702ada21aa7299 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 10 Aug 2023 22:04:30 +0200 Subject: [PATCH] Add typed commit links --- src/server/web.rs | 9 +++++---- src/server/web/commit.rs | 29 ++++++++--------------------- src/server/web/index.rs | 16 +++++++--------- src/server/web/link.rs | 36 ++++++++++++++++++++++++++++++++++++ src/server/web/runner.rs | 2 +- static/base.css | 22 ++++++++++++++++------ templates/commit.html | 15 ++++----------- templates/index.html | 12 ++---------- templates/queue_table.html | 2 +- templates/util.html | 8 ++------ 10 files changed, 82 insertions(+), 69 deletions(-) create mode 100644 src/server/web/link.rs diff --git a/src/server/web.rs b/src/server/web.rs index 0b021a6..e1ff2be 100644 --- a/src/server/web.rs +++ b/src/server/web.rs @@ -1,6 +1,7 @@ mod api; mod commit; mod index; +mod link; mod queue; mod runner; mod r#static; @@ -12,8 +13,8 @@ use crate::{config::Config, somehow}; use super::Server; pub enum Tab { + None, Index, - Commit, Queue, } @@ -21,20 +22,20 @@ pub enum Tab { pub struct Base { root: String, repo_name: String, - current: String, + current: &'static str, } impl Base { pub fn new(config: &Config, tab: Tab) -> Self { let current = match tab { + Tab::None => "", Tab::Index => "index", - Tab::Commit => "commit", Tab::Queue => "queue", }; Self { root: config.web_base.clone(), repo_name: config.repo_name.clone(), - current: current.to_string(), + current, } } } diff --git a/src/server/web/commit.rs b/src/server/web/commit.rs index 913d073..5cf0435 100644 --- a/src/server/web/commit.rs +++ b/src/server/web/commit.rs @@ -9,23 +9,8 @@ use sqlx::SqlitePool; use crate::{config::Config, server::util, somehow}; -use super::{Base, Tab}; +use super::{Base, Tab, link::CommitLink}; -struct Commit { - hash: String, - short: String, - reachable: i64, -} - -impl Commit { - fn new(hash: String, message: &str, reachable: i64) -> Self { - Self { - short: util::format_commit_short(&hash, message), - hash, - reachable, - } - } -} #[derive(Template)] #[template(path = "commit.html")] @@ -36,8 +21,8 @@ struct CommitTemplate { author_date: String, commit: String, commit_date: String, - parents: Vec, - children: Vec, + parents: Vec, + children: Vec, summary: String, message: String, reachable: i64, @@ -48,6 +33,8 @@ pub async fn get( State(config): State<&'static Config>, State(db): State, ) -> somehow::Result { + let base = Base::new(config, Tab::None); + let Some(commit) = sqlx::query!( "\ SELECT \ @@ -79,7 +66,7 @@ pub async fn get( hash ) .fetch(&db) - .map_ok(|r| Commit::new(r.hash, &r.message, r.reachable)) + .map_ok(|r| CommitLink::new(&base, r.hash, &r.message, r.reachable)) .try_collect::>() .await?; @@ -93,12 +80,12 @@ pub async fn get( hash ) .fetch(&db) - .map_ok(|r| Commit::new(r.hash, &r.message, r.reachable)) + .map_ok(|r| CommitLink::new(&base, r.hash, &r.message, r.reachable)) .try_collect::>() .await?; Ok(CommitTemplate { - base: Base::new(config, Tab::Commit), + base, hash: commit.hash, author: commit.author, author_date: util::format_time(commit.author_date), diff --git a/src/server/web/index.rs b/src/server/web/index.rs index 71ec780..131f839 100644 --- a/src/server/web/index.rs +++ b/src/server/web/index.rs @@ -3,15 +3,13 @@ use axum::{extract::State, response::IntoResponse}; use futures::TryStreamExt; use sqlx::SqlitePool; -use crate::{config::Config, server::util, somehow}; +use crate::{config::Config, somehow}; -use super::{Base, Tab}; +use super::{link::CommitLink, Base, Tab}; struct Ref { name: String, - hash: String, - short: String, - reachable: i64, + commit: CommitLink, tracked: bool, } @@ -27,6 +25,8 @@ pub async fn get( State(config): State<&'static Config>, State(db): State, ) -> somehow::Result { + let base = Base::new(config, Tab::Index); + let refs = sqlx::query!( "\ SELECT name, hash, message, reachable, tracked \ @@ -37,10 +37,8 @@ pub async fn get( ) .fetch(&db) .map_ok(|r| Ref { - short: util::format_commit_short(&r.hash, &r.message), - name: r.name, - hash: r.hash, - reachable: r.reachable, + name: r.name.clone(), + commit: CommitLink::new(&base, r.hash, &r.message, r.reachable), tracked: r.tracked != 0, }) .try_collect::>() diff --git a/src/server/web/link.rs b/src/server/web/link.rs new file mode 100644 index 0000000..823d91c --- /dev/null +++ b/src/server/web/link.rs @@ -0,0 +1,36 @@ +use askama::Template; + +use crate::server::util; + +use super::Base; + +#[derive(Template)] +#[template( + ext = "html", + source = " +{% import \"util.html\" as util %} + + + {{ short }} + +" +)] +pub struct CommitLink { + root: String, + hash: String, + short: String, + reachable: i64, +} + +impl CommitLink { + pub fn new(base: &Base, hash: String, message: &str, reachable: i64) -> Self { + Self { + root: base.root.clone(), + short: util::format_commit_short(&hash, message), + hash, + reachable, + } + } +} diff --git a/src/server/web/runner.rs b/src/server/web/runner.rs index 0c1da68..912774f 100644 --- a/src/server/web/runner.rs +++ b/src/server/web/runner.rs @@ -34,7 +34,7 @@ pub async fn get( }; Ok(RunnerTemplate { - base: Base::new(config, Tab::Commit), + base: Base::new(config, Tab::None), name, last_seen: util::format_time(info.last_seen), } diff --git a/static/base.css b/static/base.css index cd15a4f..5aab179 100644 --- a/static/base.css +++ b/static/base.css @@ -21,12 +21,6 @@ a:hover { font-weight: bold; } -dl { - display: grid; - grid: auto-flow / min-content 1fr; - column-gap: 1ch; -} - dd { margin-left: 4ch; } @@ -117,6 +111,12 @@ nav a:hover { margin: 0; } +.commit dl { + display: grid; + grid: auto-flow / max-content 1fr; + column-gap: 1ch; +} + .commit .hash { color: #b70; font-weight: bold; @@ -129,6 +129,16 @@ nav a:hover { /* Runner */ +.runner * { + margin: 0; +} + +.runner dl { + display: grid; + grid: auto-flow / max-content 1fr; + column-gap: 1ch; +} + .runner .name { color: #380; font-weight: bold; diff --git a/templates/commit.html b/templates/commit.html index 1600b11..9ca8893 100644 --- a/templates/commit.html +++ b/templates/commit.html @@ -22,22 +22,15 @@ {% for commit in parents %}
Parent:
-
- - {% call util::commit_short(commit.short, commit.reachable)%} - -
+
{{ commit|safe }}
{% endfor %} {% for commit in children %}
Child:
-
- - {% call util::commit_short(commit.short, commit.reachable)%} - -
+
{{ commit|safe }}
{% endfor %} -
{{ message }}
+
{{ message }}
{% endblock %} diff --git a/templates/index.html b/templates/index.html index 0403805..814bacd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -10,11 +10,7 @@
{% for ref in tracked_refs %}
{{ ref.name }}
-
- - {% call util::commit_short(ref.short, ref.reachable) %} - -
+
{{ ref.commit|safe }}
{% endfor %}
@@ -23,11 +19,7 @@
{% for ref in untracked_refs %}
{{ ref.name }}
-
- - {% call util::commit_short(ref.short, ref.reachable) %} - -
+
{{ ref.commit|safe }}
{% endfor %}
diff --git a/templates/queue_table.html b/templates/queue_table.html index ea90828..1b031d4 100644 --- a/templates/queue_table.html +++ b/templates/queue_table.html @@ -13,7 +13,7 @@ - {% call util::commit_short(task.short, task.reachable) %} + {# {% call util::commit_short(task.short, task.reachable) %} #} {{ task.since }} diff --git a/templates/util.html b/templates/util.html index aa9fead..0925d62 100644 --- a/templates/util.html +++ b/templates/util.html @@ -1,4 +1,4 @@ -{% macro r_class(reachable) %} +{% macro commit_class(reachable) %} {%- if reachable == 0 -%} orphaned {%- else if reachable == 1 -%} @@ -8,7 +8,7 @@ tracked {%- endif -%} {% endmacro %} -{% macro r_title(reachable) %} +{% macro commit_title(reachable) %} {%- if reachable == 0 -%} This commit is orphaned. It can't be reached from any ref. {%- else if reachable == 1 -%} @@ -17,7 +17,3 @@ This commit can only be reached from untracked refs. This commit can be reached from a tracked ref. {%- endif -%} {% endmacro %} - -{% macro commit_short(short, reachable) -%} -{{ short }} -{%- endmacro %}