2015-05-15 19:16:58 +00:00
|
|
|
//! # Cursive
|
|
|
|
//!
|
|
|
|
//! Cursive is a TUI library built on top of ncurses-rs.
|
|
|
|
//! It allows to easily build layouts for text-based applications.
|
|
|
|
//!
|
|
|
|
//! ## Example
|
|
|
|
//! ```
|
|
|
|
//! extern crate cursive;
|
|
|
|
//!
|
|
|
|
//! use cursive::Cursive;
|
|
|
|
//! use cursive::view::TextView;
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! let mut siv = Cursive::new();
|
|
|
|
//!
|
|
|
|
//! siv.add_layer(TextView::new("Hello World!\nPress q to quit."));
|
|
|
|
//!
|
|
|
|
//! siv.add_global_callback('q' as i32, |s| s.quit());
|
|
|
|
//!
|
|
|
|
//! siv.run();
|
|
|
|
//! }
|
|
|
|
//! ```
|
2015-05-09 19:18:25 +00:00
|
|
|
extern crate ncurses;
|
2015-05-22 06:29:49 +00:00
|
|
|
extern crate toml;
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
pub mod event;
|
2015-05-09 19:18:25 +00:00
|
|
|
pub mod view;
|
2015-05-15 18:58:47 +00:00
|
|
|
pub mod printer;
|
2015-05-18 18:51:30 +00:00
|
|
|
pub mod vec;
|
2015-05-22 06:29:49 +00:00
|
|
|
pub mod color;
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
mod div;
|
2015-05-16 21:02:15 +00:00
|
|
|
mod margins;
|
|
|
|
|
|
|
|
pub use margins::Margins;
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2015-05-16 00:56:38 +00:00
|
|
|
use std::any::Any;
|
2015-05-15 01:38:58 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2015-05-18 18:51:30 +00:00
|
|
|
use vec::Vec2;
|
2015-05-15 00:41:17 +00:00
|
|
|
use view::View;
|
2015-05-15 22:00:20 +00:00
|
|
|
use printer::Printer;
|
2015-05-24 00:07:22 +00:00
|
|
|
use view::{StackView,Selector};
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2015-05-15 01:38:58 +00:00
|
|
|
use event::{EventResult,Callback};
|
|
|
|
|
2015-05-15 18:58:47 +00:00
|
|
|
/// Identifies a screen in the cursive ROOT.
|
2015-05-15 01:38:58 +00:00
|
|
|
pub type ScreenId = usize;
|
2015-05-15 00:41:17 +00:00
|
|
|
|
|
|
|
/// Central part of the cursive library.
|
2015-05-16 00:56:38 +00:00
|
|
|
///
|
2015-05-15 00:41:17 +00:00
|
|
|
/// It initializes ncurses on creation and cleans up on drop.
|
|
|
|
/// To use it, you should populate it with views, layouts and callbacks,
|
|
|
|
/// then start the event loop with run().
|
2015-05-15 01:38:58 +00:00
|
|
|
///
|
|
|
|
/// It uses a list of screen, with one screen active at a time.
|
2015-05-09 19:18:25 +00:00
|
|
|
pub struct Cursive {
|
2015-05-15 01:38:58 +00:00
|
|
|
screens: Vec<StackView>,
|
|
|
|
|
|
|
|
active_screen: ScreenId,
|
2015-05-09 19:18:25 +00:00
|
|
|
|
|
|
|
running: bool,
|
2015-05-15 01:38:58 +00:00
|
|
|
|
|
|
|
global_callbacks: HashMap<i32, Rc<Callback>>,
|
2015-05-09 19:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Cursive {
|
2015-05-15 00:41:17 +00:00
|
|
|
/// Creates a new Cursive root, and initialize ncurses.
|
2015-05-09 19:18:25 +00:00
|
|
|
pub fn new() -> Self {
|
2015-05-22 06:29:49 +00:00
|
|
|
ncurses::setlocale(ncurses::LcCategory::all, "");
|
2015-05-09 19:18:25 +00:00
|
|
|
ncurses::initscr();
|
|
|
|
ncurses::keypad(ncurses::stdscr, true);
|
|
|
|
ncurses::noecho();
|
2015-05-19 17:58:42 +00:00
|
|
|
ncurses::cbreak();
|
2015-05-22 06:29:49 +00:00
|
|
|
ncurses::start_color();
|
2015-05-15 01:41:13 +00:00
|
|
|
ncurses::curs_set(ncurses::CURSOR_VISIBILITY::CURSOR_INVISIBLE);
|
2015-05-22 06:29:49 +00:00
|
|
|
color::load_legacy();
|
|
|
|
// color::load_default();
|
|
|
|
// color::load_theme("assets/style.toml").ok().unwrap();
|
|
|
|
|
|
|
|
ncurses::wbkgd(ncurses::stdscr, ncurses::COLOR_PAIR(color::BACKGROUND));
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2015-05-15 01:38:58 +00:00
|
|
|
let mut res = Cursive {
|
|
|
|
screens: Vec::new(),
|
|
|
|
active_screen: 0,
|
2015-05-09 19:18:25 +00:00
|
|
|
running: true,
|
2015-05-15 01:38:58 +00:00
|
|
|
global_callbacks: HashMap::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
res.screens.push(StackView::new());
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2015-05-23 00:24:02 +00:00
|
|
|
/// Regularly redraws everything, even when no input is given. Between 0 and 1000.
|
2015-05-22 23:28:05 +00:00
|
|
|
///
|
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 01:38:58 +00:00
|
|
|
/// Returns a mutable reference to the currently active screen.
|
|
|
|
pub fn screen_mut(&mut self) -> &mut StackView {
|
|
|
|
let id = self.active_screen;
|
|
|
|
self.screens.get_mut(id).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a new screen, and returns its ID.
|
|
|
|
pub fn add_screen(&mut self) -> ScreenId {
|
|
|
|
let res = self.screens.len();
|
|
|
|
self.screens.push(StackView::new());
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenient method to create a new screen, and set it as active.
|
|
|
|
pub fn add_active_screen(&mut self) -> ScreenId {
|
|
|
|
let res = self.add_screen();
|
|
|
|
self.set_screen(res);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the active screen. Panics if no such screen exist.
|
|
|
|
pub fn set_screen(&mut self, screen_id: ScreenId) {
|
|
|
|
if screen_id >= self.screens.len() {
|
|
|
|
panic!("Tried to set an invalid screen ID: {}, but only {} screens present.", screen_id, self.screens.len());
|
2015-05-09 19:18:25 +00:00
|
|
|
}
|
2015-05-15 01:38:58 +00:00
|
|
|
self.active_screen = screen_id;
|
|
|
|
}
|
|
|
|
|
2015-05-23 17:33:29 +00:00
|
|
|
fn find_any(&mut self, selector: &Selector) -> Option<&mut Any> {
|
2015-05-22 23:28:05 +00:00
|
|
|
// Internal find method that returns a Any object.
|
2015-05-23 17:33:29 +00:00
|
|
|
self.screen_mut().find(selector)
|
2015-05-16 00:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tries to find the view pointed to by the given path.
|
|
|
|
/// If the view is not found, or if it is not of the asked type,
|
|
|
|
/// it returns None.
|
2015-05-23 17:33:29 +00:00
|
|
|
pub fn find<V: View + Any>(&mut self, selector: &Selector) -> Option<&mut V> {
|
|
|
|
match self.find_any(selector) {
|
2015-05-16 00:56:38 +00:00
|
|
|
None => None,
|
|
|
|
Some(b) => b.downcast_mut::<V>(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 01:38:58 +00:00
|
|
|
/// Adds a global callback, triggered on the given key press when no view catches it.
|
|
|
|
pub fn add_global_callback<F>(&mut self, key: i32, cb: F)
|
2015-05-24 00:07:22 +00:00
|
|
|
where F: Fn(&mut Cursive) + 'static
|
2015-05-15 01:38:58 +00:00
|
|
|
{
|
|
|
|
self.global_callbacks.insert(key, Rc::new(Box::new(cb)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenient method to add a layer to the current screen.
|
|
|
|
pub fn add_layer<T: 'static + View>(&mut self, view: T) {
|
|
|
|
self.screen_mut().add_layer(view);
|
|
|
|
}
|
|
|
|
|
2015-05-15 22:00:20 +00:00
|
|
|
// Handles a key event when it was ignored by the current view
|
2015-05-15 01:38:58 +00:00
|
|
|
fn on_key_event(&mut self, ch: i32) {
|
|
|
|
let cb = match self.global_callbacks.get(&ch) {
|
|
|
|
None => return,
|
|
|
|
Some(cb) => cb.clone(),
|
|
|
|
};
|
2015-05-16 00:56:38 +00:00
|
|
|
// Not from a view, so no viewpath here
|
2015-05-24 00:07:22 +00:00
|
|
|
cb(self);
|
2015-05-09 19:18:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 00:56:38 +00:00
|
|
|
/// Returns the size of the screen, in characters.
|
2015-05-15 22:00:20 +00:00
|
|
|
pub fn screen_size(&self) -> Vec2 {
|
|
|
|
let mut x: i32 = 0;
|
|
|
|
let mut y: i32 = 0;
|
|
|
|
ncurses::getmaxyx(ncurses::stdscr, &mut y, &mut x);
|
|
|
|
|
|
|
|
Vec2 {
|
|
|
|
x: x as u32,
|
|
|
|
y: y as u32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&mut self) {
|
|
|
|
let size = self.screen_size();
|
|
|
|
self.screen_mut().layout(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(&mut self) {
|
|
|
|
let printer = Printer {
|
|
|
|
offset: Vec2::new(0,0),
|
|
|
|
size: self.screen_size(),
|
|
|
|
};
|
2015-05-19 22:54:11 +00:00
|
|
|
self.screen_mut().draw(&printer, true);
|
2015-05-23 22:58:06 +00:00
|
|
|
ncurses::refresh();
|
2015-05-15 22:00:20 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
/// Runs the event loop.
|
|
|
|
/// It will wait for user input (key presses) and trigger callbacks accordingly.
|
|
|
|
/// Blocks until quit() is called.
|
2015-05-09 19:18:25 +00:00
|
|
|
pub fn run(&mut self) {
|
2015-05-15 23:06:48 +00:00
|
|
|
// And the big event loop begins!
|
2015-05-09 19:18:25 +00:00
|
|
|
while self.running {
|
2015-05-15 22:00:20 +00:00
|
|
|
// Do we need to redraw everytime?
|
2015-05-22 23:28:05 +00:00
|
|
|
// Probably, actually.
|
2015-05-15 23:06:48 +00:00
|
|
|
// TODO: Do we actually need to clear everytime?
|
2015-05-15 22:00:20 +00:00
|
|
|
ncurses::clear();
|
2015-05-15 23:06:48 +00:00
|
|
|
// TODO: Do we need to re-layout everytime?
|
2015-05-15 22:00:20 +00:00
|
|
|
self.layout();
|
2015-05-15 23:06:48 +00:00
|
|
|
// TODO: Do we need to redraw every view every time?
|
|
|
|
// (Is this getting repetitive? :p)
|
2015-05-15 22:00:20 +00:00
|
|
|
self.draw();
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2015-05-15 22:00:20 +00:00
|
|
|
// Blocks until the user press a key.
|
2015-05-15 23:06:48 +00:00
|
|
|
// TODO: Add a timeout? Animations?
|
2015-05-15 00:41:17 +00:00
|
|
|
let ch = ncurses::getch();
|
2015-05-15 01:38:58 +00:00
|
|
|
|
2015-05-15 22:00:20 +00:00
|
|
|
// If the event was ignored, it is our turn to play with it.
|
2015-05-15 01:38:58 +00:00
|
|
|
match self.screen_mut().on_key_event(ch) {
|
|
|
|
EventResult::Ignored => self.on_key_event(ch),
|
2015-05-24 00:07:22 +00:00
|
|
|
EventResult::Consumed(None) => (),
|
|
|
|
EventResult::Consumed(Some(cb)) => cb(self),
|
2015-05-09 19:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 00:56:38 +00:00
|
|
|
/// Stops the event loop.
|
2015-05-09 19:18:25 +00:00
|
|
|
pub fn quit(&mut self) {
|
|
|
|
self.running = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Cursive {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
ncurses::endwin();
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
|