From 34f235daf0566475586018c0079ead6797b800b7 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sun, 26 Jun 2016 15:03:12 -0700 Subject: [PATCH] Use UTF-8 drawing characters for printer methods Instead of ncurses chtype. --- Cargo.toml | 3 ++- src/lib.rs | 1 + src/printer.rs | 24 ++++++++++-------------- src/view/edit_view.rs | 11 ++++++----- src/view/scroll.rs | 5 ++--- src/view/select_view.rs | 5 ++--- src/view/shadow_view.rs | 7 +++---- 7 files changed, 26 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 043402a..e149525 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] authors = ["Alexandre Bury "] description = "A TUI library based on ncurses-rs" -keywords = ["ncurses", "TUI"] documentation = "https://gyscos.github.io/Cursive/cursive/index.html" +keywords = ["ncurses", "TUI"] license = "MIT" name = "cursive" readme = "Readme.md" @@ -12,6 +12,7 @@ version = "0.0.2" [dependencies] ncurses = "5.80.0" toml = "0.1" +unicode-segmentation = "0.1.2" [[example]] name = "hello_world" diff --git a/src/lib.rs b/src/lib.rs index 74979bd..9528361 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,7 @@ //! ``` extern crate ncurses; extern crate toml; +extern crate unicode_segmentation; pub mod event; pub mod view; diff --git a/src/printer.rs b/src/printer.rs index 6a3c424..574dade 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -3,7 +3,6 @@ use std::cmp::min; use ncurses; -use ncurses::chtype; use theme::{ColorPair, Theme}; use vec::{Vec2, ToVec2}; @@ -54,7 +53,7 @@ impl Printer { } /// Prints a vertical line using the given character. - pub fn print_vline(&self, start: T, len: usize, c: chtype) { + pub fn print_vline(&self, start: T, len: usize, c: &str) { let p = start.to_vec2(); if p.y > self.size.y || p.x > self.size.x { return; @@ -63,13 +62,12 @@ impl Printer { let p = p + self.offset; for y in 0..len { - ncurses::mvaddstr((p.y + y) as i32, p.x as i32, "│"); - // ncurses::mvaddstr(p.y as i32, p.x as i32, "┌"); + ncurses::mvaddstr((p.y + y) as i32, p.x as i32, c); } } /// Prints a horizontal line using the given character. - pub fn print_hline(&self, start: T, len: usize, c: chtype) { + pub fn print_hline(&self, start: T, len: usize, c: &str) { let p = start.to_vec2(); if p.y > self.size.y || p.x > self.size.x { return; @@ -77,7 +75,9 @@ impl Printer { let len = min(len, self.size.x - p.x); 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, @@ -131,14 +131,10 @@ impl Printer { self.print(start_v + size_v.keep_y(), "└"); self.print(start_v + size_v, "┘"); - self.print_hline(start_v + (1, 0), size_v.x - 1, ncurses::ACS_HLINE()); - self.print_vline(start_v + (0, 1), size_v.y - 1, ncurses::ACS_VLINE()); - self.print_hline(start_v + (1, 0) + size_v.keep_y(), - size_v.x - 1, - ncurses::ACS_HLINE()); - self.print_vline(start_v + (0, 1) + size_v.keep_x(), - size_v.y - 1, - ncurses::ACS_VLINE()); + self.print_hline(start_v + (1, 0), size_v.x - 1, "─"); + self.print_vline(start_v + (0, 1), size_v.y - 1, "│"); + self.print_hline(start_v + (1, 0) + size_v.keep_y(), size_v.x - 1, "─"); + self.print_vline(start_v + (0, 1) + size_v.keep_x(), size_v.y - 1, "│"); } /// Returns a printer on a subset of this one's area. diff --git a/src/view/edit_view.rs b/src/view/edit_view.rs index 1218d2e..bee3cd3 100644 --- a/src/view/edit_view.rs +++ b/src/view/edit_view.rs @@ -1,4 +1,5 @@ -use ncurses::{self, chtype}; +use ncurses; +use unicode_segmentation::UnicodeSegmentation; use std::cmp::min; @@ -83,7 +84,7 @@ impl View for EditView { printer.with_style(ncurses::A_REVERSE(), |printer| { if len < self.last_length { 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 { let visible_end = min(self.content.len(), self.offset + self.last_length); @@ -98,17 +99,17 @@ impl View for EditView { // Now print cursor if printer.focused { let c = if self.cursor == len { - '_' + "_" } else { // Get the char from the string... Is it so hard? self.content - .chars() + .graphemes(true) .nth(self.cursor) .expect(&format!("Found no char at cursor {} in {}", self.cursor, self.content)) }; - printer.print_hline((self.cursor - self.offset, 0), 1, c as chtype); + printer.print_hline((self.cursor - self.offset, 0), 1, c); } }); } diff --git a/src/view/scroll.rs b/src/view/scroll.rs index 8f39027..b31098f 100644 --- a/src/view/scroll.rs +++ b/src/view/scroll.rs @@ -1,5 +1,4 @@ use std::cmp::{min, max}; -use ncurses::chtype; use theme::ColorPair; use vec::Vec2; @@ -134,9 +133,9 @@ impl ScrollBase { 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.print_vline((printer.size.x - 1, start), height, ' ' as chtype); + printer.print_vline((printer.size.x - 1, start), height, " "); }); } } diff --git a/src/view/select_view.rs b/src/view/select_view.rs index 666f503..f115b2c 100644 --- a/src/view/select_view.rs +++ b/src/view/select_view.rs @@ -1,6 +1,5 @@ use std::cmp::min; use std::rc::Rc; -use ncurses::chtype; use theme::ColorPair; use Cursive; @@ -140,9 +139,9 @@ impl View for SelectView { printer.with_color(style, |printer| { let l = self.items[i].label.chars().count(); 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_hline((x + l, 0), printer.size.x - l - x, ' ' as chtype); + printer.print_hline((x + l, 0), printer.size.x - l - x, " "); }); }); } diff --git a/src/view/shadow_view.rs b/src/view/shadow_view.rs index b2d00ba..5fb8a9e 100644 --- a/src/view/shadow_view.rs +++ b/src/view/shadow_view.rs @@ -1,4 +1,3 @@ -use ncurses::chtype; use view::{View, ViewWrapper, SizeRequest}; use printer::Printer; use vec::Vec2; @@ -34,7 +33,7 @@ impl ViewWrapper for ShadowView { printer.with_color(ColorPair::Primary, |printer| { // Draw the view background 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 ViewWrapper for ShadowView { let w = printer.size.x - 1; printer.with_color(ColorPair::Shadow, |printer| { - printer.print_hline((2, h), w - 1, ' ' as chtype); - printer.print_vline((w, 2), h - 1, ' ' as chtype); + printer.print_hline((2, h), w - 1, " "); + printer.print_vline((w, 2), h - 1, " "); }); } }