@@ -8,6 +8,7 @@ use redbcli::{
8
8
use redbcli:: { KvInfo , TableInfo } ;
9
9
use rustyline:: error:: ReadlineError ;
10
10
use rustyline:: DefaultEditor ;
11
+ use std:: collections:: HashMap ;
11
12
use std:: io:: Write ;
12
13
use std:: path:: PathBuf ;
13
14
#[ derive( Default ) ]
@@ -23,13 +24,20 @@ fn main() -> Result<(), String> {
23
24
if let Some ( db_path) = parse_flags. path {
24
25
clistatus. filepath = db_path;
25
26
}
26
- let history_path = PathBuf :: from ( "/tmp/redbcli/history " ) ;
27
+ let history_path = PathBuf :: from ( "/tmp/redbcli" ) ;
27
28
if !history_path. exists ( ) {
28
- std:: fs:: create_dir_all ( & history_path) . expect ( "create history failed" ) ;
29
- std:: fs:: OpenOptions :: new ( ) . create ( true ) . open ( & history_path) ;
29
+ std:: fs:: create_dir_all ( & history_path) . expect ( "create history dir failed" ) ;
30
30
}
31
+ let file_history = history_path. join ( "history.txt" ) ;
32
+ //check history file
33
+ {
34
+ if !file_history. exists ( ) {
35
+ std:: fs:: File :: create ( & file_history) . expect ( "create history file failed" ) ;
36
+ }
37
+ }
38
+
31
39
let mut rl = DefaultEditor :: new ( ) . unwrap ( ) ;
32
- if rl. load_history ( & history_path ) . is_err ( ) {
40
+ if rl. load_history ( & file_history ) . is_err ( ) {
33
41
println ! ( "No previous history." ) ;
34
42
}
35
43
loop {
@@ -38,7 +46,7 @@ fn main() -> Result<(), String> {
38
46
clistatus. filepath, clistatus. tablename
39
47
) ;
40
48
let readline = rl. readline ( & prompt) ;
41
- let _ = rl . save_history ( & history_path ) ;
49
+
42
50
match readline {
43
51
Ok ( line) => {
44
52
let _ = rl. add_history_entry ( line. as_str ( ) ) ;
@@ -67,7 +75,7 @@ fn main() -> Result<(), String> {
67
75
}
68
76
}
69
77
}
70
-
78
+ let _ = rl . save_history ( & file_history ) ;
71
79
Ok ( ( ) )
72
80
}
73
81
@@ -102,31 +110,60 @@ fn respond(line: &str, status: &mut CliStatus) -> Result<bool, String> {
102
110
write_io ( format ! ( "Use table {}" , tablename) ) ?;
103
111
}
104
112
Commands :: Edit => {
113
+ if status. tablename . is_empty ( ) {
114
+ write_io ( "you must set table first !!" . to_string ( ) ) ?;
115
+ return Ok ( false ) ;
116
+ }
105
117
let mut temp_file = tempfile:: NamedTempFile :: new ( ) . unwrap ( ) ;
106
118
let result = status. dbm . common_get_all ( ) . map_err ( |e| e. to_string ( ) ) ?;
107
- let json_data = serde_json:: to_string ( & result) . unwrap ( ) ;
119
+ let json_data = serde_json:: to_string_pretty ( & result) . unwrap ( ) ;
120
+
108
121
temp_file. write_all ( json_data. as_bytes ( ) ) . unwrap ( ) ;
109
122
110
123
let temp_path = temp_file. path ( ) . to_str ( ) . expect ( "Invalid path" ) ;
111
124
112
- // 调用 vim 编辑器
113
125
let mut child = std:: process:: Command :: new ( "vim" )
114
126
. arg ( temp_path)
127
+ . arg ( "+syntax on" )
128
+ . arg ( "+set number" )
129
+ . arg ( "+set filetype=json" )
115
130
. stdin ( std:: process:: Stdio :: inherit ( ) )
116
131
. stdout ( std:: process:: Stdio :: inherit ( ) )
117
132
. stderr ( std:: process:: Stdio :: inherit ( ) )
118
- . spawn ( ) . unwrap ( ) ;
133
+ . spawn ( )
134
+ . unwrap ( ) ;
119
135
120
- // 等待用户完成编辑
121
- let status = child. wait ( ) . unwrap ( ) ;
136
+ let vim_status = child. wait ( ) . unwrap ( ) ;
122
137
123
- if !status . success ( ) {
138
+ if !vim_status . success ( ) {
124
139
eprintln ! ( "Vim exited with an error" ) ;
125
140
return Ok ( true ) ;
126
141
}
127
142
128
143
let modified_data = std:: fs:: read_to_string ( temp_path) . unwrap ( ) ;
129
- println ! ( "Modified data:\n {}" , modified_data) ;
144
+ match serde_json:: from_str :: < HashMap < String , String > > ( & modified_data) {
145
+ Ok ( r_data) => {
146
+ if modified_data == json_data {
147
+ println ! ( "No changed!" ) ;
148
+ return Ok ( false ) ;
149
+ }
150
+ result. iter ( ) . for_each ( |( key, _) | {
151
+ let _ = status. dbm . common_remove_by_key ( key. to_string ( ) ) ;
152
+ } ) ;
153
+
154
+ println ! ( "Save data to update the database" ) ;
155
+ r_data. iter ( ) . for_each ( |( key, value) | {
156
+ let _ = status
157
+ . dbm
158
+ . common_update_by_key ( key. to_string ( ) , value. to_string ( ) ) ;
159
+ } ) ;
160
+ return Ok ( false ) ;
161
+ }
162
+ Err ( _) => {
163
+ println ! ( "This is not a valid json str" ) ;
164
+ return Ok ( false ) ;
165
+ }
166
+ } ;
130
167
}
131
168
132
169
Commands :: Info ( subcmd) => {
@@ -136,7 +173,6 @@ fn respond(line: &str, status: &mut CliStatus) -> Result<bool, String> {
136
173
if status. tablename . is_empty ( ) {
137
174
let result = status. dbm . gettables ( ) . map_err ( |e| e. to_string ( ) ) ?;
138
175
TableInfo { tablename : result } . print_data ( ) ;
139
- // write_io(format!("data \n{:?}", result))?;
140
176
return Ok ( false ) ;
141
177
} else {
142
178
let result = status. dbm . common_get_all ( ) . map_err ( |e| e. to_string ( ) ) ?;
@@ -161,7 +197,6 @@ fn respond(line: &str, status: &mut CliStatus) -> Result<bool, String> {
161
197
KvInfo { kvdatas : result } . print_data ( ) ;
162
198
return Ok ( false ) ;
163
199
}
164
-
165
200
}
166
201
}
167
202
Commands :: Exit => {
0 commit comments