tabled/settings/span/mod.rs
1//! This module contains a [`Span`] settings, it helps to
2//! make a cell take more space then it generally takes.
3//!
4//! # Example
5//!
6//! ```rust,no_run
7//! # use tabled::{Table, settings::{Style, Span, Modify, object::Columns}};
8//! # let data: Vec<&'static str> = Vec::new();
9//! let mut table = Table::new(&data);
10//! table.modify(Columns::one(0), Span::column(2));
11//! ```
12
13mod column;
14mod row;
15
16pub use column::ColumnSpan;
17pub use row::RowSpan;
18
19/// Span represent a horizontal/column span setting for any cell on a [`Table`].
20///
21/// ```
22/// use tabled::{settings::{Span, Modify}, Table};
23/// use tabled::assert::assert_table;
24///
25/// let data = [[1, 2, 3], [4, 5, 6]];
26///
27/// let mut table = Table::new(data);
28/// table.modify((0, 0), Span::row(2));
29/// table.modify((0, 1), Span::column(2));
30/// table.modify((2, 0), Span::column(1000));
31///
32/// assert_table!(
33/// table,
34/// "+---+---+---+"
35/// "| 0 | 1 |"
36/// "+ +---+---+"
37/// "| | 2 | 3 |"
38/// "+---+---+---+"
39/// "| 4 |"
40/// "+---+---+---+"
41/// );
42/// ```
43///
44/// [`Table`]: crate::Table
45#[derive(Debug)]
46pub struct Span;
47
48impl Span {
49 /// New constructs a horizontal/column [`Span`].
50 ///
51 /// Value can be:
52 /// * == 0 - which means spread the cell on the whole line
53 /// * == 1 - which is a default span so can be used for removal of spans
54 /// * > 1 - which means to spread a cell by given number of columns right
55 /// * < 0 - which means to spread a cell by given number of columns left
56 ///
57 /// # Example
58 ///
59 /// ```
60 /// use tabled::{settings::{Span, Modify}, Table};
61 ///
62 /// let data = [[1, 2, 3], [4, 5, 6]];
63 ///
64 /// let table = Table::new(data)
65 /// .modify((0, 0), Span::column(100))
66 /// .modify((1, 1), Span::column(2))
67 /// .modify((2, 1), Span::column(-1))
68 /// .to_string();
69 ///
70 /// assert_eq!(
71 /// table,
72 /// concat!(
73 /// "+---++---+\n",
74 /// "| 0 |\n",
75 /// "+---++---+\n",
76 /// "| 1 | 2 |\n",
77 /// "+---++---+\n",
78 /// "| 5 | 6 |\n",
79 /// "+---++---+",
80 /// )
81 /// )
82 /// ```
83 pub fn column(size: isize) -> ColumnSpan {
84 ColumnSpan::new(size)
85 }
86
87 /// New constructs a vertical/row [`Span`].
88 ///
89 /// Value can be:
90 /// * == 0 - which means spread the cell on the whole line
91 /// * == 1 - which is a default span so can be used for removal of spans
92 /// * > 1 - which means to spread a cell by given number of rows bottom
93 /// * < 0 - which means to spread a cell by given number of rows top
94 ///
95 /// # Example
96 ///
97 /// ```
98 /// use tabled::{settings::{Span, Modify}, Table};
99 ///
100 /// let data = [[1, 2, 3], [4, 5, 6]];
101 ///
102 /// let table = Table::new(data)
103 /// .modify((0, 0), Span::row(100))
104 /// .modify((1, 1), Span::row(2))
105 /// .modify((2, 2), Span::row(-1))
106 /// .to_string();
107 ///
108 /// assert_eq!(
109 /// table,
110 /// concat!(
111 /// "+---+---+---+\n",
112 /// "| 0 | 1 | 2 |\n",
113 /// "+ +---+---+\n",
114 /// "+ + 2 + 6 +\n",
115 /// "+---+---+---+",
116 /// )
117 /// )
118 /// ```
119 pub fn row(size: isize) -> RowSpan {
120 RowSpan::new(size)
121 }
122}