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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
// WhoAmI
// Copyright © 2017-2021 Jeron Aldaron Lau.
//
// Licensed under any of:
// - Apache License, Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0)
// - MIT License (https://mit-license.org/)
// - Boost Software License, Version 1.0 (https://www.boost.org/LICENSE_1_0.txt)
// At your choosing (See accompanying files LICENSE_APACHE_2_0.txt,
// LICENSE_MIT.txt and LICENSE_BOOST_1_0.txt).
//
//! 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`](std::string::String)s or
//! [`OsString`](std::ffi::OsString)s (with the exception of
//! [`desktop_env()`](crate::desktop_env), and [`platform()`](crate::platform)
//! which return enums, and [`lang()`](crate::lang) that returns an iterator of
//! [`String`](std::string::String)s). The following example shows how to use
//! all of the functions (except those that return
//! [`OsString`](std::ffi::OsString)):
//!
//! ```rust
//! fn main() {
//! println!(
//! "User's Name whoami::realname(): {}",
//! whoami::realname()
//! );
//! println!(
//! "User's Username whoami::username(): {}",
//! whoami::username()
//! );
//! println!(
//! "User's Language whoami::lang(): {:?}",
//! whoami::lang().collect::<Vec<String>>()
//! );
//! println!(
//! "Device's Pretty Name whoami::devicename(): {}",
//! whoami::devicename()
//! );
//! println!(
//! "Device's Hostname whoami::hostname(): {}",
//! whoami::hostname()
//! );
//! println!(
//! "Device's Platform whoami::platform(): {}",
//! whoami::platform()
//! );
//! println!(
//! "Device's OS Distro whoami::distro(): {}",
//! whoami::distro()
//! );
//! println!(
//! "Device's Desktop Env. whoami::desktop_env(): {}",
//! whoami::desktop_env()
//! );
//! }
//! ```
#![warn(missing_docs)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/libcala/whoami/main/res/icon.svg",
html_favicon_url = "https://raw.githubusercontent.com/libcala/whoami/main/res/icon.svg"
)]
use std::ffi::OsString;
/// Which Desktop Environment
#[allow(missing_docs)]
#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum DesktopEnv {
Gnome,
/// One of the desktop environments for a specific version of Windows
Windows,
Lxde,
Openbox,
Mate,
Xfce,
Kde,
Cinnamon,
I3,
/// Default desktop environment for MacOS
Aqua,
/// Desktop environment for iOS
Ios,
/// Desktop environment for Android
Android,
/// Running as Web Assembly on a web page
WebBrowser,
/// A desktop environment for a video game console
Console,
/// Ubuntu-branded GNOME
Ubuntu,
/// Default shell for Fuchsia
Ermine,
/// Default desktop environment for Redox
Orbital,
/// Unknown desktop environment
Unknown(String),
}
impl std::fmt::Display for DesktopEnv {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let DesktopEnv::Unknown(_) = self {
write!(f, "Unknown: ")?;
}
write!(
f,
"{}",
match self {
DesktopEnv::Gnome => "Gnome",
DesktopEnv::Windows => "Windows",
DesktopEnv::Lxde => "LXDE",
DesktopEnv::Openbox => "Openbox",
DesktopEnv::Mate => "Mate",
DesktopEnv::Xfce => "XFCE",
DesktopEnv::Kde => "KDE",
DesktopEnv::Cinnamon => "Cinnamon",
DesktopEnv::I3 => "I3",
DesktopEnv::Aqua => "Aqua",
DesktopEnv::Ios => "IOS",
DesktopEnv::Android => "Android",
DesktopEnv::WebBrowser => "Web Browser",
DesktopEnv::Console => "Console",
DesktopEnv::Ubuntu => "Ubuntu",
DesktopEnv::Ermine => "Ermine",
DesktopEnv::Orbital => "Orbital",
DesktopEnv::Unknown(a) => a,
}
)
}
}
/// Which Platform
#[allow(missing_docs)]
#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum Platform {
Linux,
Bsd,
Windows,
// For now, maybe deprecate and add `Mac`?
#[allow(clippy::upper_case_acronyms)]
MacOS,
Ios,
Android,
Nintendo,
Xbox,
PlayStation,
Fuchsia,
Redox,
Unknown(String),
}
impl std::fmt::Display for Platform {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Platform::Unknown(_) = self {
write!(f, "Unknown: ")?;
}
write!(
f,
"{}",
match self {
Platform::Linux => "Linux",
Platform::Bsd => "BSD",
Platform::Windows => "Windows",
Platform::MacOS => "Mac OS",
Platform::Ios => "iOS",
Platform::Android => "Android",
Platform::Nintendo => "Nintendo",
Platform::Xbox => "XBox",
Platform::PlayStation => "PlayStation",
Platform::Fuchsia => "Fuchsia",
Platform::Redox => "Redox",
Platform::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 username.
#[inline(always)]
pub fn username_os() -> OsString {
native::username_os()
}
/// Get the user's real name.
#[inline(always)]
pub fn realname() -> String {
native::realname()
}
/// Get the user's real name.
#[inline(always)]
pub fn realname_os() -> OsString {
native::realname_os()
}
/// Get the device name (also known as "Pretty Name"), used to identify device
/// for bluetooth pairing.
#[inline(always)]
pub fn devicename() -> String {
native::devicename()
}
/// Get the device name (also known as "Pretty Name"), used to identify device
/// for bluetooth pairing.
#[inline(always)]
pub fn devicename_os() -> OsString {
native::devicename_os()
}
/// Get the host device's hostname.
#[inline(always)]
pub fn hostname() -> String {
native::hostname()
}
/// Get the host device's hostname.
#[inline(always)]
pub fn hostname_os() -> OsString {
native::hostname_os()
}
/// Get the name of the operating system distribution and (possibly) version.
///
/// Example: "Windows 10" or "Fedora 26 (Workstation Edition)"
#[inline(always)]
pub fn distro() -> String {
native::distro().unwrap_or_else(|| format!("Unknown {}", platform()))
}
/// Get the name of the operating system distribution and (possibly) version.
///
/// Example: "Windows 10" or "Fedora 26 (Workstation Edition)"
#[inline(always)]
pub fn distro_os() -> OsString {
native::distro_os()
.unwrap_or_else(|| format!("Unknown {}", platform()).into())
}
/// Get the desktop environment.
///
/// Example: "gnome" or "windows"
#[inline(always)]
pub fn desktop_env() -> DesktopEnv {
native::desktop_env()
}
/// Get the platform.
#[inline(always)]
pub fn platform() -> Platform {
native::platform()
}
/// Get the user's preferred language(s).
///
/// Returned as iterator of two letter language codes (lowercase), optionally
/// followed by a dash (-) and a two letter region code (uppercase). The most
/// preferred language is returned first, followed by next preferred, and so on.
#[inline(always)]
pub fn lang() -> impl Iterator<Item = String> {
native::lang()
}