Skip to content

Commit ea21b99

Browse files
author
cht
committed
统一一下文件夹内容
1 parent 2c0d09a commit ea21b99

7 files changed

Lines changed: 89 additions & 83 deletions

File tree

src/informations/appbar.rs

Lines changed: 13 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
use crossterm:: event::{self, Event, KeyCode};
1+
use crate::app::App;
22
use std::{
33
io,
44
time::{Duration, Instant},
55
};
6-
use tui::{
7-
backend::Backend,
8-
layout::{Constraint, Direction, Layout},
9-
style::{Color, Modifier, Style},
10-
widgets::{BarChart, Block, Borders},
11-
Frame, Terminal,
12-
};
13-
use crate::app::App;
14-
use crate::state::Page;
6+
use tui::{backend::Backend, Terminal};
157
pub struct AppBar<'a> {
16-
data: Vec<(&'a str, u64)>,
8+
pub(crate) data: Vec<(&'a str, u64)>,
179
last_tick: Instant,
1810
}
19-
11+
use super::render::ui;
12+
use super::state::information_state;
2013
impl<'a> AppBar<'a> {
2114
pub fn new() -> AppBar<'a> {
2215
AppBar {
@@ -46,7 +39,7 @@ impl<'a> AppBar<'a> {
4639
("B23", 3),
4740
("B24", 5),
4841
],
49-
last_tick:Instant::now(),
42+
last_tick: Instant::now(),
5043
}
5144
}
5245

@@ -56,75 +49,21 @@ impl<'a> AppBar<'a> {
5649
}
5750
}
5851
impl<'a> App for AppBar<'a> {
59-
fn run_app_local<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> io::Result<crate::state::IFEXIT> {
60-
terminal.draw(|f| ui(f, self))?;
52+
fn run_app_local<B: Backend>(
53+
&mut self,
54+
terminal: &mut Terminal<B>,
55+
) -> io::Result<crate::state::IFEXIT> {
56+
terminal.draw(|f| ui(f, self))?;
6157
let tick_rate = Duration::from_millis(250);
6258
//let mut last_tick = Instant::now();
6359
let timeout = tick_rate
6460
.checked_sub(self.last_tick.elapsed())
6561
.unwrap_or_else(|| Duration::from_secs(0));
66-
if crossterm::event::poll(timeout)? {
67-
if let Event::Key(key) = event::read()? {
68-
match key.code {
69-
KeyCode::Char('q') => return Ok(crate::state::IFEXIT::Exit),
70-
KeyCode::Char('t') => return Ok(crate::state::IFEXIT::Change(Page::SubScribe)),
71-
_ => {}
72-
}
73-
}
74-
}
62+
7563
if self.last_tick.elapsed() >= tick_rate {
7664
self.on_tick();
7765
self.last_tick = Instant::now();
7866
}
79-
Ok(crate::state::IFEXIT::Next)
80-
67+
information_state(timeout)
8168
}
8269
}
83-
84-
85-
fn ui<B: Backend>(f: &mut Frame<B>, app: &AppBar) {
86-
let chunks = Layout::default()
87-
.direction(Direction::Vertical)
88-
.margin(2)
89-
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
90-
.split(f.size());
91-
let barchart = BarChart::default()
92-
.block(Block::default().title("Data1").borders(Borders::ALL))
93-
.data(&app.data)
94-
.bar_width(9)
95-
.bar_style(Style::default().fg(Color::Yellow))
96-
.value_style(Style::default().fg(Color::Black).bg(Color::Yellow));
97-
f.render_widget(barchart, chunks[0]);
98-
99-
let chunks = Layout::default()
100-
.direction(Direction::Horizontal)
101-
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
102-
.split(chunks[1]);
103-
104-
let barchart = BarChart::default()
105-
.block(Block::default().title("Data2").borders(Borders::ALL))
106-
.data(&app.data)
107-
.bar_width(5)
108-
.bar_gap(3)
109-
.bar_style(Style::default().fg(Color::Green))
110-
.value_style(
111-
Style::default()
112-
.bg(Color::Green)
113-
.add_modifier(Modifier::BOLD),
114-
);
115-
f.render_widget(barchart, chunks[0]);
116-
117-
let barchart = BarChart::default()
118-
.block(Block::default().title("Data3").borders(Borders::ALL))
119-
.data(&app.data)
120-
.bar_style(Style::default().fg(Color::Red))
121-
.bar_width(7)
122-
.bar_gap(0)
123-
.value_style(Style::default().bg(Color::Red))
124-
.label_style(
125-
Style::default()
126-
.fg(Color::Cyan)
127-
.add_modifier(Modifier::ITALIC),
128-
);
129-
f.render_widget(barchart, chunks[1]);
130-
}

src/informations/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
pub mod appbar;
2+
mod render;
3+
mod state;

src/informations/render.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use super::appbar::AppBar;
2+
use tui::{
3+
backend::Backend,
4+
layout::{Constraint, Direction, Layout},
5+
style::{Color, Modifier, Style},
6+
widgets::{BarChart, Block, Borders},
7+
Frame,
8+
};
9+
pub fn ui<B: Backend>(f: &mut Frame<B>, app: &AppBar) {
10+
let chunks = Layout::default()
11+
.direction(Direction::Vertical)
12+
.margin(2)
13+
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
14+
.split(f.size());
15+
let barchart = BarChart::default()
16+
.block(Block::default().title("Data1").borders(Borders::ALL))
17+
.data(&app.data)
18+
.bar_width(9)
19+
.bar_style(Style::default().fg(Color::Yellow))
20+
.value_style(Style::default().fg(Color::Black).bg(Color::Yellow));
21+
f.render_widget(barchart, chunks[0]);
22+
23+
let chunks = Layout::default()
24+
.direction(Direction::Horizontal)
25+
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
26+
.split(chunks[1]);
27+
28+
let barchart = BarChart::default()
29+
.block(Block::default().title("Data2").borders(Borders::ALL))
30+
.data(&app.data)
31+
.bar_width(5)
32+
.bar_gap(3)
33+
.bar_style(Style::default().fg(Color::Green))
34+
.value_style(
35+
Style::default()
36+
.bg(Color::Green)
37+
.add_modifier(Modifier::BOLD),
38+
);
39+
f.render_widget(barchart, chunks[0]);
40+
41+
let barchart = BarChart::default()
42+
.block(Block::default().title("Data3").borders(Borders::ALL))
43+
.data(&app.data)
44+
.bar_style(Style::default().fg(Color::Red))
45+
.bar_width(7)
46+
.bar_gap(0)
47+
.value_style(Style::default().bg(Color::Red))
48+
.label_style(
49+
Style::default()
50+
.fg(Color::Cyan)
51+
.add_modifier(Modifier::ITALIC),
52+
);
53+
f.render_widget(barchart, chunks[1]);
54+
}

src/informations/state.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crossterm::event::{self, Event, KeyCode};
2+
use std::{io, time::Duration};
3+
4+
use crate::state::{Page, IFEXIT};
5+
pub fn information_state(timeout: Duration) -> io::Result<IFEXIT> {
6+
if crossterm::event::poll(timeout)? {
7+
if let Event::Key(key) = event::read()? {
8+
match key.code {
9+
KeyCode::Char('q') => return Ok(crate::state::IFEXIT::Exit),
10+
KeyCode::Char('t') => return Ok(crate::state::IFEXIT::Change(Page::SubScribe)),
11+
_ => return Ok(IFEXIT::Next),
12+
}
13+
}
14+
}
15+
Ok(IFEXIT::Next)
16+
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use crossterm::{
1717
use std::{error::Error, io};
1818
use tui::{backend::CrosstermBackend, Terminal};
1919
mod app;
20+
mod informations;
2021
mod spider;
2122
mod state;
2223
mod subscribe;
23-
mod informations;
2424
mod utils;
2525
//use app::*;
2626

src/state.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::subscribe::appsub::AppSub;
55
use crate::utils;
66
use std::io;
77
use tui::{backend::Backend, Terminal};
8-
#[allow(dead_code)]
8+
99
pub enum Page {
1010
SubScribe,
1111
Information,
@@ -43,12 +43,7 @@ pub fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
4343
IFEXIT::Exit => return Ok(()),
4444
IFEXIT::Change(e) => local_page = e,
4545
IFEXIT::Next => {}
46-
}
46+
},
4747
}
48-
// here ,need with different tab ,use different draw funcitons
49-
//terminal.draw(|f| render::ui(f, &mut app))?;
50-
//if let IFEXIT::Exit = appsub.run_app_local(terminal)? {
51-
// return Ok(());
52-
//}
5348
}
5449
}

src/subscribe/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::app::InputMode;
22
use super::appsub::AppSub;
33
use super::spider;
44
use super::utils;
5-
use super::{IFEXIT,Page};
5+
use super::{Page, IFEXIT};
66
use crossterm::event::{self, Event, KeyCode};
77
use std::{env, io, process::Command};
88
pub(crate) fn subscribe_state(app: &mut AppSub) -> io::Result<IFEXIT> {

0 commit comments

Comments
 (0)