tabled/features/style/raw_style.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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
//! This module contains [`RawStyle`] structure, which is analogues to [`Style`] but not generic,
//! so sometimes it can be used more conviently.
use std::collections::HashMap;
use papergrid::{records::Records, Borders};
use crate::{
style::{HorizontalLine, Line, VerticalLine},
Border, Style, Table, TableOption,
};
/// A raw style data, which can be produced safely from [`Style`].
///
/// It can be useful in order to not have a generics and be able to use it as a variable more conveniently.
#[derive(Default, Debug, Clone)]
pub struct RawStyle {
borders: Borders<char>,
horizontals: HashMap<usize, Line>,
verticals: HashMap<usize, Line>,
}
impl RawStyle {
/// Set a top border character.
pub fn set_top(&mut self, s: Option<char>) -> &mut Self {
self.borders.top = s;
self
}
/// Set a bottom border character.
pub fn set_bottom(&mut self, s: Option<char>) -> &mut Self {
self.borders.bottom = s;
self
}
/// Set a left border character.
pub fn set_left(&mut self, s: Option<char>) -> &mut Self {
self.borders.vertical_left = s;
self
}
/// Set a right border character.
pub fn set_right(&mut self, s: Option<char>) -> &mut Self {
self.borders.vertical_right = s;
self
}
/// Set a top split border character.
pub fn set_top_split(&mut self, s: Option<char>) -> &mut Self {
self.borders.top_intersection = s;
self
}
/// Set a bottom split character.
pub fn set_bottom_split(&mut self, s: Option<char>) -> &mut Self {
self.borders.bottom_intersection = s;
self
}
/// Set a left split character.
pub fn set_left_split(&mut self, s: Option<char>) -> &mut Self {
self.borders.horizontal_left = s;
self
}
/// Set a right split character.
pub fn set_right_split(&mut self, s: Option<char>) -> &mut Self {
self.borders.horizontal_right = s;
self
}
/// Set an internal character.
pub fn set_internal_split(&mut self, s: Option<char>) -> &mut Self {
self.borders.intersection = s;
self
}
/// Set a vertical character.
pub fn set_vertical(&mut self, s: Option<char>) -> &mut Self {
self.borders.vertical = s;
self
}
/// Set a horizontal character.
pub fn set_horizontal(&mut self, s: Option<char>) -> &mut Self {
self.borders.horizontal = s;
self
}
/// Set a character for a top left corner.
pub fn set_top_left(&mut self, s: Option<char>) -> &mut Self {
self.borders.top_left = s;
self
}
/// Set a character for a top right corner.
pub fn set_top_right(&mut self, s: Option<char>) -> &mut Self {
self.borders.top_right = s;
self
}
/// Set a character for a bottom left corner.
pub fn set_bottom_left(&mut self, s: Option<char>) -> &mut Self {
self.borders.bottom_left = s;
self
}
/// Set a character for a bottom right corner.
pub fn set_bottom_right(&mut self, s: Option<char>) -> &mut Self {
self.borders.bottom_right = s;
self
}
/// Set horizontal border lines.
///
/// # Example
///
/// ```
/// use std::collections::HashMap;
/// use tabled::{style::{Style, Line, RawStyle}, TableIteratorExt};
///
/// let mut style = RawStyle::from(Style::re_structured_text());
///
/// let mut lines = HashMap::new();
/// lines.insert(1, Style::extended().get_horizontal());
/// style.set_horizontals(lines);
///
/// let table = (0..3)
/// .map(|i| ("Hello", i))
/// .table()
/// .with(style)
/// .to_string();
///
/// assert_eq!(
/// table,
/// concat!(
/// " ======= ===== \n",
/// " &str i32 \n",
/// "╠═══════╬═════╣\n",
/// " Hello 0 \n",
/// " Hello 1 \n",
/// " Hello 2 \n",
/// " ======= ===== ",
/// ),
/// )
/// ```
pub fn set_horizontals(&mut self, lines: HashMap<usize, Line>) -> &mut Self {
self.horizontals = lines;
self
}
/// Set vertical border lines.
///
/// # Example
///
/// ```
/// use std::collections::HashMap;
/// use tabled::{style::{Style, Line, RawStyle}, TableIteratorExt};
///
/// let mut style = RawStyle::from(Style::re_structured_text());
///
/// let mut lines = HashMap::new();
/// lines.insert(1, Style::extended().get_horizontal());
/// style.set_verticals(lines);
///
/// let table = (0..3)
/// .map(|i| ("Hello", i))
/// .table()
/// .with(style)
/// .to_string();
///
/// assert_eq!(
/// table,
/// concat!(
/// "=======╠=====\n",
/// " &str ═ i32 \n",
/// "======= =====\n",
/// " Hello ═ 0 \n",
/// " Hello ═ 1 \n",
/// " Hello ═ 2 \n",
/// "=======╣=====",
/// ),
/// )
/// ```
pub fn set_verticals(&mut self, lines: HashMap<usize, Line>) -> &mut Self {
self.verticals = lines;
self
}
/// Get a left char.
pub fn get_left(&self) -> Option<char> {
self.borders.vertical_left
}
/// Get a left intersection char.
pub fn get_left_intersection(&self) -> Option<char> {
self.borders.horizontal_left
}
/// Get a right char.
pub fn get_right(&self) -> Option<char> {
self.borders.vertical_right
}
/// Get a right intersection char.
pub fn get_right_intersection(&self) -> Option<char> {
self.borders.horizontal_right
}
/// Get a top char.
pub fn get_top(&self) -> Option<char> {
self.borders.top
}
/// Get a top left char.
pub fn get_top_left(&self) -> Option<char> {
self.borders.top_left
}
/// Get a top right char.
pub fn get_top_right(&self) -> Option<char> {
self.borders.top_right
}
/// Get a top intersection char.
pub fn get_top_intersection(&self) -> Option<char> {
self.borders.top_intersection
}
/// Get a bottom intersection char.
pub fn get_bottom(&self) -> Option<char> {
self.borders.bottom
}
/// Get a bottom intersection char.
pub fn get_bottom_left(&self) -> Option<char> {
self.borders.bottom_left
}
/// Get a bottom intersection char.
pub fn get_bottom_right(&self) -> Option<char> {
self.borders.bottom_right
}
/// Get a bottom intersection char.
pub fn get_bottom_intersection(&self) -> Option<char> {
self.borders.bottom_intersection
}
/// Returns an outer border of the style.
pub fn get_frame(&self) -> Border {
Border::new_raw(Some(papergrid::Border {
top: self.borders.top,
bottom: self.borders.bottom,
left: self.borders.vertical_left,
right: self.borders.vertical_right,
left_top_corner: self.borders.top_left,
right_top_corner: self.borders.top_right,
left_bottom_corner: self.borders.bottom_left,
right_bottom_corner: self.borders.bottom_right,
}))
}
/// Returns a [`RawStyle`] version which can set colors.
#[cfg_attr(docsrs, doc(cfg(feature = "color")))]
#[cfg(feature = "color")]
pub fn colored(self) -> crate::style::RawStyleColored {
crate::style::RawStyleColored::from(self)
}
}
impl From<Borders<char>> for RawStyle {
fn from(borders: Borders<char>) -> Self {
Self {
borders,
horizontals: HashMap::new(),
verticals: HashMap::new(),
}
}
}
impl<R> TableOption<R> for RawStyle
where
R: Records,
{
fn change(&mut self, table: &mut Table<R>) {
(&*self).change(table)
}
}
impl<R> TableOption<R> for &RawStyle
where
R: Records,
{
fn change(&mut self, table: &mut Table<R>) {
if table.is_empty() {
return;
}
let (count_rows, count_cols) = table.shape();
let cfg = table.get_config_mut();
cfg.clear_theme();
cfg.set_borders(self.borders.clone());
if count_rows > 1 {
for (&row, line) in &self.horizontals {
if line.is_empty() {
cfg.remove_horizontal_line(row);
} else {
cfg.set_horizontal_line(row, papergrid::HorizontalLine::from(*line));
}
}
}
if count_cols > 1 {
for (&col, line) in &self.verticals {
if line.is_empty() {
cfg.remove_vertical_line(col);
} else {
cfg.set_vertical_line(col, papergrid::VerticalLine::from(*line));
}
}
}
table.destroy_width_cache();
table.destroy_height_cache();
}
}
impl<T, B, L, R, H, V, HLines, VLines> From<Style<T, B, L, R, H, V, HLines, VLines>> for RawStyle
where
HLines: IntoIterator<Item = HorizontalLine>,
VLines: IntoIterator<Item = VerticalLine>,
{
fn from(style: Style<T, B, L, R, H, V, HLines, VLines>) -> Self {
let horizontals = style
.horizontals
.into_iter()
.flat_map(|hr| {
let index = hr.index;
hr.line.map(|line| (index, line))
})
.collect();
let verticals = style
.verticals
.into_iter()
.flat_map(|hr| {
let index = hr.index;
hr.line.map(|line| (index, line))
})
.collect();
Self {
borders: style.borders,
horizontals,
verticals,
}
}
}