ratatui/examples/prototype.rs

255 lines
7.3 KiB
Rust
Raw Normal View History

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;
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-14 17:44:52 +00:00
use std::cmp::min;
use rand::distributions::{IndependentSample, Range};
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-14 17:44:52 +00:00
use tui::widgets::{Widget, Block, List, Gauge, Sparkline, Text, border, Chart};
2016-10-09 17:46:53 +00:00
use tui::layout::{Group, Direction, Alignment, Size};
2016-10-13 15:30:18 +00:00
use tui::style::Color;
2016-10-09 17:46:53 +00:00
2016-10-14 17:44:52 +00:00
#[derive(Clone)]
struct RandomSignal {
range: Range<u64>,
rng: rand::ThreadRng,
}
impl RandomSignal {
fn new(r: Range<u64>) -> RandomSignal {
RandomSignal {
range: r,
rng: rand::thread_rng(),
}
}
}
impl Iterator for RandomSignal {
type Item = u64;
fn next(&mut self) -> Option<u64> {
Some(self.range.ind_sample(&mut self.rng))
}
}
#[derive(Clone)]
struct SinSignal {
x: f64,
period: f64,
scale: f64,
}
impl SinSignal {
fn new(period: f64, scale: f64) -> SinSignal {
SinSignal {
x: 0.0,
period: period,
scale: scale,
}
}
}
impl Iterator for SinSignal {
type Item = f64;
fn next(&mut self) -> Option<f64> {
self.x += 1.0;
Some(((self.x * 1.0 / self.period).sin() + 1.0) * self.scale)
}
}
2016-10-09 17:46:53 +00:00
struct App {
name: String,
fetching: bool,
2016-10-11 17:54:35 +00:00
items: Vec<String>,
selected: usize,
2016-10-14 17:44:52 +00:00
show_chart: bool,
progress: u16,
2016-10-12 17:43:39 +00:00
data: Vec<u64>,
2016-10-14 17:44:52 +00:00
data2: Vec<u64>,
colors: [Color; 2],
color_index: usize,
2016-10-09 17:46:53 +00:00
}
enum Event {
2016-10-11 17:54:35 +00:00
Input(event::Key),
Tick,
2016-10-09 17:46:53 +00:00
}
fn main() {
2016-10-11 17:54:35 +00:00
let log = FileAppender::builder()
.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)))
.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-14 17:44:52 +00:00
let mut rand_signal = RandomSignal::new(Range::new(0, 100));
let mut sin_signal = SinSignal::new(4.0, 20.0);
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-14 17:44:52 +00:00
show_chart: true,
progress: 0,
2016-10-14 17:44:52 +00:00
data: rand_signal.clone().take(100).collect(),
data2: sin_signal.clone().take(100).map(|i| i as u64).collect(),
colors: [Color::Magenta, Color::Red],
color_index: 0,
2016-10-09 17:46:53 +00:00
};
let (tx, rx) = mpsc::channel();
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();
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
thread::spawn(move || {
let tx = tx.clone();
loop {
tx.send(Event::Tick).unwrap();
2016-10-14 17:44:52 +00:00
thread::sleep(time::Duration::from_millis(500));
}
});
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 {
terminal.clear();
2016-10-09 17:46:53 +00:00
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') => {
2016-10-14 17:44:52 +00:00
app.show_chart = !app.show_chart;
2016-10-12 09:36:39 +00:00
}
2016-10-11 17:54:35 +00:00
_ => {}
}
2016-10-09 17:46:53 +00:00
}
Event::Tick => {
app.progress += 5;
if app.progress > 100 {
app.progress = 0;
}
2016-10-14 17:44:52 +00:00
app.data.insert(0, rand_signal.next().unwrap());
2016-10-12 17:43:39 +00:00
app.data.pop();
2016-10-14 17:44:52 +00:00
app.data2.remove(0);
app.data2.push(sin_signal.next().unwrap() as u64);
app.selected += 1;
if app.selected >= app.items.len() {
app.selected = 0;
}
2016-10-14 17:44:52 +00:00
app.color_index += 1;
if app.color_index >= app.colors.len() {
app.color_index = 0;
}
}
2016-10-09 17:46:53 +00:00
}
}
terminal.show_cursor();
}
fn draw(t: &mut Terminal, app: &App) {
2016-10-09 17:46:53 +00:00
Group::default()
2016-10-09 17:46:53 +00:00
.direction(Direction::Vertical)
.alignment(Alignment::Left)
2016-10-13 15:30:18 +00:00
.chunks(&[Size::Fixed(7), Size::Min(5), Size::Fixed(3)])
.render(&Terminal::size().unwrap(), |chunks| {
Block::default().borders(border::ALL).title("Graphs").render(&chunks[0], t);
Group::default()
.direction(Direction::Vertical)
.alignment(Alignment::Left)
.margin(1)
2016-10-13 15:30:18 +00:00
.chunks(&[Size::Fixed(2), Size::Fixed(3)])
.render(&chunks[0], |chunks| {
Gauge::default()
.block(*Block::default().title("Gauge:"))
2016-10-13 15:30:18 +00:00
.bg(Color::Yellow)
.percent(app.progress)
.render(&chunks[0], t);
Sparkline::default()
.block(*Block::default().title("Sparkline:"))
.fg(Color::Green)
2016-10-12 17:43:39 +00:00
.data(&app.data)
.render(&chunks[1], t);
});
2016-10-14 17:44:52 +00:00
let sizes = if app.show_chart {
vec![Size::Max(40), Size::Min(20)]
2016-10-12 09:36:39 +00:00
} else {
2016-10-14 17:44:52 +00:00
vec![Size::Max(40)]
2016-10-12 09:36:39 +00:00
};
Group::default()
2016-10-11 17:54:35 +00:00
.direction(Direction::Horizontal)
.alignment(Alignment::Left)
2016-10-12 09:36:39 +00:00
.chunks(&sizes)
.render(&chunks[1], |chunks| {
List::default()
2016-10-14 17:44:52 +00:00
.block(*Block::default().borders(border::ALL).title("List"))
.render(&chunks[0], t);
2016-10-14 17:44:52 +00:00
if app.show_chart {
Chart::default()
2016-10-14 17:44:52 +00:00
.block(*Block::default()
.borders(border::ALL)
.title("Chart"))
.fg(Color::Cyan)
.axis([0, 40])
.data(&app.data2)
.render(&chunks[1], t);
2016-10-12 09:36:39 +00:00
}
});
Text::default()
2016-10-14 17:44:52 +00:00
.block(*Block::default().borders(border::ALL).title("Footer"))
.fg(app.colors[app.color_index])
.text("This żółw is a footer")
.render(&chunks[2], t);
2016-10-09 17:46:53 +00:00
});
}