Skip to content

Commit

Permalink
shared::shell can now fetch current shell
Browse files Browse the repository at this point in the history
  • Loading branch information
grtcdr committed Aug 12, 2021
1 parent 86667fa commit e3d5b96
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ impl GeneralReadout for LinuxGeneralReadout {
}

fn shell(&self, format: ShellFormat, kind: ShellKind) -> Result<String, ReadoutError> {
crate::shared::shell(format)
crate::shared::shell(format, kind)
}

fn gpus(&self) -> Result<Vec<String>, ReadoutError> {
Expand Down
4 changes: 2 additions & 2 deletions src/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ impl GeneralReadout for MacOSGeneralReadout {
Err(MetricNotAvailable)
}

fn shell(&self, shorthand: ShellFormat, shell: ShellKind) -> Result<String, ReadoutError> {
crate::shared::shell(shorthand)
fn shell(&self, shorthand: ShellFormat, kind: ShellKind) -> Result<String, ReadoutError> {
crate::shared::shell(shorthand, kind)
}

fn cpu_model_name(&self) -> Result<String, ReadoutError> {
Expand Down
4 changes: 2 additions & 2 deletions src/netbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ impl GeneralReadout for NetBSDGeneralReadout {
}
}

fn shell(&self, shorthand: ShellFormat, shell: ShellKind) -> Result<String, ReadoutError> {
crate::shared::shell(shorthand)
fn shell(&self, shorthand: ShellFormat, kind: ShellKind) -> Result<String, ReadoutError> {
crate::shared::shell(shorthand, kind)
}

fn cpu_model_name(&self) -> Result<String, ReadoutError> {
Expand Down
19 changes: 17 additions & 2 deletions src/shared/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![allow(dead_code)]
#![allow(unused_imports)]

use crate::traits::{ReadoutError, ShellFormat};
use crate::traits::{ReadoutError, ShellFormat, ShellKind};

use crate::extra;
use std::fs::read_to_string;
use std::io::Error;
use std::path::Path;
use std::process::{Command, Stdio};
Expand Down Expand Up @@ -136,7 +137,21 @@ pub(crate) fn username() -> Result<String, ReadoutError> {
}

#[cfg(target_family = "unix")]
pub(crate) fn shell(shorthand: ShellFormat) -> Result<String, ReadoutError> {
pub(crate) fn shell(shorthand: ShellFormat, kind: ShellKind) -> Result<String, ReadoutError> {
if kind == ShellKind::Current && cfg!(linux) {
let path = PathBuf::from("/proc")
.join(unsafe { libc::getppid() }.to_string())
.join("comm");

if let Ok(shell) = read_to_string(path) {
return Ok(shell);
}

return Err(ReadoutError::Other(String::from(
"Unable to read current shell.",
)))
}

let passwd = get_passwd_struct()?;

let shell_name = unsafe { CStr::from_ptr((*passwd).pw_shell) };
Expand Down
1 change: 1 addition & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ pub enum PackageManager {
Android,
}

#[derive(PartialEq)]
/// There are two distinct kinds of shells, a so called *"current"* shell, i.e. the shell the user is currently using.
/// And a default shell, i.e. that the user sets for themselves using the `chsh` tool.
pub enum ShellKind {
Expand Down

0 comments on commit e3d5b96

Please sign in to comment.