Skip to content

Commit

Permalink
Fix displayed name for GNOME Terminal and Black Box
Browse files Browse the repository at this point in the history
libmacchina uses /proc/<pid>/comm, which is limited to 15 chars and
it's not guaranteed to hold a user friendly name.

For example GNOME Terminal is actually /usr/libexec/gnome-terminal-server
and its `comm` is `gnome-terminal-`.

Add a replacement function that returns user friendly names and
that can be expanded with more.
  • Loading branch information
bfabio committed Jun 19, 2024
1 parent 0be8883 commit 0ddcafb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ pub fn common_shells() -> [&'static str; 10] {
]
}

pub fn terminal_replacements(command: &str) -> &str {
match command {
"gnome-terminal-" => "gnome-terminal",
"blackbox-termin" => "blackbox",
_ => command,
}
}

#[cfg(test)]
#[cfg(not(target_os = "netbsd"))]
mod tests {
Expand Down
7 changes: 5 additions & 2 deletions src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use self::pci_devices::get_pci_devices;
use crate::extra;
use crate::extra::get_entries;
use crate::extra::path_extension;
use crate::extra::terminal_replacements;
use crate::shared;
use crate::traits::*;
use itertools::Itertools;
Expand Down Expand Up @@ -417,10 +418,11 @@ impl GeneralReadout for LinuxGeneralReadout {
// The below loop will traverse /proc to find the
// terminal inside of which the user is operating
if let Ok(mut terminal_name) = fs::read_to_string(path) {
terminal_name = terminal_name.trim().to_string();
// Any command_name we find that matches
// one of the elements within this table
// is effectively ignored
while extra::common_shells().contains(&terminal_name.replace('\n', "").as_str()) {
while extra::common_shells().contains(&terminal_name.as_str()) {
let ppid = get_parent(terminal_pid);
terminal_pid = ppid;

Expand All @@ -438,14 +440,15 @@ impl GeneralReadout for LinuxGeneralReadout {
}

let terminal = terminal_name();
let terminal = terminal_replacements(&terminal);

if terminal.is_empty() {
return Err(ReadoutError::Other(
"Querying terminal information failed".to_string(),
));
}

Ok(terminal)
Ok(terminal.to_string())
}

fn shell(&self, format: ShellFormat, kind: ShellKind) -> Result<String, ReadoutError> {
Expand Down

0 comments on commit 0ddcafb

Please sign in to comment.