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
use {
    crate::InsufficientWidthError,
    std::{
        cmp,
    },
};

/// A fitter, accumulating data about the table which must fit into
/// a given width, then computing the best column widths.
pub struct TblFit {
    cols: Vec<ColData>,
    available_sum_width: usize,
}

/// Information observed with calls to see_cell
#[derive(Debug, Clone, Copy)]
struct ColData {
    sum_widths: usize,
    width: usize,
    count: usize,
}

impl Default for ColData {
    fn default() -> Self {
        Self {
            sum_widths: 0,
            width: 3, // minimal col width
            count: 0, // number of observations on which we summed
        }
    }
}

impl ColData {
    const fn avg_width(self) -> usize {
        div_ceil(self.sum_widths, self.count)
    }
    pub fn see_cell(&mut self, cell_width: usize) {
        self.sum_widths += cell_width;
        self.width = self.width.max(cell_width);
        self.count += 1;
    }
}

/// Result of the fitting operation (always a success)
pub struct TblFitResult {
    /// whether some cells will have to be wrapped
    pub reduced: bool,

    /// the widths of all columns, so that they're guaranteed to fit
    /// into the available_width (taking the borders into account)
    pub col_widths: Vec<usize>,
}

impl TblFit {
    /// Build a new fitter, or return an error if the width isn't enough
    /// for the given number of columns.
    ///
    /// available_width: total available width, including external borders
    pub fn new(cols_count: usize, available_width: usize) -> Result<Self, InsufficientWidthError> {
        if available_width < cols_count*4 + 1 {
            return Err(InsufficientWidthError { available_width });
        }
        let cols = vec![ColData::default(); cols_count];
        let available_sum_width = available_width - 1 - cols_count;
        Ok(Self {
            cols,
            available_sum_width,
        })
    }
    pub fn see_cell(&mut self, col_idx: usize, cell_width: usize) {
        if let Some(col) = self.cols.get_mut(col_idx) {
            col.see_cell(cell_width);
        }
    }
    /// compute the fitting
    pub fn fit(&self) -> TblFitResult {
        let sum_widths: usize = self.cols.iter().map(|c| c.width).sum();
        if sum_widths <= self.available_sum_width {
            return TblFitResult {
                reduced: false,
                col_widths: self.cols.iter().map(|c| c.width).collect(),
            };
        }
        if self.cols.is_empty() {
            return TblFitResult {
                reduced: false,
                col_widths: Vec::new(),
            };
        }
        if self.cols.len() == 1 {
            return TblFitResult {
                reduced: false,
                col_widths: vec![self.available_sum_width],
            };
        }
        #[derive(Debug)]
        struct ColFit {
            idx: usize,       // index of the col
            std_width: usize, // col internal max width
            avg_width: usize, // col internal average width
            width: usize, // final width
        }
        let mut fits: Vec<ColFit> = self.cols.iter()
            .enumerate()
            .map(|(idx, c)| ColFit {
                idx,
                std_width: c.width,
                avg_width: c.avg_width(),
                width: c.width,
            })
            .collect();

        if self.available_sum_width >= sum_widths {
            return TblFitResult {
                reduced: false,
                col_widths: self.cols.iter().map(|c| c.width).collect(),
            };
        }

        let mut excess = sum_widths - self.available_sum_width;
        fits.sort_by_key(|c| cmp::Reverse(c.width));

        // Step 1
        // We do a first reduction, if possible, on columns wider
        // than 5, and trying to keep above the average width
        let potential_uncut_gain_1 = fits.iter()
            .filter(|c| c.width > 4 && c.width > c.avg_width + 1)
            .map(|c| (c.width - c.avg_width).min(4))
            .sum::<usize>();
        let potential_cut_gain_1 = potential_uncut_gain_1
            .min(excess);
        if potential_cut_gain_1 > 0 {
            for c in fits.iter_mut() {
                if c.std_width > 4 && c.std_width > c.avg_width {
                    let gain_1 = div_ceil((c.width - c.avg_width) * potential_cut_gain_1, potential_uncut_gain_1);
                    let gain_1 = gain_1.min(excess).min(c.width - 4);
                    c.width -= gain_1;
                    excess -= gain_1;
                    if excess == 0 {
                        break;
                    }
                }
            }
        }

        // Step 2
        // We remove excess proportionnally
        if excess > 0 {
            let potential_total_gain_2 = fits.iter()
                .map(|c| c.width - 3)
                .sum::<usize>()
                .min(excess);
            let excess_before_2 = excess;
            for c in fits.iter_mut() {
                let gain_2 = div_ceil((c.width - 3) * excess_before_2, potential_total_gain_2);
                let gain_2 = gain_2.min(excess).min(c.width - 3);
                c.width -= gain_2;
                excess -= gain_2;
                if excess == 0 {
                    break;
                }
            }
        }

        // it should be OK now

        fits.sort_by_key(|c| c.idx);
        TblFitResult {
            reduced: true,
            col_widths: fits.iter().map(|c| c.width).collect(),
        }
    }
}

/// divide, rounding to the top
const fn div_ceil(q: usize, r: usize) -> usize {
    let mut res =  q / r;
    if q%r != 0 {
        res += 1;
    }
    res
}