tabled/features/width/
truncate.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! This module contains [`Truncate`] structure, used to decrease width of a [`Table`]s or a cell on a [`Table`] by truncating the width.

use std::{borrow::Cow, marker::PhantomData};

use papergrid::{
    records::{empty::EmptyRecords, Records, RecordsMut},
    util::cut_str,
    width::{CfgWidthFunction, WidthFunc},
    Entity, GridConfig,
};

use crate::{
    peaker::{Peaker, PriorityNone},
    width::{count_borders, get_table_widths, get_table_widths_with_total, Measurment},
    CellOption, Table, TableOption, Width,
};

/// Truncate cut the string to a given width if its length exceeds it.
/// Otherwise keeps the content of a cell untouched.
///
/// The function is color aware if a `color` feature is on.
///
/// Be aware that it doesn't consider padding.
/// So if you want to set a exact width you might need to use [`Padding`] to set it to 0.
///    
/// ## Example
///
/// ```
/// use tabled::{object::Segment, Width, Modify, Table};
///
/// let table = Table::new(&["Hello World!"])
///     .with(Modify::new(Segment::all()).with(Width::truncate(3)));
/// ```
///
/// [`Padding`]: crate::Padding
#[derive(Debug)]
pub struct Truncate<'a, W = usize, P = PriorityNone> {
    width: W,
    suffix: Option<TruncateSuffix<'a>>,
    _priority: PhantomData<P>,
}

#[derive(Debug)]
struct TruncateSuffix<'a> {
    text: Cow<'a, str>,
    limit: SuffixLimit,
    #[cfg(feature = "color")]
    try_color: bool,
}

impl Default for TruncateSuffix<'_> {
    fn default() -> Self {
        Self {
            text: Cow::default(),
            limit: SuffixLimit::Cut,
            #[cfg(feature = "color")]
            try_color: false,
        }
    }
}

/// A suffix limit settings.
#[derive(Debug, Clone, Copy)]
pub enum SuffixLimit {
    /// Cut the suffix.
    Cut,
    /// Don't show the suffix.
    Ignore,
    /// Use a string with n chars instead.
    Replace(char),
}

impl<W> Truncate<'static, W>
where
    W: Measurment<Width>,
{
    /// Creates a [`Truncate`] object
    pub fn new(width: W) -> Truncate<'static, W> {
        Self {
            width,
            suffix: None,
            _priority: PhantomData::default(),
        }
    }
}

impl<'a, W, P> Truncate<'a, W, P> {
    /// Sets a suffix which will be appended to a resultant string.
    ///
    /// The suffix is used in 3 circamstances:
    ///     1. If original string is *bigger* than the suffix.
    ///        We cut more of the original string and append the suffix.
    ///     2. If suffix is bigger than the original string.
    ///        We cut the suffix to fit in the width by default.
    ///        But you can peak the behaviour by using [`Truncate::suffix_limit`]
    pub fn suffix<S: Into<Cow<'a, str>>>(self, suffix: S) -> Truncate<'a, W, P> {
        let mut suff = self.suffix.unwrap_or_default();
        suff.text = suffix.into();

        Truncate {
            width: self.width,
            suffix: Some(suff),
            _priority: PhantomData::default(),
        }
    }

    /// Sets a suffix limit, which is used when the suffix is too big to be used.
    pub fn suffix_limit(self, limit: SuffixLimit) -> Truncate<'a, W, P> {
        let mut suff = self.suffix.unwrap_or_default();
        suff.limit = limit;

        Truncate {
            width: self.width,
            suffix: Some(suff),
            _priority: PhantomData::default(),
        }
    }

    #[cfg(feature = "color")]
    /// Sets a optional logic to try to colorize a suffix.
    pub fn suffix_try_color(self, color: bool) -> Truncate<'a, W, P> {
        let mut suff = self.suffix.unwrap_or_default();
        suff.try_color = color;

        Truncate {
            width: self.width,
            suffix: Some(suff),
            _priority: PhantomData::default(),
        }
    }
}

impl<'a, W, P> Truncate<'a, W, P> {
    /// Priority defines the logic by which a truncate will be applied when is done for the whole table.
    ///
    /// - [`PriorityNone`] which cuts the columns one after another.
    /// - [`PriorityMax`] cuts the biggest columns first.
    /// - [`PriorityMin`] cuts the lowest columns first.
    ///
    /// [`PriorityMax`]: crate::peaker::PriorityMax
    /// [`PriorityMin`]: crate::peaker::PriorityMin
    pub fn priority<PP: Peaker>(self) -> Truncate<'a, W, PP> {
        Truncate {
            width: self.width,
            suffix: self.suffix,
            _priority: PhantomData::default(),
        }
    }
}

impl<W, P, R> CellOption<R> for Truncate<'_, W, P>
where
    W: Measurment<Width>,
    R: Records + RecordsMut<String>,
{
    fn change_cell(&mut self, table: &mut Table<R>, entity: Entity) {
        let width_ctrl = CfgWidthFunction::from_cfg(table.get_config());
        let set_width = self.width.measure(table.get_records(), table.get_config());

        let mut width = set_width;
        let suffix = match self.suffix.as_ref() {
            Some(suffix) => {
                let suffix_length = width_ctrl.width(&suffix.text);
                if width > suffix_length {
                    width -= suffix_length;
                    Cow::Borrowed(suffix.text.as_ref())
                } else {
                    match suffix.limit {
                        SuffixLimit::Ignore => Cow::Borrowed(""),
                        SuffixLimit::Cut => {
                            width = 0;
                            cut_str(&suffix.text, set_width)
                        }
                        SuffixLimit::Replace(c) => {
                            width = 0;
                            Cow::Owned(std::iter::repeat(c).take(set_width).collect())
                        }
                    }
                }
            }
            None => Cow::Borrowed(""),
        };

        let (count_rows, count_cols) = table.shape();
        for pos in entity.iter(count_rows, count_cols) {
            let cell_width = table.get_records().get_width(pos, &width_ctrl);
            if set_width >= cell_width {
                continue;
            }

            let suffix_color_try_keeping;
            #[cfg(not(feature = "color"))]
            {
                suffix_color_try_keeping = false;
            }
            #[cfg(feature = "color")]
            {
                suffix_color_try_keeping = self.suffix.as_ref().map_or(false, |s| s.try_color);
            }

            let records = table.get_records();
            let text = records.get_text(pos);
            // todo: Think about it.
            //       We could eliminate this allcation if we would be allowed to cut '\t' with unknown characters.
            //       Currently we don't do that.
            let text = papergrid::util::replace_tab(text, table.get_config().get_tab_width());
            let text = truncate_text(&text, width, set_width, &suffix, suffix_color_try_keeping)
                .into_owned();

            let records = table.get_records_mut();
            records.set(pos, text, &width_ctrl);
        }

        table.destroy_width_cache();
    }
}

impl<W, P, R> TableOption<R> for Truncate<'_, W, P>
where
    W: Measurment<Width>,
    P: Peaker,
    R: Records + RecordsMut<String>,
{
    fn change(&mut self, table: &mut Table<R>) {
        if table.is_empty() {
            return;
        }

        let width = self.width.measure(table.get_records(), table.get_config());
        let (widths, total_width) =
            get_table_widths_with_total(table.get_records(), table.get_config());
        if total_width <= width {
            return;
        }

        let suffix = self.suffix.as_ref().map(|s| TruncateSuffix {
            limit: s.limit,
            text: Cow::Borrowed(&s.text),
            #[cfg(feature = "color")]
            try_color: s.try_color,
        });

        truncate_total_width(table, widths, total_width, width, suffix, P::create());
    }
}

fn truncate_text<'a>(
    content: &'a str,
    width: usize,
    original_width: usize,
    suffix: &'a str,
    _suffix_color_try_keeping: bool,
) -> Cow<'a, str> {
    if width == 0 {
        if original_width == 0 {
            Cow::Borrowed("")
        } else {
            Cow::Borrowed(suffix)
        }
    } else {
        let content = cut_str(content, width);

        if suffix.is_empty() {
            content
        } else {
            #[cfg(feature = "color")]
            {
                if _suffix_color_try_keeping {
                    if let Some(clr) = ansi_str::get_blocks(&content).last() {
                        if clr.has_ansi() {
                            Cow::Owned(format!("{}{}{}{}", content, clr.start(), suffix, clr.end()))
                        } else {
                            let mut content = content.into_owned();
                            content.push_str(suffix);
                            Cow::Owned(content)
                        }
                    } else {
                        let mut content = content.into_owned();
                        content.push_str(suffix);
                        Cow::Owned(content)
                    }
                } else {
                    let mut content = content.into_owned();
                    content.push_str(suffix);
                    Cow::Owned(content)
                }
            }

            #[cfg(not(feature = "color"))]
            {
                let mut content = content.into_owned();
                content.push_str(suffix);
                Cow::Owned(content)
            }
        }
    }
}

pub(crate) fn get_decrease_cell_list(
    cfg: &GridConfig,
    widths: &[usize],
    min_widths: &[usize],
    (count_rows, count_cols): (usize, usize),
) -> Vec<((usize, usize), usize)> {
    let mut points = Vec::new();
    (0..count_cols).for_each(|col| {
        (0..count_rows)
            .filter(|&row| cfg.is_cell_visible((row, col), (count_rows, count_cols)))
            .for_each(|row| {
                let (width, width_min) =
                    match cfg.get_column_span((row, col), (count_rows, count_cols)) {
                        Some(span) => {
                            let width = (col..col + span).map(|i| widths[i]).sum::<usize>();
                            let min_width = (col..col + span).map(|i| min_widths[i]).sum::<usize>();
                            let count_borders = count_borders(cfg, col, col + span, count_cols);
                            (width + count_borders, min_width + count_borders)
                        }
                        None => (widths[col], min_widths[col]),
                    };

                if width >= width_min {
                    let padding = cfg.get_padding((row, col).into());
                    let width = width.saturating_sub(padding.left.size + padding.right.size);

                    points.push(((row, col), width));
                }
            });
    });

    points
}

pub(crate) fn decrease_widths<F>(
    widths: &mut [usize],
    min_widths: &[usize],
    total_width: usize,
    mut width: usize,
    mut peeaker: F,
) where
    F: Peaker,
{
    let mut empty_list = 0;
    for col in 0..widths.len() {
        if widths[col] == 0 || widths[col] <= min_widths[col] {
            empty_list += 1;
        }
    }

    while width != total_width {
        if empty_list == widths.len() {
            break;
        }

        let col = match peeaker.peak(min_widths, widths) {
            Some(col) => col,
            None => break,
        };

        if widths[col] == 0 || widths[col] <= min_widths[col] {
            continue;
        }

        widths[col] -= 1;

        if widths[col] == 0 || widths[col] <= min_widths[col] {
            empty_list += 1;
        }

        width += 1;
    }
}

fn truncate_total_width<P, R>(
    table: &mut Table<R>,
    mut widths: Vec<usize>,
    widths_total: usize,
    width: usize,
    suffix: Option<TruncateSuffix<'_>>,
    priority: P,
) where
    P: Peaker,
    R: Records + RecordsMut<String>,
{
    let (count_rows, count_cols) = table.shape();
    let cfg = table.get_config();
    let min_widths = get_table_widths(EmptyRecords::new(count_rows, count_cols), cfg);

    decrease_widths(&mut widths, &min_widths, widths_total, width, priority);

    let points = get_decrease_cell_list(cfg, &widths, &min_widths, (count_rows, count_cols));

    let mut truncate = Truncate::new(0);
    truncate.suffix = suffix;
    for ((row, col), width) in points {
        truncate.width = width;
        truncate.change_cell(table, (row, col).into());
    }

    table.destroy_width_cache();
    table.destroy_height_cache();
    table.cache_width(widths);
}

#[cfg(feature = "color")]
#[cfg(test)]
mod tests {
    use owo_colors::{colors::Yellow, OwoColorize};
    use papergrid::util::cut_str;

    #[test]
    fn test_color_strip() {
        let s = "Collored string"
            .fg::<Yellow>()
            .on_truecolor(12, 200, 100)
            .blink()
            .to_string();
        assert_eq!(
            cut_str(&s, 1),
            "\u{1b}[5m\u{1b}[48;2;12;200;100m\u{1b}[33mC\u{1b}[25m\u{1b}[39m\u{1b}[49m"
        )
    }
}