1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#[macro_use] extern crate itertools; extern crate prototty; use std::slice; use prototty::*; pub trait DefaultForeground { fn default_foreground() -> Self; } pub trait DefaultBackground { fn default_background() -> Self; } #[derive(Debug, Clone)] pub struct CommonCell<F, B> where F: From<Rgb24> + DefaultForeground, B: From<Rgb24> + DefaultBackground, { pub character: char, pub bold: bool, pub underline: bool, pub foreground_colour: F, pub background_colour: B, } impl<F, B> ViewCell for CommonCell<F, B> where F: From<Rgb24> + DefaultForeground, B: From<Rgb24> + DefaultBackground, { fn set_character(&mut self, character: char) { self.character = character; } fn set_bold(&mut self, bold: bool) { self.bold = bold; } fn set_underline(&mut self, underline: bool) { self.underline = underline; } fn set_foreground_colour(&mut self, colour: Rgb24) { self.foreground_colour = colour.into(); } fn set_background_colour(&mut self, colour: Rgb24) { self.background_colour = colour.into(); } } impl<F, B> Default for CommonCell<F, B> where F: From<Rgb24> + DefaultForeground, B: From<Rgb24> + DefaultBackground, { fn default() -> Self { CommonCell { character: ' ', bold: false, underline: false, foreground_colour: F::default_foreground(), background_colour: B::default_background(), } } } pub type Iter<'a, C> = slice::Iter<'a, C>; pub type IterMut<'a, C> = slice::IterMut<'a, C>; pub struct CoordEnumerate<'a, T: 'a> { coords: CoordIter, iter: slice::Iter<'a, T>, } impl<'a, T> CoordEnumerate<'a, T> { fn new(coords: CoordIter, iter: slice::Iter<'a, T>) -> Self { Self { coords, iter } } } impl<'a, T> Iterator for CoordEnumerate<'a, T> { type Item = (Coord, &'a T); fn next(&mut self) -> Option<Self::Item> { self.coords.next().and_then(|c| { self.iter.next().map(|t| (c, t)) }) } } #[derive(Debug, Clone)] pub struct Grid<C: Default + Clone> { size: Size, cells: Vec<C>, depth: Vec<i32>, } impl<C: Default + Clone> Grid<C> { pub fn new(size: Size) -> Self { let num_cells = size.count(); let mut cells = Vec::with_capacity(num_cells); cells.resize(num_cells, Default::default()); let mut depth = Vec::with_capacity(num_cells); depth.resize(num_cells, 0); Self { size, cells, depth, } } pub fn size(&self) -> Size { self.size } pub fn resize(&mut self, size: Size) { let num_cells = size.count(); self.cells.resize(num_cells, Default::default()); self.depth.resize(num_cells, 0); self.size = size; } pub fn clear(&mut self) { for (cell, depth) in izip!(self.cells.iter_mut(), self.depth.iter_mut()) { *cell = Default::default(); *depth = 0; } } pub fn enumerate(&self) -> CoordEnumerate<C> { CoordEnumerate::new(self.size.coords(), self.cells.iter()) } pub fn iter(&self) -> Iter<C> { self.cells.iter() } pub fn iter_mut(&mut self) -> IterMut<C> { self.cells.iter_mut() } } impl<C: ViewCell + Default + Clone> ViewGrid for Grid<C> { type Cell = C; fn get_mut(&mut self, coord: Coord, depth: i32) -> Option<&mut Self::Cell> { if let Some(index) = self.size.index(coord) { let cell_depth = &mut self.depth[index]; if depth >= *cell_depth { *cell_depth = depth; Some(&mut self.cells[index]) } else { None } } else { None } } }