feat: Add operating system module (#4109)
* docs(os): Add os module documentation * docs(os): Add os to Default Prompt Format * chore(os): Update config file schema * feat(os): Add os entries and declarations * feat(os): Add os module and config * fix(os): Obey config.disabled * feat(os): make variables 'Unknown'-aware refactor(os): calculate variables in dedicated functions * test(os): Add os module tests * feat(os): make 'name' variable less 'Unknown'-aware * docs(os): Add Preset configurations docs(os): Use emoji as default * feat(os): Use emoji as default test(os): Use emoji as default * fix(os): Add spaces after emoji symbols * chore(os): Update config schema * feat(os): Remove `bitness` variable docs(os): Remove `bitness` variable test(os): Remove `bitness` test * feat(os): Add Cargo.toml upgrade caution for os_info * refactor(os): Clarify get_symbol function * docs(os): Mention supported operating systems and feature requests * docs(os): Mention os_info inacurracy * test(os): Remove `bitness` leftovers * refactor(os): use nu_ansi_term * refactor(os): add cfg_attr(schemars(deny_unknown_fields)) * chore(os): update config schema * docs(os): expose details block * feat(os): add garuda linux * chore(os): update config schema * feat(os): add case insensitivity * feat(os): add symbols `IndexMap` use `os_info::Type` instead of `String` * test(os): add clippy warn on new os_info::Type case * leave missing case to test github tests * test(os): re-add missing test case * style(os): fix formatting * docs(os): update to match os_info::Type serialization - docs(os): add missing garuda to config - test(os): mention docs updates in warn_on_os_info_update
This commit is contained in:
parent
0ba8533e2d
commit
3109943822
|
@ -1027,6 +1027,56 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"os": {
|
||||
"default": {
|
||||
"disabled": true,
|
||||
"format": "[$symbol]($style)",
|
||||
"style": "bold white",
|
||||
"symbols": {
|
||||
"Alpine": "🏔️ ",
|
||||
"Amazon": "🙂 ",
|
||||
"Android": "🤖 ",
|
||||
"Arch": "🎗️ ",
|
||||
"CentOS": "💠 ",
|
||||
"Debian": "🌀 ",
|
||||
"DragonFly": "🐉 ",
|
||||
"Emscripten": "🔗 ",
|
||||
"EndeavourOS": "🚀 ",
|
||||
"Fedora": "🎩 ",
|
||||
"FreeBSD": "😈 ",
|
||||
"Garuda": "🦅 ",
|
||||
"Gentoo": "🗜️ ",
|
||||
"HardenedBSD": "🛡️ ",
|
||||
"Illumos": "🐦 ",
|
||||
"Linux": "🐧 ",
|
||||
"Macos": "🍎 ",
|
||||
"Manjaro": "🥭 ",
|
||||
"Mariner": "🌊 ",
|
||||
"MidnightBSD": "🌘 ",
|
||||
"Mint": "🌿 ",
|
||||
"NetBSD": "🚩 ",
|
||||
"NixOS": "❄️ ",
|
||||
"OpenBSD": "🐡 ",
|
||||
"OracleLinux": "🦴 ",
|
||||
"Pop": "🍭 ",
|
||||
"Raspbian": "🍓 ",
|
||||
"RedHatEnterprise": "🎩 ",
|
||||
"Redhat": "🎩 ",
|
||||
"Redox": "🧪 ",
|
||||
"SUSE": "🦎 ",
|
||||
"Solus": "⛵ ",
|
||||
"Ubuntu": "🎯 ",
|
||||
"Unknown": "❓ ",
|
||||
"Windows": "🪟 ",
|
||||
"openSUSE": "🦎 "
|
||||
}
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/OSConfig"
|
||||
}
|
||||
]
|
||||
},
|
||||
"package": {
|
||||
"default": {
|
||||
"disabled": false,
|
||||
|
@ -3989,6 +4039,68 @@
|
|||
},
|
||||
"additionalProperties": false
|
||||
},
|
||||
"OSConfig": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"format": {
|
||||
"default": "[$symbol]($style)",
|
||||
"type": "string"
|
||||
},
|
||||
"style": {
|
||||
"default": "bold white",
|
||||
"type": "string"
|
||||
},
|
||||
"symbols": {
|
||||
"default": {
|
||||
"Alpine": "🏔️ ",
|
||||
"Amazon": "🙂 ",
|
||||
"Android": "🤖 ",
|
||||
"Arch": "🎗️ ",
|
||||
"CentOS": "💠 ",
|
||||
"Debian": "🌀 ",
|
||||
"DragonFly": "🐉 ",
|
||||
"Emscripten": "🔗 ",
|
||||
"EndeavourOS": "🚀 ",
|
||||
"Fedora": "🎩 ",
|
||||
"FreeBSD": "😈 ",
|
||||
"Garuda": "🦅 ",
|
||||
"Gentoo": "🗜️ ",
|
||||
"HardenedBSD": "🛡️ ",
|
||||
"Illumos": "🐦 ",
|
||||
"Linux": "🐧 ",
|
||||
"Macos": "🍎 ",
|
||||
"Manjaro": "🥭 ",
|
||||
"Mariner": "🌊 ",
|
||||
"MidnightBSD": "🌘 ",
|
||||
"Mint": "🌿 ",
|
||||
"NetBSD": "🚩 ",
|
||||
"NixOS": "❄️ ",
|
||||
"OpenBSD": "🐡 ",
|
||||
"OracleLinux": "🦴 ",
|
||||
"Pop": "🍭 ",
|
||||
"Raspbian": "🍓 ",
|
||||
"RedHatEnterprise": "🎩 ",
|
||||
"Redhat": "🎩 ",
|
||||
"Redox": "🧪 ",
|
||||
"SUSE": "🦎 ",
|
||||
"Solus": "⛵ ",
|
||||
"Ubuntu": "🎯 ",
|
||||
"Unknown": "❓ ",
|
||||
"Windows": "🪟 ",
|
||||
"openSUSE": "🦎 "
|
||||
},
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"disabled": {
|
||||
"default": true,
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
},
|
||||
"PackageConfig": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
|
|
@ -60,6 +60,7 @@ notify-rust = { version = "4.5.10", optional = true }
|
|||
nu-ansi-term = "0.46.0"
|
||||
once_cell = "1.16.0"
|
||||
open = "3.0.3"
|
||||
# update os module config and tests when upgrading os_info
|
||||
os_info = "3.5.1"
|
||||
path-slash = "0.2.1"
|
||||
pest = "2.4.1"
|
||||
|
|
|
@ -109,6 +109,9 @@ format = '\[[$symbol($version)]($style)\]'
|
|||
[openstack]
|
||||
format = '\[[$symbol$cloud(\($project\))]($style)\]'
|
||||
|
||||
[os]
|
||||
format = '\[[$symbol]($style)\]'
|
||||
|
||||
[package]
|
||||
format = '\[[$symbol$version]($style)\]'
|
||||
|
||||
|
|
|
@ -64,6 +64,44 @@ symbol = " "
|
|||
[nodejs]
|
||||
symbol = " "
|
||||
|
||||
[os.symbols]
|
||||
Alpine = " "
|
||||
Amazon = " "
|
||||
Android = " "
|
||||
Arch = " "
|
||||
CentOS = " "
|
||||
Debian = " "
|
||||
DragonFly = " "
|
||||
Emscripten = " "
|
||||
EndeavourOS = " "
|
||||
Fedora = " "
|
||||
FreeBSD = " "
|
||||
Garuda = " "
|
||||
Gentoo = " "
|
||||
HardenedBSD = "ﲊ "
|
||||
Illumos = " "
|
||||
Linux = " "
|
||||
Macos = " "
|
||||
Manjaro = " "
|
||||
Mariner = " "
|
||||
MidnightBSD = " "
|
||||
Mint = " "
|
||||
NetBSD = " "
|
||||
NixOS = " "
|
||||
OpenBSD = " "
|
||||
openSUSE = " "
|
||||
OracleLinux = " "
|
||||
Pop = " "
|
||||
Raspbian = " "
|
||||
Redhat = " "
|
||||
RedHatEnterprise = " "
|
||||
Redox = " "
|
||||
Solus = "ﴱ "
|
||||
SUSE = " "
|
||||
Ubuntu = " "
|
||||
Unknown = " "
|
||||
Windows = " "
|
||||
|
||||
[package]
|
||||
symbol = " "
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
format = """
|
||||
[](#9A348E)\
|
||||
$os\
|
||||
$username\
|
||||
[](bg:#DA627D fg:#9A348E)\
|
||||
$directory\
|
||||
|
@ -28,12 +29,20 @@ $time\
|
|||
# Disable the blank line at the start of the prompt
|
||||
# add_newline = false
|
||||
|
||||
# You can also replace your username with a neat symbol like to save some space
|
||||
# You can also replace your username with a neat symbol like or disable this
|
||||
# and use the os module below
|
||||
[username]
|
||||
show_always = true
|
||||
style_user = "bg:#9A348E"
|
||||
style_root = "bg:#9A348E"
|
||||
format = '[$user ]($style)'
|
||||
disabled = false
|
||||
|
||||
# An alternative to the username module which displays a symbol that
|
||||
# represents the current operating system
|
||||
[os]
|
||||
style = "bg:#9A348E"
|
||||
disabled = true # Disabled by default
|
||||
|
||||
[directory]
|
||||
style = "bg:#DA627D"
|
||||
|
|
|
@ -103,6 +103,44 @@ symbol = "ml "
|
|||
[opa]
|
||||
symbol = "opa "
|
||||
|
||||
[os.symbols]
|
||||
Alpine = "alp "
|
||||
Amazon = "amz "
|
||||
Android = "andr "
|
||||
Arch = "rch "
|
||||
CentOS = "cent "
|
||||
Debian = "deb "
|
||||
DragonFly = "dfbsd "
|
||||
Emscripten = "emsc "
|
||||
EndeavourOS = "ndev "
|
||||
Fedora = "fed "
|
||||
FreeBSD = "fbsd "
|
||||
Garuda = "garu "
|
||||
Gentoo = "gent "
|
||||
HardenedBSD = "hbsd "
|
||||
Illumos = "lum "
|
||||
Linux = "lnx "
|
||||
Macos = "mac "
|
||||
Manjaro = "mjo "
|
||||
Mariner = "mrn "
|
||||
MidnightBSD = "mid "
|
||||
Mint = "mint "
|
||||
NetBSD = "nbsd "
|
||||
NixOS = "nix "
|
||||
OpenBSD = "obsd "
|
||||
openSUSE = "osuse "
|
||||
OracleLinux = "orac "
|
||||
Pop = "pop "
|
||||
Raspbian = "rasp "
|
||||
Redhat = "rhl "
|
||||
RedHatEnterprise = "rhel "
|
||||
Redox = "redox "
|
||||
Solus = "sol "
|
||||
SUSE = "suse "
|
||||
Ubuntu = "ubnt "
|
||||
Unknown = "unk "
|
||||
Windows = "win "
|
||||
|
||||
[package]
|
||||
symbol = "pkg "
|
||||
|
||||
|
|
|
@ -331,6 +331,7 @@ $jobs\
|
|||
$battery\
|
||||
$time\
|
||||
$status\
|
||||
$os\
|
||||
$container\
|
||||
$shell\
|
||||
$character"""
|
||||
|
@ -2782,6 +2783,108 @@ style = 'bold yellow'
|
|||
symbol = '☁️ '
|
||||
```
|
||||
|
||||
## OS
|
||||
|
||||
The `os` module shows the current operating system.
|
||||
OS information is detected via the [os_info](https://lib.rs/crates/os_info) crate.
|
||||
|
||||
::: warning
|
||||
|
||||
The [os_info](https://lib.rs/crates/os_info) crate used by this module is known to be inaccurate on some systems.
|
||||
|
||||
:::
|
||||
|
||||
::: tip
|
||||
|
||||
This module is disabled by default.
|
||||
To enable it, set `disabled` to `false` in your configuration file.
|
||||
|
||||
:::
|
||||
|
||||
### Options
|
||||
|
||||
| Option | Default | Description |
|
||||
| ---------- | --------------------- | ------------------------------------------------------ |
|
||||
| `format` | `"[$symbol]($style)"` | The format for the module. |
|
||||
| `style` | `"bold white"` | The style for the module. |
|
||||
| `disabled` | `true` | Disables the `os` module. |
|
||||
| `symbols` | | A table that maps each operating system to its symbol. |
|
||||
|
||||
`symbols` allows you to define arbitrary symbols to display for each operating system type.
|
||||
Operating system types not defined by your configuration use the default symbols table below.
|
||||
All operating systems currently supported by the module are listed below.
|
||||
If you would like an operating system to be added, feel free to open a [feature request](https://github.com/starship/starship/issues/new/choose).
|
||||
|
||||
```toml
|
||||
# This is the default symbols table.
|
||||
[os.symbols]
|
||||
Alpine = "🏔️ "
|
||||
Amazon = "🙂 "
|
||||
Android = "🤖 "
|
||||
Arch = "🎗️ "
|
||||
CentOS = "💠 "
|
||||
Debian = "🌀 "
|
||||
DragonFly = "🐉 "
|
||||
Emscripten = "🔗 "
|
||||
EndeavourOS = "🚀 "
|
||||
Fedora = "🎩 "
|
||||
FreeBSD = "😈 "
|
||||
Garuda = "🦅 "
|
||||
Gentoo = "🗜️ "
|
||||
HardenedBSD = "🛡️ "
|
||||
Illumos = "🐦 "
|
||||
Linux = "🐧 "
|
||||
Macos = "🍎 "
|
||||
Manjaro = "🥭 "
|
||||
Mariner = "🌊 "
|
||||
MidnightBSD = "🌘 "
|
||||
Mint = "🌿 "
|
||||
NetBSD = "🚩 "
|
||||
NixOS = "❄️ "
|
||||
OpenBSD = "🐡 "
|
||||
openSUSE = "🦎 "
|
||||
OracleLinux = "🦴 "
|
||||
Pop = "🍭 "
|
||||
Raspbian = "🍓 "
|
||||
Redhat = "🎩 "
|
||||
RedHatEnterprise = "🎩 "
|
||||
Redox = "🧪 "
|
||||
Solus = "⛵ "
|
||||
SUSE = "🦎 "
|
||||
Ubuntu = "🎯 "
|
||||
Unknown = "❓ "
|
||||
Windows = "🪟 "
|
||||
```
|
||||
|
||||
### Variables
|
||||
|
||||
| Variable | Example | Description |
|
||||
| -------- | ------------ | ------------------------------------------------------------------ |
|
||||
| symbol | `🎗️` | The current operating system symbol from advanced option `symbols` |
|
||||
| name | `Arch Linux` | The current operating system name |
|
||||
| type | `Arch` | The current operating system type |
|
||||
| codename | | The current operating system codename, if applicable |
|
||||
| edition | | The current operating system edition, if applicable |
|
||||
| version | | The current operating system version, if applicable |
|
||||
| style\* | | Mirrors the value of option `style` |
|
||||
|
||||
*: This variable can only be used as a part of a style string
|
||||
|
||||
### Example
|
||||
|
||||
```toml
|
||||
# ~/.config/starship.toml
|
||||
|
||||
[os]
|
||||
format = "on [($name )]($style)"
|
||||
style = "bold blue"
|
||||
disabled = false
|
||||
|
||||
[os.symbols]
|
||||
Windows = " "
|
||||
Arch = "Arch is the best! "
|
||||
```
|
||||
|
||||
## Package Version
|
||||
|
||||
The `package` module is shown when the current directory is the repository for a
|
||||
|
|
|
@ -54,6 +54,7 @@ pub mod nodejs;
|
|||
pub mod ocaml;
|
||||
pub mod opa;
|
||||
pub mod openstack;
|
||||
pub mod os;
|
||||
pub mod package;
|
||||
pub mod perl;
|
||||
pub mod php;
|
||||
|
@ -203,6 +204,8 @@ pub struct FullConfig<'a> {
|
|||
#[serde(borrow)]
|
||||
openstack: openstack::OspConfig<'a>,
|
||||
#[serde(borrow)]
|
||||
os: os::OSConfig<'a>,
|
||||
#[serde(borrow)]
|
||||
package: package::PackageConfig<'a>,
|
||||
#[serde(borrow)]
|
||||
perl: perl::PerlConfig<'a>,
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
use indexmap::{indexmap, IndexMap};
|
||||
use os_info::Type;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
#[cfg_attr(
|
||||
feature = "config-schema",
|
||||
derive(schemars::JsonSchema),
|
||||
schemars(deny_unknown_fields)
|
||||
)]
|
||||
#[serde(default)]
|
||||
pub struct OSConfig<'a> {
|
||||
pub format: &'a str,
|
||||
pub style: &'a str,
|
||||
pub symbols: IndexMap<Type, &'a str>,
|
||||
pub disabled: bool,
|
||||
}
|
||||
|
||||
impl<'a> OSConfig<'a> {
|
||||
pub fn get_symbol(&self, key: &Type) -> Option<&'a str> {
|
||||
self.symbols.get(key).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Default for OSConfig<'a> {
|
||||
fn default() -> Self {
|
||||
OSConfig {
|
||||
format: "[$symbol]($style)",
|
||||
style: "bold white",
|
||||
symbols: indexmap! {
|
||||
Type::Alpine => "🏔️ ",
|
||||
Type::Amazon => "🙂 ",
|
||||
Type::Android => "🤖 ",
|
||||
Type::Arch => "🎗️ ",
|
||||
Type::CentOS => "💠 ",
|
||||
Type::Debian => "🌀 ",
|
||||
Type::DragonFly => "🐉 ",
|
||||
Type::Emscripten => "🔗 ",
|
||||
Type::EndeavourOS => "🚀 ",
|
||||
Type::Fedora => "🎩 ",
|
||||
Type::FreeBSD => "😈 ",
|
||||
Type::Garuda => "🦅 ",
|
||||
Type::Gentoo => "🗜️ ",
|
||||
Type::HardenedBSD => "🛡️ ",
|
||||
Type::Illumos => "🐦 ",
|
||||
Type::Linux => "🐧 ",
|
||||
Type::Macos => "🍎 ",
|
||||
Type::Manjaro => "🥭 ",
|
||||
Type::Mariner => "🌊 ",
|
||||
Type::MidnightBSD => "🌘 ",
|
||||
Type::Mint => "🌿 ",
|
||||
Type::NetBSD => "🚩 ",
|
||||
Type::NixOS => "❄️ ",
|
||||
Type::OpenBSD => "🐡 ",
|
||||
Type::openSUSE => "🦎 ",
|
||||
Type::OracleLinux => "🦴 ",
|
||||
Type::Pop => "🍭 ",
|
||||
Type::Raspbian => "🍓 ",
|
||||
Type::Redhat => "🎩 ",
|
||||
Type::RedHatEnterprise => "🎩 ",
|
||||
Type::Redox => "🧪 ",
|
||||
Type::Solus => "⛵ ",
|
||||
Type::SUSE => "🦎 ",
|
||||
Type::Ubuntu => "🎯 ",
|
||||
Type::Unknown => "❓ ",
|
||||
Type::Windows => "🪟 ",
|
||||
// Future symbols.
|
||||
//aosc => " ",
|
||||
//artix => " ",
|
||||
//coreos => " ",
|
||||
//devuan => " ",
|
||||
//elementary => " ",
|
||||
//mageia => " ",
|
||||
//mandriva => " ",
|
||||
//sabayon => " ",
|
||||
//slackware => " ",
|
||||
//void => " ",
|
||||
//solaris => " ",
|
||||
},
|
||||
disabled: true,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -108,6 +108,7 @@ pub const PROMPT_ORDER: &[&str] = &[
|
|||
"time",
|
||||
"status",
|
||||
"container",
|
||||
"os",
|
||||
"shell",
|
||||
"character",
|
||||
];
|
||||
|
|
|
@ -62,6 +62,7 @@ pub const ALL_MODULES: &[&str] = &[
|
|||
"ocaml",
|
||||
"opa",
|
||||
"openstack",
|
||||
"os",
|
||||
"package",
|
||||
"perl",
|
||||
"php",
|
||||
|
|
|
@ -51,6 +51,7 @@ mod nodejs;
|
|||
mod ocaml;
|
||||
mod opa;
|
||||
mod openstack;
|
||||
mod os;
|
||||
mod package;
|
||||
mod perl;
|
||||
mod php;
|
||||
|
@ -148,6 +149,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
|
|||
"ocaml" => ocaml::module(context),
|
||||
"opa" => opa::module(context),
|
||||
"openstack" => openstack::module(context),
|
||||
"os" => os::module(context),
|
||||
"package" => package::module(context),
|
||||
"perl" => perl::module(context),
|
||||
"php" => php::module(context),
|
||||
|
@ -258,6 +260,7 @@ pub fn description(module: &str) -> &'static str {
|
|||
"ocaml" => "The currently installed version of OCaml",
|
||||
"opa" => "The currently installed version of Open Platform Agent",
|
||||
"openstack" => "The current OpenStack cloud and project",
|
||||
"os" => "The current operating system",
|
||||
"package" => "The package version of the current directory's project",
|
||||
"perl" => "The currently installed version of Perl",
|
||||
"php" => "The currently installed version of PHP",
|
||||
|
|
|
@ -0,0 +1,373 @@
|
|||
use super::{Context, Module, ModuleConfig};
|
||||
|
||||
use crate::configs::os::OSConfig;
|
||||
use crate::formatter::StringFormatter;
|
||||
|
||||
/// Creates a module with the current operating system
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let mut module = context.new_module("os");
|
||||
let config: OSConfig = OSConfig::try_load(module.config);
|
||||
|
||||
if config.disabled {
|
||||
return None;
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
let os = os_info::get();
|
||||
|
||||
#[cfg(test)]
|
||||
let os = os_info::Info::default();
|
||||
|
||||
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
||||
formatter
|
||||
.map_meta(|variable, _| match variable {
|
||||
"symbol" => get_symbol(&config, &os.os_type()),
|
||||
_ => None,
|
||||
})
|
||||
.map_style(|variable| match variable {
|
||||
"style" => Some(Ok(config.style)),
|
||||
_ => None,
|
||||
})
|
||||
.map(|variable| match variable {
|
||||
"codename" => get_codename(&os).map(Ok),
|
||||
"edition" => get_edition(&os).map(Ok),
|
||||
"name" => get_name(&os).map(Ok),
|
||||
"type" => get_type(&os).map(Ok),
|
||||
"version" => get_version(&os).map(Ok),
|
||||
_ => None,
|
||||
})
|
||||
.parse(None, Some(context))
|
||||
});
|
||||
module.set_segments(match parsed {
|
||||
Ok(segments) => segments,
|
||||
Err(error) => {
|
||||
log::warn!("Error in module `os`:\n{}", error);
|
||||
return None;
|
||||
}
|
||||
});
|
||||
|
||||
Some(module)
|
||||
}
|
||||
|
||||
// Get the operating system symbol from user config, or else default config
|
||||
// when user has not defined a symbol for the operating system.
|
||||
fn get_symbol<'a>(config: &'a OSConfig, os_type: &os_info::Type) -> Option<&'a str> {
|
||||
config
|
||||
.get_symbol(os_type)
|
||||
.or_else(|| OSConfig::default().get_symbol(os_type))
|
||||
}
|
||||
|
||||
fn get_codename(os: &os_info::Info) -> Option<String> {
|
||||
os.codename().map(String::from)
|
||||
}
|
||||
|
||||
fn get_edition(os: &os_info::Info) -> Option<String> {
|
||||
os.edition().map(String::from)
|
||||
}
|
||||
|
||||
fn get_name(os: &os_info::Info) -> Option<String> {
|
||||
Some(os.os_type().to_string())
|
||||
}
|
||||
|
||||
fn get_type(os: &os_info::Info) -> Option<String> {
|
||||
// String from os_info::Type
|
||||
Some(format!("{:?}", os.os_type()))
|
||||
}
|
||||
|
||||
fn get_version(os: &os_info::Info) -> Option<String> {
|
||||
Some(os.version())
|
||||
.filter(|&x| x != &os_info::Version::Unknown)
|
||||
.map(|x| x.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::test::ModuleRenderer;
|
||||
use nu_ansi_term::Color;
|
||||
use os_info::Type;
|
||||
|
||||
#[test]
|
||||
fn default() {
|
||||
let actual = ModuleRenderer::new("os").collect();
|
||||
|
||||
assert_eq!(actual, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_enabled() {
|
||||
let actual = ModuleRenderer::new("os")
|
||||
.config(toml::toml! {
|
||||
[os]
|
||||
disabled = false
|
||||
})
|
||||
.collect();
|
||||
|
||||
let expected = Some(format!("{}", Color::White.bold().paint("❓ ")));
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_segments() {
|
||||
let actual = ModuleRenderer::new("os")
|
||||
.config(toml::toml! {
|
||||
[os]
|
||||
disabled = false
|
||||
format = "[$symbol($codename )($edition )($name )($type )($version )]($style)"
|
||||
})
|
||||
.collect();
|
||||
|
||||
let expected = Some(format!(
|
||||
"{}",
|
||||
Color::White.bold().paint("❓ Unknown Unknown ")
|
||||
));
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_symbol_default() {
|
||||
let config = OSConfig::try_load(None);
|
||||
|
||||
let type_expected_pairs = [
|
||||
(Type::Alpine, Some("🏔️ ")),
|
||||
(Type::Amazon, Some("🙂 ")),
|
||||
(Type::Android, Some("🤖 ")),
|
||||
(Type::Arch, Some("🎗️ ")),
|
||||
(Type::CentOS, Some("💠 ")),
|
||||
(Type::Debian, Some("🌀 ")),
|
||||
(Type::DragonFly, Some("🐉 ")),
|
||||
(Type::Emscripten, Some("🔗 ")),
|
||||
(Type::EndeavourOS, Some("🚀 ")),
|
||||
(Type::Fedora, Some("🎩 ")),
|
||||
(Type::FreeBSD, Some("😈 ")),
|
||||
(Type::Garuda, Some("🦅 ")),
|
||||
(Type::Gentoo, Some("🗜️ ")),
|
||||
(Type::HardenedBSD, Some("🛡️ ")),
|
||||
(Type::Illumos, Some("🐦 ")),
|
||||
(Type::Linux, Some("🐧 ")),
|
||||
(Type::Macos, Some("🍎 ")),
|
||||
(Type::Manjaro, Some("🥭 ")),
|
||||
(Type::Mariner, Some("🌊 ")),
|
||||
(Type::MidnightBSD, Some("🌘 ")),
|
||||
(Type::Mint, Some("🌿 ")),
|
||||
(Type::NetBSD, Some("🚩 ")),
|
||||
(Type::NixOS, Some("❄️ ")),
|
||||
(Type::OpenBSD, Some("🐡 ")),
|
||||
(Type::openSUSE, Some("🦎 ")),
|
||||
(Type::OracleLinux, Some("🦴 ")),
|
||||
(Type::Pop, Some("🍭 ")),
|
||||
(Type::Raspbian, Some("🍓 ")),
|
||||
(Type::Redhat, Some("🎩 ")),
|
||||
(Type::RedHatEnterprise, Some("🎩 ")),
|
||||
(Type::Redox, Some("🧪 ")),
|
||||
(Type::Solus, Some("⛵ ")),
|
||||
(Type::SUSE, Some("🦎 ")),
|
||||
(Type::Ubuntu, Some("🎯 ")),
|
||||
(Type::Unknown, Some("❓ ")),
|
||||
(Type::Windows, Some("🪟 ")),
|
||||
];
|
||||
|
||||
for (t, e) in type_expected_pairs {
|
||||
assert_eq!(get_symbol(&config, &t), e);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_symbol_custom() {
|
||||
let config_toml = toml::toml! {
|
||||
[symbols]
|
||||
Alpine = " "
|
||||
Amazon = " "
|
||||
Android = " "
|
||||
Arch = " "
|
||||
CentOS = " "
|
||||
Debian = " "
|
||||
DragonFly = " "
|
||||
Emscripten = " "
|
||||
EndeavourOS = " "
|
||||
Fedora = " "
|
||||
FreeBSD = " "
|
||||
Garuda = " "
|
||||
Gentoo = " "
|
||||
HardenedBSD = "ﲊ "
|
||||
Illumos = " "
|
||||
Linux = " "
|
||||
Macos = " "
|
||||
Manjaro = " "
|
||||
Mariner = " "
|
||||
MidnightBSD = " "
|
||||
Mint = " "
|
||||
NetBSD = " "
|
||||
NixOS = " "
|
||||
OpenBSD = " "
|
||||
SUSE = " "
|
||||
OracleLinux = " "
|
||||
Pop = " "
|
||||
Raspbian = " "
|
||||
Redhat = " "
|
||||
RedHatEnterprise = " "
|
||||
Redox = " "
|
||||
Solus = "ﴱ "
|
||||
openSUSE = " "
|
||||
Ubuntu = " "
|
||||
Unknown = " "
|
||||
Windows = " "
|
||||
};
|
||||
|
||||
let config = OSConfig::load(&config_toml);
|
||||
|
||||
let type_expected_pairs = [
|
||||
(Type::Alpine, Some(" ")),
|
||||
(Type::Amazon, Some(" ")),
|
||||
(Type::Android, Some(" ")),
|
||||
(Type::Arch, Some(" ")),
|
||||
(Type::CentOS, Some(" ")),
|
||||
(Type::Debian, Some(" ")),
|
||||
(Type::DragonFly, Some(" ")),
|
||||
(Type::Emscripten, Some(" ")),
|
||||
(Type::EndeavourOS, Some(" ")),
|
||||
(Type::Fedora, Some(" ")),
|
||||
(Type::FreeBSD, Some(" ")),
|
||||
(Type::Garuda, Some(" ")),
|
||||
(Type::Gentoo, Some(" ")),
|
||||
(Type::HardenedBSD, Some("ﲊ ")),
|
||||
(Type::Illumos, Some(" ")),
|
||||
(Type::Linux, Some(" ")),
|
||||
(Type::Macos, Some(" ")),
|
||||
(Type::Manjaro, Some(" ")),
|
||||
(Type::Mariner, Some(" ")),
|
||||
(Type::MidnightBSD, Some(" ")),
|
||||
(Type::Mint, Some(" ")),
|
||||
(Type::NetBSD, Some(" ")),
|
||||
(Type::NixOS, Some(" ")),
|
||||
(Type::OpenBSD, Some(" ")),
|
||||
(Type::SUSE, Some(" ")),
|
||||
(Type::OracleLinux, Some(" ")),
|
||||
(Type::Pop, Some(" ")),
|
||||
(Type::Raspbian, Some(" ")),
|
||||
(Type::Redhat, Some(" ")),
|
||||
(Type::RedHatEnterprise, Some(" ")),
|
||||
(Type::Redox, Some(" ")),
|
||||
(Type::Solus, Some("ﴱ ")),
|
||||
(Type::openSUSE, Some(" ")),
|
||||
(Type::Ubuntu, Some(" ")),
|
||||
(Type::Unknown, Some(" ")),
|
||||
(Type::Windows, Some(" ")),
|
||||
];
|
||||
|
||||
for (t, e) in type_expected_pairs {
|
||||
assert_eq!(get_symbol(&config, &t), e);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_symbol_fallback() {
|
||||
let config_toml = toml::toml! {
|
||||
[symbols]
|
||||
Unknown = ""
|
||||
Arch = "Arch is the best!"
|
||||
};
|
||||
|
||||
let config = OSConfig::load(&config_toml);
|
||||
|
||||
let type_expected_pairs = [
|
||||
(Type::Alpine, Some("🏔️ ")),
|
||||
(Type::Amazon, Some("🙂 ")),
|
||||
(Type::Android, Some("🤖 ")),
|
||||
(Type::Arch, Some("Arch is the best!")),
|
||||
(Type::CentOS, Some("💠 ")),
|
||||
(Type::Debian, Some("🌀 ")),
|
||||
(Type::DragonFly, Some("🐉 ")),
|
||||
(Type::Emscripten, Some("🔗 ")),
|
||||
(Type::EndeavourOS, Some("🚀 ")),
|
||||
(Type::Fedora, Some("🎩 ")),
|
||||
(Type::FreeBSD, Some("😈 ")),
|
||||
(Type::Garuda, Some("🦅 ")),
|
||||
(Type::Gentoo, Some("🗜️ ")),
|
||||
(Type::HardenedBSD, Some("🛡️ ")),
|
||||
(Type::Illumos, Some("🐦 ")),
|
||||
(Type::Linux, Some("🐧 ")),
|
||||
(Type::Macos, Some("🍎 ")),
|
||||
(Type::Manjaro, Some("🥭 ")),
|
||||
(Type::Mariner, Some("🌊 ")),
|
||||
(Type::MidnightBSD, Some("🌘 ")),
|
||||
(Type::Mint, Some("🌿 ")),
|
||||
(Type::NetBSD, Some("🚩 ")),
|
||||
(Type::NixOS, Some("❄️ ")),
|
||||
(Type::OpenBSD, Some("🐡 ")),
|
||||
(Type::openSUSE, Some("🦎 ")),
|
||||
(Type::OracleLinux, Some("🦴 ")),
|
||||
(Type::Pop, Some("🍭 ")),
|
||||
(Type::Raspbian, Some("🍓 ")),
|
||||
(Type::Redhat, Some("🎩 ")),
|
||||
(Type::RedHatEnterprise, Some("🎩 ")),
|
||||
(Type::Redox, Some("🧪 ")),
|
||||
(Type::Solus, Some("⛵ ")),
|
||||
(Type::SUSE, Some("🦎 ")),
|
||||
(Type::Ubuntu, Some("🎯 ")),
|
||||
(Type::Unknown, Some("")),
|
||||
(Type::Windows, Some("🪟 ")),
|
||||
];
|
||||
|
||||
for (t, e) in type_expected_pairs {
|
||||
assert_eq!(get_symbol(&config, &t), e);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn warn_on_os_info_update() {
|
||||
#[warn(clippy::wildcard_enum_match_arm)]
|
||||
// This closure is the same as the default config symbols list.
|
||||
// When this clippy test fails, a new default symbol should be added to
|
||||
// `config/os.rs` to exhaustively match new possible `os_info::Type` cases.
|
||||
// Affects:
|
||||
// - crate::configs::os::OSConfig::default()
|
||||
// - crate::modules::os::tests
|
||||
// - docs/config/README.md/#Configuration/#OS/#Options
|
||||
// - docs/config/README.md/#Configuration/#OS/#Example
|
||||
// - docs/.vuepress/public/presets/toml/plain-text-symbols.toml
|
||||
// - dosc/.vuepress/public/presets/toml/nerd-font-symbols.toml
|
||||
// - .github/config-schema.json
|
||||
let _ = |t: Type| match t {
|
||||
Type::Alpine => "🏔️ ",
|
||||
Type::Amazon => "🙂 ",
|
||||
Type::Android => "🤖 ",
|
||||
Type::Arch => "🎗️ ",
|
||||
Type::CentOS => "💠 ",
|
||||
Type::Debian => "🌀 ",
|
||||
Type::DragonFly => "🐉 ",
|
||||
Type::Emscripten => "🔗 ",
|
||||
Type::EndeavourOS => "🚀 ",
|
||||
Type::Fedora => "🎩 ",
|
||||
Type::FreeBSD => "😈 ",
|
||||
Type::Garuda => "🦅 ",
|
||||
Type::Gentoo => "🗜️ ",
|
||||
Type::HardenedBSD => "🛡️ ",
|
||||
Type::Illumos => "🐦 ",
|
||||
Type::Linux => "🐧 ",
|
||||
Type::Macos => "🍎 ",
|
||||
Type::Manjaro => "🥭 ",
|
||||
Type::Mariner => "🌊 ",
|
||||
Type::MidnightBSD => "🌘 ",
|
||||
Type::Mint => "🌿 ",
|
||||
Type::NetBSD => "🚩 ",
|
||||
Type::NixOS => "❄️ ",
|
||||
Type::OpenBSD => "🐡 ",
|
||||
Type::openSUSE => "🦎 ",
|
||||
Type::OracleLinux => "🦴 ",
|
||||
Type::Pop => "🍭 ",
|
||||
Type::Raspbian => "🍓 ",
|
||||
Type::Redhat => "🎩 ",
|
||||
Type::RedHatEnterprise => "🎩 ",
|
||||
Type::Redox => "🧪 ",
|
||||
Type::Solus => "⛵ ",
|
||||
Type::SUSE => "🦎 ",
|
||||
Type::Ubuntu => "🎯 ",
|
||||
Type::Unknown => "❓ ",
|
||||
Type::Windows => "🪟 ",
|
||||
_ => "",
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue