mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Update examples/logs.rs to use VecDeque
Instead of custom ring-buffer
This commit is contained in:
parent
d4f1439abd
commit
381e2d9d73
@ -1,14 +1,15 @@
|
|||||||
extern crate cursive;
|
extern crate cursive;
|
||||||
|
|
||||||
use cursive::{Cursive, Printer};
|
use cursive::{Cursive, Printer};
|
||||||
use cursive::vec::Vec2;
|
|
||||||
use cursive::traits::*;
|
use cursive::traits::*;
|
||||||
|
use cursive::vec::Vec2;
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
use std::iter::Chain;
|
||||||
|
use std::slice::Iter;
|
||||||
|
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::iter::Chain;
|
|
||||||
use std::slice::Iter;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// As usual, create the Cursive root
|
// As usual, create the Cursive root
|
||||||
@ -22,9 +23,7 @@ fn main() {
|
|||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
// Generate data in a separate thread.
|
// Generate data in a separate thread.
|
||||||
thread::spawn(move || {
|
thread::spawn(move || { generate_logs(&tx); });
|
||||||
generate_logs(&tx);
|
|
||||||
});
|
|
||||||
|
|
||||||
// And sets the view to read from the other end of the channel.
|
// And sets the view to read from the other end of the channel.
|
||||||
siv.add_layer(BufferView::new(200, rx).full_screen());
|
siv.add_layer(BufferView::new(200, rx).full_screen());
|
||||||
@ -50,10 +49,8 @@ fn generate_logs(tx: &mpsc::Sender<String>) {
|
|||||||
|
|
||||||
// Let's define a buffer view, that shows the last lines from a stream.
|
// Let's define a buffer view, that shows the last lines from a stream.
|
||||||
struct BufferView {
|
struct BufferView {
|
||||||
// We will emulate a ring buffer
|
// We'll use a ring buffer
|
||||||
buffer: Vec<String>,
|
buffer: VecDeque<String>,
|
||||||
// Current position in the buffer
|
|
||||||
pos: usize,
|
|
||||||
// Receiving end of the stream
|
// Receiving end of the stream
|
||||||
rx: mpsc::Receiver<String>,
|
rx: mpsc::Receiver<String>,
|
||||||
}
|
}
|
||||||
@ -61,29 +58,21 @@ struct BufferView {
|
|||||||
impl BufferView {
|
impl BufferView {
|
||||||
// Creates a new view with the given buffer size
|
// Creates a new view with the given buffer size
|
||||||
fn new(size: usize, rx: mpsc::Receiver<String>) -> Self {
|
fn new(size: usize, rx: mpsc::Receiver<String>) -> Self {
|
||||||
|
let mut buffer = VecDeque::new();
|
||||||
|
buffer.resize(size, String::new());
|
||||||
BufferView {
|
BufferView {
|
||||||
rx: rx,
|
rx: rx,
|
||||||
buffer: (0..size).map(|_| String::new()).collect(),
|
buffer: buffer,
|
||||||
pos: 0,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reads available data from the stream into the buffer
|
// Reads available data from the stream into the buffer
|
||||||
fn update(&mut self) {
|
fn update(&mut self) {
|
||||||
let mut i = self.pos;
|
// Add each available line to the end of the buffer.
|
||||||
while let Ok(line) = self.rx.try_recv() {
|
while let Ok(line) = self.rx.try_recv() {
|
||||||
self.buffer[i] = line;
|
self.buffer.push_back(line);
|
||||||
i = (i + 1) % self.buffer.len();
|
self.buffer.pop_front();
|
||||||
}
|
}
|
||||||
self.pos = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Chain together the two parts of the buffer to appear as a circular one.
|
|
||||||
// TODO: Use `-> impl Iterator<Item=String>`...
|
|
||||||
fn ring(&self) -> Chain<Iter<String>, Iter<String>> {
|
|
||||||
// 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())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,18 +83,13 @@ impl View for BufferView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn draw(&self, printer: &Printer) {
|
fn draw(&self, printer: &Printer) {
|
||||||
|
// Print the end of the buffer
|
||||||
// If the buffer is large, we'll discard the beginning and keep the end.
|
for (i, line) in self.buffer
|
||||||
// If the buffer is too small, only print a part of it with an offset.
|
.iter()
|
||||||
let (discard, offset) = if self.buffer.len() >
|
.rev()
|
||||||
printer.size.y as usize {
|
.take(printer.size.y)
|
||||||
(self.buffer.len() - printer.size.y as usize, 0)
|
.enumerate() {
|
||||||
} else {
|
printer.print((0, printer.size.y - 1 - i), line);
|
||||||
(0, printer.size.y - self.buffer.len())
|
|
||||||
};
|
|
||||||
|
|
||||||
for (i, line) in self.ring().skip(discard).enumerate() {
|
|
||||||
printer.print((0, offset + i), line);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user