@@ -27,7 +27,7 @@ pub struct Config {
27
27
#[ derive( Deserialize , PartialEq , Debug ) ]
28
28
pub struct Settings {
29
29
pub default_editor : String ,
30
- pub max_history_entries : usize ,
30
+ pub max_history : usize ,
31
31
pub ls_display_block : usize ,
32
32
}
33
33
@@ -100,7 +100,7 @@ impl Hopper {
100
100
let mut new_conf =
101
101
fs:: File :: create ( & env. config_file ) . expect ( "[error] Unable to create config file." ) ;
102
102
new_conf
103
- . write_all ( b"[settings]\n default_editor=\" nvim\" \n max_history_entries=200 \n ls_display_block=0" )
103
+ . write_all ( b"[settings]\n default_editor=\" nvim\" \n max_history=0 \n ls_display_block=0" )
104
104
. expect ( "[error] Unable to generate default config file." ) ;
105
105
} ;
106
106
let toml_str: String = fs:: read_to_string ( env. config_file . clone ( ) ) . unwrap ( ) ;
@@ -163,6 +163,25 @@ impl Hopper {
163
163
Ok ( ( ) )
164
164
}
165
165
166
+ fn map_editor < T : AsRef < Path > > ( & self , f : T ) -> String {
167
+ let ext_option = f. as_ref ( ) . extension ( ) ;
168
+ match & self . config . editors {
169
+ Some ( editor_map) => match ext_option {
170
+ Some ( ext) => match editor_map. get (
171
+ & ( ext
172
+ . to_str ( )
173
+ . expect ( "[error] Cannot extract extension." )
174
+ . to_string ( ) ) ,
175
+ ) {
176
+ Some ( special_editor) => special_editor. to_string ( ) ,
177
+ None => self . config . settings . default_editor . to_string ( ) ,
178
+ } ,
179
+ None => self . config . settings . default_editor . to_string ( ) ,
180
+ } ,
181
+ None => self . config . settings . default_editor . to_string ( ) ,
182
+ }
183
+ }
184
+
166
185
pub fn use_hop ( & mut self , shortcut_name : String ) -> anyhow:: Result < ( ) > {
167
186
let query = format ! (
168
187
"SELECT location FROM named_hops WHERE name=\" {}\" " ,
@@ -173,9 +192,10 @@ impl Hopper {
173
192
let location = statement. read :: < String , _ > ( "location" ) ?;
174
193
let location_path = PathBuf :: from ( & location) ;
175
194
if location_path. is_file ( ) {
195
+ let editor = self . map_editor ( & location_path) ;
176
196
println ! (
177
197
"__cmd__ {} {}" ,
178
- self . config . settings . default_editor , location
198
+ editor , location
179
199
) ;
180
200
} else {
181
201
println ! ( "__cd__ {}" , location) ;
@@ -187,22 +207,7 @@ impl Hopper {
187
207
Some ( ( dir, short) ) => {
188
208
self . log_history ( dir. as_path ( ) . display ( ) . to_string ( ) , short) ?;
189
209
if dir. is_file ( ) {
190
- let ext_option = dir. extension ( ) ;
191
- let editor = match & self . config . editors {
192
- Some ( editor_map) => match ext_option {
193
- Some ( ext) => match editor_map. get (
194
- & ( ext
195
- . to_str ( )
196
- . expect ( "[error] Cannot extract extension." )
197
- . to_string ( ) ) ,
198
- ) {
199
- Some ( special_editor) => special_editor,
200
- None => & self . config . settings . default_editor ,
201
- } ,
202
- None => & self . config . settings . default_editor ,
203
- } ,
204
- None => & self . config . settings . default_editor ,
205
- } ;
210
+ let editor = self . map_editor ( & dir) ;
206
211
println ! ( "__cmd__ {} {}" , editor, dir. as_path( ) . display( ) . to_string( ) ) ;
207
212
} else {
208
213
println ! ( "__cd__ {}" , dir. as_path( ) . display( ) . to_string( ) ) ;
@@ -226,13 +231,23 @@ impl Hopper {
226
231
}
227
232
228
233
pub fn log_history ( & self , location : String , name : String ) -> anyhow:: Result < ( ) > {
229
- let query = format ! (
230
- "INSERT INTO history (time, name, location) VALUES ({}, \" {}\" , \" {}\" ) " ,
231
- Local :: now( ) . format( "%Y%m%d%H%M%S" ) ,
232
- name,
233
- location
234
- ) ;
235
- self . db . execute ( & query) ?;
234
+ if self . config . settings . max_history > 0 {
235
+ let query = format ! (
236
+ "INSERT INTO history (time, name, location) VALUES ({}, \" {}\" , \" {}\" ) " ,
237
+ Local :: now( ) . format( "%Y%m%d%H%M%S" ) ,
238
+ name,
239
+ location
240
+ ) ;
241
+ self . db . execute ( & query) ?;
242
+ let mut count_result = self . db . prepare ( "SELECT COUNT(*) AS hist_count, MIN(time) AS hist_min FROM history" ) ?;
243
+ if let Ok ( sqlite:: State :: Row ) = count_result. next ( ) {
244
+ let history_count = count_result. read :: < i64 , _ > ( "hist_count" ) ?;
245
+ let history_min = count_result. read :: < String , _ > ( "hist_min" ) ?;
246
+ if history_count > self . config . settings . max_history as i64 {
247
+ self . db . execute ( & format ! ( "DELETE FROM history WHERE time=\" {}\" " , history_min) ) ?;
248
+ } ;
249
+ } ;
250
+ } ;
236
251
Ok ( ( ) )
237
252
}
238
253
0 commit comments