Skip to content

Commit 28bb377

Browse files
author
cht
committed
use dyn
1 parent 8e508c5 commit 28bb377

6 files changed

Lines changed: 27 additions & 37 deletions

File tree

src/app.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::state::IFEXIT;
1+
use crate::state::{MyBackend, IFEXIT};
22
use std::io;
3-
use tui::{backend::Backend, Terminal};
3+
use tui::Terminal;
44

55
pub trait App {
6-
fn run_app_local<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> io::Result<IFEXIT>;
6+
fn run_app_local(&mut self, terminal: &mut Terminal<MyBackend>) -> io::Result<IFEXIT>;
77
}

src/informations/appbar.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::app::App;
2+
use crate::state::MyBackend;
23
use std::{
34
io,
45
time::{Duration, Instant},
56
};
6-
use tui::{backend::Backend, Terminal};
7+
use tui::Terminal;
78
pub struct AppBar<'a> {
89
pub(crate) data: Vec<(&'a str, u64)>,
910
last_tick: Instant,
@@ -49,9 +50,9 @@ impl<'a> AppBar<'a> {
4950
}
5051
}
5152
impl<'a> App for AppBar<'a> {
52-
fn run_app_local<B: Backend>(
53+
fn run_app_local(
5354
&mut self,
54-
terminal: &mut Terminal<B>,
55+
terminal: &mut Terminal<MyBackend>,
5556
) -> io::Result<crate::state::IFEXIT> {
5657
terminal.draw(|f| ui(f, self))?;
5758
let tick_rate = Duration::from_millis(250);

src/main.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
/// A simple example demonstrating how to handle user input. This is
2-
/// a bit out of the scope of the library as it does not provide any
3-
/// input handling out of the box. However, it may helps some to get
4-
/// started.
5-
///
6-
/// This is a very simple example:
7-
/// * A input box always focused. Every character you type is registered
8-
/// here
9-
/// * Pressing Backspace erases a character
10-
/// * Pressing Enter pushes the current input in the history of previous
11-
/// messages
121
use crossterm::{
132
event::{DisableMouseCapture, EnableMouseCapture},
143
execute,

src/state.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@ use crate::spider;
44
use crate::subscribe::appsub::AppSub;
55
use crate::utils;
66
use std::io;
7-
use tui::{backend::Backend, Terminal};
8-
7+
use std::io::Stdout;
8+
use tui::backend::CrosstermBackend;
9+
use tui::Terminal;
10+
#[derive(Clone, Copy)]
911
pub enum Page {
10-
SubScribe,
11-
Information,
12+
SubScribe = 0,
13+
Information = 1,
1214
}
1315
// 这个做成tab的选择入口
1416
pub enum IFEXIT {
1517
Next,
1618
Exit,
1719
Change(Page),
1820
}
21+
22+
pub type MyBackend = CrosstermBackend<Stdout>;
1923
//计划将它设置成一个入口
20-
pub fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
24+
pub fn run_app(terminal: &mut Terminal<MyBackend>) -> io::Result<()> {
2125
let mut appsub = AppSub::default();
2226
let informations = utils::start();
2327
if !informations.is_empty() {
@@ -29,21 +33,16 @@ pub fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
2933
appsub.state.select(Some(0));
3034
appsub.informations = informations;
3135
}
32-
let mut appbar = AppBar::new();
36+
let appbar = AppBar::new();
3337
appsub.settings_input[0] = utils::start_v2core();
3438
let mut local_page = Page::SubScribe;
39+
let mut pages: Vec<Box<dyn App>> = vec![Box::new(appsub), Box::new(appbar)];
40+
3541
loop {
36-
match local_page {
37-
Page::SubScribe => match appsub.run_app_local(terminal)? {
38-
IFEXIT::Exit => return Ok(()),
39-
IFEXIT::Change(e) => local_page = e,
40-
IFEXIT::Next => {}
41-
},
42-
Page::Information => match appbar.run_app_local(terminal)? {
43-
IFEXIT::Exit => return Ok(()),
44-
IFEXIT::Change(e) => local_page = e,
45-
IFEXIT::Next => {}
46-
},
42+
match pages[local_page as usize].run_app_local(terminal)? {
43+
IFEXIT::Exit => return Ok(()),
44+
IFEXIT::Change(e) => local_page = e,
45+
IFEXIT::Next => {}
4746
}
4847
}
4948
}

src/subscribe/appsub.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use super::app::*;
22
use super::render::ui;
33
use super::state::subscribe_state;
44
use crate::spider;
5+
use crate::state::MyBackend;
56
use crate::state::IFEXIT;
67
use std::io;
78
use tui::widgets::ListState;
8-
use tui::{backend::Backend, Terminal};
9+
use tui::Terminal;
910
pub(crate) enum InputMode {
1011
Normal,
1112
Editing,
@@ -98,7 +99,7 @@ impl AppSub {
9899
}
99100
}
100101
impl App for AppSub {
101-
fn run_app_local<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> io::Result<IFEXIT> {
102+
fn run_app_local(&mut self, terminal: &mut Terminal<MyBackend>) -> io::Result<IFEXIT> {
102103
terminal.draw(|f| ui(f, self))?;
103104
subscribe_state(self)
104105
}

src/subscribe/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::appsub::InputMode;
21
use super::appsub::AppSub;
2+
use super::appsub::InputMode;
33
use tui::{
44
backend::Backend,
55
layout::{Constraint, Direction, Layout, Rect},

0 commit comments

Comments
 (0)