summaryrefslogtreecommitdiffstats
path: root/src/env.rs
blob: 676b20e457acdbcc5b2cf36ba0310afb05c9e179 (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
pub fn pwd() -> anyhow::Result<String> {
    let mut pwd = std::env::current_dir()?.display().to_string();
    if let Ok(home) = std::env::var("HOME") {
        if pwd.starts_with(&home) {
            pwd.replace_range(..home.len(), "~");
        }
    }
    Ok(pwd)
}

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())
}

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),
    ))
}