Skip to content

Commit

Permalink
Merge pull request #1427 from dtolnay-contrib/longosversion
Browse files Browse the repository at this point in the history
Improve System::long_os_version() on Linux and Android
  • Loading branch information
GuillaumeGomez authored Dec 14, 2024
2 parents 4fc41f0 + d375fc6 commit b290678
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/common/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,8 +735,8 @@ impl System {
///
/// | example platform | value of `System::long_os_version()` |
/// |---|---|
/// | linux laptop | "Linux 24.04 Ubuntu" |
/// | android phone | "Android 15 Pixel 9 Pro" |
/// | linux laptop | "Linux (Ubuntu 24.04)" |
/// | android phone | "Android 15 on Pixel 9 Pro" |
/// | apple laptop | "macOS 15.1.1 Sequoia" |
/// | windows server | "Windows Server 2022 Datacenter" |
///
Expand Down
37 changes: 29 additions & 8 deletions src/unix/linux/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,23 +385,44 @@ impl SystemInner {
get_system_info_android(InfoType::Name)
}

#[cfg(not(target_os = "android"))]
pub(crate) fn long_os_version() -> Option<String> {
#[cfg(target_os = "android")]
let system_name = "Android";
let mut long_name = "Linux".to_owned();

let distro_name = Self::name();
let distro_version = Self::os_version();
if let Some(distro_version) = &distro_version {
// "Linux (Ubuntu 24.04)"
long_name.push_str(" (");
long_name.push_str(distro_name.as_deref().unwrap_or("unknown"));
long_name.push(' ');
long_name.push_str(distro_version);
long_name.push(')');
} else if let Some(distro_name) = &distro_name {
// "Linux (Ubuntu)"
long_name.push_str(" (");
long_name.push_str(distro_name);
long_name.push(')');
}

#[cfg(not(target_os = "android"))]
let system_name = "Linux";
Some(long_name)
}

let mut long_name = system_name.to_owned();
#[cfg(target_os = "android")]
pub(crate) fn long_os_version() -> Option<String> {
let mut long_name = "Android".to_owned();

if let Some(os_version) = Self::os_version() {
long_name.push(' ');
long_name.push_str(&os_version);
}

if let Some(short_name) = Self::name() {
long_name.push(' ');
long_name.push_str(&short_name);
// Android's name() is extracted from the system property "ro.product.model"
// which is documented as "The end-user-visible name for the end product."
// So this produces a long_os_version like "Android 15 on Pixel 9 Pro".
if let Some(product_name) = Self::name() {
long_name.push_str(" on ");
long_name.push_str(&product_name);
}

Some(long_name)
Expand Down

0 comments on commit b290678

Please sign in to comment.