@@ -62,7 +62,7 @@ mod imp {
6262
6363 impl Default for TerminalRepository {
6464 fn default ( ) -> Self {
65- let custom_list_path = glib:: user_data_dir ( ) . join ( "distroshelf-terminals.json" ) ;
65+ let custom_list_path = glib:: user_data_dir ( ) . join ( "distroshelf-terminals.json" ) ;
6666 Self {
6767 list : RefCell :: new ( vec ! [ ] ) ,
6868 custom_list_path,
@@ -79,30 +79,32 @@ mod imp {
7979 }
8080}
8181
82-
8382glib:: wrapper! {
8483 pub struct TerminalRepository ( ObjectSubclass <imp:: TerminalRepository >) ;
8584}
8685
8786impl TerminalRepository {
8887 pub fn new ( ) -> Self {
89- let this: Self = glib:: Object :: builder ( )
90- . build ( ) ;
88+ let this: Self = glib:: Object :: builder ( ) . build ( ) ;
9189
9290 let mut list = SUPPORTED_TERMINALS . clone ( ) ;
9391 if let Ok ( loaded_list) = Self :: load_terminals_from_json ( & this. imp ( ) . custom_list_path ) {
9492 list. extend ( loaded_list) ;
9593 } else {
96- error ! ( "Failed to load custom terminals from JSON file {:?}" , & this. imp( ) . custom_list_path) ;
94+ error ! (
95+ "Failed to load custom terminals from JSON file {:?}" ,
96+ & this. imp( ) . custom_list_path
97+ ) ;
9798 }
98-
99+
99100 list. sort_by ( |a, b| a. name . cmp ( & b. name ) ) ;
100101 this. imp ( ) . list . replace ( list) ;
101102 this
102103 }
103104
104105 pub fn is_read_only ( & self , name : & str ) -> bool {
105- self . imp ( ) . list
106+ self . imp ( )
107+ . list
106108 . borrow ( )
107109 . iter ( )
108110 . find ( |x| x. name == name)
@@ -113,11 +115,13 @@ impl TerminalRepository {
113115 if self . is_read_only ( terminal. name . as_str ( ) ) {
114116 return Err ( anyhow:: anyhow!( "Cannot modify read-only terminal" ) ) ;
115117 }
116- let mut list = self . imp ( ) . list . borrow_mut ( ) ;
117- list. retain ( |x| x. name != terminal. name ) ;
118- list. push ( terminal) ;
118+ {
119+ let mut list = self . imp ( ) . list . borrow_mut ( ) ;
120+ list. retain ( |x| x. name != terminal. name ) ;
121+ list. push ( terminal) ;
119122
120- list. sort_by ( |a, b| a. name . cmp ( & b. name ) ) ;
123+ list. sort_by ( |a, b| a. name . cmp ( & b. name ) ) ;
124+ }
121125
122126 self . save_terminals_to_json ( ) ;
123127 Ok ( ( ) )
@@ -127,16 +131,25 @@ impl TerminalRepository {
127131 if self . is_read_only ( name) {
128132 return Err ( anyhow:: anyhow!( "Cannot modify read-only terminal" ) ) ;
129133 }
130- self . imp ( ) . list . borrow_mut ( ) . retain ( |x| x. name != name) ;
134+ {
135+ self . imp ( ) . list . borrow_mut ( ) . retain ( |x| x. name != name) ;
136+ }
137+ self . save_terminals_to_json ( ) ;
131138 Ok ( ( ) )
132139 }
133140
134141 pub fn terminal_by_name ( & self , name : & str ) -> Option < Terminal > {
135- self . imp ( ) . list . borrow ( ) . iter ( ) . find ( |x| x. name == name) . cloned ( )
142+ self . imp ( )
143+ . list
144+ . borrow ( )
145+ . iter ( )
146+ . find ( |x| x. name == name)
147+ . cloned ( )
136148 }
137149
138150 pub fn terminal_by_program ( & self , program : & str ) -> Option < Terminal > {
139- self . imp ( ) . list
151+ self . imp ( )
152+ . list
140153 . borrow ( )
141154 . iter ( )
142155 . find ( |x| x. program == program)
@@ -148,7 +161,14 @@ impl TerminalRepository {
148161 }
149162
150163 fn save_terminals_to_json ( & self ) {
151- let list: Vec < Terminal > = self . imp ( ) . list . borrow ( ) . iter ( ) . filter ( |x| !x. read_only ) . cloned ( ) . collect :: < Vec < _ > > ( ) ;
164+ let list: Vec < Terminal > = self
165+ . imp ( )
166+ . list
167+ . borrow ( )
168+ . iter ( )
169+ . filter ( |x| !x. read_only )
170+ . cloned ( )
171+ . collect :: < Vec < _ > > ( ) ;
152172 let json = serde_json:: to_string ( & * list) . unwrap ( ) ;
153173 std:: fs:: write ( & self . imp ( ) . custom_list_path , json) . unwrap ( ) ;
154174 }
@@ -160,9 +180,16 @@ impl TerminalRepository {
160180 }
161181
162182 pub async fn default_terminal ( & self ) -> Option < Terminal > {
163- let command = Command :: new_with_args ( "flatpak-spawn" , & [ "--host" , "--" , "get" ,
183+ let command = Command :: new_with_args (
184+ "flatpak-spawn" ,
185+ & [
186+ "--host" ,
187+ "--" ,
188+ "get" ,
164189 "org.gnome.desktop.default-applications.terminal" ,
165- "exec" , ] ) ;
190+ "exec" ,
191+ ] ,
192+ ) ;
166193 let output = self . imp ( ) . command_runner . borrow ( ) . output ( command. clone ( ) ) ;
167194 let Ok ( output) = output. await else {
168195 error ! ( "Failed to get default terminal, running {:?}" , & command) ;
@@ -173,11 +200,13 @@ impl TerminalRepository {
173200 return None ;
174201 }
175202 info ! ( "Default terminal program: {}" , terminal_program) ;
176- self . terminal_by_program ( & terminal_program)
177- . or_else ( || {
178- error ! ( "Terminal program {} not found in the list" , terminal_program) ;
179- None
180- } )
203+ self . terminal_by_program ( & terminal_program) . or_else ( || {
204+ error ! (
205+ "Terminal program {} not found in the list" ,
206+ terminal_program
207+ ) ;
208+ None
209+ } )
181210 }
182211}
183212
0 commit comments