aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-04-01 22:35:06 -0500
committerJesse Luehrs <doy@tozt.net>2013-04-01 22:35:06 -0500
commit8dcdcdb964895df998ea06e84acd5577e2fe31b6 (patch)
tree18821dd6cb2a913761c19dff75a4d3b66b81fd83
parent760190349b5bde2c6dc336bb2b64cd41c2783667 (diff)
downloadrust-term-8dcdcdb964895df998ea06e84acd5577e2fe31b6.tar.gz
rust-term-8dcdcdb964895df998ea06e84acd5577e2fe31b6.zip
basic color support
-rw-r--r--src/term.rs65
-rw-r--r--test/rl.rs31
2 files changed, 69 insertions, 27 deletions
diff --git a/src/term.rs b/src/term.rs
index d7e8fb2..374105a 100644
--- a/src/term.rs
+++ b/src/term.rs
@@ -8,9 +8,38 @@
use core::libc::c_int;
pub use ios::{cooked,cbreak,raw,echo,size};
-use info::{escape,escape2};
+use info::{escape,escape1,escape2};
use trie::Trie;
+enum Keypress {
+ KeyCharacter(char),
+ KeyBackspace,
+ KeyReturn,
+ KeyTab,
+ KeyCtrl(char),
+ KeyF(int),
+ KeyUp,
+ KeyDown,
+ KeyLeft,
+ KeyRight,
+ KeyHome,
+ KeyEnd,
+ KeyInsert,
+ KeyDelete,
+ KeyEscape,
+}
+
+enum Color {
+ ColorBlack = 0,
+ ColorRed,
+ ColorGreen,
+ ColorYellow,
+ ColorBlue,
+ ColorMagenta,
+ ColorCyan,
+ ColorWhite,
+}
+
struct Term {
priv r: Reader,
priv w: Writer,
@@ -39,6 +68,14 @@ impl Term {
self.w.move(col, row);
}
+ pub fn fg_color (&mut self, color: Color) {
+ self.w.fg_color(color);
+ }
+
+ pub fn bg_color (&mut self, color: Color) {
+ self.w.bg_color(color);
+ }
+
pub fn cursor (&mut self, enabled: bool) {
self.w.cursor(enabled);
}
@@ -84,6 +121,14 @@ impl Writer {
}
}
+ fn fg_color (&mut self, color: Color) {
+ self.buf.push_str(escape1("setaf", color as int));
+ }
+
+ fn bg_color (&mut self, color: Color) {
+ self.buf.push_str(escape1("setab", color as int));
+ }
+
fn cursor (&mut self, enabled: bool) {
if enabled {
self.buf.push_str(escape("civis"));
@@ -123,24 +168,6 @@ impl Drop for Writer {
}
}
-enum Keypress {
- KeyCharacter(char),
- KeyBackspace,
- KeyReturn,
- KeyTab,
- KeyCtrl(char),
- KeyF(int),
- KeyUp,
- KeyDown,
- KeyLeft,
- KeyRight,
- KeyHome,
- KeyEnd,
- KeyInsert,
- KeyDelete,
- KeyEscape,
-}
-
struct Reader {
priv escapes: ~Trie<Keypress>,
priv buf: ~str,
diff --git a/test/rl.rs b/test/rl.rs
index ecf91ea..4787c78 100644
--- a/test/rl.rs
+++ b/test/rl.rs
@@ -1,5 +1,6 @@
extern mod term;
-use term::{KeyCharacter,KeyEscape,KeyUp,KeyDown,KeyLeft,KeyRight};
+use term::{KeyCharacter,KeyEscape,KeyUp,KeyDown,KeyLeft,KeyRight,KeyF};
+use term::{Color,ColorWhite,ColorRed};
fn term_app (body: &fn (r: &mut term::Term)) {
do term::ios::preserve {
@@ -9,21 +10,24 @@ fn term_app (body: &fn (r: &mut term::Term)) {
}
}
-fn draw_map (term: &mut term::Term, rows: uint, cols: uint) {
+fn draw_map (term: &mut term::Term, color: Color, rows: uint, cols: uint) {
+ term.fg_color(color);
for uint::range(0, rows) |i| {
term.move(0, i);
term.write(str::repeat(".", cols));
}
}
-fn draw_character (term: &mut term::Term, x: uint, y: uint) {
+fn draw_character (term: &mut term::Term, color: Color, x: uint, y: uint) {
term.move(x, y);
+ term.fg_color(color);
term.write("@");
term.move(x, y);
}
-fn draw_ground (term: &mut term::Term, x: uint, y: uint) {
+fn draw_ground (term: &mut term::Term, color: Color, x: uint, y: uint) {
term.move(x, y);
+ term.fg_color(color);
term.write(".");
}
@@ -31,17 +35,19 @@ fn main () {
let (cols, rows) = term::size();
do term_app |term| {
- draw_map(term, rows, cols);
-
let mut (x, y) = (0u, 0u);
let mut cursor = true;
+ let mut color = ColorWhite;
+
+ draw_map(term, color, rows, cols);
+
loop {
- draw_character(term, x, y);
+ draw_character(term, ColorWhite, x, y);
let k = match term.read() {
Some(key) => key,
None => break,
};
- draw_ground(term, x, y);
+ draw_ground(term, color, x, y);
match k {
KeyCharacter('q') | KeyEscape => { break }
@@ -51,6 +57,15 @@ fn main () {
KeyCharacter('k') | KeyUp if y > 0 => { y -= 1 }
KeyCharacter('l') | KeyRight if x < cols - 1 => { x += 1 }
+ KeyF(1) => {
+ color = ColorRed;
+ draw_map(term, color, rows, cols);
+ }
+ KeyF(6) => {
+ color = ColorWhite;
+ draw_map(term, color, rows, cols);
+ }
+
KeyCharacter(' ') => { term.cursor(cursor); cursor = !cursor }
_ => { }