feat(widgets/canvas): added type Block in canvas markers (#350)

This allows for clearer colors than using Dot, especially when
decreasing the size of the terminal font in order to increase the
resolution of the canvas
pull/380/head
Amjad Alsharafi 4 years ago committed by GitHub
parent e0b2572eba
commit c35a1dd79f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -224,8 +224,10 @@ pub mod braille {
/// Marker to use when plotting data points /// Marker to use when plotting data points
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum Marker { pub enum Marker {
/// One point per cell /// One point per cell in shape of dot
Dot, Dot,
/// One point per cell in shape of a block
Block,
/// Up to 8 points per cell /// Up to 8 points per cell
Braille, Braille,
} }

@ -111,26 +111,28 @@ impl Grid for BrailleGrid {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct DotGrid { struct CharGrid {
width: u16, width: u16,
height: u16, height: u16,
cells: Vec<char>, cells: Vec<char>,
colors: Vec<Color>, colors: Vec<Color>,
cell_char: char,
} }
impl DotGrid { impl CharGrid {
fn new(width: u16, height: u16) -> DotGrid { fn new(width: u16, height: u16, cell_char: char) -> CharGrid {
let length = usize::from(width * height); let length = usize::from(width * height);
DotGrid { CharGrid {
width, width,
height, height,
cells: vec![' '; length], cells: vec![' '; length],
colors: vec![Color::Reset; length], colors: vec![Color::Reset; length],
cell_char,
} }
} }
} }
impl Grid for DotGrid { impl Grid for CharGrid {
fn width(&self) -> u16 { fn width(&self) -> u16 {
self.width self.width
} }
@ -162,7 +164,7 @@ impl Grid for DotGrid {
fn paint(&mut self, x: usize, y: usize, color: Color) { fn paint(&mut self, x: usize, y: usize, color: Color) {
let index = y * self.width as usize + x; let index = y * self.width as usize + x;
if let Some(c) = self.cells.get_mut(index) { if let Some(c) = self.cells.get_mut(index) {
*c = '•'; *c = self.cell_char;
} }
if let Some(c) = self.colors.get_mut(index) { if let Some(c) = self.colors.get_mut(index) {
*c = color; *c = color;
@ -259,7 +261,8 @@ impl<'a> Context<'a> {
marker: symbols::Marker, marker: symbols::Marker,
) -> Context<'a> { ) -> Context<'a> {
let grid: Box<dyn Grid> = match marker { let grid: Box<dyn Grid> = match marker {
symbols::Marker::Dot => Box::new(DotGrid::new(width, height)), symbols::Marker::Dot => Box::new(CharGrid::new(width, height, '•')),
symbols::Marker::Block => Box::new(CharGrid::new(width, height, '▄')),
symbols::Marker::Braille => Box::new(BrailleGrid::new(width, height)), symbols::Marker::Braille => Box::new(BrailleGrid::new(width, height)),
}; };
Context { Context {
@ -396,8 +399,8 @@ where
} }
/// Change the type of points used to draw the shapes. By default the braille patterns are used /// Change the type of points used to draw the shapes. By default the braille patterns are used
/// as they provide a more fine grained result but you might want to use the simple dot instead /// as they provide a more fine grained result but you might want to use the simple dot or
/// if the targeted terminal does not support those symbols. /// block instead if the targeted terminal does not support those symbols.
/// ///
/// # Examples /// # Examples
/// ///
@ -407,6 +410,8 @@ where
/// Canvas::default().marker(symbols::Marker::Braille).paint(|ctx| {}); /// Canvas::default().marker(symbols::Marker::Braille).paint(|ctx| {});
/// ///
/// Canvas::default().marker(symbols::Marker::Dot).paint(|ctx| {}); /// Canvas::default().marker(symbols::Marker::Dot).paint(|ctx| {});
///
/// Canvas::default().marker(symbols::Marker::Block).paint(|ctx| {});
/// ``` /// ```
pub fn marker(mut self, marker: symbols::Marker) -> Canvas<'a, F> { pub fn marker(mut self, marker: symbols::Marker) -> Canvas<'a, F> {
self.marker = marker; self.marker = marker;

Loading…
Cancel
Save