Skip to content

Commit

Permalink
Properly support issue content with significant ident as well as \d, …
Browse files Browse the repository at this point in the history
…\t, and \U modifiers.
  • Loading branch information
apognu committed Aug 9, 2024
1 parent 2cc9fd9 commit da6b129
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
52 changes: 49 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ rand = "0.8.5"
tracing-appender = "0.2.3"
tracing-subscriber = "0.3.18"
tracing = "0.1.40"
utmp-rs = "0.3.0"

[profile.release]
lto = true
Expand Down
21 changes: 20 additions & 1 deletion src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ use std::{
process::Command,
};

use chrono::Local;
use ini::Ini;
use lazy_static::lazy_static;
use nix::sys::utsname;
use utmp_rs::{UtmpEntry, UtmpParser};
use uzers::os::unix::UserExt;

use crate::{
Expand Down Expand Up @@ -49,6 +51,20 @@ pub fn get_hostname() -> String {
}

pub fn get_issue() -> Option<String> {
let (date, time) = {
let now = Local::now();

(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())
.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 uts = utsname::uname();

Expand All @@ -63,10 +79,13 @@ pub fn get_issue() -> Option<String> {
.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("\\\\", "\\"),
.replace(r"\\", r"\"),
),

_ => Some(issue),
Expand Down
6 changes: 3 additions & 3 deletions src/ui/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn get_greeting_height(greeter: &Greeter, padding: u16, fallback: u16) -> (O
Err(_) => Text::raw(greeting),
};

let paragraph = Paragraph::new(text.clone()).wrap(Wrap { trim: true });
let paragraph = Paragraph::new(text.clone()).wrap(Wrap { trim: false });
let height = paragraph.line_count(width - (2 * padding)) + 1;

(Some(paragraph), height as u16)
Expand Down Expand Up @@ -290,7 +290,7 @@ mod test {
Span::styled("Hello", Style::default().fg(Color::Red)),
Span::styled(" World", Style::reset()),
])]))
.wrap(Wrap { trim: true });
.wrap(Wrap { trim: false });

assert_eq!(text, Some(expected));
assert_eq!(height, 2);
Expand All @@ -308,7 +308,7 @@ mod test {
Span::styled("Hello", Style::default().fg(Color::Red)),
Span::styled(" World", Style::reset()),
])]))
.wrap(Wrap { trim: true });
.wrap(Wrap { trim: false });

assert_eq!(text, Some(expected));
assert_eq!(height, 3);
Expand Down

0 comments on commit da6b129

Please sign in to comment.