summaryrefslogtreecommitdiffstats
path: root/src/ui.rs
blob: becacb5fc23c383d7ab4e18cb2b6d0fd95cc0175 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! Utility functions for the UI

use std::collections::BTreeMap;
use std::io::Write;
use std::path::Path;

use anyhow::Error;
use anyhow::Result;
use anyhow::anyhow;
use handlebars::Handlebars;
use itertools::Itertools;
use log::error;
use syntect::easy::HighlightLines;
use syntect::highlighting::{ThemeSet, Style};
use syntect::parsing::SyntaxSet;
use syntect::util::{as_24_bit_terminal_escaped, LinesWithEndings};

use crate::package::Package;
use crate::package::ScriptBuilder;
use crate::package::Shebang;
use crate::config::Configuration;

pub fn package_repo_cleanness_check(repo_path: &Path) -> Result<()> {
    if !crate::util::git::repo_is_clean(&repo_path)? {
        error!("Repository not clean, refusing to go on: {}", repo_path.display());
        Err(anyhow!("Repository not clean, refusing to go on: {}", repo_path.display()))
    } else {
        Ok(())
    }
}

pub struct PackagePrintFlags {
    pub print_runtime_deps: bool,
    pub print_build_deps: bool,
    pub print_sources: bool,
    pub print_dependencies: bool,
    pub print_patches: bool,
    pub print_env: bool,
    pub print_flags: bool,
    pub print_deny_images: bool,
    pub print_phases: bool,
    pub print_script: bool,
    pub script_line_numbers: bool,
    pub script_highlighting: bool,
}

pub fn print_packages<'a, I>(out: &mut dyn Write,
                             format: &str,
                             iter: I,
                             config: &Configuration,
                             flags: &PackagePrintFlags)
-> Result<()>
    where I: Iterator<Item = &'a Package>
{
    let mut hb = Handlebars::new();
    hb.register_template_string("package", format)?;

    for (i, package) in iter.enumerate() {
        print_package(out, &hb, i, package, config, flags)?;
    }

    Ok(())
}

fn print_package(out: &mut dyn Write,
                 hb: &Handlebars,
                 i: usize,
                 package: &Package,
                 config: &Configuration,
                 flags: &PackagePrintFlags)
    -> Result<()>
{
    let script = ScriptBuilder::new(&Shebang::from(config.shebang().clone()))
        .build(package, config.available_phases(), *config.strict_script_interpolation())?;

    let script = crate::ui::script_to_printable(script.as_ref(),
        flags.script_highlighting,
        config.script_highlight_theme().as_ref(),
        flags.script_line_numbers)?;

    let mut data = BTreeMap::new();
    data.insert("i"                  , serde_json::Value::Number(serde_json::Number::from(i)));
    data.insert("p"                  , serde_json::to_value(package)?);
    data.insert("script"             , serde_json::Value::String(script));
    data.insert("print_runtime_deps" , serde_json::Value::Bool(flags.print_runtime_deps));
    data.insert("print_build_deps"   , serde_json::Value::Bool(flags.print_build_deps));
    data.insert("print_sources"      , serde_json::Value::Bool(flags.print_sources));
    data.insert("print_dependencies" , serde_json::Value::Bool(flags.print_dependencies));
    data.insert("print_patches"      , serde_json::Value::Bool(flags.print_patches));
    data.insert("print_env"          , serde_json::Value::Bool(flags.print_env));
    data.insert("print_flags"        , serde_json::Value::Bool(flags.print_flags));
    data.insert("print_deny_images"  , serde_json::Value::Bool(flags.print_deny_images));
    data.insert("print_phases"       , serde_json::Value::Bool(flags.print_phases));
    data.insert("print_script"       , serde_json::Value::Bool(flags.print_script));


    hb.render("package", &data)
        .map_err(Error::from)
        .and_then(|r| writeln!(out, "{}", r).map_err(Error::from))
}

pub fn script_to_printable(script: &str,
                          highlight: bool,
                          highlight_theme: Option<&String>,
                          line_numbers: bool)
    -> Result<String>
{
    if highlight {
        if let Some(configured_theme) = highlight_theme {
            // Load these once at the start of your program
            let ps = SyntaxSet::load_defaults_newlines();
            let ts = ThemeSet::load_defaults();

            let syntax = ps
                .find_syntax_by_first_line(script)
                .ok_or_else(|| anyhow!("Failed to load syntax for highlighting script"))?;

            let theme = ts
                .themes
                .get(configured_theme)
                .ok_or_else(|| anyhow!("Theme not available: {}", configured_theme))?;

            let mut h = HighlightLines::new(syntax, &theme);

            let output = LinesWithEndings::from(script)
                .enumerate()
                .map(|(i, line)| {
                    let ranges: Vec<(Style, &str)> = h.highlight(line, &ps);
                    if line_numbers {
                        format!("{:>4} | {}", i, as_24_bit_terminal_escaped(&ranges[..], true))
                    } else {
                        as_24_bit_terminal_escaped(&ranges[..], true)
                    }
                })
                .join("");

            Ok(output)
        } else {
            Err(anyhow!("Highlighting for script enabled, but no theme configured"))
        }
    } else {
        if line_numbers {
            Ok({
                script.lines()
                    .enumerate()
                    .map(|(i, s)| format!("{:>4} | {}", i, s))
                    .join("\n")
            })
        } else {
            Ok(script.to_string())
        }
    }
}