Add option to remember the last selected session instead of relying solely on the --cmd option (#30).

This commit is contained in:
Antoine POPINEAU 2021-07-03 21:51:39 +02:00
parent 8a7a2bf58b
commit 2be3b60437
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
4 changed files with 39 additions and 2 deletions

View File

@ -18,6 +18,8 @@ Options:
show custom text above login prompt
-t, --time display the current date and time
-r, --remember remember last logged-in username
--remember-session
remember last selected session
--asterisks display asterisks when a secret is typed
--asterisks-char CHAR
character to be used to redact secrets (default: *)

View File

@ -13,7 +13,7 @@ use greetd_ipc::Request;
use i18n_embed::DesktopLanguageRequester;
use zeroize::Zeroize;
use crate::info::{get_issue, get_last_username};
use crate::info::{get_issue, get_last_session, get_last_username};
const DEFAULT_LOCALE: Locale = Locale::en_US;
const DEFAULT_ASTERISKS_CHAR: char = '*';
@ -70,6 +70,7 @@ pub struct Greeter {
pub secret: bool,
pub remember: bool,
pub remember_session: bool,
pub asterisks: bool,
#[default(DEFAULT_ASTERISKS_CHAR)]
pub asterisks_char: char,
@ -96,12 +97,25 @@ impl Greeter {
greeter.set_locale();
greeter.parse_options();
greeter.sessions = crate::info::get_sessions(&greeter).unwrap_or_default();
greeter.selected_session = greeter.sessions.iter().position(|(_, command)| Some(command) == greeter.command.as_ref()).unwrap_or(0);
if let Some((_, command)) = greeter.sessions.get(0) {
if greeter.command.is_none() {
greeter.command = Some(command.clone());
}
}
if greeter.remember {
greeter.username = get_last_username().unwrap_or_default().trim().to_string();
}
if greeter.remember_session {
if let Ok(session) = get_last_session() {
greeter.command = Some(session.trim().to_string());
}
}
greeter.selected_session = greeter.sessions.iter().position(|(_, command)| Some(command) == greeter.command.as_ref()).unwrap_or(0);
greeter
}
@ -203,6 +217,7 @@ impl Greeter {
opts.optopt("g", "greeting", "show custom text above login prompt", "GREETING");
opts.optflag("t", "time", "display the current date and time");
opts.optflag("r", "remember", "remember last logged-in username");
opts.optflag("", "remember-session", "remember last selected session");
opts.optflag("", "asterisks", "display asterisks when a secret is typed");
opts.optopt("", "asterisks-char", "character to be used to redact secrets (default: *)", "CHAR");
opts.optopt("", "window-padding", "padding inside the terminal area (default: 0)", "PADDING");
@ -253,6 +268,7 @@ impl Greeter {
}
self.remember = self.config().opt_present("remember");
self.remember_session = self.config().opt_present("remember-session");
self.asterisks = self.config().opt_present("asterisks");
self.greeting = self.option("greeting");
self.command = self.option("cmd");

View File

@ -14,6 +14,7 @@ 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_SESSION: &str = "/var/cache/tuigreet/lastsession";
pub fn get_hostname() -> String {
utsname::uname().nodename().to_string()
@ -48,6 +49,14 @@ pub fn write_last_username(username: &str) {
let _ = fs::write(LAST_USERNAME, username);
}
pub fn get_last_session() -> Result<String, io::Error> {
fs::read_to_string(LAST_SESSION)
}
pub fn write_last_session(session: &str) {
let _ = fs::write(LAST_SESSION, session);
}
pub fn get_sessions(greeter: &Greeter) -> Result<Vec<(String, String)>, Box<dyn Error>> {
let sessions = match greeter.sessions_path {
Some(ref dirs) => env::split_paths(&dirs).collect(),

View File

@ -6,6 +6,7 @@ use termion::event::Key;
use crate::{
event::{Event, Events},
info::write_last_session,
ipc::cancel,
ui::{PowerOption, POWER_OPTIONS},
Greeter, Mode,
@ -111,12 +112,21 @@ pub fn handle(greeter: &mut Greeter, events: &Events) -> Result<(), Box<dyn Erro
Mode::Command => {
greeter.command = Some(greeter.new_command.clone());
greeter.selected_session = greeter.sessions.iter().position(|(_, command)| Some(command) == greeter.command.as_ref()).unwrap_or(0);
if greeter.remember_session {
write_last_session(&greeter.new_command);
}
greeter.mode = greeter.previous_mode;
}
Mode::Sessions => {
if let Some((_, command)) = greeter.sessions.get(greeter.selected_session) {
greeter.command = Some(command.clone());
if greeter.remember_session {
write_last_session(command);
}
}
greeter.mode = greeter.previous_mode;