add line numbers

This commit is contained in:
Michael Zhang 2020-04-21 20:39:49 -05:00
parent 7c9ed689ba
commit 2bfba6f981
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
4 changed files with 95 additions and 13 deletions

View file

@ -162,6 +162,13 @@ impl Fedhub {
tree_name: String,
filepath: Option<PathBuf>,
) -> Result<Response<Body>> {
#[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::<Vec<_>>())
};
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()));

View file

@ -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;
}

View file

@ -3,5 +3,23 @@
{% block content %}
<h1>{{ repo_name }}: {{ tree_name }}: {{ blob.name }}</h1>
<pre>{{ blob.contents }}</pre>
{% if blob.lines %}
<div class="blob-contents">
<div class="left">
{% for _ in blob.lines %}
<a name="L{{ loop.index }}" />
<a href="#L{{ loop.index }}" title="permalink to line {{ loop.index }}">{{ loop.index }}</a>
<br />
{% endfor %}
</div>
<div class="right">
{% for line in blob.lines %}
{{ line }}
<br />
{% endfor %}
</div>
</pre>
{% else %}
This blob contains binary data.
{% endif %}
{% endblock %}

View file

@ -4,14 +4,21 @@
<h1>{{ repo_name }}: {{ tree_name }}{% if filepath %}: {{ filepath }}{% endif %}</h1>
<h3>entries:</h3>
<ul>
{% for entry in entries %}
<li><a href="{{ entry.url | safe }}">{{ entry.name }}</a></li>
{% endfor %}
</ul>
<table class="file-list striped">
<tbody>
{% for entry in entries %}
<tr>
<td>
<a href="{{ entry.url | safe }}">{{ entry.name }}</a>
{% if entry.is_directory %}/{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if readme %}
<div>
<div class="readme">
{{ readme.rendered | safe }}
</div>
{% endif %}