on master help sign in
Use upstream implementation of table column calculation
7a76c6cGokul Soumya
helix-term/src/ui/picker.rs+1−4
@@ -15,10 +15,7 @@ use tui::{
15use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
15use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
16use tui::widgets::Widget;
16use tui::widgets::Widget;
17
17
18−use std::{
19− cmp::{self, Ordering},
18+use std::cmp::{self, Ordering};
20− time::Instant,
21−};
22use std::{collections::HashMap, io::Read, path::PathBuf};
19use std::{collections::HashMap, io::Read, path::PathBuf};
23
20
24use crate::ui::{Prompt, PromptEvent};
21use crate::ui::{Prompt, PromptEvent};
helix-tui/src/widgets/table.rs+19−62
@@ -4,14 +4,8 @@ use crate::{
4 text::Text,
4 text::Text,
5 widgets::{Block, Widget},
5 widgets::{Block, Widget},
6};
6};
7−use cassowary::{
8− strength::{MEDIUM, REQUIRED, WEAK},
9− WeightedRelation::*,
10− {Expression, Solver},
11−};
12use helix_core::unicode::width::UnicodeWidthStr;
7use helix_core::unicode::width::UnicodeWidthStr;
13use helix_view::graphics::{Rect, Style};
8use helix_view::graphics::{Rect, Style};
14−use std::collections::HashMap;
15
9
16/// A [`Cell`] contains the [`Text`] to be displayed in a [`Row`] of a [`Table`].
10/// A [`Cell`] contains the [`Text`] to be displayed in a [`Row`] of a [`Table`].
17///
11///
@@ -268,69 +262,32 @@ impl<'a> Table<'a> {
268 }
262 }
269
263
270 fn get_columns_widths(&self, max_width: u16, has_selection: bool) -> Vec<u16> {
264 fn get_columns_widths(&self, max_width: u16, has_selection: bool) -> Vec<u16> {
271− let mut solver = Solver::new();
265+ let mut constraints = Vec::with_capacity(self.widths.len() * 2 + 1);
272− let mut var_indices = HashMap::new();
273− let mut ccs = Vec::new();
274− let mut variables = Vec::new();
275− for i in 0..self.widths.len() {
276− let var = cassowary::Variable::new();
277− variables.push(var);
278− var_indices.insert(var, i);
279− }
280− let spacing_width = (variables.len() as u16).saturating_sub(1) * self.column_spacing;
281− let mut available_width = max_width.saturating_sub(spacing_width);
282 if has_selection {
266 if has_selection {
283 let highlight_symbol_width =
267 let highlight_symbol_width =
284 self.highlight_symbol.map(|s| s.width() as u16).unwrap_or(0);
268 self.highlight_symbol.map(|s| s.width() as u16).unwrap_or(0);
285− available_width = available_width.saturating_sub(highlight_symbol_width);
269+ constraints.push(Constraint::Length(highlight_symbol_width));
286 }
270 }
287− for (i, constraint) in self.widths.iter().enumerate() {
271+ for constraint in self.widths {
288− ccs.push(variables[i] | GE(WEAK) | 0.);
272+ constraints.push(*constraint);
289− ccs.push(match *constraint {
273+ constraints.push(Constraint::Length(self.column_spacing));
290− Constraint::Length(v) => variables[i] | EQ(MEDIUM) | f64::from(v),
291− Constraint::Percentage(v) => {
292− variables[i] | EQ(WEAK) | (f64::from(v * available_width) / 100.0)
293− }
294− Constraint::Ratio(n, d) => {
295− variables[i]
296− | EQ(WEAK)
297− | (f64::from(available_width) * f64::from(n) / f64::from(d))
298− }
299− Constraint::Min(v) => variables[i] | GE(WEAK) | f64::from(v),
300− Constraint::Max(v) => variables[i] | LE(WEAK) | f64::from(v),
301− })
302 }
274 }
303− solver
275+ if !self.widths.is_empty() {
304− .add_constraint(
276+ constraints.pop();
305− variables
306− .iter()
307− .fold(Expression::from_constant(0.), |acc, v| acc + *v)
308− | LE(REQUIRED)
309− | f64::from(available_width),
310− )
311− .unwrap();
312− solver.add_constraints(&ccs).unwrap();
313− let mut widths = vec![0; variables.len()];
314− for &(var, value) in solver.fetch_changes() {
315− let index = var_indices[&var];
316− let value = if value.is_sign_negative() {
317− 0
318− } else {
319− value.round() as u16
320− };
321− widths[index] = value;
322 }
277 }
323− // Cassowary could still return columns widths greater than the max width when there are
278+ let mut chunks = crate::layout::Layout::default()
324− // fixed length constraints that cannot be satisfied. Therefore, we clamp the widths from
279+ .direction(crate::layout::Direction::Horizontal)
325− // left to right.
280+ .constraints(constraints)
281+ .split(Rect {
282+ x: 0,
283+ y: 0,
326− let mut available_width = max_width;
284+ width: max_width,
327− for w in &mut widths {
285+ height: 1,
328− *w = available_width.min(*w);
286+ });
329− available_width = available_width
287+ if has_selection {
330− .saturating_sub(*w)
288+ chunks.remove(0);
331− .saturating_sub(self.column_spacing);
332 }
289 }
333− widths
290+ chunks.iter().step_by(2).map(|c| c.width).collect()
334 }
291 }
335
292
336 fn get_row_bounds(
293 fn get_row_bounds(