| 1 | use std::{ |
| 2 | borrow::Cow, |
| 3 | cmp::{max, min}, |
| 4 | num::NonZeroU16, |
| 5 | }; |
| 6 | |
| 7 | /// A bound on the width of a column. |
| 8 | #[derive(Clone, Copy, Debug)] |
| 9 | pub enum ColumnWidthBounds { |
| 10 | /// A width of this type is as long as `desired`, but can otherwise shrink |
| 11 | /// and grow up to a point. |
| 12 | Soft { |
| 13 | /// The desired, calculated width. Take this if possible as the base |
| 14 | /// starting width. |
| 15 | desired: u16, |
| 16 | |
| 17 | /// The max width, as a percentage of the total width available. If |
| 18 | /// [`None`], then it can grow as desired. |
| 19 | max_percentage: Option<f32>, |
| 20 | }, |
| 21 | |
| 22 | /// A width of this type is either as long as specified, or does not appear |
| 23 | /// at all. |
| 24 | Hard(u16), |
| 25 | |
| 26 | /// A width of this type always resizes to the column header's text width. |
| 27 | FollowHeader, |
| 28 | } |
| 29 | |
| 30 | pub trait ColumnHeader { |
| 31 | /// The "text" version of the column header. |
| 32 | fn text(&self) -> Cow<'static, str>; |
| 33 | |
| 34 | /// The version displayed when drawing the table. Defaults to |
| 35 | /// [`ColumnHeader::text`]. |
| 36 | #[inline(always)] |
| 37 | fn header(&self) -> Cow<'static, str> { |
| 38 | self.text() |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl ColumnHeader for &'static str { |
| 43 | fn text(&self) -> Cow<'static, str> { |
| 44 | Cow::Borrowed(self) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl ColumnHeader for String { |
| 49 | fn text(&self) -> Cow<'static, str> { |
| 50 | Cow::Owned(self.clone()) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub trait DataTableColumn<H: ColumnHeader> { |
| 55 | fn inner(&self) -> &H; |
| 56 | |
| 57 | fn inner_mut(&mut self) -> &mut H; |
| 58 | |
| 59 | fn bounds(&self) -> ColumnWidthBounds; |
| 60 | |
| 61 | fn bounds_mut(&mut self) -> &mut ColumnWidthBounds; |
| 62 | |
| 63 | fn is_hidden(&self) -> bool; |
| 64 | |
| 65 | fn set_hidden(&mut self, hidden: bool); |
| 66 | |
| 67 | /// The actually displayed "header". |
| 68 | fn header(&self) -> Cow<'static, str>; |
| 69 | |
| 70 | /// The header length, along with any required additional lengths for things |
| 71 | /// like arrows. Defaults to getting the length of |
| 72 | /// [`DataTableColumn::header`]. |
| 73 | fn header_len(&self) -> usize { |
| 74 | self.header().len() |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | #[derive(Clone, Debug)] |
| 79 | pub struct Column<H> { |
| 80 | /// The inner column header. |
| 81 | inner: H, |
| 82 | |
| 83 | /// A restriction on this column's width. |
| 84 | bounds: ColumnWidthBounds, |
| 85 | |
| 86 | /// Marks that this column is currently "hidden", and should *always* be |
| 87 | /// skipped. |
| 88 | is_hidden: bool, |
| 89 | } |
| 90 | |
| 91 | impl<H: ColumnHeader> DataTableColumn<H> for Column<H> { |
| 92 | #[inline] |
| 93 | fn inner(&self) -> &H { |
| 94 | &self.inner |
| 95 | } |
| 96 | |
| 97 | #[inline] |
| 98 | fn inner_mut(&mut self) -> &mut H { |
| 99 | &mut self.inner |
| 100 | } |
| 101 | |
| 102 | #[inline] |
| 103 | fn bounds(&self) -> ColumnWidthBounds { |
| 104 | self.bounds |
| 105 | } |
| 106 | |
| 107 | #[inline] |
| 108 | fn bounds_mut(&mut self) -> &mut ColumnWidthBounds { |
| 109 | &mut self.bounds |
| 110 | } |
| 111 | |
| 112 | #[inline] |
| 113 | fn is_hidden(&self) -> bool { |
| 114 | self.is_hidden |
| 115 | } |
| 116 | |
| 117 | #[inline] |
| 118 | fn set_hidden(&mut self, hidden: bool) { |
| 119 | self.is_hidden = hidden; |
| 120 | } |
| 121 | |
| 122 | fn header(&self) -> Cow<'static, str> { |
| 123 | self.inner.header() |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | impl<H: ColumnHeader> Column<H> { |
| 128 | pub const fn new(inner: H) -> Self { |
| 129 | Self { |
| 130 | inner, |
| 131 | bounds: ColumnWidthBounds::FollowHeader, |
| 132 | is_hidden: false, |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | pub const fn hard(inner: H, width: u16) -> Self { |
| 137 | Self { |
| 138 | inner, |
| 139 | bounds: ColumnWidthBounds::Hard(width), |
| 140 | is_hidden: false, |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | pub const fn soft(inner: H, max_percentage: Option<f32>) -> Self { |
| 145 | Self { |
| 146 | inner, |
| 147 | bounds: ColumnWidthBounds::Soft { |
| 148 | desired: 0, |
| 149 | max_percentage, |
| 150 | }, |
| 151 | is_hidden: false, |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | pub trait CalculateColumnWidths<H> { |
| 157 | /// Calculates widths for the columns of this table, given the current width |
| 158 | /// when called. |
| 159 | /// |
| 160 | /// * `total_width` is the total width on the canvas that the columns can |
| 161 | /// try and work with. |
| 162 | /// * `left_to_right` is whether to size from left-to-right (`true`) or |
| 163 | /// right-to-left (`false`). |
| 164 | fn calculate_column_widths(&self, total_width: u16, left_to_right: bool) -> Vec<NonZeroU16>; |
| 165 | } |
| 166 | |
| 167 | impl<H, C> CalculateColumnWidths<H> for [C] |
| 168 | where |
| 169 | H: ColumnHeader, |
| 170 | C: DataTableColumn<H>, |
| 171 | { |
| 172 | fn calculate_column_widths(&self, total_width: u16, left_to_right: bool) -> Vec<NonZeroU16> { |
| 173 | use itertools::Either; |
| 174 | |
| 175 | const COLUMN_SPACING: u16 = 1; |
| 176 | |
| 177 | #[inline] |
| 178 | fn stop_allocating_space(desired: u16, available: u16) -> bool { |
| 179 | desired > available || desired == 0 |
| 180 | } |
| 181 | |
| 182 | let mut total_width_left = total_width; |
| 183 | let mut calculated_widths = vec![]; |
| 184 | let columns = if left_to_right { |
| 185 | Either::Left(self.iter()) |
| 186 | } else { |
| 187 | Either::Right(self.iter().rev()) |
| 188 | }; |
| 189 | |
| 190 | for column in columns { |
| 191 | if column.is_hidden() { |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | let new_width = match &column.bounds() { |
| 196 | ColumnWidthBounds::Soft { |
| 197 | desired, |
| 198 | max_percentage, |
| 199 | } => { |
| 200 | let min_width = column.header_len() as u16; |
| 201 | if min_width > total_width_left { |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | let soft_limit = max( |
| 206 | if let Some(max_percentage) = max_percentage { |
| 207 | ((*max_percentage * f32::from(total_width)).ceil()) as u16 |
| 208 | } else { |
| 209 | *desired |
| 210 | }, |
| 211 | min_width, |
| 212 | ); |
| 213 | |
| 214 | min(min(soft_limit, *desired), total_width_left) |
| 215 | } |
| 216 | ColumnWidthBounds::Hard(width) => *width, |
| 217 | ColumnWidthBounds::FollowHeader => column.header_len() as u16, |
| 218 | }; |
| 219 | |
| 220 | if stop_allocating_space(new_width, total_width_left) { |
| 221 | break; |
| 222 | } else { |
| 223 | total_width_left = total_width_left.saturating_sub(new_width + COLUMN_SPACING); |
| 224 | |
| 225 | // SAFETY: This is safe as we call `stop_allocating_space` which checks that |
| 226 | // the value pushed is greater than zero. |
| 227 | unsafe { |
| 228 | calculated_widths.push(NonZeroU16::new_unchecked(new_width)); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if !calculated_widths.is_empty() { |
| 234 | if !left_to_right { |
| 235 | calculated_widths.reverse(); |
| 236 | } |
| 237 | |
| 238 | // Redistribute remaining space. |
| 239 | let mut num_dist = calculated_widths.len() as u16; |
| 240 | let amount_per_slot = total_width_left / num_dist; // Safe from DBZ by above empty check. |
| 241 | total_width_left %= num_dist; |
| 242 | |
| 243 | for width in calculated_widths.iter_mut() { |
| 244 | if num_dist == 0 { |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | if total_width_left > 0 { |
| 249 | *width = width.saturating_add(amount_per_slot + 1); |
| 250 | total_width_left -= 1; |
| 251 | } else { |
| 252 | *width = width.saturating_add(amount_per_slot); |
| 253 | } |
| 254 | |
| 255 | num_dist -= 1; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | calculated_widths |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | #[cfg(test)] |
| 264 | mod test { |
| 265 | use super::*; |
| 266 | |
| 267 | struct TestInnerColumn; |
| 268 | |
| 269 | impl ColumnHeader for TestInnerColumn { |
| 270 | fn text(&self) -> Cow<'static, str> { |
| 271 | "text".into() |
| 272 | } |
| 273 | |
| 274 | fn header(&self) -> Cow<'static, str> { |
| 275 | "header(h)".into() |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /// Ensure that the [`DataTableColumn`] implementation for [`Column`] calls the right method. |
| 280 | /// Yes, this is a somewhat meaningless test but it may catch a regression if this happens again in the future |
| 281 | /// during a refactor. |
| 282 | /// |
| 283 | /// See <https://github.com/ClementTsang/bottom/issues/2159> for details of the issue. |
| 284 | #[test] |
| 285 | fn column_header_impl_uses_right_function() { |
| 286 | let column = Column::new(TestInnerColumn); |
| 287 | assert_eq!(column.header(), TestInnerColumn.header()); |
| 288 | } |
| 289 | } |