Skip to content

Commit edb1fe2

Browse files
author
cht
committed
存储多个订阅
1 parent 025d197 commit edb1fe2

5 files changed

Lines changed: 72 additions & 17 deletions

File tree

src/state.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ pub fn run_app(terminal: &mut Terminal<MyBackend>) -> io::Result<()> {
2525
let mut appsub = AppSub::default();
2626
let informations = utils::start();
2727
if !informations.is_empty() {
28-
appsub.messages = informations
28+
appsub.subs = informations
2929
.iter()
3030
.map(|amessage| spider::remove_quotation(amessage.ps.clone()))
3131
.collect();
3232
appsub.stateoflist = true;
3333
appsub.state.select(Some(0));
3434
appsub.informations = informations;
3535
}
36+
appsub.subscription = utils::get_subs();
3637
appsub.settings_input[0] = utils::start_v2core();
3738
let appbar = AppBar::new();
3839
let mut local_page = Page::SubScribe;

src/subscribe/appsub.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use super::app::*;
22
use super::render::ui;
33
use super::state::subscribe_state;
44
use crate::spider;
5-
use crate::state::MyBackend;
6-
use crate::state::IFEXIT;
5+
use crate::state::{MyBackend, IFEXIT};
76
use std::io;
87
use tui::widgets::ListState;
98
use tui::Terminal;
@@ -18,25 +17,29 @@ pub(crate) enum InputMode {
1817
/// App holds the state of the application
1918
pub struct AppSub {
2019
/// Current value of the input box
20+
// search bar
2121
pub input: String,
22+
// settings , include coresetting and subscribe setting
2223
pub settings_input: Vec<String>,
2324
/// Current input mode
2425
pub(crate) input_mode: InputMode,
25-
/// History of recorded messages
26-
pub messages: Vec<String>,
26+
// History of recorded subs
27+
// subs's names
28+
pub subs: Vec<String>,
2729
pub state: ListState,
2830
pub index_subscription: ListState,
2931
pub index_settings: usize,
3032
pub stateoflist: bool,
3133
pub show_popup: bool,
34+
// subscribes's information
3235
pub informations: Vec<spider::Information>,
3336
pub subscription: Vec<String>,
3437
}
3538
impl AppSub {
3639
pub fn next(&mut self) {
3740
let i = match self.state.selected() {
3841
Some(i) => {
39-
if i >= self.messages.len() - 1 {
42+
if i >= self.subs.len() - 1 {
4043
0
4144
} else {
4245
i + 1
@@ -51,7 +54,7 @@ impl AppSub {
5154
let i = match self.state.selected() {
5255
Some(i) => {
5356
if i == 0 {
54-
self.messages.len() - 1
57+
self.subs.len() - 1
5558
} else {
5659
i - 1
5760
}
@@ -110,7 +113,7 @@ impl Default for AppSub {
110113
input: String::new(),
111114
settings_input: vec![String::new(), String::new()],
112115
input_mode: InputMode::Normal,
113-
messages: Vec::new(),
116+
subs: Vec::new(),
114117
state: ListState::default(),
115118
index_subscription: ListState::default(),
116119
index_settings: 0,

src/subscribe/render.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ pub(crate) fn ui<B: Backend>(f: &mut Frame<B>, app: &mut AppSub) {
8888
.constraints([Constraint::Percentage(40), Constraint::Percentage(60)].as_ref())
8989
.split(chunks[2]);
9090

91-
let messages: Vec<ListItem> = app
92-
.messages
91+
let subs: Vec<ListItem> = app
92+
.subs
9393
.iter()
9494
.enumerate()
9595
.map(|(i, m)| {
9696
let content = vec![Spans::from(Span::raw(format!("{}: {}", i, m)))];
9797
ListItem::new(content)
9898
})
9999
.collect();
100-
let messages = List::new(messages)
100+
let subs = List::new(subs)
101101
.block(Block::default().borders(Borders::ALL).title("List"))
102102
.highlight_style(
103103
Style::default()
@@ -106,7 +106,7 @@ pub(crate) fn ui<B: Backend>(f: &mut Frame<B>, app: &mut AppSub) {
106106
)
107107
.highlight_symbol(">> ");
108108
// popup wiget
109-
f.render_stateful_widget(messages, bottom_chunks[0], &mut app.state);
109+
f.render_stateful_widget(subs, bottom_chunks[0], &mut app.state);
110110
//let block : Box<dyn Widget> = {
111111
if let Some(a) = app.state.selected() {
112112
let list = app.informations[a].information_to_list();

src/subscribe/state.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ pub(crate) fn subscribe_state(app: &mut AppSub) -> io::Result<IFEXIT> {
3737
storge.push('[');
3838
storge.push('\n');
3939
if !list[0].is_empty() {
40-
//app.messages = list[0].clone();
40+
//app.subs = list[0].clone();
4141
app.stateoflist = true;
4242
app.state.select(Some(0));
4343
for alist in &list[0] {
4444
let information = spider::Information::new(alist.to_string());
4545
app.informations.push(information.clone());
4646
storge.push_str(information.get_the_json_node().as_str());
4747
}
48-
app.messages = app
48+
app.subs = app
4949
.informations
5050
.iter()
5151
.map(|ainformation| {
@@ -61,7 +61,7 @@ pub(crate) fn subscribe_state(app: &mut AppSub) -> io::Result<IFEXIT> {
6161
.unwrap_or_else(|err| panic!("err {}", err));
6262
}
6363

64-
//app.messages.push(app.input.drain(..).collect());
64+
//app.subs.push(app.input.drain(..).collect());
6565
}
6666
KeyCode::Char(c) => {
6767
app.input.push(c);
@@ -133,6 +133,22 @@ pub(crate) fn subscribe_state(app: &mut AppSub) -> io::Result<IFEXIT> {
133133
),
134134
)
135135
.unwrap_or_else(|err| panic!("{}", err));
136+
let mut subscribe_json: String = "[\n".to_string();
137+
for asub in &app.subscription {
138+
subscribe_json.push_str(&format!(
139+
"
140+
{{
141+
\"url\": \"{}\"
142+
}},\n",
143+
asub
144+
));
145+
}
146+
subscribe_json.pop();
147+
subscribe_json.pop();
148+
subscribe_json.push_str("\n]");
149+
utils::create_json_file(utils::Save::Subscribes, subscribe_json)
150+
.unwrap_or_else(|err| panic!("{}", err));
151+
// .collect();
136152
}
137153
_ => {}
138154
},
@@ -175,6 +191,16 @@ pub(crate) fn subscribe_state(app: &mut AppSub) -> io::Result<IFEXIT> {
175191
InputMode::SubscriptView => match key.code {
176192
KeyCode::Up => app.previous_sub(),
177193
KeyCode::Down => app.next_sub(),
194+
KeyCode::Char('d') => {
195+
app.subscription.remove(app.index_subscription.selected().unwrap());
196+
if app.subscription.is_empty() {
197+
app.unselect_sub();
198+
app.input_mode = InputMode::Popup;
199+
} else {
200+
app.index_subscription.select(Some(0));
201+
}
202+
//app.settings_input[app.index_settings].push(c);
203+
}
178204
KeyCode::Esc => {
179205
app.index_settings = 0;
180206
app.unselect_sub();

src/utils.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub enum Save {
1010
Storage,
1111
Running,
1212
V2ray,
13+
Subscribes,
1314
}
1415
fn create_storage_before() {
1516
let home = env::var("HOME").unwrap();
@@ -21,6 +22,7 @@ pub fn create_json_file(save: Save, input: String) -> Result<()> {
2122
Save::Storage => format!("{}/.config/tv2ray/storage.json", home),
2223
Save::Running => format!("{}/.config/tv2ray/running.json", home),
2324
Save::V2ray => format!("{}/.config/tv2ray/v2core.json", home),
25+
Save::Subscribes => format!("{}/.config/tv2ray/subscribes.json", home),
2426
};
2527
let path = Path::new(location.as_str());
2628
let mut file = File::create(&path)?;
@@ -34,6 +36,7 @@ fn get_json(save: Save) -> Result<String> {
3436
Save::Storage => format!("{}/.config/tv2ray/storage.json", home),
3537
Save::Running => format!("{}/.config/tv2ray/running.json", home),
3638
Save::V2ray => format!("{}/.config/tv2ray/v2core.json", home),
39+
Save::Subscribes => format!("{}/.config/tv2ray/subscribes.json", home),
3740
};
3841
let mut file = File::open(location)?;
3942
let mut output = String::new();
@@ -54,6 +57,28 @@ pub fn start_v2core() -> String {
5457
let message_pre = v["v2core"].to_string();
5558
crate::spider::remove_quotation(message_pre)
5659
}
60+
pub fn get_subs() -> Vec<String> {
61+
create_storage_before();
62+
let messages = match get_json(Save::Subscribes) {
63+
Ok(output) => output,
64+
Err(_) => {
65+
create_json_file(Save::Storage, "[]".to_string())
66+
.unwrap_or_else(|err| panic!("{}", err));
67+
"[]".to_string()
68+
}
69+
};
70+
let mut subscribes = Vec::new();
71+
let v: Value = serde_json::from_str(messages.as_str()).unwrap();
72+
let mut index = 0;
73+
while v[index] != Value::Null {
74+
let sub = v[index]["url"].to_string();
75+
let length = sub.len();
76+
let sub = (&sub[1..length - 1]).to_string();
77+
subscribes.push(sub);
78+
index += 1;
79+
}
80+
subscribes
81+
}
5782
pub fn start() -> Vec<Information> {
5883
create_storage_before();
5984
let messages = match get_json(Save::Storage) {
@@ -69,8 +94,8 @@ pub fn start() -> Vec<Information> {
6994
let mut index = 0;
7095
while v[index] != Value::Null {
7196
let the_url = v[index]["url"].to_string();
72-
let lenghth = the_url.len();
73-
let instore = &the_url[1..lenghth - 1];
97+
let length = the_url.len();
98+
let instore = &the_url[1..length - 1];
7499
informations.push(Information {
75100
urls: instore.to_string(),
76101
func: v[index]["func"].to_string(),

0 commit comments

Comments
 (0)