Improved session gathering, init and added visual feedback
This commit is contained in:
		
							parent
							
								
									17448f5de7
								
							
						
					
					
						commit
						839570fc57
					
				| @ -1,5 +1,5 @@ | |||||||
| use std::{ | use std::{ | ||||||
|     fs::{read_dir, read_link, DirEntry}, |     fs::{read_dir, read_link, read_to_string, DirEntry}, | ||||||
|     path::{Path, PathBuf}, |     path::{Path, PathBuf}, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| @ -52,7 +52,7 @@ pub fn gather_sessions(user: &User) -> Vec<Session> { | |||||||
|         true => gather_hm_sessions(&hm_profile), |         true => gather_hm_sessions(&hm_profile), | ||||||
|         false => vec![Session { |         false => vec![Session { | ||||||
|             name: String::from("Shell"), |             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> { | fn gather_hm_sessions(path: &PathBuf) -> Vec<Session> { | ||||||
|     let generation = resolve_link(path); |     let generation = resolve_link(path); | ||||||
|     let mut sessions = vec![Session { |     let mut sessions = vec![gather_session_data(&generation)]; | ||||||
|         name: String::from("Home Manager"), |  | ||||||
|         path: generation, |  | ||||||
|     }]; |  | ||||||
| 
 | 
 | ||||||
|     sessions.append(&mut gather_specialisations(&sessions[0])); |     sessions.append(&mut gather_specialisations(&generation)); | ||||||
| 
 | 
 | ||||||
|     sessions |     sessions | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn gather_specialisations(session: &Session) -> Vec<Session> { | fn gather_specialisations(path: &PathBuf) -> Vec<Session> { | ||||||
|     let specialisation_path = session.path.join("specialisation"); |     let specialisation_path = path.join("specialisation"); | ||||||
|     if !specialisation_path.exists() { |     if !specialisation_path.exists() { | ||||||
|         return vec![]; |         return vec![]; | ||||||
|     } |     } | ||||||
| @ -91,14 +88,24 @@ fn gather_specialisations(session: &Session) -> Vec<Session> { | |||||||
|         .flatten() |         .flatten() | ||||||
|         .map(|entry| { |         .map(|entry| { | ||||||
|             let path = resolve_link(&entry.path()); |             let path = resolve_link(&entry.path()); | ||||||
|             let name = entry |             // let name = entry
 | ||||||
|                 .path() |             //     .path()
 | ||||||
|                 .file_name() |             //     .file_name()
 | ||||||
|                 .unwrap() |             //     .unwrap()
 | ||||||
|                 .to_str() |             //     .to_str()
 | ||||||
|                 .unwrap() |             //     .unwrap()
 | ||||||
|                 .to_owned(); |             //     .to_owned();
 | ||||||
|             Session { name, path } |             // Session { name, path }
 | ||||||
|  |             gather_session_data(&path) | ||||||
|         }) |         }) | ||||||
|         .collect() |         .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 } | ||||||
|  | } | ||||||
|  | |||||||
| @ -30,7 +30,7 @@ pub fn login( | |||||||
|                     return Ok(LoginResult::Success); |                     return Ok(LoginResult::Success); | ||||||
|                 } else { |                 } else { | ||||||
|                     starting = true; |                     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![]; |                     let env = vec![]; | ||||||
|                     Request::StartSession { cmd, env }.write_to(&mut stream)?; |                     Request::StartSession { cmd, env }.write_to(&mut stream)?; | ||||||
|                 } |                 } | ||||||
|  | |||||||
							
								
								
									
										48
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										48
									
								
								src/main.rs
									
									
									
									
									
								
							| @ -32,6 +32,7 @@ struct App { | |||||||
|     exit: bool, |     exit: bool, | ||||||
|     error: bool, |     error: bool, | ||||||
|     focus: Focus, |     focus: Focus, | ||||||
|  |     status: Status, | ||||||
|     user_input: SelectorInput<User>, |     user_input: SelectorInput<User>, | ||||||
|     session_input: SelectorInput<Session>, |     session_input: SelectorInput<Session>, | ||||||
|     password_input: TextInput, |     password_input: TextInput, | ||||||
| @ -44,6 +45,13 @@ enum Focus { | |||||||
|     Password, |     Password, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #[derive(PartialEq, Eq)] | ||||||
|  | enum Status { | ||||||
|  |     Nothing, | ||||||
|  |     Error, | ||||||
|  |     Success, | ||||||
|  | } | ||||||
|  | 
 | ||||||
| struct SelectorInput<T: Display> { | struct SelectorInput<T: Display> { | ||||||
|     values: Vec<T>, |     values: Vec<T>, | ||||||
|     state: usize, |     state: usize, | ||||||
| @ -103,7 +111,7 @@ impl Display for User { | |||||||
| #[derive(PartialEq, Eq, Clone)] | #[derive(PartialEq, Eq, Clone)] | ||||||
| struct Session { | struct Session { | ||||||
|     name: String, |     name: String, | ||||||
|     path: PathBuf, |     init: PathBuf, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl Display for Session { | impl Display for Session { | ||||||
| @ -118,6 +126,7 @@ impl Default for App { | |||||||
|         let exit = false; |         let exit = false; | ||||||
|         let error = false; |         let error = false; | ||||||
|         let focus = Focus::User; |         let focus = Focus::User; | ||||||
|  |         let status = Status::Nothing; | ||||||
|         let user_input = SelectorInput { |         let user_input = SelectorInput { | ||||||
|             values: gather::gather_users(), |             values: gather::gather_users(), | ||||||
|             state: 0, |             state: 0, | ||||||
| @ -135,6 +144,7 @@ impl Default for App { | |||||||
|             exit, |             exit, | ||||||
|             error, |             error, | ||||||
|             focus, |             focus, | ||||||
|  |             status, | ||||||
|             user_input, |             user_input, | ||||||
|             session_input, |             session_input, | ||||||
|             password_input, |             password_input, | ||||||
| @ -188,7 +198,17 @@ impl App { | |||||||
| 
 | 
 | ||||||
|         let box_area = middle_rows[1]; |         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(Clear, box_area); | ||||||
|         frame.render_widget(box_widget, box_area); |         frame.render_widget(box_widget, box_area); | ||||||
| @ -204,12 +224,15 @@ impl App { | |||||||
|             }); |             }); | ||||||
|         frame.render_widget(user_row, rows[0]); |         frame.render_widget(user_row, rows[0]); | ||||||
| 
 | 
 | ||||||
|         let session_row = Line::from(format!("Session: {}", self.session_input.value())).style( |         let session_row = Line::from(format!( | ||||||
|             match self.focus { |             "Session: {} {}", | ||||||
|  |             self.session_input.value(), | ||||||
|  |             self.session_input.state | ||||||
|  |         )) | ||||||
|  |         .style(match self.focus { | ||||||
|             Focus::Session => Color::White, |             Focus::Session => Color::White, | ||||||
|             _ => Color::Gray, |             _ => Color::Gray, | ||||||
|             }, |         }); | ||||||
|         ); |  | ||||||
|         frame.render_widget(session_row, rows[2]); |         frame.render_widget(session_row, rows[2]); | ||||||
| 
 | 
 | ||||||
|         let password_row = Line::from(format!( |         let password_row = Line::from(format!( | ||||||
| @ -291,17 +314,26 @@ impl App { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fn submit(&mut self) { |     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( |         match greetd::login( | ||||||
|             self.user_input.value(), |             self.user_input.value(), | ||||||
|             self.session_input.value(), |             self.session_input.value(), | ||||||
|             self.password_input.value(), |             &password, | ||||||
|         ) |         ) | ||||||
|         .unwrap() |         .unwrap() | ||||||
|         { |         { | ||||||
|             LoginResult::Success => { |             LoginResult::Success => { | ||||||
|                 self.error = false; |                 self.error = false; | ||||||
|  |                 self.status = Status::Success | ||||||
|  |             } | ||||||
|  |             LoginResult::Failure => { | ||||||
|  |                 self.error = true; | ||||||
|  |                 self.status = Status::Error | ||||||
|             } |             } | ||||||
|             LoginResult::Failure => self.error = true, |  | ||||||
|         }; |         }; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jan-Bulthuis
						Jan-Bulthuis