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
use crate::{bounds_to_usize, TableOption};
use papergrid::Grid;
use std::ops::RangeBounds;

/// Disable represent a disable setting for a [`Table`](./struct.Table.html)
///
/// ```rust,no_run
///   # use tabled::{Disable, Table};
///   # let data: Vec<&'static str> = Vec::new();
///     let table = Table::new(&data).with(Disable::Row(..1));
/// ```
///
#[derive(Debug)]
pub enum Disable<R: RangeBounds<usize>> {
    /// Columns of the grid. Range is used to locate columns.
    Column(R),
    /// Rows of the grid. Range is used to locate rows.
    Row(R),
}

impl<R: RangeBounds<usize>> TableOption for Disable<R> {
    fn change(&self, grid: &mut Grid) {
        match self {
            Self::Column(range) => {
                let (x, y) =
                    bounds_to_usize(range.start_bound(), range.end_bound(), grid.count_columns());
                for (shifted, i) in (x..y).enumerate() {
                    grid.remove_column(i - shifted);
                }
            }
            Self::Row(range) => {
                let (x, y) =
                    bounds_to_usize(range.start_bound(), range.end_bound(), grid.count_rows());

                // It's kindof a bad design that we must controll shift.
                // It basically unveils an implementation...
                for (shifted, i) in (x..y).enumerate() {
                    grid.remove_row(i - shifted);
                }
            }
        }
    }
}