tabled/features/shadow.rs
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
//! This module contains a [`Shadow`] option for a [`Table`].
//!
//! # Example
//!
//! ```
//! use tabled::{Style, TableIteratorExt, shadow::Shadow};
//!
//! let data = vec!["Hello", "World", "!"];
//!
//! let table = data.table()
//! .with(Style::markdown())
//! .with(Shadow::new(1))
//! .to_string();
//!
//! assert_eq!(
//! table,
//! concat!(
//! "| &str | \n",
//! "|-------|▒\n",
//! "| Hello |▒\n",
//! "| World |▒\n",
//! "| ! |▒\n",
//! " ▒▒▒▒▒▒▒▒▒",
//! )
//! );
//! ```
//!
//! [`Table`]: crate::Table
use crate::{
papergrid::{Offset, Sides},
Table, TableOption,
};
#[cfg(feature = "color")]
use crate::color::Color;
/// The structure represents a shadow of a table.
///
/// NOTICE: It uses [`Margin`] therefore it often can't be combined.
///
/// [`Margin`]: crate::Margin
#[derive(Debug, Clone)]
pub struct Shadow {
c: char,
size: usize,
size_offset: usize,
direction: Sides<bool>,
#[cfg(feature = "color")]
color: Option<Color>,
}
impl Shadow {
/// A default fill character to be used.
pub const DEFAULT_FILL: char = '▒';
/// Construct's an [`Shadow`] object with default fill [`Shadow::DEFAULT_FILL`].
///
/// It uses space(' ') as a default fill character.
/// To set a custom character you can use [`Self::set_fill`] function.
pub fn new(size: usize) -> Self {
Self {
c: Self::DEFAULT_FILL,
size,
size_offset: 1,
direction: Sides::new(false, true, false, true),
#[cfg(feature = "color")]
color: None,
}
}
/// The function, sets a characters for the [`Shadow`] to be used.
pub fn set_fill(&mut self, c: char) -> &mut Self {
self.c = c;
self
}
/// Set an offset value (default is '1').
pub fn set_offset(&mut self, size: usize) -> &mut Self {
self.size_offset = size;
self
}
/// Switch shadow to top.
pub fn set_top(&mut self) -> &mut Self {
self.direction.top = true;
self.direction.bottom = false;
self
}
/// Switch shadow to bottom.
pub fn set_bottom(&mut self) -> &mut Self {
self.direction.bottom = true;
self.direction.top = false;
self
}
/// Switch shadow to left.
pub fn set_left(&mut self) -> &mut Self {
self.direction.left = true;
self.direction.right = false;
self
}
/// Switch shadow to right.
pub fn set_right(&mut self) -> &mut Self {
self.direction.right = true;
self.direction.left = false;
self
}
/// Sets a color for a shadow.
#[cfg(feature = "color")]
pub fn set_color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
}
impl<R> TableOption<R> for Shadow {
fn change(&mut self, table: &mut Table<R>) {
let mut margin = *table.get_config().get_margin();
if self.direction.top {
margin.top.size = self.size;
margin.top.fill = self.c;
}
if self.direction.bottom {
margin.bottom.size = self.size;
margin.bottom.fill = self.c;
}
if self.direction.left {
margin.left.size = self.size;
margin.left.fill = self.c;
}
if self.direction.right {
margin.right.size = self.size;
margin.right.fill = self.c;
}
let mut offset = Sides::new(
Offset::Begin(0),
Offset::Begin(0),
Offset::Begin(0),
Offset::Begin(0),
);
if self.direction.right && self.direction.bottom {
offset.bottom = Offset::Begin(self.size_offset);
offset.right = Offset::Begin(self.size_offset);
}
if self.direction.right && self.direction.top {
offset.top = Offset::Begin(self.size_offset);
offset.right = Offset::End(self.size_offset);
}
if self.direction.left && self.direction.bottom {
offset.bottom = Offset::End(self.size_offset);
offset.left = Offset::Begin(self.size_offset);
}
if self.direction.left && self.direction.top {
offset.top = Offset::End(self.size_offset);
offset.left = Offset::End(self.size_offset);
}
let cfg = table.get_config_mut();
cfg.set_margin(margin);
cfg.set_margin_offset(offset);
#[cfg(feature = "color")]
if let Some(color) = self.color.as_ref() {
let mut colors = Sides::default();
if self.direction.top {
colors.top = color.clone().into();
}
if self.direction.bottom {
colors.bottom = color.clone().into();
}
if self.direction.left {
colors.left = color.clone().into();
}
if self.direction.right {
colors.right = color.clone().into();
}
cfg.set_margin_color(colors);
}
}
}