aboutsummaryrefslogtreecommitdiffstats
path: root/src/data.rs
blob: be6ea9f86271a35d6b60bf07f2c49adbfdce92d6 (plain) (blame)
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
use chrono;
use hostname;
use term_size;
use std;
use users;

use args;
use colors;
use power;
use vcs;

pub struct PromptData {
    pub shell: colors::ShellType,
    pub error_code: u8,
    pub hostname: Option<String>,
    pub terminal_cols: Option<usize>,
    pub pwd: Option<std::path::PathBuf>,
    pub home: Option<std::path::PathBuf>,
    pub user: Option<String>,
    pub is_root: bool,
    pub time: chrono::DateTime<chrono::Local>,
    pub power_info: power::PowerInfo,
    pub vcs_info: Option<Box<vcs::VcsInfo>>,
}

pub fn collect(opts: args::CommandLineOptions) -> PromptData {
    start_talking_about_time!("collecting data");

    let hostname = hostname();
    talk_about_time!("hostname");
    let terminal_cols = terminal_cols();
    talk_about_time!("terminal_cols");
    let pwd = pwd();
    talk_about_time!("pwd");
    let home = home();
    talk_about_time!("home");
    let user = user();
    talk_about_time!("user");
    let is_root = is_root();
    talk_about_time!("is_root");
    let time = time();
    talk_about_time!("time");
    let power_info = power_info();
    talk_about_time!("power_info");
    let vcs_info = vcs_info();
    talk_about_time!("vcs_info");

    stop_talking_about_time!();

    PromptData {
        shell: opts.shell,
        error_code: opts.error_code,
        hostname,
        terminal_cols,
        pwd,
        home,
        user,
        is_root,
        time,
        power_info,
        vcs_info,
    }
}

fn hostname() -> Option<String> {
    hostname::get_hostname()
}

fn terminal_cols() -> Option<usize> {
    if let Some((w, _h)) = term_size::dimensions() {
        Some(w)
    }
    else {
        None
    }
}

fn pwd() -> Option<std::path::PathBuf> {
    std::env::var("PWD").map(std::path::PathBuf::from).ok()
}

fn home() -> Option<std::path::PathBuf> {
    std::env::var("HOME").map(std::path::PathBuf::from).ok()
}

fn user() -> Option<String> {
    users::get_current_username()
}

fn is_root() -> bool {
    users::get_current_uid() == 0
}

fn time() -> chrono::DateTime<chrono::Local> {
    chrono::Local::now()
}

fn power_info() -> power::PowerInfo {
    power::PowerInfo::new()
}

fn vcs_info() -> Option<Box<vcs::VcsInfo>> {
    vcs::detect()
}