diff --git a/src/lib.rs b/src/lib.rs index eb5a6ad..72fc235 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,6 +162,13 @@ impl Fedhub { tree_name: String, filepath: Option, ) -> Result> { + #[derive(Serialize)] + struct Entry { + name: String, + is_directory: bool, + url: String, + } + let tree = repo.find_tree(tree_id)?; let mut ctx = self.context(); ctx.insert("repo_name", &path); @@ -170,6 +177,14 @@ impl Fedhub { let mut entries = Vec::new(); let mut readme = None; for entry in tree.iter() { + let mut is_directory = false; + match entry.kind().unwrap() { + ObjectType::Blob => {} + ObjectType::Tree => { + is_directory = true; + } + _ => {} + } if let (Some(ObjectType::Blob), Some("README.md")) = (entry.kind(), entry.name()) { let object = entry.to_object(&repo)?; let blob = object.as_blob().unwrap(); @@ -191,11 +206,19 @@ impl Fedhub { entry.name().unwrap() ), }; - entries.push(json!({ - "name": entry.name().unwrap(), - "url": url, - })); + entries.push(Entry { + name: entry.name().unwrap().to_string(), + is_directory, + url + }); } + entries.sort_by(|left, right| { + let mut left_string = (!left.is_directory as u8).to_string(); + left_string += &left.name; + let mut right_string = (!right.is_directory as u8).to_string(); + right_string += &right.name; + left_string.cmp(&right_string) + }); ctx.insert("entries", &entries); if let Some(readme) = readme { ctx.insert( @@ -226,14 +249,14 @@ impl Fedhub { json!(null) } else { let str_contents = String::from_utf8(blob.content().to_vec())?; - json!(str_contents) + json!(str_contents.lines().collect::>()) }; ctx.insert( "blob", &json!({ "name": filepath, "binary": blob.is_binary(), - "contents": contents, + "lines": contents, }), ); return Ok(Response::new(TERA.render("repo_blob.html", &ctx)?.into())); diff --git a/static/main.css b/static/main.css index 78715f0..453278b 100644 --- a/static/main.css +++ b/static/main.css @@ -3,6 +3,7 @@ --text-color: #D4D4D4; --link-color: lightskyblue; --sans-font: "Helvetica", "Arial", "Liberation Sans", sans-serif; + --mono-font: "Roboto Mono", "Roboto Mono for Powerline", "Inconsolata", "Consolas", monospace; } body { @@ -44,3 +45,36 @@ a { a:hover { text-decoration: underline; } + +.file-list { + margin-bottom: 24px; + width: 100%; +} + +.file-list td { + padding: 3px 4px; +} + +table.striped tr:nth-child(even) { + background-color: rgba(255, 255, 255, 0.05); +} + +.blob-contents { + width: 100%; + overflow-x: auto; + font-family: var(--mono-font); + display: flex; +} + +.blob-contents .left { + margin-right: 12px; + text-align: right; +} + +.readme { + border-top: 1px dotted var(--link-color); +} + +.right-justified { + text-align: right; +} \ No newline at end of file diff --git a/templates/repo_blob.html b/templates/repo_blob.html index 78207d9..7852ba2 100644 --- a/templates/repo_blob.html +++ b/templates/repo_blob.html @@ -3,5 +3,23 @@ {% block content %}

{{ repo_name }}: {{ tree_name }}: {{ blob.name }}

-
{{ blob.contents }}
+ {% if blob.lines %} +
+
+ {% for _ in blob.lines %} + + {{ loop.index }} +
+ {% endfor %} +
+
+ {% for line in blob.lines %} + {{ line }} +
+ {% endfor %} +
+ + {% else %} + This blob contains binary data. + {% endif %} {% endblock %} diff --git a/templates/repo_tree.html b/templates/repo_tree.html index 0e1ed52..ed5da46 100644 --- a/templates/repo_tree.html +++ b/templates/repo_tree.html @@ -4,14 +4,21 @@

{{ repo_name }}: {{ tree_name }}{% if filepath %}: {{ filepath }}{% endif %}

entries:

- + + + {% for entry in entries %} + + + + {% endfor %} + +
+ {{ entry.name }} + {% if entry.is_directory %}/{% endif %} +
{% if readme %} -
+
{{ readme.rendered | safe }}
{% endif %}