Skip to content

Commit

Permalink
Make styling work under canvas. Bold, anyway.
Browse files Browse the repository at this point in the history
  • Loading branch information
eevee committed May 23, 2013
1 parent 6f36de3 commit ed4444c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
36 changes: 29 additions & 7 deletions amulet/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ll::{Character,Key,Style}; // TODO move these somewhere dealing with keys a
struct CanvasCell {
dirty: bool,
glyph: char,
style: (),
style: Style,
}

struct CanvasRow {
Expand Down Expand Up @@ -38,7 +38,7 @@ pub fn Canvas(term: @Terminal, start_row: uint, start_col: uint, height: uint, w
CanvasCell{
dirty: false,
glyph: ' ',
style: (),
style: Style(),
}
}),
}
Expand Down Expand Up @@ -90,13 +90,13 @@ impl Canvas {
*cell = CanvasCell{
dirty: true,
glyph: ' ',
style: (),
style: Style(),
};
}
}
}

pub fn attrwrite(&mut self, s: &str, style: &Style) {
pub fn attrwrite(&mut self, s: &str, style: Style) {
for str::each_char(s) |glyph| {
if glyph == '\n' {
// TODO this probably needs (a) more cases, (b) termcap
Expand All @@ -117,7 +117,7 @@ impl Canvas {
row.cells[self.cur_col] = CanvasCell{
dirty: true,
glyph: glyph,
style: (),
style: copy style,
};
row.is_dirty = true;
if self.cur_col > row.last_dirty {
Expand All @@ -140,14 +140,16 @@ impl Canvas {
}

pub fn write(&mut self, s: &str) {
self.attrwrite(s, &Style());
self.attrwrite(s, Style());
}

pub fn repaint(&mut self) {
// TODO wrap this
// TODO check for existence of cup? fallback?
//self.term.write_cap2("cup", self.start_col as int, self.start_row as int);

let mut is_bold = false;

for uint::range(0, self.height) |row_i| {
let row = &mut self.rows[row_i];
if ! row.is_dirty {
Expand All @@ -158,7 +160,21 @@ impl Canvas {
self.term.move(self.start_col + row.first_dirty, self.start_row + row_i);
// TODO with this level of optimization, imo, there should also be a method for forcibly redrawing the entire screen from (presumed) scratch
for uint::range(row.first_dirty, row.last_dirty + 1) |col| {
self.term.write(fmt!("%c", row.cells[col].glyph));
let cell = row.cells[col];

// Deal with formatting
if cell.style.is_bold && ! is_bold {
self.term.write_cap("bold");
is_bold = true;
}
else if is_bold && ! cell.style.is_bold {
// TODO this resets formatting entirely -- there's no way
// to turn off bold/underline individually :|
self.term.write_cap("sgr0");
is_bold = false;
}

self.term.write(fmt!("%c", cell.glyph));
row.cells[col].dirty = false;
}

Expand All @@ -167,6 +183,12 @@ impl Canvas {
row.last_dirty = 0;
}

// Clean up attribute settings when done
// TODO optimization possibilities here if we remember the current cursor style -- which we may need to do anyway once we're tracking more than bold
if is_bold {
self.term.write_cap("sgr0");
}

// TODO move the cursor to its original position if that's not where it is now
}

Expand Down
22 changes: 11 additions & 11 deletions amulet/ll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl Terminal {

// TODO seems it would make sense to cache non-formatted capabilities (as
// well as numeric/flag ones), which i think blessings does
fn write_cap(&self, cap_name: &str) {
pub fn write_cap(&self, cap_name: &str) {
// If we're calling this function then this capability really shouldn't
// take any arguments, but someone might have screwed up, or it may
// have an escaped % or something. Best do the whole formatting thing.
Expand Down Expand Up @@ -312,7 +312,7 @@ impl Terminal {
io::stdout().flush();
}

pub fn attrwrite(&self, s: &str, style: &Style) {
pub fn attrwrite(&self, s: &str, style: Style) {
// TODO try to cut down on the amount of back-and-forth between c
// strings and rust strings all up in here
if style.is_underline {
Expand Down Expand Up @@ -519,7 +519,7 @@ impl Window {
self.term.write(msg);
}

pub fn attrwrite(&self, s: &str, style: &Style) {
pub fn attrwrite(&self, s: &str, style: Style) {
// TODO same as above
self.term.attrwrite(s, style);
}
Expand Down Expand Up @@ -762,12 +762,12 @@ pub fn Style() -> Style {
return NORMAL;
}
impl Style {
pub fn bold(&self) -> ~Style {
return ~Style{ is_bold: true, ..*self };
pub fn bold(&self) -> Style {
return Style{ is_bold: true, ..*self };
}

pub fn underline(&self) -> ~Style {
return ~Style{ is_underline: true, ..*self };
pub fn underline(&self) -> Style {
return Style{ is_underline: true, ..*self };
}

// TODO this pretty much blows; color pairs are super archaic and i am
Expand All @@ -778,11 +778,11 @@ impl Style {
// before capturing the window... :|
// TODO this doesn't handle default colors correctly, because those are
// color index -1.
pub fn fg(&self, color: int) -> ~Style {
return ~Style{ fg_color: color, ..*self };
pub fn fg(&self, color: int) -> Style {
return Style{ fg_color: color, ..*self };
}
pub fn bg(&self, color: int) -> ~Style {
return ~Style{ bg_color: color, ..*self };
pub fn bg(&self, color: int) -> Style {
return Style{ bg_color: color, ..*self };
}

fn c_value(&self) -> c_int {
Expand Down
12 changes: 6 additions & 6 deletions demos/howto-05-attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ fn main() {
let mut ch;
let (rows, _cols) = canvas.size();

let plain = ~amulet::ll::Style();
let plain = amulet::ll::Style();
let bold = plain.bold();
let mut cur_style = copy plain;
let mut cur_style = &plain;

while ! fh.eof() {
ch = fh.read_byte();
Expand All @@ -51,19 +51,19 @@ fn main() {

if prev as char == '/' && ch as char == '*' {
//canvas.write(#fmt("%c", ch as char));
cur_style = copy bold;
cur_style = &bold;

canvas.move(row, col - 1);
canvas.attrwrite(fmt!("%c%c", prev as char, ch as char), cur_style);
canvas.attrwrite(fmt!("%c%c", prev as char, ch as char), *cur_style);
}
else {
canvas.attrwrite(fmt!("%c", ch as char), cur_style);
canvas.attrwrite(fmt!("%c", ch as char), *cur_style);
}

canvas.repaint();

if prev as char == '*' && ch as char == '/' {
cur_style = copy plain;
cur_style = &plain;
}

prev = ch;
Expand Down

0 comments on commit ed4444c

Please sign in to comment.