Use Vec2 in backend methods

This commit is contained in:
Alexandre Bury 2018-04-02 18:08:12 -07:00
parent 1c72ef7ade
commit fd09b5f806
11 changed files with 81 additions and 42 deletions

View File

@ -4,5 +4,6 @@ rust:
- stable - stable
- nightly - nightly
script: script:
- cargo check --all-features
- cargo build --verbose --features "pancurses-backend termion-backend" - cargo build --verbose --features "pancurses-backend termion-backend"
- cargo test --verbose --features "pancurses-backend termion-backend" - cargo test --verbose --features "pancurses-backend termion-backend"

View File

@ -1,3 +1,8 @@
//! Backend using BearLibTerminal
//!
//! Requires the `blt-backend` feature.
#![cfg(feature = "bear-lib-terminal")]
extern crate bear_lib_terminal; extern crate bear_lib_terminal;
use self::bear_lib_terminal::Color as BltColor; use self::bear_lib_terminal::Color as BltColor;
@ -220,9 +225,9 @@ impl backend::Backend for Backend {
true true
} }
fn screen_size(&self) -> (usize, usize) { fn screen_size(&self) -> Vec2 {
let Size { width, height } = terminal::state::size(); let Size { width, height } = terminal::state::size();
(width as usize, height as usize) (width, height).into()
} }
fn clear(&self, color: Color) { fn clear(&self, color: Color) {
@ -237,8 +242,8 @@ impl backend::Backend for Backend {
terminal::refresh(); terminal::refresh();
} }
fn print_at(&self, (x, y): (usize, usize), text: &str) { fn print_at(&self, pos: Vec2, text: &str) {
terminal::print_xy(x as i32, y as i32, text); terminal::print_xy(pos.x as i32, pos.y as i32, text);
} }
fn set_refresh_rate(&mut self, _: u32) { fn set_refresh_rate(&mut self, _: u32) {

View File

@ -1,3 +1,8 @@
//! Common module for the ncurses and pancurses backends.
//!
//! Requires either of `ncurses-backend` or `pancurses-backend`.
#![cfg(any(feature = "ncurses", feature = "pancurses"))]
use event::{Event, Key}; use event::{Event, Key};
use std::collections::HashMap; use std::collections::HashMap;
use theme::{BaseColor, Color}; use theme::{BaseColor, Color};

View File

@ -73,7 +73,9 @@ impl Backend {
/// Save a new color pair. /// Save a new color pair.
fn insert_color( fn insert_color(
&self, pairs: &mut HashMap<ColorPair, i16>, pair: ColorPair &self,
pairs: &mut HashMap<ColorPair, i16>,
pair: ColorPair,
) -> i16 { ) -> i16 {
let n = 1 + pairs.len() as i16; let n = 1 + pairs.len() as i16;
let target = if ncurses::COLOR_PAIRS() > i32::from(n) { let target = if ncurses::COLOR_PAIRS() > i32::from(n) {
@ -199,11 +201,11 @@ impl Backend {
} }
impl backend::Backend for Backend { impl backend::Backend for Backend {
fn screen_size(&self) -> (usize, usize) { fn screen_size(&self) -> Vec2 {
let mut x: i32 = 0; let mut x: i32 = 0;
let mut y: i32 = 0; let mut y: i32 = 0;
ncurses::getmaxyx(ncurses::stdscr(), &mut y, &mut x); ncurses::getmaxyx(ncurses::stdscr(), &mut y, &mut x);
(x as usize, y as usize) (x, y).into()
} }
fn has_colors(&self) -> bool { fn has_colors(&self) -> bool {
@ -261,8 +263,8 @@ impl backend::Backend for Backend {
ncurses::refresh(); ncurses::refresh();
} }
fn print_at(&self, (x, y): (usize, usize), text: &str) { fn print_at(&self, pos: Vec2, text: &str) {
ncurses::mvaddstr(y as i32, x as i32, text); ncurses::mvaddstr(pos.y as i32, pos.x as i32, text);
} }
fn poll_event(&mut self) -> Event { fn poll_event(&mut self) -> Event {

View File

@ -61,7 +61,9 @@ impl Backend {
/// Save a new color pair. /// Save a new color pair.
fn insert_color( fn insert_color(
&self, pairs: &mut HashMap<ColorPair, i32>, pair: ColorPair &self,
pairs: &mut HashMap<ColorPair, i32>,
pair: ColorPair,
) -> i32 { ) -> i32 {
let n = 1 + pairs.len() as i32; let n = 1 + pairs.len() as i32;
@ -167,9 +169,10 @@ impl Backend {
} }
impl backend::Backend for Backend { impl backend::Backend for Backend {
fn screen_size(&self) -> (usize, usize) { fn screen_size(&self) -> Vec2 {
// Coordinates are reversed here
let (y, x) = self.window.get_max_yx(); let (y, x) = self.window.get_max_yx();
(x as usize, y as usize) (x, y).into()
} }
fn has_colors(&self) -> bool { fn has_colors(&self) -> bool {
@ -227,8 +230,8 @@ impl backend::Backend for Backend {
self.window.refresh(); self.window.refresh();
} }
fn print_at(&self, (x, y): (usize, usize), text: &str) { fn print_at(&self, pos: Vec2, text: &str) {
self.window.mvaddstr(y as i32, x as i32, text); self.window.mvaddstr(pos.y as i32, pos.x as i32, text);
} }
fn poll_event(&mut self) -> Event { fn poll_event(&mut self) -> Event {

View File

@ -2,6 +2,7 @@
use backend; use backend;
use theme; use theme;
use event; use event;
use vec::Vec2;
pub struct Backend; pub struct Backend;
@ -23,15 +24,15 @@ impl backend::Backend for Backend {
false false
} }
fn screen_size(&self) -> (usize, usize) { fn screen_size(&self) -> Vec2 {
(1, 1) (1, 1).into()
} }
fn poll_event(&mut self) -> event::Event { fn poll_event(&mut self) -> event::Event {
event::Event::Exit event::Event::Exit
} }
fn print_at(&self, _: (usize, usize), _: &str) {} fn print_at(&self, _: Vec2, _: &str) {}
fn clear(&self, _: theme::Color) {} fn clear(&self, _: theme::Color) {}

View File

@ -1,42 +1,64 @@
//! Define backends using common libraries.
//!
//! Cursive doesn't print anything by itself: it delegates this job to a
//! backend library, which handles all actual input and output.
//!
//! This module defines the `Backend` trait, as well as a few implementations
//! using some common libraries. Each of those included backends needs a
//! corresonding feature to be enabled.
use event; use event;
use theme; use theme;
use vec::Vec2;
pub mod dummy; pub mod dummy;
/// Backend using the pure-rust termion library.
#[cfg(feature = "termion")]
pub mod termion; pub mod termion;
/// Backend using BearLibTerminal
#[cfg(feature = "bear-lib-terminal")]
pub mod blt; pub mod blt;
#[cfg(any(feature = "ncurses", feature = "pancurses"))]
pub mod curses; pub mod curses;
/// Trait defining the required methods to be a backend.
pub trait Backend { pub trait Backend {
// TODO: take `self` by value? // TODO: take `self` by value?
// Or implement Drop? // Or implement Drop?
/// Prepares to close the backend.
///
/// This should clear any state in the terminal.
fn finish(&mut self); fn finish(&mut self);
/// Refresh the screen.
fn refresh(&mut self); fn refresh(&mut self);
/// Should return `true` if this backend supports colors.
fn has_colors(&self) -> bool; fn has_colors(&self) -> bool;
fn screen_size(&self) -> (usize, usize);
/// Returns the screen size.
fn screen_size(&self) -> Vec2;
/// Main input method /// Main input method
fn poll_event(&mut self) -> event::Event; fn poll_event(&mut self) -> event::Event;
/// Main method used for printing /// Main method used for printing
fn print_at(&self, (usize, usize), &str); fn print_at(&self, pos: Vec2, text: &str);
/// Clears the screen with the given color.
fn clear(&self, color: theme::Color); fn clear(&self, color: theme::Color);
/// Sets the refresh rate for the backend.
///
/// If no event is detected in the interval, send an `Event::Refresh`.
fn set_refresh_rate(&mut self, fps: u32); fn set_refresh_rate(&mut self, fps: u32);
// This sets the Colours and returns the previous colours /// Starts using a new color.
// to allow you to set them back when you're done. ///
/// This should return the previously active color.
fn set_color(&self, colors: theme::ColorPair) -> theme::ColorPair; fn set_color(&self, colors: theme::ColorPair) -> theme::ColorPair;
/// Enables the given effect.
fn set_effect(&self, effect: theme::Effect); fn set_effect(&self, effect: theme::Effect);
/// Disables the given effect.
fn unset_effect(&self, effect: theme::Effect); fn unset_effect(&self, effect: theme::Effect);
} }

View File

@ -1,3 +1,8 @@
//! Backend using the pure-rust termion library.
//!
//! Requires the `termion-backend` feature.
#![cfg(feature = "termion")]
extern crate termion; extern crate termion;
extern crate chan_signal; extern crate chan_signal;
@ -203,9 +208,9 @@ impl backend::Backend for Backend {
true true
} }
fn screen_size(&self) -> (usize, usize) { fn screen_size(&self) -> Vec2 {
let (x, y) = termion::terminal_size().unwrap_or((1, 1)); let (x, y) = termion::terminal_size().unwrap_or((1, 1));
(x as usize, y as usize) (x, y).into()
} }
fn clear(&self, color: theme::Color) { fn clear(&self, color: theme::Color) {
@ -220,10 +225,10 @@ impl backend::Backend for Backend {
self.terminal.flush().unwrap(); self.terminal.flush().unwrap();
} }
fn print_at(&self, (x, y): (usize, usize), text: &str) { fn print_at(&self, pos: Vec2, text: &str) {
print!( print!(
"{}{}", "{}{}",
termion::cursor::Goto(1 + x as u16, 1 + y as u16), termion::cursor::Goto(1 + pos.x as u16, 1 + pos.y as u16),
text text
); );
} }

View File

@ -551,12 +551,7 @@ impl Cursive {
/// Returns the size of the screen, in characters. /// Returns the size of the screen, in characters.
pub fn screen_size(&self) -> Vec2 { pub fn screen_size(&self) -> Vec2 {
let (x, y) = self.backend.screen_size(); self.backend.screen_size()
Vec2 {
x: x as usize,
y: y as usize,
}
} }
fn layout(&mut self) { fn layout(&mut self) {

View File

@ -78,7 +78,7 @@ impl<'a> Printer<'a> {
let text = &text[..prefix_len]; let text = &text[..prefix_len];
let p = p + self.offset; let p = p + self.offset;
self.backend.print_at((p.x, p.y), text); self.backend.print_at(p, text);
} }
/// Prints a vertical line using the given character. /// Prints a vertical line using the given character.
@ -93,7 +93,7 @@ impl<'a> Printer<'a> {
let p = p + self.offset; let p = p + self.offset;
for y in 0..len { for y in 0..len {
self.backend.print_at((p.x, (p.y + y)), c); self.backend.print_at(p + (0,y), c);
} }
} }
@ -109,7 +109,7 @@ impl<'a> Printer<'a> {
let text: String = ::std::iter::repeat(c).take(len).collect(); let text: String = ::std::iter::repeat(c).take(len).collect();
let p = p + self.offset; let p = p + self.offset;
self.backend.print_at((p.x, p.y), &text); self.backend.print_at(p, &text);
} }
/// Call the given closure with a colored printer, /// Call the given closure with a colored printer,

View File

@ -18,7 +18,7 @@ where
let spans = parse_spans(&input); let spans = parse_spans(&input);
StyledString::new(input, spans) StyledString::with_spans(input, spans)
} }
/// Iterator that parse a markdown text and outputs styled spans. /// Iterator that parse a markdown text and outputs styled spans.