Skip to content

Commit 2c0d09a

Browse files
author
cht
committed
add a information page
1 parent 44ee05e commit 2c0d09a

6 files changed

Lines changed: 168 additions & 11 deletions

File tree

src/informations/appbar.rs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
use crossterm:: event::{self, Event, KeyCode};
2+
use std::{
3+
io,
4+
time::{Duration, Instant},
5+
};
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;
15+
pub struct AppBar<'a> {
16+
data: Vec<(&'a str, u64)>,
17+
last_tick: Instant,
18+
}
19+
20+
impl<'a> AppBar<'a> {
21+
pub fn new() -> AppBar<'a> {
22+
AppBar {
23+
data: vec![
24+
("B1", 9),
25+
("B2", 12),
26+
("B3", 5),
27+
("B4", 8),
28+
("B5", 2),
29+
("B6", 4),
30+
("B7", 5),
31+
("B8", 9),
32+
("B9", 14),
33+
("B10", 15),
34+
("B11", 1),
35+
("B12", 0),
36+
("B13", 4),
37+
("B14", 6),
38+
("B15", 4),
39+
("B16", 6),
40+
("B17", 4),
41+
("B18", 7),
42+
("B19", 13),
43+
("B20", 8),
44+
("B21", 11),
45+
("B22", 9),
46+
("B23", 3),
47+
("B24", 5),
48+
],
49+
last_tick:Instant::now(),
50+
}
51+
}
52+
53+
fn on_tick(&mut self) {
54+
let value = self.data.pop().unwrap();
55+
self.data.insert(0, value);
56+
}
57+
}
58+
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))?;
61+
let tick_rate = Duration::from_millis(250);
62+
//let mut last_tick = Instant::now();
63+
let timeout = tick_rate
64+
.checked_sub(self.last_tick.elapsed())
65+
.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+
}
75+
if self.last_tick.elapsed() >= tick_rate {
76+
self.on_tick();
77+
self.last_tick = Instant::now();
78+
}
79+
Ok(crate::state::IFEXIT::Next)
80+
81+
}
82+
}
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod appbar;

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod app;
2020
mod spider;
2121
mod state;
2222
mod subscribe;
23+
mod informations;
2324
mod utils;
2425
//use app::*;
2526

src/state.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,54 @@
11
use crate::app::App;
2+
use crate::informations::appbar::AppBar;
23
use crate::spider;
34
use crate::subscribe::appsub::AppSub;
45
use crate::utils;
56
use std::io;
67
use tui::{backend::Backend, Terminal};
8+
#[allow(dead_code)]
9+
pub enum Page {
10+
SubScribe,
11+
Information,
12+
}
13+
// 这个做成tab的选择入口
714
pub enum IFEXIT {
815
Next,
916
Exit,
17+
Change(Page),
1018
}
1119
//计划将它设置成一个入口
1220
pub fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
13-
let mut app = AppSub::default();
21+
let mut appsub = AppSub::default();
1422
let informations = utils::start();
1523
if !informations.is_empty() {
16-
app.messages = informations
24+
appsub.messages = informations
1725
.iter()
1826
.map(|amessage| spider::remove_quotation(amessage.ps.clone()))
1927
.collect();
20-
app.stateoflist = true;
21-
app.state.select(Some(0));
22-
app.informations = informations;
28+
appsub.stateoflist = true;
29+
appsub.state.select(Some(0));
30+
appsub.informations = informations;
2331
}
24-
app.settings_input[0] = utils::start_v2core();
32+
let mut appbar = AppBar::new();
33+
appsub.settings_input[0] = utils::start_v2core();
34+
let mut local_page = Page::SubScribe;
2535
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+
}
47+
}
2648
// here ,need with different tab ,use different draw funcitons
2749
//terminal.draw(|f| render::ui(f, &mut app))?;
28-
if let IFEXIT::Exit = app.run_app_local(terminal)? {
29-
return Ok(());
30-
}
50+
//if let IFEXIT::Exit = appsub.run_app_local(terminal)? {
51+
// return Ok(());
52+
//}
3153
}
3254
}

src/subscribe/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
pub mod appsub;
22
mod render;
33
mod state;
4-
use super::{app, spider, state::IFEXIT, utils};
4+
use super::{app, spider, state::*, utils};

src/subscribe/state.rs

Lines changed: 4 additions & 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;
5+
use super::{IFEXIT,Page};
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> {
@@ -23,6 +23,9 @@ pub(crate) fn subscribe_state(app: &mut AppSub) -> io::Result<IFEXIT> {
2323
app.show_popup = true;
2424
app.input_mode = InputMode::Popup;
2525
}
26+
KeyCode::Char('t') => {
27+
return Ok(IFEXIT::Change(Page::Information));
28+
}
2629

2730
_ => {}
2831
},

0 commit comments

Comments
 (0)