Skip to content

Commit 24ceeff

Browse files
author
cht
committed
auto load dot
1 parent 231705c commit 24ceeff

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

src/main.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use tui::{
2525
};
2626
use unicode_width::UnicodeWidthStr;
2727
mod spider;
28+
mod utils;
2829
enum InputMode {
2930
Normal,
3031
Editing,
@@ -105,7 +106,18 @@ fn main() -> Result<(), Box<dyn Error>> {
105106
let mut terminal = Terminal::new(backend)?;
106107

107108
// create app and run it
108-
let app = App::default();
109+
let mut app = App::default();
110+
let informations = utils::start();
111+
if !informations.is_empty() {
112+
app.messages = informations
113+
.iter()
114+
.map(|amessage| amessage.urls.clone())
115+
.collect();
116+
app.stateoflist = true;
117+
app.state.select(Some(0));
118+
app.index = Some(0);
119+
app.informations = informations;
120+
}
109121
let res = run_app(&mut terminal, app);
110122

111123
// restore terminal

src/utils.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use crate::spider::Information;
2+
use serde_json::Value;
3+
use std::{
4+
env,
5+
fs::{self, File},
6+
io::prelude::*,
7+
io::Error,
8+
path::Path,
9+
result::Result,
10+
};
11+
fn create_storage_before() {
12+
let home = env::var("HOME").unwrap();
13+
fs::create_dir_all(home + "/.config/tv2ray").unwrap();
14+
}
15+
fn create_json_file() -> Result<String, Error> {
16+
let home = env::var("HOME").unwrap();
17+
let location = format!("{}/.config/tv2ray/storage.json", home);
18+
let path = Path::new(location.as_str());
19+
let mut file = File::create(&path)?;
20+
let storge: String = "[]".to_string();
21+
file.write_all(storge.as_bytes())?;
22+
Ok(storge)
23+
}
24+
fn get_json() -> Result<String, Error> {
25+
let home = env::var("HOME").unwrap();
26+
let location = format!("{}/.config/tv2ray/storage.json", home);
27+
let mut file = File::open(location)?;
28+
let mut output = String::new();
29+
file.read_to_string(&mut output).unwrap();
30+
Ok(output)
31+
}
32+
pub fn start() -> Vec<Information> {
33+
create_storage_before();
34+
let messages = match get_json() {
35+
Ok(output) => output,
36+
Err(_) => create_json_file().unwrap(),
37+
};
38+
let mut informations = Vec::new();
39+
let v: Value = serde_json::from_str(&messages.as_str()).unwrap();
40+
let mut index = 0;
41+
while v[index] != Value::Null {
42+
let the_url = v[index]["url"].to_string();
43+
let lenghth = the_url.len();
44+
let instore = &the_url[1..lenghth - 1];
45+
informations.push(Information {
46+
urls: instore.to_string(),
47+
func: v[index]["func"].to_string(),
48+
add: v[index]["add"].to_string(),
49+
aid: v[index]["aid"].to_string(),
50+
host: v[index]["host"].to_string(),
51+
id: v[index]["id"].to_string(),
52+
net: v[index]["net"].to_string(),
53+
path: v[index]["path"].to_string(),
54+
port: v[index]["port"].to_string(),
55+
ps: v[index]["ps"].to_string(),
56+
tls: v[index]["tls"].to_string(),
57+
typpe: v[index]["type"].to_string(),
58+
});
59+
//let names = v[index]["ps"].to_string();
60+
//start.add_item(remove_quotation(names), url);
61+
index += 1;
62+
}
63+
informations
64+
}

0 commit comments

Comments
 (0)