whoami/
lib.rs

1//! Crate for getting the user's username, realname and environment.
2//!
3//! ## Getting Started
4//! 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`, which returns an enum).  The following example shows how to use all of the functions:
5//!
6//! ```rust
7//! fn main() {
8//!     print!(
9//!         "--------------------------------------\n\
10//!          user's full name (user):              {}\n\
11//!          username (username):                  {}\n\
12//!          --------------------------------------\n\
13//!          host's fancy name (host):             {}\n\
14//!          hostname (hostname):                  {}\n\
15//!          --------------------------------------\n\
16//!          platform (platform):                  {}\n\
17//!          operating system (os):                {}\n\
18//!          desktop environment (env):            {}\n\
19//!          --------------------------------------\n\
20//!          ",
21//!         whoami::user(),
22//!         whoami::username(),
23//!         whoami::host(),
24//!         whoami::hostname(),
25//!         whoami::platform(),
26//!         whoami::os(),
27//!         whoami::env(),
28//!     );
29//! }
30//! ```
31
32#![warn(missing_docs)]
33#![doc(
34    html_logo_url = "https://libcala.github.io/whoami/icon.svg",
35    html_favicon_url = "https://libcala.github.io/whoami/icon.svg"
36)]
37
38/// Which Desktop Environment
39#[allow(missing_docs)]
40pub enum DesktopEnv {
41    Gnome,
42    Windows,
43    Lxde,
44    Openbox,
45    Mate,
46    Xfce,
47    Kde,
48    Cinnamon,
49    I3,
50    Mac,
51    Ios,
52    Android,
53    Wasm,
54    Console,
55    Ubuntu,
56    Dive,
57    Fuchsia,
58    Redox,
59    Unknown(String),
60}
61
62impl std::fmt::Display for DesktopEnv {
63    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
64        use self::DesktopEnv::*;
65
66        write!(
67            f,
68            "{}",
69            match self {
70                Gnome => "Gnome".to_string(),
71                Windows => "Windows".to_string(),
72                Lxde => "LXDE".to_string(),
73                Openbox => "Openbox".to_string(),
74                Mate => "Mate".to_string(),
75                Xfce => "XFCE".to_string(),
76                Kde => "KDE".to_string(),
77                Cinnamon => "Cinnamon".to_string(),
78                I3 => "I3".to_string(),
79                Mac => "Mac OS".to_string(),
80                Ios => "IOS".to_string(),
81                Android => "Android".to_string(),
82                Wasm => "Wasm".to_string(),
83                Console => "Console".to_string(),
84                Ubuntu => "Ubuntu".to_string(),
85                Dive => "Dive".to_string(),
86                Fuchsia => "Fuchsia".to_string(),
87                Redox => "Redox".to_string(),
88                Unknown(a) => format!("Unknown: \"{}\"", a),
89            }
90        )
91    }
92}
93
94/// Which Platform
95#[allow(missing_docs)]
96pub enum Platform {
97    Linux,
98    FreeBsd,
99    Windows,
100    MacOS,
101    Ios,
102    Android,
103    Nintendo,
104    Xbox,
105    PlayStation,
106    Dive,
107    Fuchsia,
108    Redox,
109    Unknown(String),
110}
111
112impl std::fmt::Display for Platform {
113    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
114        use self::Platform::*;
115
116        write!(
117            f,
118            "{}",
119            match self {
120                Linux => "Linux".to_string(),
121                FreeBsd => "Free BSD".to_string(),
122                Windows => "Windows".to_string(),
123                MacOS => "Mac OS".to_string(),
124                Ios => "iOS".to_string(),
125                Android => "Android".to_string(),
126                Nintendo => "Nintendo".to_string(),
127                Xbox => "XBox".to_string(),
128                PlayStation => "PlayStation".to_string(),
129                Dive => "Dive".to_string(),
130                Fuchsia => "Fuchsia".to_string(),
131                Redox => "Redox".to_string(),
132                Unknown(a) => format!("Unknown: \"{}\"", a),
133            }
134        )
135    }
136}
137
138#[cfg(target_os = "windows")]
139mod windows;
140#[cfg(target_os = "windows")]
141use self::windows as native;
142#[cfg(target_arch = "wasm32")]
143mod wasm;
144#[cfg(target_arch = "wasm32")]
145use self::wasm as native;
146#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
147mod unix;
148#[cfg(not(any(target_os = "windows", target_arch = "wasm32")))]
149use self::unix as native;
150
151/// Get the user's username.
152#[inline(always)]
153pub fn username() -> String {
154    native::username()
155}
156
157/// Get the user's full name.
158#[inline(always)]
159pub fn user() -> String {
160    native::realname()
161}
162
163/// Get the host device's (pretty) name.
164#[inline(always)]
165pub fn host() -> String {
166    native::computer()
167}
168
169/// Get the host device's hostname.
170#[inline(always)]
171pub fn hostname() -> String {
172    native::hostname()
173}
174
175/// Get the the operating system name and version.
176///
177/// Example: "Windows 10" or "Fedora 26 (Workstation Edition)"
178#[inline(always)]
179pub fn os() -> String {
180    native::os()
181}
182
183/// Get the desktop environment.
184///
185/// Example: "gnome" or "windows"
186#[inline(always)]
187pub fn env() -> DesktopEnv {
188    native::env()
189}
190
191/// Get the platform.
192#[inline(always)]
193pub fn platform() -> Platform {
194    native::platform()
195}