Improved session gathering, init and added visual feedback

This commit is contained in:
Jan-Bulthuis 2025-02-21 20:24:49 +01:00
parent 17448f5de7
commit 839570fc57
3 changed files with 67 additions and 28 deletions

View File

@ -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<Session> {
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<Session> {
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<Session> {
let specialisation_path = session.path.join("specialisation");
fn gather_specialisations(path: &PathBuf) -> Vec<Session> {
let specialisation_path = path.join("specialisation");
if !specialisation_path.exists() {
return vec![];
}
@ -91,14 +88,24 @@ fn gather_specialisations(session: &Session) -> Vec<Session> {
.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 }
}

View File

@ -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)?;
}

View File

@ -32,6 +32,7 @@ struct App {
exit: bool,
error: bool,
focus: Focus,
status: Status,
user_input: SelectorInput<User>,
session_input: SelectorInput<Session>,
password_input: TextInput,
@ -44,6 +45,13 @@ enum Focus {
Password,
}
#[derive(PartialEq, Eq)]
enum Status {
Nothing,
Error,
Success,
}
struct SelectorInput<T: Display> {
values: Vec<T>,
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,
};
}
}