Skip to content

Commit 44ee05e

Browse files
author
cht
committed
终于有点抽象的样子了,我哭死
1 parent 35592f9 commit 44ee05e

7 files changed

Lines changed: 149 additions & 136 deletions

File tree

src/app.rs

Lines changed: 5 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use crate::spider;
2-
use tui::widgets::ListState;
1+
use crate::state::IFEXIT;
2+
use std::io;
3+
use tui::{backend::Backend, Terminal};
34
pub enum InputMode {
45
Normal,
56
Editing,
@@ -8,104 +9,6 @@ pub enum InputMode {
89
PopupEdit,
910
SubscriptView,
1011
}
11-
12-
/// App holds the state of the application
13-
pub struct App {
14-
/// Current value of the input box
15-
pub input: String,
16-
pub settings_input: Vec<String>,
17-
/// Current input mode
18-
pub input_mode: InputMode,
19-
/// History of recorded messages
20-
pub messages: Vec<String>,
21-
pub state: ListState,
22-
pub index_subscription: ListState,
23-
pub index_settings: usize,
24-
pub stateoflist: bool,
25-
pub show_popup: bool,
26-
pub informations: Vec<spider::Information>,
27-
pub subscription: Vec<String>,
28-
}
29-
impl App {
30-
pub fn next(&mut self) {
31-
let i = match self.state.selected() {
32-
Some(i) => {
33-
if i >= self.messages.len() - 1 {
34-
0
35-
} else {
36-
i + 1
37-
}
38-
}
39-
None => 0,
40-
};
41-
self.state.select(Some(i));
42-
}
43-
44-
pub fn previous(&mut self) {
45-
let i = match self.state.selected() {
46-
Some(i) => {
47-
if i == 0 {
48-
self.messages.len() - 1
49-
} else {
50-
i - 1
51-
}
52-
}
53-
None => 0,
54-
};
55-
self.state.select(Some(i));
56-
}
57-
58-
pub fn unselect(&mut self) {
59-
self.state.select(None);
60-
}
61-
pub fn next_sub(&mut self) {
62-
let i = match self.index_subscription.selected() {
63-
Some(i) => {
64-
if i >= self.subscription.len() - 1 {
65-
0
66-
} else {
67-
i + 1
68-
}
69-
}
70-
None => 0,
71-
};
72-
self.index_subscription.select(Some(i));
73-
//self.index = Some(i);
74-
}
75-
76-
pub fn previous_sub(&mut self) {
77-
let i = match self.index_subscription.selected() {
78-
Some(i) => {
79-
if i == 0 {
80-
self.subscription.len() - 1
81-
} else {
82-
i - 1
83-
}
84-
}
85-
None => 0,
86-
};
87-
self.index_subscription.select(Some(i));
88-
//self.index = Some(i);
89-
}
90-
91-
pub fn unselect_sub(&mut self) {
92-
self.index_subscription.select(None);
93-
}
94-
}
95-
impl Default for App {
96-
fn default() -> App {
97-
App {
98-
input: String::new(),
99-
settings_input: vec![String::new(), String::new()],
100-
input_mode: InputMode::Normal,
101-
messages: Vec::new(),
102-
state: ListState::default(),
103-
index_subscription: ListState::default(),
104-
index_settings: 0,
105-
stateoflist: false,
106-
show_popup: false,
107-
informations: Vec::new(),
108-
subscription: Vec::new(),
109-
}
110-
}
12+
pub trait App {
13+
fn run_app_local<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> io::Result<IFEXIT>;
11114
}

src/main.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod spider;
2121
mod state;
2222
mod subscribe;
2323
mod utils;
24-
use app::*;
24+
//use app::*;
2525

2626
fn main() -> Result<(), Box<dyn Error>> {
2727
// setup terminal
@@ -31,20 +31,7 @@ fn main() -> Result<(), Box<dyn Error>> {
3131
let backend = CrosstermBackend::new(stdout);
3232
let mut terminal = Terminal::new(backend)?;
3333

34-
// create app and run it
35-
let mut app = App::default();
36-
let informations = utils::start();
37-
if !informations.is_empty() {
38-
app.messages = informations
39-
.iter()
40-
.map(|amessage| spider::remove_quotation(amessage.ps.clone()))
41-
.collect();
42-
app.stateoflist = true;
43-
app.state.select(Some(0));
44-
app.informations = informations;
45-
}
46-
app.settings_input[0] = utils::start_v2core();
47-
let res = state::run_app(&mut terminal, app);
34+
let res = state::run_app(&mut terminal);
4835

4936
// restore terminal
5037
disable_raw_mode()?;

src/state.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1-
use crate::app::*;
2-
use crate::subscribe::render;
1+
use crate::app::App;
2+
use crate::spider;
3+
use crate::subscribe::appsub::AppSub;
4+
use crate::utils;
35
use std::io;
46
use tui::{backend::Backend, Terminal};
57
pub enum IFEXIT {
68
Next,
79
Exit,
810
}
911
//计划将它设置成一个入口
10-
pub fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<()> {
12+
pub fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
13+
let mut app = AppSub::default();
14+
let informations = utils::start();
15+
if !informations.is_empty() {
16+
app.messages = informations
17+
.iter()
18+
.map(|amessage| spider::remove_quotation(amessage.ps.clone()))
19+
.collect();
20+
app.stateoflist = true;
21+
app.state.select(Some(0));
22+
app.informations = informations;
23+
}
24+
app.settings_input[0] = utils::start_v2core();
1125
loop {
1226
// here ,need with different tab ,use different draw funcitons
1327
//terminal.draw(|f| render::ui(f, &mut app))?;
14-
if let IFEXIT::Exit = render::run_app_subscribe(terminal, &mut app)? {
28+
if let IFEXIT::Exit = app.run_app_local(terminal)? {
1529
return Ok(());
1630
}
1731
}

src/subscribe/appsub.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
use super::app::*;
2+
use super::render::ui;
3+
use super::state::subscribe_state;
4+
use crate::spider;
5+
use crate::state::IFEXIT;
6+
use std::io;
7+
use tui::widgets::ListState;
8+
use tui::{backend::Backend, Terminal};
9+
/// App holds the state of the application
10+
pub struct AppSub {
11+
/// Current value of the input box
12+
pub input: String,
13+
pub settings_input: Vec<String>,
14+
/// Current input mode
15+
pub input_mode: InputMode,
16+
/// History of recorded messages
17+
pub messages: Vec<String>,
18+
pub state: ListState,
19+
pub index_subscription: ListState,
20+
pub index_settings: usize,
21+
pub stateoflist: bool,
22+
pub show_popup: bool,
23+
pub informations: Vec<spider::Information>,
24+
pub subscription: Vec<String>,
25+
}
26+
impl AppSub {
27+
pub fn next(&mut self) {
28+
let i = match self.state.selected() {
29+
Some(i) => {
30+
if i >= self.messages.len() - 1 {
31+
0
32+
} else {
33+
i + 1
34+
}
35+
}
36+
None => 0,
37+
};
38+
self.state.select(Some(i));
39+
}
40+
41+
pub fn previous(&mut self) {
42+
let i = match self.state.selected() {
43+
Some(i) => {
44+
if i == 0 {
45+
self.messages.len() - 1
46+
} else {
47+
i - 1
48+
}
49+
}
50+
None => 0,
51+
};
52+
self.state.select(Some(i));
53+
}
54+
55+
pub fn unselect(&mut self) {
56+
self.state.select(None);
57+
}
58+
pub fn next_sub(&mut self) {
59+
let i = match self.index_subscription.selected() {
60+
Some(i) => {
61+
if i >= self.subscription.len() - 1 {
62+
0
63+
} else {
64+
i + 1
65+
}
66+
}
67+
None => 0,
68+
};
69+
self.index_subscription.select(Some(i));
70+
//self.index = Some(i);
71+
}
72+
73+
pub fn previous_sub(&mut self) {
74+
let i = match self.index_subscription.selected() {
75+
Some(i) => {
76+
if i == 0 {
77+
self.subscription.len() - 1
78+
} else {
79+
i - 1
80+
}
81+
}
82+
None => 0,
83+
};
84+
self.index_subscription.select(Some(i));
85+
//self.index = Some(i);
86+
}
87+
88+
pub fn unselect_sub(&mut self) {
89+
self.index_subscription.select(None);
90+
}
91+
}
92+
impl App for AppSub {
93+
fn run_app_local<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> io::Result<IFEXIT> {
94+
terminal.draw(|f| ui(f, self))?;
95+
subscribe_state(self)
96+
}
97+
}
98+
impl Default for AppSub {
99+
fn default() -> AppSub {
100+
AppSub {
101+
input: String::new(),
102+
settings_input: vec![String::new(), String::new()],
103+
input_mode: InputMode::Normal,
104+
messages: Vec::new(),
105+
state: ListState::default(),
106+
index_subscription: ListState::default(),
107+
index_settings: 0,
108+
stateoflist: false,
109+
show_popup: false,
110+
informations: Vec::new(),
111+
subscription: Vec::new(),
112+
}
113+
}
114+
}

src/subscribe/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
pub mod render;
1+
pub mod appsub;
2+
mod render;
23
mod state;
34
use super::{app, spider, state::IFEXIT, utils};

src/subscribe/render.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
use super::app::*;
2-
use super::IFEXIT;
3-
use std::io;
1+
use super::app::InputMode;
2+
use super::appsub::AppSub;
43
use tui::{
54
backend::Backend,
65
layout::{Constraint, Direction, Layout, Rect},
76
style::{Color, Modifier, Style},
87
text::{Span, Spans, Text},
98
widgets::{Block, Borders, Clear, List, ListItem, Paragraph},
10-
Frame, Terminal,
9+
Frame,
1110
};
1211
use unicode_width::UnicodeWidthStr;
13-
pub fn run_app_subscribe<B: Backend>(
14-
terminal: &mut Terminal<B>,
15-
app: &mut App,
16-
) -> io::Result<IFEXIT> {
17-
terminal.draw(|f| ui(f, app))?;
18-
super::state::subscribe_state(app)
19-
}
20-
fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
12+
13+
pub(crate) fn ui<B: Backend>(f: &mut Frame<B>, app: &mut AppSub) {
2114
let chunks = Layout::default()
2215
.direction(Direction::Vertical)
2316
.margin(2)

src/subscribe/state.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use super::app::*;
1+
use super::app::InputMode;
2+
use super::appsub::AppSub;
23
use super::spider;
34
use super::utils;
45
use super::IFEXIT;
56
use crossterm::event::{self, Event, KeyCode};
67
use std::{env, io, process::Command};
7-
pub(crate) fn subscribe_state(app: &mut App) -> io::Result<IFEXIT> {
8+
pub(crate) fn subscribe_state(app: &mut AppSub) -> io::Result<IFEXIT> {
89
if let Event::Key(key) = event::read()? {
910
match app.input_mode {
1011
InputMode::Normal => match key.code {

0 commit comments

Comments
 (0)