2016-10-09 17:46:53 +00:00
|
|
|
extern crate tui;
|
2016-10-11 17:54:35 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
extern crate log4rs;
|
2016-10-09 17:46:53 +00:00
|
|
|
extern crate termion;
|
2016-10-12 17:43:39 +00:00
|
|
|
extern crate rand;
|
2016-10-09 17:46:53 +00:00
|
|
|
|
|
|
|
use std::thread;
|
2016-10-12 14:12:42 +00:00
|
|
|
use std::time;
|
2016-10-09 17:46:53 +00:00
|
|
|
use std::sync::mpsc;
|
2016-10-12 17:43:39 +00:00
|
|
|
use std::io::stdin;
|
2016-10-09 17:46:53 +00:00
|
|
|
|
|
|
|
use termion::event;
|
|
|
|
use termion::input::TermRead;
|
|
|
|
|
2016-10-11 17:54:35 +00:00
|
|
|
use log::LogLevelFilter;
|
|
|
|
use log4rs::append::file::FileAppender;
|
|
|
|
use log4rs::encode::pattern::PatternEncoder;
|
|
|
|
use log4rs::config::{Appender, Config, Logger, Root};
|
|
|
|
|
2016-10-09 17:46:53 +00:00
|
|
|
use tui::Terminal;
|
2016-10-12 17:43:39 +00:00
|
|
|
use tui::widgets::{Widget, Block, List, Gauge, Sparkline, border};
|
2016-10-09 17:46:53 +00:00
|
|
|
use tui::layout::{Group, Direction, Alignment, Size};
|
|
|
|
|
|
|
|
struct App {
|
|
|
|
name: String,
|
|
|
|
fetching: bool,
|
2016-10-11 17:54:35 +00:00
|
|
|
items: Vec<String>,
|
|
|
|
selected: usize,
|
2016-10-12 09:36:39 +00:00
|
|
|
show_episodes: bool,
|
2016-10-12 14:12:42 +00:00
|
|
|
progress: u16,
|
2016-10-12 17:43:39 +00:00
|
|
|
data: Vec<u64>,
|
2016-10-09 17:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Event {
|
2016-10-11 17:54:35 +00:00
|
|
|
Input(event::Key),
|
2016-10-12 14:12:42 +00:00
|
|
|
Tick,
|
2016-10-09 17:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
2016-10-11 17:54:35 +00:00
|
|
|
let log = FileAppender::builder()
|
2016-10-12 14:12:42 +00:00
|
|
|
.encoder(Box::new(PatternEncoder::new("{l} / {d(%H:%M:%S)} / {M}:{L}{n}{m}{n}{n}")))
|
2016-10-11 17:54:35 +00:00
|
|
|
.build("prototype.log")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let config = Config::builder()
|
|
|
|
.appender(Appender::builder().build("log", Box::new(log)))
|
2016-10-12 14:12:42 +00:00
|
|
|
.build(Root::builder().appender("log").build(LogLevelFilter::Debug))
|
2016-10-11 17:54:35 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let handle = log4rs::init_config(config).unwrap();
|
|
|
|
info!("Start");
|
|
|
|
|
2016-10-09 17:46:53 +00:00
|
|
|
let mut app = App {
|
|
|
|
name: String::from("Test app"),
|
|
|
|
fetching: false,
|
2016-10-11 17:54:35 +00:00
|
|
|
items: ["1", "2", "3"].into_iter().map(|e| String::from(*e)).collect(),
|
|
|
|
selected: 0,
|
2016-10-12 09:36:39 +00:00
|
|
|
show_episodes: false,
|
2016-10-12 14:12:42 +00:00
|
|
|
progress: 0,
|
2016-10-12 17:43:39 +00:00
|
|
|
data: (0..100).map(|_| rand::random::<u8>() as u64).collect(),
|
2016-10-09 17:46:53 +00:00
|
|
|
};
|
|
|
|
let (tx, rx) = mpsc::channel();
|
2016-10-12 14:12:42 +00:00
|
|
|
let input_tx = tx.clone();
|
2016-10-09 17:46:53 +00:00
|
|
|
|
|
|
|
thread::spawn(move || {
|
|
|
|
let stdin = stdin();
|
|
|
|
for c in stdin.keys() {
|
|
|
|
let evt = c.unwrap();
|
2016-10-12 14:12:42 +00:00
|
|
|
input_tx.send(Event::Input(evt)).unwrap();
|
2016-10-11 17:54:35 +00:00
|
|
|
if evt == event::Key::Char('q') {
|
|
|
|
break;
|
2016-10-09 17:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-10-11 17:54:35 +00:00
|
|
|
|
2016-10-12 14:12:42 +00:00
|
|
|
thread::spawn(move || {
|
|
|
|
let tx = tx.clone();
|
|
|
|
loop {
|
|
|
|
tx.send(Event::Tick).unwrap();
|
|
|
|
thread::sleep(time::Duration::from_millis(1000));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-09 17:46:53 +00:00
|
|
|
let mut terminal = Terminal::new().unwrap();
|
|
|
|
terminal.clear();
|
|
|
|
terminal.hide_cursor();
|
2016-10-11 17:54:35 +00:00
|
|
|
|
2016-10-09 17:46:53 +00:00
|
|
|
loop {
|
|
|
|
draw(&mut terminal, &app);
|
|
|
|
let evt = rx.recv().unwrap();
|
|
|
|
match evt {
|
2016-10-11 17:54:35 +00:00
|
|
|
Event::Input(input) => {
|
|
|
|
match input {
|
|
|
|
event::Key::Char('q') => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
event::Key::Up => {
|
|
|
|
if app.selected > 0 {
|
|
|
|
app.selected -= 1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
event::Key::Down => {
|
|
|
|
if app.selected < app.items.len() - 1 {
|
|
|
|
app.selected += 1;
|
|
|
|
}
|
|
|
|
}
|
2016-10-12 09:36:39 +00:00
|
|
|
event::Key::Char('t') => {
|
|
|
|
app.show_episodes = !app.show_episodes;
|
|
|
|
}
|
2016-10-11 17:54:35 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2016-10-09 17:46:53 +00:00
|
|
|
}
|
2016-10-12 14:12:42 +00:00
|
|
|
Event::Tick => {
|
|
|
|
app.progress += 5;
|
|
|
|
if app.progress > 100 {
|
|
|
|
app.progress = 0;
|
|
|
|
}
|
2016-10-12 17:43:39 +00:00
|
|
|
app.data.insert(0, rand::random::<u8>() as u64);
|
|
|
|
app.data.pop();
|
2016-10-12 14:12:42 +00:00
|
|
|
}
|
2016-10-09 17:46:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
terminal.show_cursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(terminal: &mut Terminal, app: &App) {
|
|
|
|
|
|
|
|
let ui = Group::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.alignment(Alignment::Left)
|
2016-10-12 14:12:42 +00:00
|
|
|
.chunks(&[Size::Fixed(5), Size::Percent(80), Size::Fixed(10)])
|
2016-10-11 17:54:35 +00:00
|
|
|
.render(&terminal.area(), |chunks, tree| {
|
2016-10-12 17:43:39 +00:00
|
|
|
info!("{:?}", terminal.area());
|
2016-10-12 14:12:42 +00:00
|
|
|
tree.add(Block::default().borders(border::ALL).title("Gauges").render(&chunks[0]));
|
|
|
|
tree.add(Group::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.alignment(Alignment::Left)
|
|
|
|
.margin(1)
|
|
|
|
.chunks(&[Size::Fixed(1), Size::Fixed(1), Size::Fixed(1)])
|
|
|
|
.render(&chunks[0], |chunks, tree| {
|
|
|
|
tree.add(Gauge::new()
|
|
|
|
.percent(app.progress)
|
|
|
|
.render(&chunks[0]));
|
2016-10-12 17:43:39 +00:00
|
|
|
tree.add(Sparkline::new()
|
|
|
|
.data(&app.data)
|
2016-10-12 14:12:42 +00:00
|
|
|
.render(&chunks[2]));
|
|
|
|
}));
|
2016-10-12 09:36:39 +00:00
|
|
|
let sizes = if app.show_episodes {
|
|
|
|
vec![Size::Percent(50), Size::Percent(50)]
|
|
|
|
} else {
|
2016-10-12 17:43:39 +00:00
|
|
|
vec![Size::Percent(100)]
|
2016-10-12 09:36:39 +00:00
|
|
|
};
|
2016-10-11 17:54:35 +00:00
|
|
|
tree.add(Group::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.alignment(Alignment::Left)
|
2016-10-12 09:36:39 +00:00
|
|
|
.chunks(&sizes)
|
2016-10-11 17:54:35 +00:00
|
|
|
.render(&chunks[1], |chunks, tree| {
|
|
|
|
tree.add(List::default()
|
2016-10-12 17:43:39 +00:00
|
|
|
.block(*Block::default().borders(border::ALL).title("Podcasts"))
|
2016-10-11 17:54:35 +00:00
|
|
|
.items(&app.items)
|
|
|
|
.select(app.selected)
|
|
|
|
.formatter(|i, s| {
|
|
|
|
let prefix = if s { ">" } else { "*" };
|
|
|
|
format!("{} {}", prefix, i)
|
|
|
|
})
|
|
|
|
.render(&chunks[0]));
|
2016-10-12 09:36:39 +00:00
|
|
|
if app.show_episodes {
|
|
|
|
tree.add(Block::default()
|
2016-10-12 14:12:42 +00:00
|
|
|
.borders(border::ALL)
|
2016-10-12 09:36:39 +00:00
|
|
|
.title("Episodes")
|
|
|
|
.render(&chunks[1]));
|
|
|
|
}
|
2016-10-11 17:54:35 +00:00
|
|
|
}));
|
2016-10-12 14:12:42 +00:00
|
|
|
tree.add(Block::default().borders(border::ALL).title("Footer").render(&chunks[2]));
|
2016-10-09 17:46:53 +00:00
|
|
|
});
|
2016-10-11 17:54:35 +00:00
|
|
|
terminal.render(ui);
|
2016-10-09 17:46:53 +00:00
|
|
|
}
|