summaryrefslogtreecommitdiffstats
path: root/src/info.rs
blob: bd9420571ba4fe473f4dce6c876db99a6545f010 (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
pub fn user() -> anyhow::Result<String> {
    Ok(users::get_current_username()
        .ok_or_else(|| anyhow::anyhow!("couldn't get username"))?
        .to_string_lossy()
        .into_owned())
}

#[allow(clippy::unnecessary_wraps)]
pub fn prompt_char() -> anyhow::Result<String> {
    if users::get_current_uid() == 0 {
        Ok("#".into())
    } else {
        Ok("$".into())
    }
}

pub fn hostname() -> anyhow::Result<String> {
    let mut hostname = hostname::get()?.to_string_lossy().into_owned();
    if let Some(idx) = hostname.find('.') {
        hostname.truncate(idx);
    }
    Ok(hostname)
}

#[allow(clippy::unnecessary_wraps)]
pub fn time(offset: time::UtcOffset) -> anyhow::Result<String> {
    Ok(crate::format::time(
        time::OffsetDateTime::now_utc().to_offset(offset),
    ))
}

pub fn pid() -> String {
    nix::unistd::getpid().to_string()
}

// the time crate is currently unable to get the local offset on unix due to
// soundness concerns, so we have to do it manually/:
//
// https://github.com/time-rs/time/issues/380
pub fn get_offset() -> time::UtcOffset {
    let offset_str =
        std::process::Command::new("date").args(&["+%:z"]).output();
    if let Ok(offset_str) = offset_str {
        let offset_str = String::from_utf8(offset_str.stdout).unwrap();
        time::UtcOffset::parse(
            offset_str.trim(),
            &time::format_description::parse("[offset_hour]:[offset_minute]")
                .unwrap(),
        )
        .unwrap_or(time::UtcOffset::UTC)
    } else {
        time::UtcOffset::UTC
    }
}