1
use std::fs;
2
use std::io::Write;
3
use std::process::Command;
4
use std::result::Result::Err;
5
use termion::{color, style};
6

            
7
/// # Panics
8
///
9
/// Can panic if the file can't be written to
10
pub fn log(s: &str) {
11
    let mut file = fs::OpenOptions::new()
12
        .write(true)
13
        .create(true)
14
        .append(true)
15
        .open("bo.log")
16
        .unwrap();
17
    if let Err(e) = writeln!(file, "{}", s) {
18
        eprintln!("Couldn't write to file: {}", e);
19
    }
20
}
21

            
22
7
pub fn zfill(s: &str, fill_by: &str, size: usize) -> String {
23
7
    if size == 0 {
24
1
        return "".to_string();
25
    }
26
6
    format!("{}{}", fill_by.repeat(size - s.len()), s)
27
7
}
28

            
29
3
pub fn red(s: &str) -> String {
30
3
    format!("{}{}{}", color::Fg(color::Red), s, color::Fg(color::Reset))
31
3
}
32

            
33
5
pub fn expand_tilde(s: &str) -> String {
34
5
    if !s.starts_with('~') {
35
4
        return s.to_string();
36
    }
37
1
    s.replace('~', env!("HOME"))
38
5
}
39

            
40
#[must_use]
41
pub fn git_head_short_ref() -> String {
42
    let git_commit = Command::new("git")
43
        .arg("rev-parse")
44
        .arg("--short")
45
        .arg("HEAD")
46
        .output()
47
        .expect("Failed to parse git commit");
48
    String::from_utf8(git_commit.stdout)
49
        .unwrap_or_default()
50
        .trim_end()
51
        .to_string()
52
}
53

            
54
#[must_use]
55
pub fn bo_version() -> String {
56
    if cfg!(debug_assertions) {
57
        format!("{}-{}", env!("CARGO_PKG_VERSION"), git_head_short_ref())
58
    } else {
59
        env!("CARGO_PKG_VERSION").to_string()
60
    }
61
}
62

            
63
147
pub fn as_bold(message: &str) -> String {
64
147
    format!("{}{}{}", style::Bold, message, style::Reset)
65
147
}
66

            
67
#[cfg(test)]
68
#[path = "./utils_test.rs"]
69
mod utils_test;