Skip to content

Commit

Permalink
Add breadcrumbs to tree view
Browse files Browse the repository at this point in the history
  • Loading branch information
w4 committed Oct 16, 2024
1 parent 98383dc commit abe0c20
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/database/indexer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
borrow::Cow,
collections::HashSet,
ffi::OsStr,
fmt::Debug,
Expand Down
8 changes: 8 additions & 0 deletions src/methods/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ pub fn format_time(s: impl Into<Timestamp>) -> Result<String, askama::Error> {
.map_err(askama::Error::Custom)
}

pub fn branch_query(branch: Option<&str>) -> String {
if let Some(b) = branch {
format!("?h={b}")
} else {
String::new()
}
}

pub fn timeago(s: impl Into<Timestamp>) -> Result<String, askama::Error> {
Ok(timeago::Formatter::new()
.convert((OffsetDateTime::now_utc() - s.into().0).try_into().unwrap()))
Expand Down
16 changes: 10 additions & 6 deletions src/methods/repo/tree.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{
fmt::{Display, Formatter},
sync::Arc,
};

use askama::Template;
use axum::{extract::Query, response::IntoResponse, Extension};
use itertools::Itertools;
use serde::Deserialize;
use std::path::PathBuf;
use std::{
fmt::{Display, Formatter},
sync::Arc,
};

use crate::{
git::{FileWithContent, PathDestination, TreeItem},
Expand Down Expand Up @@ -51,13 +51,15 @@ pub struct TreeView {
pub repo: Repository,
pub items: Vec<TreeItem>,
pub query: UriQuery,
pub repo_path: PathBuf,
pub branch: Option<Arc<str>>,
}

#[derive(Template)]
#[template(path = "repo/file.html")]
pub struct FileView {
pub repo: Repository,
pub repo_path: PathBuf,
pub file: FileWithContent,
pub branch: Option<Arc<str>>,
}
Expand All @@ -73,7 +75,7 @@ pub async fn handle(

Ok(
match open_repo
.path(child_path, query.id.as_deref(), !query.raw)
.path(child_path.clone(), query.id.as_deref(), !query.raw)
.await?
{
PathDestination::Tree(items) => {
Expand All @@ -82,6 +84,7 @@ pub async fn handle(
items,
branch: query.branch.clone(),
query,
repo_path: child_path.unwrap_or_default(),
})))
}
PathDestination::File(file) if query.raw => ResponseEither::Right(file.content),
Expand All @@ -90,6 +93,7 @@ pub async fn handle(
repo,
file,
branch: query.branch,
repo_path: child_path.unwrap_or_default(),
})))
}
},
Expand Down
9 changes: 9 additions & 0 deletions statics/sass/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ nav {
}
}

aside {
background: #f7f7f7;
padding: 0.3rem 2rem;

@media (prefers-color-scheme: dark) {
background: #111;
}
}

main {
padding: 2rem;
margin: 0;
Expand Down
4 changes: 4 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ <h1>
</nav>
{%- endblock -%}

<aside>
{%- block subnav %}{% endblock %}
</aside>

<main>
{%- block content %}{% endblock -%}
</main>
Expand Down
5 changes: 5 additions & 0 deletions templates/repo/file.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% import "macros/link.html" as link %}
{% import "macros/breadcrumbs.html" as breadcrumbs %}
{% extends "repo/base.html" %}

{% block head %}
Expand All @@ -8,6 +9,10 @@

{% block tree_nav_class %}active{% endblock %}

{% block subnav %}
{% call breadcrumbs::breadcrumbs(repo_path, filters::branch_query(branch.as_deref())) %}
{% endblock %}

{% block extra_nav_links %}
<a href="?raw=true{% call link::maybe_branch_suffix(branch) %}">plain</a>
{% endblock %}
Expand Down
11 changes: 11 additions & 0 deletions templates/repo/macros/breadcrumbs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{%- macro breadcrumbs(repo_path, query) -%}
path:&nbsp;
<a href="/{{ repo.display() }}/tree/{{ query }}">{{ repo.display() }}</a>
{%- for child in repo_path.ancestors().collect_vec().into_iter().rev() -%}
{%- if let Some(file_name) = child.file_name() -%}
/<a href="/{{ repo.display() }}/tree/{{ child.display() }}{{ query }}">
{{- file_name.to_string_lossy() -}}
</a>
{%- endif -%}
{%- endfor -%}
{%- endmacro -%}
5 changes: 5 additions & 0 deletions templates/repo/tree.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{% import "macros/breadcrumbs.html" as breadcrumbs %}
{% extends "repo/base.html" %}

{% block tree_nav_class %}active{% endblock %}

{% block subnav %}
{% call breadcrumbs::breadcrumbs(repo_path, query) %}
{% endblock %}

{% block content %}
<div class="table-responsive">
<table class="repositories">
Expand Down

0 comments on commit abe0c20

Please sign in to comment.