Skip to content

Commit 309b35e

Browse files
author
cht
committed
尝试变异步代码,但是好像不是异步,但是好歹是在tokio里跑了
1 parent 36d58ce commit 309b35e

8 files changed

Lines changed: 22 additions & 15 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ description = "a v2ray tui"
1010
[dependencies]
1111
tui = {version = "0.16.0", features = ["crossterm"] }
1212
crossterm = "0.20"
13-
reqwest = { version = "0.11", default-features = false, features = ["blocking", "json","rustls-tls"] }
13+
reqwest = { version = "0.11", default-features = false, features = ["json","rustls-tls"] }
1414
base64 = "0.13.0"
1515
serde_json = "1.0"
1616
urlencoding = "2.1.0"
1717
unicode-width = "0.1"
18+
tokio = { version = "1", features = ["full"] }
19+
async-trait = "0.1.51"

src/app.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::state::{MyBackend, IFEXIT};
22
use std::io;
33
use tui::Terminal;
4-
4+
use async_trait::async_trait;
5+
#[async_trait]
56
pub trait App {
6-
fn run_app_local(&mut self, terminal: &mut Terminal<MyBackend>) -> io::Result<IFEXIT>;
7+
async fn run_app_local(&mut self, terminal: &mut Terminal<MyBackend>) -> io::Result<IFEXIT>;
78
}

src/informations/appbar.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ impl<'a> AppBar<'a> {
4949
self.data.insert(0, value);
5050
}
5151
}
52+
use async_trait::async_trait;
53+
#[async_trait]
5254
impl<'a> App for AppBar<'a> {
53-
fn run_app_local(
55+
async fn run_app_local(
5456
&mut self,
5557
terminal: &mut Terminal<MyBackend>,
5658
) -> io::Result<crate::state::IFEXIT> {

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ mod state;
1212
mod subscribe;
1313
mod utils;
1414
//use app::*;
15-
16-
fn main() -> Result<(), Box<dyn Error>> {
15+
#[tokio::main]
16+
async fn main() -> Result<(), Box<dyn Error>> {
1717
// setup terminal
1818
enable_raw_mode()?;
1919
let mut stdout = io::stdout();
2020
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
2121
let backend = CrosstermBackend::new(stdout);
2222
let mut terminal = Terminal::new(backend)?;
2323

24-
let res = state::run_app(&mut terminal);
24+
let res = state::run_app(&mut terminal).await;
2525

2626
// restore terminal
2727
disable_raw_mode()?;

src/spider.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ fn ascii_to_string2(code: Vec<u8>) -> String {
3131
}
3232
test
3333
}
34-
pub fn get_the_key(paths: Vec<String>) -> Result<Vec<Vec<String>>> {
34+
pub async fn get_the_key(paths: Vec<String>) -> Result<Vec<Vec<String>>> {
3535
let mut output: Vec<Vec<String>> = vec![];
3636
for apath in paths {
37-
let temp = reqwest::blocking::get(apath)?.bytes()?.to_vec();
37+
let temp = reqwest::get(apath).await?.bytes().await?.to_vec();
3838
let code = base64::decode(temp);
3939
match code {
4040
Ok(input) => {

src/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub enum IFEXIT {
2222

2323
pub(crate) type MyBackend = CrosstermBackend<Stdout>;
2424
//计划将它设置成一个入口
25-
pub fn run_app(terminal: &mut Terminal<MyBackend>) -> io::Result<()> {
25+
pub async fn run_app(terminal: &mut Terminal<MyBackend>) -> io::Result<()> {
2626
let mut appsub = AppSub::default();
2727
let informations = utils::start();
2828
if !informations.is_empty() {
@@ -46,7 +46,7 @@ pub fn run_app(terminal: &mut Terminal<MyBackend>) -> io::Result<()> {
4646
let mut pages: Vec<Box<dyn App>> = vec![Box::new(appsub), Box::new(appbar)];
4747

4848
loop {
49-
match pages[local_page as usize].run_app_local(terminal)? {
49+
match pages[local_page as usize].run_app_local(terminal).await? {
5050
IFEXIT::Exit => return Ok(()),
5151
IFEXIT::Change(e) => local_page = e,
5252
IFEXIT::Next => {}

src/subscribe/appsub.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,12 @@ impl AppSub {
115115
self.index_subscription.select(None);
116116
}
117117
}
118+
use async_trait::async_trait;
119+
#[async_trait]
118120
impl App for AppSub {
119-
fn run_app_local(&mut self, terminal: &mut Terminal<MyBackend>) -> io::Result<IFEXIT> {
121+
async fn run_app_local(&mut self, terminal: &mut Terminal<MyBackend>) -> io::Result<IFEXIT> {
120122
terminal.draw(|f| ui(f, self))?;
121-
subscribe_state(self)
123+
subscribe_state(self).await
122124
}
123125
}
124126
impl Default for AppSub {

src/subscribe/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{Page, IFEXIT};
55
use crossterm::event::{self, Event, KeyCode};
66
use std::{env, io, process::Command};
77
use tui::widgets::ListState;
8-
pub(crate) fn subscribe_state(app: &mut AppSub) -> io::Result<IFEXIT> {
8+
pub(crate) async fn subscribe_state(app: &mut AppSub) -> io::Result<IFEXIT> {
99
if let Event::Key(key) = event::read()? {
1010
match app.input_mode {
1111
InputMode::Normal => match key.code {
@@ -121,7 +121,7 @@ pub(crate) fn subscribe_state(app: &mut AppSub) -> io::Result<IFEXIT> {
121121
utils::create_json_file(utils::Save::Subscribes, subscribe_json)
122122
.unwrap_or_else(|err| panic!("{}", err));
123123
// .collect();
124-
let get_list = spider::get_the_key(app.subscription.clone());
124+
let get_list = spider::get_the_key(app.subscription.clone()).await;
125125
if let Ok(list) = get_list {
126126
if !list.is_empty(){
127127
let mut storge: String = "[\n\n".to_string();

0 commit comments

Comments
 (0)