From 396f00606fe0306ddfa56f12666c1fe670c9b6d5 Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Wed, 10 Feb 2016 11:10:54 +1300 Subject: [PATCH 1/9] Add basic support for 256 color mode Usage: Simply set the output mode: rustbox.set_output_mode(OutputMode::EightBit); And use Color::Byte(0x13) to select a specific color, or use the same old Color::Red num values for the standard colors. --- examples/hello-256-world.rs | 30 ++++++++++ src/rustbox.rs | 114 +++++++++++++++++++++++++++++++----- 2 files changed, 129 insertions(+), 15 deletions(-) create mode 100644 examples/hello-256-world.rs diff --git a/examples/hello-256-world.rs b/examples/hello-256-world.rs new file mode 100644 index 0000000..3888f95 --- /dev/null +++ b/examples/hello-256-world.rs @@ -0,0 +1,30 @@ +extern crate rustbox; + +use std::default::Default; + +use rustbox::{Color, RustBox, OutputMode}; +use rustbox::Key; + +fn main() { + let mut rustbox = match RustBox::init(Default::default()) { + Result::Ok(v) => v, + Result::Err(e) => panic!("{}", e), + }; + rustbox.set_output_mode(OutputMode::EightBit); + + rustbox.print(1, 1, rustbox::RB_BOLD, Color::Byte(0xa2), Color::Black, "Hello, world!"); + rustbox.print(1, 3, rustbox::RB_NORMAL, Color::Black, Color::Byte(0x9a), "Press 'q' to quit."); + loop { + rustbox.present(); + match rustbox.poll_event(false) { + Ok(rustbox::Event::KeyEvent(key)) => { + match key { + Key::Char('q') => { break; } + _ => { } + } + }, + Err(e) => panic!("{}", e), + _ => { } + } + } +} diff --git a/src/rustbox.rs b/src/rustbox.rs index 219211d..9092535 100644 --- a/src/rustbox.rs +++ b/src/rustbox.rs @@ -51,18 +51,59 @@ pub enum InputMode { AltMouse = 0x06 } +#[derive(Clone, Copy, Debug)] +pub enum OutputMode { + Current = 0, + Normal = 1, + EightBit = 2, // 256 Colors + WebSafe = 3, // 216 Colors + Grayscale = 4, +} + + #[derive(Clone, Copy, PartialEq, Debug)] -#[repr(C,u16)] pub enum Color { - Default = 0x00, - Black = 0x01, - Red = 0x02, - Green = 0x03, - Yellow = 0x04, - Blue = 0x05, - Magenta = 0x06, - Cyan = 0x07, - White = 0x08, + Black, + Red, + Green, + Yellow, + Blue, + Magenta, + Cyan, + White, + Byte(u16), + Default, +} +impl Color { + pub fn as_256color(&self) -> u16 { + match *self { + Color::Black => 0x00, + Color::Red => 0x01, + Color::Green => 0x02, + Color::Yellow => 0x03, + Color::Blue => 0x04, + Color::Magenta => 0x05, + Color::Cyan => 0x06, + Color::White => 0x07, + Color::Byte(b) => b, + Color::Default => panic!("Attempted to cast default color to byte"), + } + } + + pub fn as_16color(&self) -> u16 { + match *self { + Color::Default => 0x00, + Color::Black => 0x01, + Color::Red => 0x02, + Color::Green => 0x03, + Color::Yellow => 0x04, + Color::Blue => 0x05, + Color::Magenta => 0x06, + Color::Cyan => 0x07, + Color::White => 0x08, + Color::Byte(b) => panic!("Attempted to cast color byte {} to 16 color mode", b), + } + } } mod style { @@ -80,7 +121,11 @@ mod style { impl Style { pub fn from_color(color: super::Color) -> Style { - Style { bits: color as u16 & TB_NORMAL_COLOR.bits } + Style { bits: color.as_16color() & TB_NORMAL_COLOR.bits } + } + + pub fn from_256color(color: super::Color) -> Style { + Style { bits: color.as_256color() } } } } @@ -223,6 +268,9 @@ pub struct RustBox { _running: running::RunningGuard, // Termbox is not thread safe. See #39. _phantom: PhantomData<*mut ()>, + + // Store this so we know which colours to use + output_mode: OutputMode, } #[derive(Clone, Copy,Debug)] @@ -232,6 +280,11 @@ pub struct InitOptions { /// See InputMode enum for details on the variants. pub input_mode: InputMode, + /// Use this option to initialize with a specific output mode + /// + /// See OutputMode enum for details on the variants. + pub output_mode: OutputMode, + /// Use this option to automatically buffer stderr while RustBox is running. It will be /// written when RustBox exits. /// @@ -246,6 +299,7 @@ impl Default for InitOptions { fn default() -> Self { InitOptions { input_mode: InputMode::Current, + output_mode: OutputMode::Current, buffer_stderr: false, } } @@ -321,11 +375,12 @@ impl RustBox { }; // Create the RustBox. - let rb = unsafe { match termbox::tb_init() { + let mut rb = unsafe { match termbox::tb_init() { 0 => RustBox { _stderr: stderr, _running: running, _phantom: PhantomData, + output_mode: OutputMode::Current, }, res => { return Err(FromPrimitive::from_isize(res as isize).unwrap()) @@ -335,6 +390,11 @@ impl RustBox { InputMode::Current => (), _ => rb.set_input_mode(opts.input_mode), } + match opts.output_mode { + OutputMode::Current => (), + _ => rb.set_output_mode(opts.output_mode), + } + Ok(rb) } @@ -363,11 +423,26 @@ impl RustBox { } pub fn print(&self, x: usize, y: usize, sty: Style, fg: Color, bg: Color, s: &str) { - let fg = Style::from_color(fg) | (sty & style::TB_ATTRIB); - let bg = Style::from_color(bg); + let fg_int; + let bg_int; + + match self.output_mode { + // 256 color mode + OutputMode::EightBit => { + fg_int = Style::from_256color(fg) | (sty & style::TB_ATTRIB); + bg_int = Style::from_256color(bg); + }, + + // 16 color mode + _ => { + fg_int = Style::from_color(fg) | (sty & style::TB_ATTRIB); + bg_int = Style::from_color(bg); + } + } + for (i, ch) in s.chars().enumerate() { unsafe { - self.change_cell(x+i, y, ch as u32, fg.bits(), bg.bits()); + self.change_cell(x+i, y, ch as u32, fg_int.bits(), bg_int.bits()); } } } @@ -401,6 +476,15 @@ impl RustBox { termbox::tb_select_input_mode(mode as c_int); } } + + pub fn set_output_mode(&mut self, mode: OutputMode) { + self.output_mode = mode; + + unsafe { + termbox::tb_select_output_mode(mode as c_int); + } + } + } impl Drop for RustBox { From 89721833f3991590ce4bc437aef1614445e7027a Mon Sep 17 00:00:00 2001 From: Kaiyin Zhong Date: Fri, 15 Apr 2016 19:17:32 +0200 Subject: [PATCH 2/9] Projects that use this crate --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 80bdb88..dafd720 100644 --- a/README.md +++ b/README.md @@ -64,3 +64,9 @@ fn main() { ``` **NOTE:** this example can also be run with `cargo run --example hello-world`. + +### Projects that use this crate: + +* [hostblock](https://github.com/cgag/hostblock) +* [rust-2048](https://github.com/kdar/rust-2048) +* [marching-squares](https://github.com/crespyl/marching-squares) From ca49730ed37562ee5a1a690cfb4129faed80acee Mon Sep 17 00:00:00 2001 From: Greg Chapple Date: Wed, 20 Jul 2016 09:11:10 +0100 Subject: [PATCH 3/9] Added new color handling to print_char --- src/rustbox.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/rustbox.rs b/src/rustbox.rs index bfcc682..dc9ac96 100644 --- a/src/rustbox.rs +++ b/src/rustbox.rs @@ -447,10 +447,24 @@ impl RustBox { } pub fn print_char(&self, x: usize, y: usize, sty: Style, fg: Color, bg: Color, ch: char) { - let fg = Style::from_color(fg) | (sty & style::TB_ATTRIB); - let bg = Style::from_color(bg); + let fg_int; + let bg_int; + + match self.output_mode { + // 256 color mode + OutputMode::EightBit => { + fg_int = Style::from_256color(fg); + bg_int = Style::from_256color(bg); + }, + + // 16 color mode + _ => { + fg_int = Style::from_color(fg); + bg_int = Style::from_color(bg); + } + } unsafe { - self.change_cell(x, y, ch as u32, fg.bits(), bg.bits()); + self.change_cell(x, y, ch as u32, fg_int.bits(), bg_int.bits()); } } From df7830c5bad2a16199b67541c33f75187ada56ad Mon Sep 17 00:00:00 2001 From: Greg Chapple Date: Wed, 20 Jul 2016 09:14:06 +0100 Subject: [PATCH 4/9] Re-add style usage --- src/rustbox.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rustbox.rs b/src/rustbox.rs index dc9ac96..29ef087 100644 --- a/src/rustbox.rs +++ b/src/rustbox.rs @@ -453,13 +453,13 @@ impl RustBox { match self.output_mode { // 256 color mode OutputMode::EightBit => { - fg_int = Style::from_256color(fg); + fg_int = Style::from_256color(fg) | (sty & style::TB_ATTRIB); bg_int = Style::from_256color(bg); }, // 16 color mode _ => { - fg_int = Style::from_color(fg); + fg_int = Style::from_color(fg) | (sty & style::TB_ATTRIB); bg_int = Style::from_color(bg); } } From 1c475d946e1a8c27ead86eaa70671f6f098facb3 Mon Sep 17 00:00:00 2001 From: Greg Chapple Date: Wed, 27 Jul 2016 11:57:16 +0100 Subject: [PATCH 5/9] Bump version number --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 29cde17..0e6dd4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustbox" -version = "0.8.1" +version = "0.8.2" authors = ["Greg Chapple "] description = "A rust implementation of the termbox library" repository = "https://github.com/gchp/rustbox" From 1fd8648a2b8f1e45f6effb39fb6ac6ef22ebdd86 Mon Sep 17 00:00:00 2001 From: Greg Chapple Date: Wed, 27 Jul 2016 11:58:45 +0100 Subject: [PATCH 6/9] Revert "Bump version number" This reverts commit 1c475d946e1a8c27ead86eaa70671f6f098facb3. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0e6dd4a..29cde17 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustbox" -version = "0.8.2" +version = "0.8.1" authors = ["Greg Chapple "] description = "A rust implementation of the termbox library" repository = "https://github.com/gchp/rustbox" From 3970d151f4e83a220dcc0b06df210accd88128f7 Mon Sep 17 00:00:00 2001 From: Greg Chapple Date: Thu, 8 Sep 2016 13:30:13 +0100 Subject: [PATCH 7/9] 0.9.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 29cde17..aa7ea5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustbox" -version = "0.8.1" +version = "0.9.0" authors = ["Greg Chapple "] description = "A rust implementation of the termbox library" repository = "https://github.com/gchp/rustbox" From 8838de3ecd675b5eea3d63d4a3ae9ba97e8f1fe2 Mon Sep 17 00:00:00 2001 From: David Obrite Date: Sat, 11 Feb 2017 10:12:55 -0800 Subject: [PATCH 8/9] feat(Color): impl Default --- src/rustbox.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/rustbox.rs b/src/rustbox.rs index 29ef087..a10a705 100644 --- a/src/rustbox.rs +++ b/src/rustbox.rs @@ -105,6 +105,12 @@ impl Color { } } +impl Default for Color { + fn default() -> Color { + Color::Black + } +} + mod style { bitflags! { #[repr(C)] From e73d1b26ed0501100c93e2d4b21ac1f1201618f6 Mon Sep 17 00:00:00 2001 From: equal-l2 Date: Wed, 7 Jun 2017 21:59:41 +0900 Subject: [PATCH 9/9] Use rust 1.8.0 for testing --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f418588..a7a5249 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ language: rust rust: - - 1.5.0 + - 1.8.0 - nightly