1+ pub mod update;
2+
3+ use ratatui:: style:: Color ;
4+ use ratatui:: widgets:: TableState ;
5+ use std:: time:: Instant ;
6+
7+ use crate :: sys;
8+
9+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
10+ pub enum ActiveTab {
11+ Users ,
12+ Groups ,
13+ }
14+
15+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
16+ pub enum UsersFocus {
17+ UsersList ,
18+ MemberOf ,
19+ }
20+
21+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
22+ pub enum InputMode {
23+ Normal ,
24+ SearchUsers ,
25+ SearchGroups ,
26+ Modal ,
27+ }
28+
29+ #[ derive( Clone , Copy , Debug ) ]
30+ pub struct Theme {
31+ pub text : Color ,
32+ pub _muted : Color ,
33+ pub title : Color ,
34+ pub border : Color ,
35+ pub header_bg : Color ,
36+ pub header_fg : Color ,
37+ pub status_bg : Color ,
38+ pub status_fg : Color ,
39+ pub highlight_fg : Color ,
40+ pub highlight_bg : Color ,
41+ }
42+
43+ impl Theme {
44+ pub fn dark ( ) -> Self {
45+ Self {
46+ text : Color :: Gray ,
47+ _muted : Color :: DarkGray ,
48+ title : Color :: Cyan ,
49+ border : Color :: Gray ,
50+ header_bg : Color :: Black ,
51+ header_fg : Color :: Cyan ,
52+ status_bg : Color :: DarkGray ,
53+ status_fg : Color :: Black ,
54+ highlight_fg : Color :: Yellow ,
55+ highlight_bg : Color :: Reset ,
56+ }
57+ }
58+ }
59+
60+ #[ derive( Clone , Debug ) ]
61+ pub enum ModalState {
62+ Actions { selected : usize } ,
63+ ModifyMenu { selected : usize } ,
64+ ModifyGroupsAdd { selected : usize , offset : usize } ,
65+ ModifyGroupsRemove { selected : usize , offset : usize } ,
66+ ModifyDetailsMenu { selected : usize } ,
67+ ModifyShell { selected : usize , offset : usize , shells : Vec < String > } ,
68+ ModifyTextInput { field : ModifyField , value : String } ,
69+ DeleteConfirm { selected : usize , allowed : bool } ,
70+ Info { message : String } ,
71+ SudoPrompt { next : PendingAction , password : String , error : Option < String > } ,
72+ GroupsActions { selected : usize , target_gid : Option < u32 > } ,
73+ GroupAddInput { name : String } ,
74+ GroupDeleteConfirm { selected : usize } ,
75+ GroupModifyMenu { selected : usize , target_gid : Option < u32 > } ,
76+ GroupModifyAddMembers { selected : usize , offset : usize , target_gid : Option < u32 > } ,
77+ GroupModifyRemoveMembers { selected : usize , offset : usize , target_gid : Option < u32 > } ,
78+ }
79+
80+ #[ derive( Clone , Debug ) ]
81+ pub enum ModifyField { Username , Fullname }
82+
83+ #[ derive( Clone , Debug ) ]
84+ pub enum PendingAction {
85+ AddUserToGroup { username : String , groupname : String } ,
86+ RemoveUserFromGroup { username : String , groupname : String } ,
87+ ChangeShell { username : String , new_shell : String } ,
88+ ChangeFullname { username : String , new_fullname : String } ,
89+ ChangeUsername { old_username : String , new_username : String } ,
90+ CreateGroup { groupname : String } ,
91+ DeleteGroup { groupname : String } ,
92+ }
93+
94+ pub struct AppState {
95+ pub started_at : Instant ,
96+ pub users_all : Vec < sys:: SystemUser > ,
97+ pub users : Vec < sys:: SystemUser > ,
98+ pub groups_all : Vec < sys:: SystemGroup > ,
99+ pub groups : Vec < sys:: SystemGroup > ,
100+ pub active_tab : ActiveTab ,
101+ pub selected_user_index : usize ,
102+ pub selected_group_index : usize ,
103+ pub rows_per_page : usize ,
104+ pub _table_state : TableState ,
105+ pub input_mode : InputMode ,
106+ pub search_query : String ,
107+ pub theme : Theme ,
108+ pub modal : Option < ModalState > ,
109+ pub users_focus : UsersFocus ,
110+ pub sudo_password : Option < String > ,
111+ }
112+
113+ impl AppState {
114+ pub fn new ( ) -> Self {
115+ let adapter = crate :: sys:: SystemAdapter :: new ( ) ;
116+ let mut users_all = adapter. list_users ( ) . unwrap_or_default ( ) ;
117+ users_all. sort_by_key ( |u| u. uid ) ;
118+ let groups_all = adapter. list_groups ( ) . unwrap_or_default ( ) ;
119+ Self {
120+ started_at : Instant :: now ( ) ,
121+ users : users_all. clone ( ) ,
122+ users_all,
123+ groups : groups_all. clone ( ) ,
124+ groups_all,
125+ active_tab : ActiveTab :: Users ,
126+ selected_user_index : 0 ,
127+ selected_group_index : 0 ,
128+ rows_per_page : 10 ,
129+ _table_state : TableState :: default ( ) ,
130+ input_mode : InputMode :: Normal ,
131+ search_query : String :: new ( ) ,
132+ theme : Theme :: dark ( ) ,
133+ modal : None ,
134+ users_focus : UsersFocus :: UsersList ,
135+ sudo_password : None ,
136+ }
137+ }
138+ }
139+
140+ pub use update:: run_app as run;
0 commit comments