aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.rs13
-rw-r--r--src/renderer/mod.rs16
-rw-r--r--src/term.rs51
3 files changed, 54 insertions, 26 deletions
diff --git a/src/main.rs b/src/main.rs
index 5c52dcda..be45ed11 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -110,6 +110,7 @@ fn main() {
.with_vsync()
.with_title("Alacritty")
.build().unwrap();
+
window.set_window_resize_callback(Some(window_resize_handler as fn(u32, u32)));
gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);
@@ -271,18 +272,14 @@ fn main() {
}
{
- // Draw grid + cursor
+ // Draw grid
{
let _sampler = meter.sampler();
- renderer.with_api(terminal.size_info(), |mut api| {
+ let size_info = terminal.size_info().clone();
+ renderer.with_api(&size_info, |mut api| {
// Draw the grid
- api.render_grid(terminal.grid(), &mut glyph_cache);
-
- // Also draw the cursor
- if terminal.mode().contains(term::mode::SHOW_CURSOR) {
- api.render_cursor(terminal.cursor(), &mut glyph_cache);
- }
+ api.render_grid(&terminal.render_grid(), &mut glyph_cache);
})
}
diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs
index 4cd26771..d5946c76 100644
--- a/src/renderer/mod.rs
+++ b/src/renderer/mod.rs
@@ -577,22 +577,6 @@ impl<'a> RenderApi<'a> {
}
}
- pub fn render_cursor(&mut self, cursor: &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: cell::Flags::empty(),
- };
-
- 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<Cell>, glyph_cache: &mut GlyphCache) {
for (i, line) in grid.lines().enumerate() {
for (j, cell) in line.cells().enumerate() {
diff --git a/src/term.rs b/src/term.rs
index 22e72810..0471fd28 100644
--- a/src/term.rs
+++ b/src/term.rs
@@ -13,8 +13,9 @@
// limitations under the License.
//
//! Exports the `Term` type which is a high-level API for the Grid
-use std::ops::Range;
use std::fmt;
+use std::mem;
+use std::ops::{Deref, Range};
use ansi::{self, Attr};
use grid::{Grid, ClearRegion};
@@ -31,6 +32,48 @@ macro_rules! debug_println {
}
}
+/// RAII type which manages grid state for render
+///
+/// This manages the cursor during a render. The cursor location is inverted to
+/// draw it, and reverted after drawing to maintain state.
+pub struct RenderGrid<'a> {
+ inner: &'a mut Grid<Cell>,
+ cursor: &'a Cursor,
+ mode: TermMode,
+}
+
+impl<'a> RenderGrid<'a> {
+ fn new<'b>(grid: &'b mut Grid<Cell>, cursor: &'b Cursor, mode: TermMode) -> RenderGrid<'b> {
+ if mode.contains(mode::SHOW_CURSOR) {
+ let cell = &mut grid[cursor];
+ mem::swap(&mut cell.fg, &mut cell.bg);
+ }
+
+ RenderGrid {
+ inner: grid,
+ cursor: cursor,
+ mode: mode,
+ }
+ }
+}
+
+impl<'a> Drop for RenderGrid<'a> {
+ fn drop(&mut self) {
+ if self.mode.contains(mode::SHOW_CURSOR) {
+ let cell = &mut self.inner[self.cursor];
+ mem::swap(&mut cell.fg, &mut cell.bg);
+ }
+ }
+}
+
+impl<'a> Deref for RenderGrid<'a> {
+ type Target = Grid<Cell>;
+
+ fn deref(&self) -> &Self::Target {
+ self.inner
+ }
+}
+
/// coerce val to be between min and max
fn limit<T: PartialOrd>(val: T, min: T, max: T) -> T {
if val < min {
@@ -175,7 +218,7 @@ pub struct Term {
}
/// Terminal size info
-#[derive(Debug)]
+#[derive(Debug, Copy, Clone)]
pub struct SizeInfo {
/// Terminal window width
pub width: f32,
@@ -245,6 +288,10 @@ impl Term {
}
}
+ pub fn render_grid<'a>(&'a mut self) -> RenderGrid<'a> {
+ RenderGrid::new(&mut self.grid, &self.cursor, self.mode)
+ }
+
/// Resize terminal to new dimensions
pub fn resize(&mut self, width: f32, height: f32) {
let size = SizeInfo {