Skip to content

Commit

Permalink
Refactored /etc/issue parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
apognu committed Aug 15, 2024
1 parent fa58d89 commit 7acf49d
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,44 @@ pub fn get_issue() -> Option<String> {
(now.format("%a %b %_d %Y").to_string(), now.format("%H:%M:%S").to_string())
};

let count = match UtmpParser::from_path("/var/run/utmp")
.map(|utmp| utmp.into_iter().filter(|user| matches!(user, Ok(UtmpEntry::UserProcess { .. }))).count())
let user_count = match UtmpParser::from_path("/var/run/utmp")
.map(|utmp| {
utmp.into_iter().fold(0, |acc, entry| match entry {
Ok(UtmpEntry::UserProcess { .. }) => acc + 1,
Ok(UtmpEntry::LoginProcess { .. }) => acc + 1,
_ => acc,
})
})
.unwrap_or(0)
{
n if n < 2 => format!("{n} user"),
n => format!("{n} users"),
};

let vtnr: usize = env::var("XDG_VTNR").unwrap_or_else(|_| "0".to_string()).parse().expect("unable to parse VTNR");
let vtnr: usize = env::var("XDG_VTNR").unwrap_or_else(|_| "0".to_string()).parse().unwrap_or(0);
let uts = utsname::uname();

if let Ok(issue) = fs::read_to_string("/etc/issue") {
let issue = issue.replace("\\S", "Linux").replace("\\l", &format!("tty{vtnr}"));

return match uts {
Ok(uts) => Some(
issue
.replace("\\s", uts.sysname().to_str().unwrap_or(""))
.replace("\\r", uts.release().to_str().unwrap_or(""))
.replace("\\v", uts.version().to_str().unwrap_or(""))
.replace("\\n", uts.nodename().to_str().unwrap_or(""))
.replace("\\m", uts.machine().to_str().unwrap_or(""))
.replace("\\d", &date)
.replace("\\t", &time)
.replace("\\U", &count)
.replace("\\x1b", "\x1b")
.replace("\\033", "\x1b")
.replace("\\e", "\x1b")
.replace(r"\\", r"\"),
),

_ => Some(issue),
let issue = issue
.replace("\\S", "Linux")
.replace("\\l", &format!("tty{vtnr}"))
.replace("\\d", &date)
.replace("\\t", &time)
.replace("\\U", &user_count);

let issue = match uts {
Ok(uts) => issue
.replace("\\s", uts.sysname().to_str().unwrap_or(""))
.replace("\\r", uts.release().to_str().unwrap_or(""))
.replace("\\v", uts.version().to_str().unwrap_or(""))
.replace("\\n", uts.nodename().to_str().unwrap_or(""))
.replace("\\m", uts.machine().to_str().unwrap_or(""))
.replace("\\o", uts.domainname().to_str().unwrap_or("")),

_ => issue,
};

return Some(issue.replace("\\x1b", "\x1b").replace("\\033", "\x1b").replace("\\e", "\x1b").replace(r"\\", r"\"));
}

None
Expand Down

0 comments on commit 7acf49d

Please sign in to comment.