cursive/examples/logs.rs

94 lines
2.7 KiB
Rust
Raw Normal View History

use cursive::traits::*;
use cursive::vec::Vec2;
2018-06-11 06:29:10 +00:00
use cursive::{Cursive, Printer};
use std::collections::VecDeque;
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;
2015-05-22 23:28:05 +00:00
2018-01-16 02:55:27 +00:00
// This example will print a stream of logs generated from a separate thread.
//
// We will use a custom view using a channel to receive data asynchronously.
2015-05-22 23:28:05 +00:00
fn main() {
// As usual, create the Cursive root
let mut siv = Cursive::default();
2015-05-22 23:28:05 +00:00
2019-02-28 01:07:55 +00:00
let cb_sink = siv.cb_sink().clone();
2015-05-22 23:28:05 +00:00
// We want to refresh the page even when no input is given.
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.
2017-10-12 23:38:55 +00:00
thread::spawn(move || {
2019-02-28 01:07:55 +00:00
generate_logs(&tx, cb_sink);
2017-10-12 23:38:55 +00:00
});
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, ...
2019-02-28 01:07:55 +00:00
fn generate_logs(tx: &mpsc::Sender<String>, cb_sink: cursive::CbSink) {
2015-05-22 23:28:05 +00:00
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
}
2019-02-28 01:07:55 +00:00
cb_sink.send(Box::new(Cursive::noop)).unwrap();
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'll use a ring buffer
buffer: VecDeque<String>,
2015-05-22 23:28:05 +00:00
// 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 {
let mut buffer = VecDeque::new();
buffer.resize(size, String::new());
BufferView { rx, buffer }
2015-05-22 23:28:05 +00:00
}
// Reads available data from the stream into the buffer
fn update(&mut self) {
// Add each available line to the end of the buffer.
2015-05-22 23:28:05 +00:00
while let Ok(line) = self.rx.try_recv() {
self.buffer.push_back(line);
self.buffer.pop_front();
2015-05-22 23:28:05 +00:00
}
}
}
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) {
// Print the end of the buffer
2018-06-16 20:23:09 +00:00
for (i, line) in
self.buffer.iter().rev().take(printer.size.y).enumerate()
2017-10-12 23:38:55 +00:00
{
printer.print((0, printer.size.y - 1 - i), line);
2015-05-22 23:28:05 +00:00
}
}
}