Fixed inconsistent display of session name on remember global session (#139).

This commit is contained in:
Antoine POPINEAU 2024-05-19 10:09:28 +02:00 committed by Antoine POPINEAU
parent 0baa018679
commit 976534d90d
4 changed files with 64 additions and 53 deletions

View File

@ -22,9 +22,7 @@ use zeroize::Zeroize;
use crate::{
event::Event,
info::{
get_issue, get_last_session, get_last_session_path, get_last_user_name, get_last_user_session, get_last_user_session_path, get_last_user_username, get_min_max_uids, get_sessions, get_users,
},
info::{get_issue, get_last_command, get_last_session_path, get_last_user_command, get_last_user_name, get_last_user_session, get_last_user_username, get_min_max_uids, get_sessions, get_users},
power::PowerOption,
ui::{
common::{masked::MaskedString, menu::Menu, style::Theme},
@ -96,7 +94,7 @@ pub enum GreetAlign {
#[default]
Center,
Left,
Right
Right,
}
#[derive(SmartDefault)]
@ -225,15 +223,18 @@ impl Greeter {
// If, on top of that, we should remember their last session.
if greeter.remember_user_session {
if let Ok(ref session_path) = get_last_user_session_path(greeter.username.get()) {
// Set the selected menu option and the session source.
greeter.sessions.selected = greeter.sessions.options.iter().position(|Session { path, .. }| path.as_deref() == Some(session_path)).unwrap_or(0);
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
// See if we have the last free-form command from the user.
if let Ok(command) = get_last_user_command(greeter.username.get()) {
greeter.session_source = SessionSource::Command(command);
}
// See if we have the last free-form command from the user.
if let Ok(command) = get_last_user_session(greeter.username.get()) {
greeter.session_source = SessionSource::Command(command);
// If a session was saved, use it and its name.
if let Ok(ref session_path) = get_last_user_session(greeter.username.get()) {
// Set the selected menu option and the session source.
if let Some(index) = greeter.sessions.options.iter().position(|Session { path, .. }| path.as_deref() == Some(session_path)) {
greeter.sessions.selected = index;
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
}
}
}
}
@ -241,13 +242,15 @@ impl Greeter {
// Same thing, but not user specific.
if greeter.remember_session {
if let Ok(ref session_path) = get_last_session_path() {
greeter.sessions.selected = greeter.sessions.options.iter().position(|Session { path, .. }| path.as_deref() == Some(session_path)).unwrap_or(0);
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
if let Ok(command) = get_last_command() {
greeter.session_source = SessionSource::Command(command.trim().to_string());
}
if let Ok(command) = get_last_session() {
greeter.session_source = SessionSource::Command(command.trim().to_string());
if let Ok(ref session_path) = get_last_session_path() {
if let Some(index) = greeter.sessions.options.iter().position(|Session { path, .. }| path.as_deref() == Some(session_path)) {
greeter.sessions.selected = index;
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
}
}
}
@ -362,7 +365,7 @@ impl Greeter {
match value.as_str() {
"left" => GreetAlign::Left,
"right" => GreetAlign::Right,
_ => GreetAlign::Center
_ => GreetAlign::Center,
}
} else {
GreetAlign::default()
@ -413,7 +416,12 @@ impl Greeter {
opts.optopt("", "window-padding", "padding inside the terminal area (default: 0)", "PADDING");
opts.optopt("", "container-padding", "padding inside the main prompt container (default: 1)", "PADDING");
opts.optopt("", "prompt-padding", "padding between prompt rows (default: 1)", "PADDING");
opts.optopt("", "greet-align", "alignment of the greeting text in the main prompt container (default: 'center')", "[left|center|right]");
opts.optopt(
"",
"greet-align",
"alignment of the greeting text in the main prompt container (default: 'center')",
"[left|center|right]",
);
opts.optopt("", "power-shutdown", "command to run to shut down the system", "'CMD [ARGS]...'");
opts.optopt("", "power-reboot", "command to run to reboot the system", "'CMD [ARGS]...'");

View File

@ -23,8 +23,8 @@ use crate::{
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 LAST_SESSION_PATH: &str = "/var/cache/tuigreet/lastsession-path";
const LAST_COMMAND: &str = "/var/cache/tuigreet/lastsession";
const LAST_SESSION: &str = "/var/cache/tuigreet/lastsession-path";
const DEFAULT_MIN_UID: u16 = 1000;
const DEFAULT_MAX_UID: u16 = 60000;
@ -114,55 +114,59 @@ pub fn write_last_username(username: &MaskedString) {
}
pub fn get_last_session_path() -> Result<PathBuf, io::Error> {
Ok(PathBuf::from(fs::read_to_string(LAST_SESSION_PATH)?.trim()))
Ok(PathBuf::from(fs::read_to_string(LAST_SESSION)?.trim()))
}
pub fn get_last_session() -> Result<String, io::Error> {
Ok(fs::read_to_string(LAST_SESSION)?.trim().to_string())
pub fn get_last_command() -> Result<String, io::Error> {
Ok(fs::read_to_string(LAST_COMMAND)?.trim().to_string())
}
pub fn write_last_session_path<P>(session: &P)
where
P: AsRef<Path>,
{
let _ = fs::write(LAST_SESSION_PATH, session.as_ref().to_string_lossy().as_bytes());
let _ = fs::write(LAST_SESSION, session.as_ref().to_string_lossy().as_bytes());
}
pub fn write_last_session(session: &str) {
let _ = fs::write(LAST_SESSION, session);
pub fn write_last_command(session: &str) {
let _ = fs::write(LAST_COMMAND, session);
}
pub fn get_last_user_session_path(username: &str) -> Result<PathBuf, io::Error> {
Ok(PathBuf::from(fs::read_to_string(format!("{LAST_SESSION_PATH}-{username}"))?.trim()))
pub fn get_last_user_session(username: &str) -> Result<PathBuf, io::Error> {
Ok(PathBuf::from(fs::read_to_string(format!("{LAST_SESSION}-{username}"))?.trim()))
}
pub fn get_last_user_session(username: &str) -> Result<String, io::Error> {
Ok(fs::read_to_string(format!("{LAST_SESSION}-{username}"))?.trim().to_string())
pub fn get_last_user_command(username: &str) -> Result<String, io::Error> {
Ok(fs::read_to_string(format!("{LAST_COMMAND}-{username}"))?.trim().to_string())
}
pub fn write_last_user_session_path<P>(username: &str, session: P)
pub fn write_last_user_session<P>(username: &str, session: P)
where
P: AsRef<Path>,
{
let _ = fs::write(format!("{LAST_SESSION_PATH}-{username}"), session.as_ref().to_string_lossy().as_bytes());
let _ = fs::write(format!("{LAST_SESSION}-{username}"), session.as_ref().to_string_lossy().as_bytes());
}
pub fn delete_last_session_path() {
let _ = fs::remove_file(LAST_SESSION_PATH);
pub fn delete_last_session() {
let _ = fs::remove_file(LAST_SESSION);
}
pub fn write_last_user_session(username: &str, session: &str) {
let _ = fs::write(format!("{LAST_SESSION}-{username}"), session);
}
pub fn delete_last_user_session_path(username: &str) {
let _ = fs::remove_file(format!("{LAST_SESSION_PATH}-{username}"));
pub fn write_last_user_command(username: &str, session: &str) {
let _ = fs::write(format!("{LAST_COMMAND}-{username}"), session);
}
pub fn delete_last_user_session(username: &str) {
let _ = fs::remove_file(format!("{LAST_SESSION}-{username}"));
}
pub fn delete_last_command() {
let _ = fs::remove_file(LAST_COMMAND);
}
pub fn delete_last_user_command(username: &str) {
let _ = fs::remove_file(format!("{LAST_COMMAND}-{username}"));
}
pub fn get_users(min_uid: u16, max_uid: u16) -> Vec<User> {
let users = unsafe { uzers::all_users() };

View File

@ -8,7 +8,7 @@ use tokio::sync::{
use crate::{
event::Event,
info::{delete_last_user_session, delete_last_user_session_path, write_last_user_session, write_last_user_session_path, write_last_username},
info::{delete_last_user_command, delete_last_user_session, write_last_user_command, write_last_user_session, write_last_username},
macros::SafeDebug,
ui::sessions::{Session, SessionSource, SessionType},
AuthStatus, Greeter, Mode,
@ -128,16 +128,16 @@ impl Ipc {
SessionSource::Command(ref command) => {
tracing::info!("caching last user command: {command}");
write_last_user_session(&greeter.username.value, command);
delete_last_user_session_path(&greeter.username.value);
write_last_user_command(&greeter.username.value, command);
delete_last_user_session(&greeter.username.value);
}
SessionSource::Session(index) => {
if let Some(Session { path: Some(session_path), .. }) = greeter.sessions.options.get(index) {
tracing::info!("caching last user session: {session_path:?}");
write_last_user_session_path(&greeter.username.value, session_path);
delete_last_user_session(&greeter.username.value);
write_last_user_session(&greeter.username.value, session_path);
delete_last_user_command(&greeter.username.value);
}
}

View File

@ -5,7 +5,7 @@ use greetd_ipc::Request;
use tokio::sync::RwLock;
use crate::{
info::{delete_last_session_path, get_last_user_session, get_last_user_session_path, write_last_session, write_last_session_path},
info::{delete_last_command, delete_last_session, get_last_user_command, get_last_user_session, write_last_command, write_last_session_path},
ipc::Ipc,
power::power,
ui::{
@ -225,8 +225,8 @@ pub async fn handle(greeter: Arc<RwLock<Greeter>>, input: KeyEvent, ipc: Ipc) ->
greeter.session_source = SessionSource::Command(greeter.buffer.clone());
if greeter.remember_session {
write_last_session(&greeter.buffer);
delete_last_session_path();
write_last_command(&greeter.buffer);
delete_last_session();
}
greeter.buffer = greeter.previous_buffer.take().unwrap_or_default();
@ -248,13 +248,12 @@ pub async fn handle(greeter: Arc<RwLock<Greeter>>, input: KeyEvent, ipc: Ipc) ->
Mode::Sessions => {
let session = greeter.sessions.options.get(greeter.sessions.selected).cloned();
if let Some(Session { path, command, .. }) = session {
if let Some(Session { path, .. }) = session {
if greeter.remember_session {
if let Some(ref path) = path {
write_last_session_path(path);
delete_last_command();
}
write_last_session(&command);
}
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
@ -365,7 +364,7 @@ async fn validate_username(greeter: &mut Greeter, ipc: &Ipc) {
greeter.buffer = String::new();
if greeter.remember_user_session {
if let Ok(last_session) = get_last_user_session_path(&greeter.username.value) {
if let Ok(last_session) = get_last_user_session(&greeter.username.value) {
if let Some(last_session) = Session::from_path(greeter, last_session).cloned() {
tracing::info!("remembered user session is {}", last_session.name);
@ -374,7 +373,7 @@ async fn validate_username(greeter: &mut Greeter, ipc: &Ipc) {
}
}
if let Ok(command) = get_last_user_session(&greeter.username.value) {
if let Ok(command) = get_last_user_command(&greeter.username.value) {
tracing::info!("remembered user command is {}", command);
greeter.session_source = SessionSource::Command(command);