From 7f1c1efe474851a129e4a2e5bc012d9b76ed2ed0 Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Sun, 3 Jul 2016 17:00:00 -0700 Subject: Grid API is now generic and strongly typed The Grid no longer knows about a `Cell` and is instead generic. The `Cell` type is coupled to the `term` module already, and it's been moved there to reflect the strong relationship. Grid APIs previously accepted `usize` for many arguments. If the caller intended rows to be columns, but the function accepted them in reverse, there would be no compiler error. Now there is, and this should prevent such bugs from entering the code. The Grid internals grew significantly to accomodate the strongly typed APIs. There is now a `grid::index` module which defines Cursor, Line, and Column. The Grid APIs are all based on these types now. Indexing for Ranges proved to be somewhat awkward. A new range had to be constructed in the implementation. If the optimizer can't figure out what's going on in that case, the ranges may not be a zero-cost abstraction. --- src/renderer/mod.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'src/renderer') diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 19a69b8b..c967fa05 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -27,8 +27,8 @@ use gl; use notify::{Watcher as WatcherApi, RecommendedWatcher as Watcher, op}; use font::{Rasterizer, RasterizedGlyph, FontDesc}; -use grid::{self, Grid, Cell, CellFlags}; -use term; +use grid::{self, Grid}; +use term::{self, cell, Cell}; use super::Rgb; @@ -80,8 +80,8 @@ pub struct Glyph { /// Naïve glyph cache /// -/// Currently only keyed by `char`, and thus not possible to hold different representations of the -/// same code point. +/// Currently only keyed by `char`, and thus not possible to hold different +/// representations of the same code point. pub struct GlyphCache { /// Cache of buffered glyphs cache: HashMap, @@ -236,7 +236,7 @@ impl Batch { bg_b: cell.bg.b as f32, }; - if cell.flags.contains(grid::INVERSE) { + if cell.flags.contains(cell::INVERSE) { instance.r = cell.bg.r as f32; instance.g = cell.bg.g as f32; instance.b = cell.bg.b as f32; @@ -550,7 +550,7 @@ impl<'a> RenderApi<'a> { c: c, fg: *color, bg: term::DEFAULT_BG, - flags: grid::INVERSE, + flags: cell::INVERSE, }; self.add_render_item(row, col, &cell, glyph); } @@ -576,22 +576,25 @@ impl<'a> RenderApi<'a> { } } - pub fn render_cursor(&mut self, cursor: term::Cursor, glyph_cache: &mut GlyphCache) { + pub fn render_cursor(&mut self, cursor: &grid::index::Cursor, glyph_cache: &mut GlyphCache) { if let Some(glyph) = glyph_cache.get(term::CURSOR_SHAPE, self) { let cell = Cell { c: term::CURSOR_SHAPE, fg: term::DEFAULT_FG, bg: term::DEFAULT_BG, - flags: CellFlags::empty(), + flags: cell::Flags::empty(), }; - self.add_render_item(cursor.y as f32, cursor.x as f32, &cell, glyph); + let y: usize = *cursor.line; + let x: usize = *cursor.col; + + self.add_render_item(y as f32, x as f32, &cell, glyph); } } - pub fn render_grid(&mut self, grid: &Grid, glyph_cache: &mut GlyphCache) { - for (i, row) in grid.rows().enumerate() { - for (j, cell) in row.cells().enumerate() { + pub fn render_grid(&mut self, grid: &Grid, glyph_cache: &mut GlyphCache) { + for (i, line) in grid.lines().enumerate() { + for (j, cell) in line.cells().enumerate() { // Skip empty cells if cell.c == ' ' && cell.bg == term::DEFAULT_BG { continue; -- cgit