cursive/examples/logs.rs

112 lines
3.2 KiB
Rust
Raw Permalink Normal View History

2015-05-22 23:28:05 +00:00
extern crate cursive;
use cursive::{Cursive, Printer};
use cursive::vec::Vec2;
use cursive::traits::*;
2015-05-22 23:28:05 +00:00
use std::sync::mpsc;
use std::thread;
2016-03-15 22:37:57 +00:00
use std::time::Duration;
2016-09-28 22:07:02 +00:00
use std::iter::Chain;
use std::slice::Iter;
2015-05-22 23:28:05 +00:00
fn main() {
// As usual, create the Cursive root
let mut siv = Cursive::new();
// We want to refresh the page even when no input is given.
siv.set_fps(10);
siv.add_global_callback('q', |s| s.quit());
2015-05-22 23:28:05 +00:00
// A channel will communicate data from our running task to the UI.
2016-06-26 00:10:18 +00:00
let (tx, rx) = mpsc::channel();
2015-05-22 23:28:05 +00:00
// Generate data in a separate thread.
2016-06-26 00:10:18 +00:00
thread::spawn(|| {
generate_logs(tx);
});
2015-05-22 23:28:05 +00:00
// And sets the view to read from the other end of the channel.
2016-08-05 03:03:48 +00:00
siv.add_layer(BufferView::new(200, rx).full_screen());
2015-05-22 23:28:05 +00:00
siv.run();
}
// We will only simulate log generation here.
// In real life, this may come from a running task, a separate process, ...
fn generate_logs(tx: mpsc::Sender<String>) {
let mut i = 1;
loop {
let line = format!("Interesting log line {}", i);
i += 1;
2015-05-23 00:12:39 +00:00
// The send will fail when the other side is dropped.
// (When the application ends).
2016-09-28 22:07:02 +00:00
if tx.send(line).is_err() {
return;
2015-05-22 23:28:05 +00:00
}
2016-03-15 22:37:57 +00:00
thread::sleep(Duration::from_millis(30));
2015-05-22 23:28:05 +00:00
}
}
// Let's define a buffer view, that shows the last lines from a stream.
struct BufferView {
// We will emulate a ring buffer
buffer: Vec<String>,
// Current position in the buffer
pos: usize,
// Receiving end of the stream
rx: mpsc::Receiver<String>,
}
impl BufferView {
// Creates a new view with the given buffer size
fn new(size: usize, rx: mpsc::Receiver<String>) -> Self {
BufferView {
rx: rx,
buffer: (0..size).map(|_| String::new()).collect(),
pos: 0,
}
}
// Reads available data from the stream into the buffer
fn update(&mut self) {
let mut i = self.pos;
while let Ok(line) = self.rx.try_recv() {
self.buffer[i] = line;
2016-06-26 00:10:18 +00:00
i = (i + 1) % self.buffer.len();
2015-05-22 23:28:05 +00:00
}
2015-05-22 23:57:29 +00:00
self.pos = i;
2015-05-22 23:28:05 +00:00
}
// Chain together the two parts of the buffer to appear as a circular one.
2016-09-28 22:07:02 +00:00
// TODO: Use `-> impl Iterator<Item=String>`...
fn ring(&self) -> Chain<Iter<String>, Iter<String>> {
2015-05-22 23:28:05 +00:00
// The main buffer is "circular" starting at self.pos
// So we chain the two parts as one
self.buffer[self.pos..].iter().chain(self.buffer[..self.pos].iter())
}
}
impl View for BufferView {
2016-07-16 18:20:40 +00:00
fn layout(&mut self, _: Vec2) {
2015-05-22 23:28:05 +00:00
// Before drawing, we'll want to update the buffer
self.update();
2016-07-16 18:20:40 +00:00
}
fn draw(&self, printer: &Printer) {
2015-05-22 23:28:05 +00:00
2016-09-28 22:07:02 +00:00
// If the buffer is large, we'll discard the beginning and keep the end.
2015-05-23 00:24:02 +00:00
// If the buffer is too small, only print a part of it with an offset.
2016-09-28 22:07:02 +00:00
let (discard, offset) = if self.buffer.len() >
printer.size.y as usize {
2015-05-23 00:24:02 +00:00
(self.buffer.len() - printer.size.y as usize, 0)
2015-05-22 23:28:05 +00:00
} else {
2015-05-26 23:48:27 +00:00
(0, printer.size.y - self.buffer.len())
2015-05-23 00:24:02 +00:00
};
2015-05-22 23:28:05 +00:00
2015-05-23 00:24:02 +00:00
for (i, line) in self.ring().skip(discard).enumerate() {
2016-06-26 00:10:18 +00:00
printer.print((0, offset + i), line);
2015-05-22 23:28:05 +00:00
}
}
}