Skip to content

Commit 8ba68b5

Browse files
author
cht
committed
save done
1 parent 24ceeff commit 8ba68b5

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

src/main.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,59 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
159159
let input = vec![app.input.clone()];
160160
let get_list = spider::get_the_key(input.clone());
161161
if let Ok(list) = get_list {
162+
let mut storge: String = String::new();
163+
storge.push('[');
164+
storge.push('\n');
162165
if !list[0].is_empty() {
163166
app.messages = list[0].clone();
164167
app.stateoflist = true;
165168
app.state.select(Some(0));
166169
app.index = Some(0);
167170
for alist in &list[0] {
168-
app.informations
169-
.push(spider::Information::new(alist.to_string()));
171+
let information = spider::Information::new(alist.to_string());
172+
app.informations.push(information.clone());
173+
storge.push_str(
174+
format!(
175+
"{{
176+
\"func\":{},
177+
\"url\":\"{}\",
178+
\"add\":{},
179+
\"aid\":{},
180+
\"host\":{},
181+
\"id\":{},
182+
\"net\":{},
183+
\"path\":{},
184+
\"port\":{},
185+
\"ps\":{},
186+
\"tls\":{},
187+
\"type\":{}
188+
}},\n",
189+
information.clone().func,
190+
information.clone().urls,
191+
information.clone().add,
192+
information.clone().aid,
193+
information.clone().host,
194+
information.clone().id,
195+
information.clone().net,
196+
information.clone().path,
197+
information.clone().port,
198+
information.clone().ps,
199+
information.clone().tls,
200+
information.clone().typpe
201+
)
202+
.as_str(),
203+
);
170204
}
171205
}
206+
storge.pop();
207+
storge.pop();
208+
storge.push('\n');
209+
storge.push(']');
210+
if let Err(err) = utils::create_json_file(storge) {
211+
panic!("err {}", err);
212+
};
172213
}
214+
173215
//app.messages.push(app.input.drain(..).collect());
174216
}
175217
KeyCode::Char(c) => {

src/utils.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ fn create_storage_before() {
1212
let home = env::var("HOME").unwrap();
1313
fs::create_dir_all(home + "/.config/tv2ray").unwrap();
1414
}
15-
fn create_json_file() -> Result<String, Error> {
15+
pub fn create_json_file(input: String) -> Result<(), Error> {
1616
let home = env::var("HOME").unwrap();
1717
let location = format!("{}/.config/tv2ray/storage.json", home);
1818
let path = Path::new(location.as_str());
1919
let mut file = File::create(&path)?;
20-
let storge: String = "[]".to_string();
21-
file.write_all(storge.as_bytes())?;
22-
Ok(storge)
20+
//let storge: String = input;
21+
file.write_all(input.as_bytes())?;
22+
Ok(())
2323
}
2424
fn get_json() -> Result<String, Error> {
2525
let home = env::var("HOME").unwrap();
@@ -33,7 +33,12 @@ pub fn start() -> Vec<Information> {
3333
create_storage_before();
3434
let messages = match get_json() {
3535
Ok(output) => output,
36-
Err(_) => create_json_file().unwrap(),
36+
Err(_) => {
37+
if let Err(err) = create_json_file("[]".to_string()) {
38+
panic!("{}",err);
39+
};
40+
"[]".to_string()
41+
}
3742
};
3843
let mut informations = Vec::new();
3944
let v: Value = serde_json::from_str(&messages.as_str()).unwrap();

0 commit comments

Comments
 (0)