mirror of
https://github.com/apognu/tuigreet.git
synced 2024-09-11 07:25:29 +03:00
Fixed inconsistent display of session name on remember global session (#139).
This commit is contained in:
parent
0baa018679
commit
976534d90d
@ -22,9 +22,7 @@ use zeroize::Zeroize;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
event::Event,
|
event::Event,
|
||||||
info::{
|
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},
|
||||||
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,
|
|
||||||
},
|
|
||||||
power::PowerOption,
|
power::PowerOption,
|
||||||
ui::{
|
ui::{
|
||||||
common::{masked::MaskedString, menu::Menu, style::Theme},
|
common::{masked::MaskedString, menu::Menu, style::Theme},
|
||||||
@ -96,7 +94,7 @@ pub enum GreetAlign {
|
|||||||
#[default]
|
#[default]
|
||||||
Center,
|
Center,
|
||||||
Left,
|
Left,
|
||||||
Right
|
Right,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(SmartDefault)]
|
#[derive(SmartDefault)]
|
||||||
@ -225,15 +223,18 @@ impl Greeter {
|
|||||||
|
|
||||||
// If, on top of that, we should remember their last session.
|
// If, on top of that, we should remember their last session.
|
||||||
if greeter.remember_user_session {
|
if greeter.remember_user_session {
|
||||||
if let Ok(ref session_path) = get_last_user_session_path(greeter.username.get()) {
|
// See if we have the last free-form command from the user.
|
||||||
// Set the selected menu option and the session source.
|
if let Ok(command) = get_last_user_command(greeter.username.get()) {
|
||||||
greeter.sessions.selected = greeter.sessions.options.iter().position(|Session { path, .. }| path.as_deref() == Some(session_path)).unwrap_or(0);
|
greeter.session_source = SessionSource::Command(command);
|
||||||
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// See if we have the last free-form command from the user.
|
// If a session was saved, use it and its name.
|
||||||
if let Ok(command) = get_last_user_session(greeter.username.get()) {
|
if let Ok(ref session_path) = get_last_user_session(greeter.username.get()) {
|
||||||
greeter.session_source = SessionSource::Command(command);
|
// 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.
|
// Same thing, but not user specific.
|
||||||
if greeter.remember_session {
|
if greeter.remember_session {
|
||||||
if let Ok(ref session_path) = get_last_session_path() {
|
if let Ok(command) = get_last_command() {
|
||||||
greeter.sessions.selected = greeter.sessions.options.iter().position(|Session { path, .. }| path.as_deref() == Some(session_path)).unwrap_or(0);
|
greeter.session_source = SessionSource::Command(command.trim().to_string());
|
||||||
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(command) = get_last_session() {
|
if let Ok(ref session_path) = get_last_session_path() {
|
||||||
greeter.session_source = SessionSource::Command(command.trim().to_string());
|
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() {
|
match value.as_str() {
|
||||||
"left" => GreetAlign::Left,
|
"left" => GreetAlign::Left,
|
||||||
"right" => GreetAlign::Right,
|
"right" => GreetAlign::Right,
|
||||||
_ => GreetAlign::Center
|
_ => GreetAlign::Center,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
GreetAlign::default()
|
GreetAlign::default()
|
||||||
@ -413,7 +416,12 @@ impl Greeter {
|
|||||||
opts.optopt("", "window-padding", "padding inside the terminal area (default: 0)", "PADDING");
|
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("", "container-padding", "padding inside the main prompt container (default: 1)", "PADDING");
|
||||||
opts.optopt("", "prompt-padding", "padding between prompt rows (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-shutdown", "command to run to shut down the system", "'CMD [ARGS]...'");
|
||||||
opts.optopt("", "power-reboot", "command to run to reboot the system", "'CMD [ARGS]...'");
|
opts.optopt("", "power-reboot", "command to run to reboot the system", "'CMD [ARGS]...'");
|
||||||
|
48
src/info.rs
48
src/info.rs
@ -23,8 +23,8 @@ use crate::{
|
|||||||
|
|
||||||
const LAST_USER_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_USER_NAME: &str = "/var/cache/tuigreet/lastuser-name";
|
||||||
const LAST_SESSION: &str = "/var/cache/tuigreet/lastsession";
|
const LAST_COMMAND: &str = "/var/cache/tuigreet/lastsession";
|
||||||
const LAST_SESSION_PATH: &str = "/var/cache/tuigreet/lastsession-path";
|
const LAST_SESSION: &str = "/var/cache/tuigreet/lastsession-path";
|
||||||
|
|
||||||
const DEFAULT_MIN_UID: u16 = 1000;
|
const DEFAULT_MIN_UID: u16 = 1000;
|
||||||
const DEFAULT_MAX_UID: u16 = 60000;
|
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> {
|
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> {
|
pub fn get_last_command() -> Result<String, io::Error> {
|
||||||
Ok(fs::read_to_string(LAST_SESSION)?.trim().to_string())
|
Ok(fs::read_to_string(LAST_COMMAND)?.trim().to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_last_session_path<P>(session: &P)
|
pub fn write_last_session_path<P>(session: &P)
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
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) {
|
pub fn write_last_command(session: &str) {
|
||||||
let _ = fs::write(LAST_SESSION, session);
|
let _ = fs::write(LAST_COMMAND, session);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_last_user_session_path(username: &str) -> Result<PathBuf, io::Error> {
|
pub fn get_last_user_session(username: &str) -> Result<PathBuf, io::Error> {
|
||||||
Ok(PathBuf::from(fs::read_to_string(format!("{LAST_SESSION_PATH}-{username}"))?.trim()))
|
Ok(PathBuf::from(fs::read_to_string(format!("{LAST_SESSION}-{username}"))?.trim()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_last_user_session(username: &str) -> Result<String, io::Error> {
|
pub fn get_last_user_command(username: &str) -> Result<String, io::Error> {
|
||||||
Ok(fs::read_to_string(format!("{LAST_SESSION}-{username}"))?.trim().to_string())
|
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
|
where
|
||||||
P: AsRef<Path>,
|
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() {
|
pub fn delete_last_session() {
|
||||||
let _ = fs::remove_file(LAST_SESSION_PATH);
|
let _ = fs::remove_file(LAST_SESSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_last_user_session(username: &str, session: &str) {
|
pub fn write_last_user_command(username: &str, session: &str) {
|
||||||
let _ = fs::write(format!("{LAST_SESSION}-{username}"), session);
|
let _ = fs::write(format!("{LAST_COMMAND}-{username}"), session);
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delete_last_user_session_path(username: &str) {
|
|
||||||
let _ = fs::remove_file(format!("{LAST_SESSION_PATH}-{username}"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_last_user_session(username: &str) {
|
pub fn delete_last_user_session(username: &str) {
|
||||||
let _ = fs::remove_file(format!("{LAST_SESSION}-{username}"));
|
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> {
|
pub fn get_users(min_uid: u16, max_uid: u16) -> Vec<User> {
|
||||||
let users = unsafe { uzers::all_users() };
|
let users = unsafe { uzers::all_users() };
|
||||||
|
|
||||||
|
10
src/ipc.rs
10
src/ipc.rs
@ -8,7 +8,7 @@ use tokio::sync::{
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
event::Event,
|
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,
|
macros::SafeDebug,
|
||||||
ui::sessions::{Session, SessionSource, SessionType},
|
ui::sessions::{Session, SessionSource, SessionType},
|
||||||
AuthStatus, Greeter, Mode,
|
AuthStatus, Greeter, Mode,
|
||||||
@ -128,16 +128,16 @@ impl Ipc {
|
|||||||
SessionSource::Command(ref command) => {
|
SessionSource::Command(ref command) => {
|
||||||
tracing::info!("caching last user command: {command}");
|
tracing::info!("caching last user command: {command}");
|
||||||
|
|
||||||
write_last_user_session(&greeter.username.value, command);
|
write_last_user_command(&greeter.username.value, command);
|
||||||
delete_last_user_session_path(&greeter.username.value);
|
delete_last_user_session(&greeter.username.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
SessionSource::Session(index) => {
|
SessionSource::Session(index) => {
|
||||||
if let Some(Session { path: Some(session_path), .. }) = greeter.sessions.options.get(index) {
|
if let Some(Session { path: Some(session_path), .. }) = greeter.sessions.options.get(index) {
|
||||||
tracing::info!("caching last user session: {session_path:?}");
|
tracing::info!("caching last user session: {session_path:?}");
|
||||||
|
|
||||||
write_last_user_session_path(&greeter.username.value, session_path);
|
write_last_user_session(&greeter.username.value, session_path);
|
||||||
delete_last_user_session(&greeter.username.value);
|
delete_last_user_command(&greeter.username.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ use greetd_ipc::Request;
|
|||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
use crate::{
|
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,
|
ipc::Ipc,
|
||||||
power::power,
|
power::power,
|
||||||
ui::{
|
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());
|
greeter.session_source = SessionSource::Command(greeter.buffer.clone());
|
||||||
|
|
||||||
if greeter.remember_session {
|
if greeter.remember_session {
|
||||||
write_last_session(&greeter.buffer);
|
write_last_command(&greeter.buffer);
|
||||||
delete_last_session_path();
|
delete_last_session();
|
||||||
}
|
}
|
||||||
|
|
||||||
greeter.buffer = greeter.previous_buffer.take().unwrap_or_default();
|
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 => {
|
Mode::Sessions => {
|
||||||
let session = greeter.sessions.options.get(greeter.sessions.selected).cloned();
|
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 greeter.remember_session {
|
||||||
if let Some(ref path) = path {
|
if let Some(ref path) = path {
|
||||||
write_last_session_path(path);
|
write_last_session_path(path);
|
||||||
|
delete_last_command();
|
||||||
}
|
}
|
||||||
|
|
||||||
write_last_session(&command);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
|
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();
|
greeter.buffer = String::new();
|
||||||
|
|
||||||
if greeter.remember_user_session {
|
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() {
|
if let Some(last_session) = Session::from_path(greeter, last_session).cloned() {
|
||||||
tracing::info!("remembered user session is {}", last_session.name);
|
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);
|
tracing::info!("remembered user command is {}", command);
|
||||||
|
|
||||||
greeter.session_source = SessionSource::Command(command);
|
greeter.session_source = SessionSource::Command(command);
|
||||||
|
Loading…
Reference in New Issue
Block a user