From 839570fc572a833b9cdf90a84cb9747725887ccd Mon Sep 17 00:00:00 2001 From: Jan-Bulthuis Date: Fri, 21 Feb 2025 20:24:49 +0100 Subject: [PATCH] Improved session gathering, init and added visual feedback --- src/gather.rs | 41 +++++++++++++++++++++++----------------- src/greetd.rs | 2 +- src/main.rs | 52 +++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 67 insertions(+), 28 deletions(-) diff --git a/src/gather.rs b/src/gather.rs index 0ad31c6..48df415 100644 --- a/src/gather.rs +++ b/src/gather.rs @@ -1,5 +1,5 @@ use std::{ - fs::{read_dir, read_link, DirEntry}, + fs::{read_dir, read_link, read_to_string, DirEntry}, path::{Path, PathBuf}, }; @@ -52,7 +52,7 @@ pub fn gather_sessions(user: &User) -> Vec { true => gather_hm_sessions(&hm_profile), false => vec![Session { name: String::from("Shell"), - path: user.shell.clone(), + init: user.shell.clone(), }], } } @@ -70,18 +70,15 @@ fn resolve_link(path: &PathBuf) -> PathBuf { fn gather_hm_sessions(path: &PathBuf) -> Vec { let generation = resolve_link(path); - let mut sessions = vec![Session { - name: String::from("Home Manager"), - path: generation, - }]; + let mut sessions = vec![gather_session_data(&generation)]; - sessions.append(&mut gather_specialisations(&sessions[0])); + sessions.append(&mut gather_specialisations(&generation)); sessions } -fn gather_specialisations(session: &Session) -> Vec { - let specialisation_path = session.path.join("specialisation"); +fn gather_specialisations(path: &PathBuf) -> Vec { + let specialisation_path = path.join("specialisation"); if !specialisation_path.exists() { return vec![]; } @@ -91,14 +88,24 @@ fn gather_specialisations(session: &Session) -> Vec { .flatten() .map(|entry| { let path = resolve_link(&entry.path()); - let name = entry - .path() - .file_name() - .unwrap() - .to_str() - .unwrap() - .to_owned(); - Session { name, path } + // let name = entry + // .path() + // .file_name() + // .unwrap() + // .to_str() + // .unwrap() + // .to_owned(); + // Session { name, path } + gather_session_data(&path) }) .collect() } + +fn gather_session_data(path: &PathBuf) -> Session { + let session_path = path.join("session"); + + let name = read_to_string(session_path.join("name")).unwrap(); + let init = session_path.join("init"); + + Session { name, init } +} diff --git a/src/greetd.rs b/src/greetd.rs index b681831..d62a2e4 100644 --- a/src/greetd.rs +++ b/src/greetd.rs @@ -30,7 +30,7 @@ pub fn login( return Ok(LoginResult::Success); } else { starting = true; - let cmd = vec![user.shell.to_str().unwrap().to_owned()]; + let cmd = vec![session.init.to_str().unwrap().to_owned()]; let env = vec![]; Request::StartSession { cmd, env }.write_to(&mut stream)?; } diff --git a/src/main.rs b/src/main.rs index ce4b762..8962e82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,7 @@ struct App { exit: bool, error: bool, focus: Focus, + status: Status, user_input: SelectorInput, session_input: SelectorInput, password_input: TextInput, @@ -44,6 +45,13 @@ enum Focus { Password, } +#[derive(PartialEq, Eq)] +enum Status { + Nothing, + Error, + Success, +} + struct SelectorInput { values: Vec, state: usize, @@ -103,7 +111,7 @@ impl Display for User { #[derive(PartialEq, Eq, Clone)] struct Session { name: String, - path: PathBuf, + init: PathBuf, } impl Display for Session { @@ -118,6 +126,7 @@ impl Default for App { let exit = false; let error = false; let focus = Focus::User; + let status = Status::Nothing; let user_input = SelectorInput { values: gather::gather_users(), state: 0, @@ -135,6 +144,7 @@ impl Default for App { exit, error, focus, + status, user_input, session_input, password_input, @@ -188,7 +198,17 @@ impl App { let box_area = middle_rows[1]; - let box_widget = Block::bordered().title("Bone jaw").style(Color::Gray); + let box_widget = Block::bordered() + .title(match self.status { + Status::Nothing => "Hewwo! >_< :3", + Status::Error => "Error :(", + Status::Success => "Success!!!", + }) + .style(match self.status { + Status::Nothing => Color::Gray, + Status::Error => Color::Red, + Status::Success => Color::Green, + }); frame.render_widget(Clear, box_area); frame.render_widget(box_widget, box_area); @@ -204,12 +224,15 @@ impl App { }); frame.render_widget(user_row, rows[0]); - let session_row = Line::from(format!("Session: {}", self.session_input.value())).style( - match self.focus { - Focus::Session => Color::White, - _ => Color::Gray, - }, - ); + let session_row = Line::from(format!( + "Session: {} {}", + self.session_input.value(), + self.session_input.state + )) + .style(match self.focus { + Focus::Session => Color::White, + _ => Color::Gray, + }); frame.render_widget(session_row, rows[2]); let password_row = Line::from(format!( @@ -291,17 +314,26 @@ impl App { } fn submit(&mut self) { + let password = self.password_input.value().to_owned(); + self.password_input.state.clear(); + if self.status == Status::Success { + return; + } match greetd::login( self.user_input.value(), self.session_input.value(), - self.password_input.value(), + &password, ) .unwrap() { LoginResult::Success => { self.error = false; + self.status = Status::Success + } + LoginResult::Failure => { + self.error = true; + self.status = Status::Error } - LoginResult::Failure => self.error = true, }; } }