Some style updates and display of full name on user menu selection.

This commit is contained in:
Antoine POPINEAU 2022-02-16 23:08:21 +01:00
parent d71d505756
commit 2fffad7ac4
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
9 changed files with 39 additions and 23 deletions

View File

@ -7,7 +7,7 @@ fn main() {
env!("CARGO_PKG_VERSION").to_string()
};
println!("cargo:rustc-env=VERSION={}", version);
println!("cargo:rustc-env=VERSION={version}");
println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
}

View File

@ -22,7 +22,7 @@ use tokio::{
use zeroize::Zeroize;
use crate::{
info::{get_issue, get_last_session, get_last_user_session, get_last_username, get_min_max_uids, get_users},
info::{get_issue, get_last_session, get_last_user_name, get_last_user_session, get_last_user_username, get_min_max_uids, get_users},
power::PowerOption,
};
@ -79,6 +79,7 @@ pub struct Greeter {
pub selected_power_option: usize,
pub username: String,
pub username_mask: Option<String>,
pub prompt: Option<String>,
pub answer: String,
pub secret: bool,
@ -125,8 +126,9 @@ impl Greeter {
}
if greeter.remember {
if let Ok(username) = get_last_username() {
if let Ok(username) = get_last_user_username() {
greeter.username = username.clone();
greeter.username_mask = get_last_user_name();
if greeter.remember_user_session {
if let Ok(command) = get_last_user_session(&username) {
@ -150,6 +152,7 @@ impl Greeter {
fn scrub(&mut self, scrub_message: bool) {
self.prompt.zeroize();
self.username.zeroize();
self.username_mask.zeroize();
self.answer.zeroize();
if scrub_message {
@ -172,7 +175,7 @@ impl Greeter {
Ok(stream) => self.stream = Some(Arc::new(RwLock::new(stream))),
Err(err) => {
eprintln!("{}", err);
eprintln!("{err}");
process::exit(1);
}
}
@ -234,7 +237,7 @@ impl Greeter {
let locale = DesktopLanguageRequester::requested_languages()
.into_iter()
.next()
.and_then(|locale| locale.region.map(|region| format!("{}_{}", locale.language, region)))
.and_then(|locale| locale.region.map(|region| format!("{}_{region}", locale.language)))
.and_then(|id| id.as_str().try_into().ok());
if let Some(locale) = locale {
@ -274,7 +277,7 @@ impl Greeter {
Ok(matches) => Some(matches),
Err(err) => {
eprintln!("{}", err);
eprintln!("{err}");
print_usage(opts);
process::exit(1);
}
@ -372,7 +375,7 @@ impl Greeter {
}
pub fn set_prompt(&mut self, prompt: &str) {
self.prompt = if prompt.ends_with(' ') { Some(prompt.into()) } else { Some(format!("{} ", prompt)) };
self.prompt = if prompt.ends_with(' ') { Some(prompt.into()) } else { Some(format!("{prompt} ")) };
}
pub fn remove_prompt(&mut self) {

View File

@ -14,7 +14,8 @@ use crate::Greeter;
const X_SESSIONS: &str = "/usr/share/xsessions";
const WAYLAND_SESSIONS: &str = "/usr/share/wayland-sessions";
const LAST_USERNAME: &str = "/var/cache/tuigreet/lastuser";
const LAST_USER_USERNAME: &str = "/var/cache/tuigreet/lastuser";
const LAST_USER_NAME: &str = "/var/cache/tuigreet/lastuser-name";
const LAST_SESSION: &str = "/var/cache/tuigreet/lastsession";
const DEFAULT_MIN_UID: u16 = 1000;
@ -32,7 +33,7 @@ pub fn get_issue() -> Option<String> {
return Some(
issue
.replace("\\S", "Linux")
.replace("\\l", &format!("tty{}", vtnr))
.replace("\\l", &format!("tty{vtnr}"))
.replace("\\s", uts.sysname())
.replace("\\r", uts.release())
.replace("\\v", uts.version())
@ -45,12 +46,22 @@ pub fn get_issue() -> Option<String> {
None
}
pub fn get_last_username() -> Result<String, io::Error> {
fs::read_to_string(LAST_USERNAME)
pub fn get_last_user_username() -> Result<String, io::Error> {
fs::read_to_string(LAST_USER_USERNAME)
}
pub fn write_last_username(username: &str) {
let _ = fs::write(LAST_USERNAME, username);
pub fn get_last_user_name() -> Option<String> {
fs::read_to_string(LAST_USER_NAME).ok()
}
pub fn write_last_username(username: &str, name: Option<&str>) {
let _ = fs::write(LAST_USER_USERNAME, username);
if let Some(name) = name {
let _ = fs::write(LAST_USER_NAME, name);
} else {
let _ = fs::remove_file(LAST_USER_NAME);
}
}
pub fn get_last_session() -> Result<String, io::Error> {
@ -62,11 +73,11 @@ pub fn write_last_session(session: &str) {
}
pub fn get_last_user_session(username: &str) -> Result<String, io::Error> {
fs::read_to_string(&format!("{}-{}", LAST_SESSION, username))
fs::read_to_string(&format!("{LAST_SESSION}-{username}"))
}
pub fn write_last_user_session(username: &str, session: &str) {
let _ = fs::write(&format!("{}-{}", LAST_SESSION, username), session);
let _ = fs::write(&format!("{LAST_SESSION}-{username}"), session);
}
pub fn get_users(min_uid: u16, max_uid: u16) -> Vec<(String, Option<String>)> {

View File

@ -103,7 +103,7 @@ impl Ipc {
Response::Success => {
if greeter.done {
if greeter.remember {
write_last_username(&greeter.username);
write_last_username(&greeter.username, greeter.username_mask.as_deref());
if greeter.remember_user_session {
if let Some(command) = &greeter.command {

View File

@ -181,8 +181,9 @@ pub async fn handle(greeter: Arc<RwLock<Greeter>>, events: &mut Events, ipc: Ipc
Mode::Users => {
let username = greeter.users.get(greeter.selected_user).cloned();
if let Some((username, _)) = username {
if let Some((username, name)) = username {
greeter.username = username;
greeter.username_mask = name;
}
validate_username(&mut greeter, &ipc).await;

View File

@ -60,14 +60,14 @@ pub async fn run(greeter: &Arc<RwLock<Greeter>>, mut command: Command) {
Ok(result) => match (result.status, result.stderr) {
(status, _) if status.success() => None,
(status, output) => {
let status = format!("{} {}", fl!("command_exited"), status);
let status = format!("{} {status}", fl!("command_exited"));
let output = String::from_utf8(output).unwrap_or_default();
Some(format!("{}\n{}", status, output))
Some(format!("{status}\n{output}"))
}
},
Err(err) => Some(format!("{}: {}", fl!("command_failed"), err)),
Err(err) => Some(format!("{}: {err}", fl!("command_failed"))),
};
let mode = greeter.read().await.previous_mode;

View File

@ -66,7 +66,8 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box<dyn
Paragraph::new(username_text)
};
let username_value_text = Span::from(greeter.username.as_str());
let username = greeter.username_mask.as_deref().unwrap_or_else(|| greeter.username.as_ref());
let username_value_text = Span::from(username);
let username_value = Paragraph::new(username_value_text);
match greeter.mode {

View File

@ -27,7 +27,7 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box<dyn
for (index, (username, name)) in greeter.users.iter().enumerate() {
let name = match name {
Some(name) => format!("{} ({})", name, username),
Some(name) => format!("{name} ({username})"),
None => username.clone(),
};

View File

@ -1,7 +1,7 @@
use crate::{Greeter, Mode};
pub fn titleize(message: &str) -> String {
format!(" {} ", message)
format!(" {message} ")
}
pub fn should_hide_cursor(greeter: &Greeter) -> bool {