~/selfhut

selfhut/src/repository/blame.rs -rw-r--r-- 2.55 kB
0016c2cc — arthurmelton clippy 2 years ago
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::config::CONFIG;
use crate::git::blame::blame;
use crate::git::commits::get_commits;
use crate::git::file::file;

use crate::utils::repo_config::repo_config;
use crate::PathBufWithDotfiles;
use rocket_dyn_templates::{context, Template};
use std::ffi::OsStr;
use std::path::Path;
use syntect::highlighting::ThemeSet;
use syntect::html::highlighted_html_for_string;
use syntect::parsing::SyntaxSet;

#[get("/<repo>/blame/<branch>/<location..>", rank = 2)]
pub fn blames(repo: String, branch: String, location: PathBufWithDotfiles) -> Option<Template> {
    let blames = blame(
        repo.clone(),
        branch.clone(),
        location.get().display().to_string(),
    );
    let file = file(
        repo.clone(),
        branch.clone(),
        location.get().display().to_string(),
    )?;
    let mut content = "".to_string();
    let mut lines: Vec<usize> = (1..1).collect();
    if file.1.is_some() {
        let ps = SyntaxSet::load_defaults_newlines();
        let ts = ThemeSet::load_defaults();
        let syntax = match ps.find_syntax_by_extension(
            Path::new(&file.0.name)
                .extension()
                .and_then(OsStr::to_str)
                .unwrap_or("txt"),
        ) {
            Some(x) => x,
            None => ps.find_syntax_by_extension("txt").unwrap(),
        };
        let s = file.1.as_ref().unwrap();
        lines = (1..s.lines().count() + 1).collect();
        content =
            highlighted_html_for_string(s, &ps, syntax, &ts.themes["Solarized (light)"]).ok()?
    }
    Some(Template::render(
        "repository/blame",
        context! {
            title: format!("/{} :: {}", location.get().display(), repo),
            repo: repo.clone(),
            config: repo_config(repo.clone()),
            domain: CONFIG.domain.to_string(),
            user: CONFIG.name.to_string(),
            active: "tree",
            commit: match get_commits(repo, 1, Some(branch.clone()), Some(format!("{}", location.get().display()).replace("//", "/"))) {
                Some(x) => x.get(0).map(|x| x.clone()),
                None => None
            },
            files: file,
            content,
            lines,
            fluid: "true",
            blame: blames,
            branch,
            current_dir_file: format!("/{}/", location.get().display()).replace("//", "/"),
            current_dir: format!("/{}", location.get().display()),
            payment: CONFIG.payment_link.clone(),
            mailing_list: CONFIG.mailing_list.clone()
        },
    ))
}