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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//! Crate for getting the user's username, realname and environment.
//!
//! ## Getting Started
//! Using the whoami crate is super easy!  All of the public items are simple
//! functions with no parameters that return `String`s (with the exception of
//! [`env()`](fn.env.html), and [`platform()`](fn.platform.html) which return
//! enums).  The following example shows how to use all of the functions:
//!
//! ```rust
//! fn main() {
//!     print!(
//!         "user's full name     whoami::user():      {}\n\
//!          username             whoami::username():  {}\n\
//!          host's fancy name    whoami::host():      {}\n\
//!          hostname             whoami::hostname():  {}\n\
//!          platform             whoami::platform():  {}\n\
//!          operating system     whoami::os():        {}\n\
//!          desktop environment  whoami::env():       {}\n\
//!          ",
//!         whoami::user(),
//!         whoami::username(),
//!         whoami::host(),
//!         whoami::hostname(),
//!         whoami::platform(),
//!         whoami::os(),
//!         whoami::env(),
//!     );
//! }
//! ```

#![warn(missing_docs)]
#![doc(
    html_logo_url = "https://libcala.github.io/whoami/icon.svg",
    html_favicon_url = "https://libcala.github.io/whoami/icon.svg"
)]

/// Which Desktop Environment
#[allow(missing_docs)]
#[derive(Debug)]
#[non_exhaustive]
pub enum DesktopEnv {
    Gnome,
    Windows,
    Lxde,
    Openbox,
    Mate,
    Xfce,
    Kde,
    Cinnamon,
    I3,
    Mac,
    Ios,
    Android,
    Wasm,
    Console,
    Ubuntu,
    Dive,
    Fuchsia,
    Redox,
    Unknown(String),
}

impl std::fmt::Display for DesktopEnv {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        use self::DesktopEnv::*;

        if let Unknown(_) = self {
            write!(f, "Unknown: ")?;
        }

        write!(
            f,
            "{}",
            match self {
                Gnome => "Gnome",
                Windows => "Windows",
                Lxde => "LXDE",
                Openbox => "Openbox",
                Mate => "Mate",
                Xfce => "XFCE",
                Kde => "KDE",
                Cinnamon => "Cinnamon",
                I3 => "I3",
                Mac => "Mac OS",
                Ios => "IOS",
                Android => "Android",
                Wasm => "Wasm",
                Console => "Console",
                Ubuntu => "Ubuntu",
                Dive => "Dive",
                Fuchsia => "Fuchsia",
                Redox => "Redox",
                Unknown(a) => &a,
            }
        )
    }
}

/// Which Platform
#[allow(missing_docs)]
#[derive(Debug)]
#[non_exhaustive]
pub enum Platform {
    Linux,
    FreeBsd,
    Windows,
    MacOS,
    Ios,
    Android,
    Nintendo,
    Xbox,
    PlayStation,
    Dive,
    Fuchsia,
    Redox,
    Unknown(String),
}

impl std::fmt::Display for Platform {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        use self::Platform::*;

        if let Unknown(_) = self {
            write!(f, "Unknown: ")?;
        }

        write!(
            f,
            "{}",
            match self {
                Linux => "Linux",
                FreeBsd => "Free BSD",
                Windows => "Windows",
                MacOS => "Mac OS",
                Ios => "iOS",
                Android => "Android",
                Nintendo => "Nintendo",
                Xbox => "XBox",
                PlayStation => "PlayStation",
                Dive => "Dive",
                Fuchsia => "Fuchsia",
                Redox => "Redox",
                Unknown(a) => a,
            }
        )
    }
}

#[cfg(all(target_os = "windows", not(target_arch = "wasm32")))]
mod windows;
#[cfg(all(target_os = "windows", not(target_arch = "wasm32")))]
use self::windows as native;
#[cfg(target_arch = "wasm32")]
mod wasm;
#[cfg(target_arch = "wasm32")]
use self::wasm as native;
#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
mod unix;
#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
use self::unix as native;

/// Get the user's username.
#[inline(always)]
pub fn username() -> String {
    native::username()
}

/// Get the user's full name.
#[inline(always)]
pub fn user() -> String {
    native::realname()
}

/// Get the host device's (pretty) name.
#[inline(always)]
pub fn host() -> String {
    native::computer()
}

/// Get the host device's hostname.
#[inline(always)]
pub fn hostname() -> String {
    native::hostname()
}

/// Get the the operating system name and version.
///
/// Example: "Windows 10" or "Fedora 26 (Workstation Edition)"
#[inline(always)]
pub fn os() -> String {
    native::os().unwrap_or_else(|| "Unknown".to_string())
}

/// Get the desktop environment.
///
/// Example: "gnome" or "windows"
#[inline(always)]
pub fn env() -> DesktopEnv {
    native::env()
}

/// Get the platform.
#[inline(always)]
pub fn platform() -> Platform {
    native::platform()
}