tabled/features/merge.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
//! The module contains a set of methods to merge cells together via [`Span`]s.
//!
//! [`Span`]: crate::Span
use crate::{papergrid::records::Records, Table, TableOption};
/// Merge to combine duplicates together, using [`Span`].
///
/// [`Span`]: crate::Span
#[derive(Debug)]
pub struct Merge;
impl Merge {
/// Vertical merge.
pub fn vertical() -> MergeDuplicatesVertical {
MergeDuplicatesVertical
}
/// Horizontal merge.
pub fn horizontal() -> MergeDuplicatesHorizontal {
MergeDuplicatesHorizontal
}
}
/// A modificator for [`Table`] which looks up for duplicates in columns and
/// in case of duplicate merges the cells together using [`Span`].
///
/// [`Table`]: crate::Table
/// [`Span`]: crate::Span
#[derive(Debug)]
pub struct MergeDuplicatesVertical;
impl<R> TableOption<R> for MergeDuplicatesVertical
where
R: Records,
{
fn change(&mut self, table: &mut Table<R>) {
if table.is_empty() {
return;
}
for column in 0..table.get_records().count_columns() {
let mut repeat_length = 0;
let mut repeat_value = String::new();
let mut repeat_is_set = false;
let mut last_is_row_span = false;
for row in (0..table.get_records().count_rows()).rev() {
if last_is_row_span {
last_is_row_span = false;
continue;
}
// we need to mitigate messing existing spans
let is_cell_visible = table
.get_config()
.is_cell_visible((row, column), table.shape());
let is_row_span_cell = table
.get_config()
.get_column_span((row, column), table.shape())
.is_some();
if !repeat_is_set {
if !is_cell_visible {
continue;
}
if is_row_span_cell {
continue;
}
repeat_length = 1;
repeat_value = table.get_records().get_text((row, column)).to_owned();
repeat_is_set = true;
continue;
}
if is_row_span_cell {
repeat_is_set = false;
last_is_row_span = true;
continue;
}
if !is_cell_visible {
repeat_is_set = false;
continue;
}
let text = table.get_records().get_text((row, column));
let is_duplicate = text == repeat_value;
if is_duplicate {
repeat_length += 1;
continue;
}
if repeat_length > 1 {
table
.get_config_mut()
.set_row_span((row + 1, column), repeat_length);
}
repeat_length = 1;
repeat_value = table.get_records().get_text((row, column)).to_owned();
}
if repeat_length > 1 {
table
.get_config_mut()
.set_row_span((0, column), repeat_length);
}
}
table.destroy_width_cache();
table.destroy_height_cache();
}
}
/// A modificator for [`Table`] which looks up for duplicates in rows and
/// in case of duplicate merges the cells together using [`Span`].
///
/// [`Table`]: crate::Table
/// [`Span`]: crate::Span
#[derive(Debug)]
pub struct MergeDuplicatesHorizontal;
impl<R> TableOption<R> for MergeDuplicatesHorizontal
where
R: Records,
{
fn change(&mut self, table: &mut Table<R>) {
if table.is_empty() {
return;
}
for row in 0..table.get_records().count_rows() {
let mut repeat_length = 0;
let mut repeat_value = String::new();
let mut repeat_is_set = false;
let mut last_is_col_span = false;
for column in (0..table.get_records().count_columns()).rev() {
if last_is_col_span {
last_is_col_span = false;
continue;
}
// we need to mitigate messing existing spans
let is_cell_visible = table
.get_config()
.is_cell_visible((row, column), table.shape());
let is_col_span_cell = table
.get_config()
.get_row_span((row, column), table.shape())
.is_some();
if !repeat_is_set {
if !is_cell_visible {
continue;
}
if is_col_span_cell {
continue;
}
repeat_length = 1;
repeat_value = table.get_records().get_text((row, column)).to_owned();
repeat_is_set = true;
continue;
}
if is_col_span_cell {
repeat_is_set = false;
last_is_col_span = true;
continue;
}
if !is_cell_visible {
repeat_is_set = false;
continue;
}
let text = table.get_records().get_text((row, column));
let is_duplicate = text == repeat_value;
if is_duplicate {
repeat_length += 1;
continue;
}
if repeat_length > 1 {
table
.get_config_mut()
.set_column_span((row, column + 1), repeat_length);
}
repeat_length = 1;
repeat_value = table.get_records().get_text((row, column)).to_owned();
}
if repeat_length > 1 {
table
.get_config_mut()
.set_column_span((row, 0), repeat_length);
}
}
table.destroy_width_cache();
table.destroy_height_cache();
}
}