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
//! Collection of utility functions for building a prompt

use std::{borrow::Cow, ffi::OsString, path::PathBuf, process::Command};

use anyhow::anyhow;

/// Get the full working directory
pub fn full_pwd() -> String {
    std::env::current_dir()
        .unwrap()
        .into_os_string()
        .into_string()
        .unwrap()
}

/// Get the top level working directory
pub fn top_pwd() -> String {
    let cur_dir = std::env::current_dir().unwrap();
    let home_dir = dirs::home_dir().unwrap();

    if cur_dir == home_dir {
        // home directory case
        String::from("~")
    } else if cur_dir == PathBuf::from("/") {
        // root directory case
        String::from("/")
    } else {
        cur_dir
            .file_name()
            .unwrap()
            .to_os_string()
            .into_string()
            .unwrap()
    }
}

// TODO this is very linux specific, could use crate that abstracts
// TODO this function is disgusting
/// Get the username of the current user
pub fn username() -> anyhow::Result<String> {
    let username = Command::new("whoami").output()?.stdout;
    let encoded = std::str::from_utf8(&username)?
        .strip_suffix("\n")
        .unwrap()
        .to_string();
    Ok(encoded)
}

/// Get the hostname
pub fn hostname() -> anyhow::Result<String> {
    let username = Command::new("hostname").output()?.stdout;
    let encoded = std::str::from_utf8(&username)?
        .strip_suffix("\n")
        .unwrap()
        .to_string();
    Ok(encoded)
}

/// Get the current time
pub fn current_time() {
    todo!()
}