mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-12 20:23:35 +00:00
Add animated example.
This commit is contained in:
parent
44cb27c594
commit
d9652ac728
@ -28,3 +28,7 @@ path = "examples/lorem.rs"
|
|||||||
[[example]]
|
[[example]]
|
||||||
name = "dialog"
|
name = "dialog"
|
||||||
path = "examples/dialog.rs"
|
path = "examples/dialog.rs"
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "logs"
|
||||||
|
path = "examples/logs.rs"
|
||||||
|
103
examples/logs.rs
Normal file
103
examples/logs.rs
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
extern crate cursive;
|
||||||
|
|
||||||
|
use std::sync::mpsc;
|
||||||
|
use std::thread;
|
||||||
|
|
||||||
|
use cursive::Cursive;
|
||||||
|
use cursive::printer::Printer;
|
||||||
|
use cursive::view::{View,FullView};
|
||||||
|
|
||||||
|
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' as i32, |s,_| s.quit());
|
||||||
|
|
||||||
|
// A channel will communicate data from our running task to the UI.
|
||||||
|
let (tx,rx) = mpsc::channel();
|
||||||
|
|
||||||
|
// Generate data in a separate thread.
|
||||||
|
thread::spawn(|| { generate_logs(tx); });
|
||||||
|
|
||||||
|
// And sets the view to read from the other end of the channel.
|
||||||
|
siv.add_layer(FullView::new(BufferView::new(200, rx)));
|
||||||
|
|
||||||
|
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;
|
||||||
|
match tx.send(line) {
|
||||||
|
Err(_) => panic!("Uh?..."),
|
||||||
|
Ok(_) => (),
|
||||||
|
}
|
||||||
|
thread::sleep_ms(30);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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() {
|
||||||
|
i = (i+1) % self.buffer.len();
|
||||||
|
self.buffer[i] = line;
|
||||||
|
self.pos = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Chain together the two parts of the buffer to appear as a circular one.
|
||||||
|
// The signature is quite ugly, but basically we return an iterator:
|
||||||
|
// a Chain of two slice iterators.
|
||||||
|
fn ring<'a>(&'a self) -> std::iter::Chain<std::slice::Iter<'a,String>, std::slice::Iter<'a,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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl View for BufferView {
|
||||||
|
fn draw(&mut self, printer: &Printer, _: bool) {
|
||||||
|
// Before drawing, we'll want to update the buffer
|
||||||
|
self.update();
|
||||||
|
|
||||||
|
// If the buffer is large enough, we can fill the window
|
||||||
|
if self.buffer.len() > printer.size.y as usize {
|
||||||
|
// We'll only print the last entries that fit.
|
||||||
|
// So discard entries if there are too many.
|
||||||
|
let discard = self.buffer.len() - printer.size.y as usize;
|
||||||
|
// And we discard the first entries if needed
|
||||||
|
for (i, line) in self.ring().skip(discard).enumerate() {
|
||||||
|
printer.print((0,i as u32), line);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,7 @@
|
|||||||
extern crate cursive;
|
extern crate cursive;
|
||||||
|
|
||||||
use cursive::Cursive;
|
use cursive::Cursive;
|
||||||
use cursive::view::TextView;
|
use cursive::view::{TextView,Dialog};
|
||||||
use cursive::view::Dialog;
|
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
15
src/lib.rs
15
src/lib.rs
@ -37,6 +37,7 @@ pub use margins::Margins;
|
|||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::sync::mpsc;
|
||||||
|
|
||||||
use vec::Vec2;
|
use vec::Vec2;
|
||||||
use view::View;
|
use view::View;
|
||||||
@ -93,6 +94,17 @@ impl Cursive {
|
|||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Regularly redraws everything, even when no input is given.
|
||||||
|
///
|
||||||
|
/// Call with fps=0 to disable (default value).
|
||||||
|
pub fn set_fps(&self, fps: u32) {
|
||||||
|
if fps == 0 {
|
||||||
|
ncurses::timeout(-1);
|
||||||
|
} else {
|
||||||
|
ncurses::timeout(1000 / fps as i32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a mutable reference to the currently active screen.
|
/// Returns a mutable reference to the currently active screen.
|
||||||
pub fn screen_mut(&mut self) -> &mut StackView {
|
pub fn screen_mut(&mut self) -> &mut StackView {
|
||||||
let id = self.active_screen;
|
let id = self.active_screen;
|
||||||
@ -122,6 +134,7 @@ impl Cursive {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn find_any(&mut self, path: &ViewPath) -> Option<&mut Any> {
|
fn find_any(&mut self, path: &ViewPath) -> Option<&mut Any> {
|
||||||
|
// Internal find method that returns a Any object.
|
||||||
self.screen_mut().find(path)
|
self.screen_mut().find(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,7 +203,7 @@ impl Cursive {
|
|||||||
// And the big event loop begins!
|
// And the big event loop begins!
|
||||||
while self.running {
|
while self.running {
|
||||||
// Do we need to redraw everytime?
|
// Do we need to redraw everytime?
|
||||||
// Probably actually.
|
// Probably, actually.
|
||||||
// TODO: Do we actually need to clear everytime?
|
// TODO: Do we actually need to clear everytime?
|
||||||
ncurses::clear();
|
ncurses::clear();
|
||||||
// TODO: Do we need to re-layout everytime?
|
// TODO: Do we need to re-layout everytime?
|
||||||
|
@ -28,7 +28,7 @@ impl Button {
|
|||||||
|
|
||||||
impl View for Button {
|
impl View for Button {
|
||||||
|
|
||||||
fn draw(&self, printer: &Printer, focused: bool) {
|
fn draw(&mut self, printer: &Printer, focused: bool) {
|
||||||
let style = if !focused { color::PRIMARY } else { color::HIGHLIGHT };
|
let style = if !focused { color::PRIMARY } else { color::HIGHLIGHT };
|
||||||
let x = printer.size.x - 1;
|
let x = printer.size.x - 1;
|
||||||
|
|
||||||
|
@ -74,13 +74,13 @@ impl Dialog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl View for Dialog {
|
impl View for Dialog {
|
||||||
fn draw(&self, printer: &Printer, focused: bool) {
|
fn draw(&mut self, printer: &Printer, focused: bool) {
|
||||||
|
|
||||||
// This will be the height used by the buttons.
|
// This will be the height used by the buttons.
|
||||||
let mut height = 0;
|
let mut height = 0;
|
||||||
// Current horizontal position of the next button we'll draw.
|
// Current horizontal position of the next button we'll draw.
|
||||||
let mut x = 0;
|
let mut x = 0;
|
||||||
for (i,button) in self.buttons.iter().enumerate().rev() {
|
for (i,button) in self.buttons.iter_mut().enumerate().rev() {
|
||||||
let size = button.size;
|
let size = button.size;
|
||||||
let offset = printer.size - self.borders.bot_right() - self.padding.bot_right() - size - Vec2::new(x, 0);
|
let offset = printer.size - self.borders.bot_right() - self.padding.bot_right() - size - Vec2::new(x, 0);
|
||||||
// Add some special effect to the focused button
|
// Add some special effect to the focused button
|
||||||
|
33
src/view/full_view.rs
Normal file
33
src/view/full_view.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
use view::{View,ViewWrapper,SizeRequest,DimensionRequest};
|
||||||
|
use vec::Vec2;
|
||||||
|
|
||||||
|
pub struct FullView<T: View> {
|
||||||
|
pub view: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T: View> FullView<T> {
|
||||||
|
pub fn new(view: T) -> Self {
|
||||||
|
FullView {
|
||||||
|
view: view,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T: View> ViewWrapper for FullView<T> {
|
||||||
|
wrap_impl!(&self.view);
|
||||||
|
|
||||||
|
fn wrap_get_min_size(&self, req: SizeRequest) -> Vec2 {
|
||||||
|
let w = match req.w {
|
||||||
|
DimensionRequest::Fixed(w) => w,
|
||||||
|
DimensionRequest::AtMost(w) => w,
|
||||||
|
DimensionRequest::Unknown => self.view.get_min_size(req).x,
|
||||||
|
};
|
||||||
|
let h = match req.h {
|
||||||
|
DimensionRequest::Fixed(h) => h,
|
||||||
|
DimensionRequest::AtMost(h) => h,
|
||||||
|
DimensionRequest::Unknown => self.view.get_min_size(req).y,
|
||||||
|
};
|
||||||
|
|
||||||
|
Vec2::new(w,h)
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,7 @@ mod view_path;
|
|||||||
mod dialog;
|
mod dialog;
|
||||||
mod button;
|
mod button;
|
||||||
mod sized_view;
|
mod sized_view;
|
||||||
|
mod full_view;
|
||||||
|
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
|
|
||||||
@ -21,6 +22,7 @@ pub use self::dialog::Dialog;
|
|||||||
pub use self::button::Button;
|
pub use self::button::Button;
|
||||||
pub use self::sized_view::SizedView;
|
pub use self::sized_view::SizedView;
|
||||||
pub use self::view_wrapper::ViewWrapper;
|
pub use self::view_wrapper::ViewWrapper;
|
||||||
|
pub use self::full_view::FullView;
|
||||||
|
|
||||||
use event::EventResult;
|
use event::EventResult;
|
||||||
use vec::{Vec2,ToVec2};
|
use vec::{Vec2,ToVec2};
|
||||||
@ -89,7 +91,7 @@ pub trait View {
|
|||||||
fn layout(&mut self, Vec2) { }
|
fn layout(&mut self, Vec2) { }
|
||||||
|
|
||||||
/// Draws the view with the given printer (includes bounds) and focus.
|
/// Draws the view with the given printer (includes bounds) and focus.
|
||||||
fn draw(&self, printer: &Printer, focused: bool);
|
fn draw(&mut self, printer: &Printer, focused: bool);
|
||||||
|
|
||||||
/// Finds the view pointed to by the given path.
|
/// Finds the view pointed to by the given path.
|
||||||
/// Returns None if the path doesn't lead to a view.
|
/// Returns None if the path doesn't lead to a view.
|
||||||
|
@ -45,9 +45,9 @@ impl StackView {
|
|||||||
|
|
||||||
|
|
||||||
impl View for StackView {
|
impl View for StackView {
|
||||||
fn draw(&self, printer: &Printer, focused: bool) {
|
fn draw(&mut self, printer: &Printer, focused: bool) {
|
||||||
ncurses::wrefresh(printer.win);
|
ncurses::wrefresh(printer.win);
|
||||||
for v in self.layers.iter() {
|
for v in self.layers.iter_mut() {
|
||||||
// Center the view
|
// Center the view
|
||||||
v.view.draw(&Printer::new(v.win.unwrap(), v.size), focused);
|
v.view.draw(&Printer::new(v.win.unwrap(), v.size), focused);
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ impl <'a> Iterator for LinesIterator<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl View for TextView {
|
impl View for TextView {
|
||||||
fn draw(&self, printer: &Printer, _: bool) {
|
fn draw(&mut self, printer: &Printer, _: bool) {
|
||||||
// We don't have a focused view
|
// We don't have a focused view
|
||||||
|
|
||||||
let lines = self.content.split("\n")
|
let lines = self.content.split("\n")
|
||||||
|
@ -11,8 +11,8 @@ pub trait ViewWrapper {
|
|||||||
fn get_view_mut(&mut self) -> &mut View;
|
fn get_view_mut(&mut self) -> &mut View;
|
||||||
|
|
||||||
/// Wraps the draw method.
|
/// Wraps the draw method.
|
||||||
fn wrap_draw(&self, printer: &Printer, focused: bool) {
|
fn wrap_draw(&mut self, printer: &Printer, focused: bool) {
|
||||||
self.get_view().draw(printer, focused);
|
self.get_view_mut().draw(printer, focused);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps the get_min_size method.
|
/// Wraps the get_min_size method.
|
||||||
@ -37,7 +37,7 @@ pub trait ViewWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl <T: ViewWrapper> View for T {
|
impl <T: ViewWrapper> View for T {
|
||||||
fn draw(&self, printer: &Printer, focused: bool) {
|
fn draw(&mut self, printer: &Printer, focused: bool) {
|
||||||
self.wrap_draw(printer, focused);
|
self.wrap_draw(printer, focused);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user