Use UTF-8 drawing characters for printer methods

Instead of ncurses chtype.
This commit is contained in:
Alexandre Bury 2016-06-26 15:03:12 -07:00
parent ed785940dd
commit 34f235daf0
7 changed files with 26 additions and 30 deletions

View File

@ -1,8 +1,8 @@
[package] [package]
authors = ["Alexandre Bury <alexandre.bury@gmail.com>"] authors = ["Alexandre Bury <alexandre.bury@gmail.com>"]
description = "A TUI library based on ncurses-rs" description = "A TUI library based on ncurses-rs"
keywords = ["ncurses", "TUI"]
documentation = "https://gyscos.github.io/Cursive/cursive/index.html" documentation = "https://gyscos.github.io/Cursive/cursive/index.html"
keywords = ["ncurses", "TUI"]
license = "MIT" license = "MIT"
name = "cursive" name = "cursive"
readme = "Readme.md" readme = "Readme.md"
@ -12,6 +12,7 @@ version = "0.0.2"
[dependencies] [dependencies]
ncurses = "5.80.0" ncurses = "5.80.0"
toml = "0.1" toml = "0.1"
unicode-segmentation = "0.1.2"
[[example]] [[example]]
name = "hello_world" name = "hello_world"

View File

@ -22,6 +22,7 @@
//! ``` //! ```
extern crate ncurses; extern crate ncurses;
extern crate toml; extern crate toml;
extern crate unicode_segmentation;
pub mod event; pub mod event;
pub mod view; pub mod view;

View File

@ -3,7 +3,6 @@
use std::cmp::min; use std::cmp::min;
use ncurses; use ncurses;
use ncurses::chtype;
use theme::{ColorPair, Theme}; use theme::{ColorPair, Theme};
use vec::{Vec2, ToVec2}; use vec::{Vec2, ToVec2};
@ -54,7 +53,7 @@ impl Printer {
} }
/// Prints a vertical line using the given character. /// Prints a vertical line using the given character.
pub fn print_vline<T: ToVec2>(&self, start: T, len: usize, c: chtype) { pub fn print_vline<T: ToVec2>(&self, start: T, len: usize, c: &str) {
let p = start.to_vec2(); let p = start.to_vec2();
if p.y > self.size.y || p.x > self.size.x { if p.y > self.size.y || p.x > self.size.x {
return; return;
@ -63,13 +62,12 @@ impl Printer {
let p = p + self.offset; let p = p + self.offset;
for y in 0..len { for y in 0..len {
ncurses::mvaddstr((p.y + y) as i32, p.x as i32, ""); ncurses::mvaddstr((p.y + y) as i32, p.x as i32, c);
// ncurses::mvaddstr(p.y as i32, p.x as i32, "┌");
} }
} }
/// Prints a horizontal line using the given character. /// Prints a horizontal line using the given character.
pub fn print_hline<T: ToVec2>(&self, start: T, len: usize, c: chtype) { pub fn print_hline<T: ToVec2>(&self, start: T, len: usize, c: &str) {
let p = start.to_vec2(); let p = start.to_vec2();
if p.y > self.size.y || p.x > self.size.x { if p.y > self.size.y || p.x > self.size.x {
return; return;
@ -77,7 +75,9 @@ impl Printer {
let len = min(len, self.size.x - p.x); let len = min(len, self.size.x - p.x);
let p = p + self.offset; let p = p + self.offset;
ncurses::mvhline(p.y as i32, p.x as i32, c, len as i32); for x in 0..len {
ncurses::mvaddstr(p.y as i32, (p.x + x) as i32, c);
}
} }
/// Call the given closure with a colored printer, /// Call the given closure with a colored printer,
@ -131,14 +131,10 @@ impl Printer {
self.print(start_v + size_v.keep_y(), ""); self.print(start_v + size_v.keep_y(), "");
self.print(start_v + size_v, ""); self.print(start_v + size_v, "");
self.print_hline(start_v + (1, 0), size_v.x - 1, ncurses::ACS_HLINE()); self.print_hline(start_v + (1, 0), size_v.x - 1, "");
self.print_vline(start_v + (0, 1), size_v.y - 1, ncurses::ACS_VLINE()); self.print_vline(start_v + (0, 1), size_v.y - 1, "");
self.print_hline(start_v + (1, 0) + size_v.keep_y(), self.print_hline(start_v + (1, 0) + size_v.keep_y(), size_v.x - 1, "");
size_v.x - 1, self.print_vline(start_v + (0, 1) + size_v.keep_x(), size_v.y - 1, "");
ncurses::ACS_HLINE());
self.print_vline(start_v + (0, 1) + size_v.keep_x(),
size_v.y - 1,
ncurses::ACS_VLINE());
} }
/// Returns a printer on a subset of this one's area. /// Returns a printer on a subset of this one's area.

View File

@ -1,4 +1,5 @@
use ncurses::{self, chtype}; use ncurses;
use unicode_segmentation::UnicodeSegmentation;
use std::cmp::min; use std::cmp::min;
@ -83,7 +84,7 @@ impl View for EditView {
printer.with_style(ncurses::A_REVERSE(), |printer| { printer.with_style(ncurses::A_REVERSE(), |printer| {
if len < self.last_length { if len < self.last_length {
printer.print((0, 0), &self.content); printer.print((0, 0), &self.content);
printer.print_hline((len, 0), printer.size.x - len, '_' as chtype); printer.print_hline((len, 0), printer.size.x - len, "_");
} else { } else {
let visible_end = min(self.content.len(), self.offset + self.last_length); let visible_end = min(self.content.len(), self.offset + self.last_length);
@ -98,17 +99,17 @@ impl View for EditView {
// Now print cursor // Now print cursor
if printer.focused { if printer.focused {
let c = if self.cursor == len { let c = if self.cursor == len {
'_' "_"
} else { } else {
// Get the char from the string... Is it so hard? // Get the char from the string... Is it so hard?
self.content self.content
.chars() .graphemes(true)
.nth(self.cursor) .nth(self.cursor)
.expect(&format!("Found no char at cursor {} in {}", .expect(&format!("Found no char at cursor {} in {}",
self.cursor, self.cursor,
self.content)) self.content))
}; };
printer.print_hline((self.cursor - self.offset, 0), 1, c as chtype); printer.print_hline((self.cursor - self.offset, 0), 1, c);
} }
}); });
} }

View File

@ -1,5 +1,4 @@
use std::cmp::{min, max}; use std::cmp::{min, max};
use ncurses::chtype;
use theme::ColorPair; use theme::ColorPair;
use vec::Vec2; use vec::Vec2;
@ -134,9 +133,9 @@ impl ScrollBase {
ColorPair::HighlightInactive ColorPair::HighlightInactive
}; };
printer.print_vline((printer.size.x - 1, 0), printer.size.y, '|' as chtype); printer.print_vline((printer.size.x - 1, 0), printer.size.y, "|");
printer.with_color(color, |printer| { printer.with_color(color, |printer| {
printer.print_vline((printer.size.x - 1, start), height, ' ' as chtype); printer.print_vline((printer.size.x - 1, start), height, " ");
}); });
} }
} }

View File

@ -1,6 +1,5 @@
use std::cmp::min; use std::cmp::min;
use std::rc::Rc; use std::rc::Rc;
use ncurses::chtype;
use theme::ColorPair; use theme::ColorPair;
use Cursive; use Cursive;
@ -140,9 +139,9 @@ impl<T: 'static> View for SelectView<T> {
printer.with_color(style, |printer| { printer.with_color(style, |printer| {
let l = self.items[i].label.chars().count(); let l = self.items[i].label.chars().count();
let x = self.align.h.get_offset(l, printer.size.x); let x = self.align.h.get_offset(l, printer.size.x);
printer.print_hline((0, 0), x, ' ' as chtype); printer.print_hline((0, 0), x, " ");
printer.print((x, 0), &self.items[i].label); printer.print((x, 0), &self.items[i].label);
printer.print_hline((x + l, 0), printer.size.x - l - x, ' ' as chtype); printer.print_hline((x + l, 0), printer.size.x - l - x, " ");
}); });
}); });
} }

View File

@ -1,4 +1,3 @@
use ncurses::chtype;
use view::{View, ViewWrapper, SizeRequest}; use view::{View, ViewWrapper, SizeRequest};
use printer::Printer; use printer::Printer;
use vec::Vec2; use vec::Vec2;
@ -34,7 +33,7 @@ impl<T: View> ViewWrapper for ShadowView<T> {
printer.with_color(ColorPair::Primary, |printer| { printer.with_color(ColorPair::Primary, |printer| {
// Draw the view background // Draw the view background
for y in 1..printer.size.y - 1 { for y in 1..printer.size.y - 1 {
printer.print_hline((1, y), printer.size.x - 2, ' ' as chtype); printer.print_hline((1, y), printer.size.x - 2, " ");
} }
}); });
@ -44,8 +43,8 @@ impl<T: View> ViewWrapper for ShadowView<T> {
let w = printer.size.x - 1; let w = printer.size.x - 1;
printer.with_color(ColorPair::Shadow, |printer| { printer.with_color(ColorPair::Shadow, |printer| {
printer.print_hline((2, h), w - 1, ' ' as chtype); printer.print_hline((2, h), w - 1, " ");
printer.print_vline((w, 2), h - 1, ' ' as chtype); printer.print_vline((w, 2), h - 1, " ");
}); });
} }
} }