summaryrefslogtreecommitdiffstats
path: root/src/ui/mod.rs
blob: 3796a7b058ec69f97b1fc84105674ad719db7f92 (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
//
// Copyright (c) 2020-2022 science+computing ag and other contributors
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//

//! Utility functions for the UI

use std::path::Path;
use std::path::PathBuf;

use anyhow::Result;
use anyhow::anyhow;
use itertools::Itertools;
use log::error;

use crate::config::Configuration;
use crate::package::Script;

mod package;
pub use crate::ui::package::*;

pub fn package_repo_cleanness_check(repo: &git2::Repository) -> Result<()> {
    if !crate::util::git::repo_is_clean(repo)? {
        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 fn script_to_printable(
    script: &Script,
    highlight: bool,
    highlight_theme: &str,
    line_numbers: bool,
) -> Result<String> {
    let script = if highlight {
        let script = script.highlighted(highlight_theme);
        if line_numbers {
            script
                .lines_numbered()?
                .map(|(i, s)| format!("{:>4} | {}", i, s))
                .join("")
        } else {
            script.lines()?.join("")
        }
    } else if line_numbers {
        script
            .lines_numbered()
            .map(|(i, s)| format!("{:>4} | {}", i, s))
            .join("")
    } else {
        script.to_string()
    };

    Ok(script)
}

pub fn find_linter_command(repo_path: &Path, config: &Configuration) -> Result<Option<PathBuf>> {
    match config.script_linter().as_ref() {
        None => Ok(None),
        Some(linter) => {
            if linter.is_absolute() {
                Ok(Some(linter.to_path_buf()))
            } else {
                let linter = repo_path.join(linter);
                if !linter.is_file() {
                    Err(anyhow!(
                        "Cannot find linter command, searched in: {}",
                        linter.display()
                    ))
                } else {
                    Ok(Some(linter))
                }
            }
        }
    }
}