Use upstream implementation of table column calculation

Changed in a68e38e59e.
pull/5568/head
Gokul Soumya 2 years ago committed by Blaž Hrastnik
parent 9aafcb2b9a
commit 7a76c6cbba

@ -15,10 +15,7 @@ use tui::{
use fuzzy_matcher::skim::SkimMatcherV2 as Matcher; use fuzzy_matcher::skim::SkimMatcherV2 as Matcher;
use tui::widgets::Widget; use tui::widgets::Widget;
use std::{ use std::cmp::{self, Ordering};
cmp::{self, Ordering},
time::Instant,
};
use std::{collections::HashMap, io::Read, path::PathBuf}; use std::{collections::HashMap, io::Read, path::PathBuf};
use crate::ui::{Prompt, PromptEvent}; use crate::ui::{Prompt, PromptEvent};

@ -4,14 +4,8 @@ use crate::{
text::Text, text::Text,
widgets::{Block, Widget}, widgets::{Block, Widget},
}; };
use cassowary::{
strength::{MEDIUM, REQUIRED, WEAK},
WeightedRelation::*,
{Expression, Solver},
};
use helix_core::unicode::width::UnicodeWidthStr; use helix_core::unicode::width::UnicodeWidthStr;
use helix_view::graphics::{Rect, Style}; use helix_view::graphics::{Rect, Style};
use std::collections::HashMap;
/// A [`Cell`] contains the [`Text`] to be displayed in a [`Row`] of a [`Table`]. /// A [`Cell`] contains the [`Text`] to be displayed in a [`Row`] of a [`Table`].
/// ///
@ -268,69 +262,32 @@ impl<'a> Table<'a> {
} }
fn get_columns_widths(&self, max_width: u16, has_selection: bool) -> Vec<u16> { fn get_columns_widths(&self, max_width: u16, has_selection: bool) -> Vec<u16> {
let mut solver = Solver::new(); let mut constraints = Vec::with_capacity(self.widths.len() * 2 + 1);
let mut var_indices = HashMap::new();
let mut ccs = Vec::new();
let mut variables = Vec::new();
for i in 0..self.widths.len() {
let var = cassowary::Variable::new();
variables.push(var);
var_indices.insert(var, i);
}
let spacing_width = (variables.len() as u16).saturating_sub(1) * self.column_spacing;
let mut available_width = max_width.saturating_sub(spacing_width);
if has_selection { if has_selection {
let highlight_symbol_width = let highlight_symbol_width =
self.highlight_symbol.map(|s| s.width() as u16).unwrap_or(0); self.highlight_symbol.map(|s| s.width() as u16).unwrap_or(0);
available_width = available_width.saturating_sub(highlight_symbol_width); constraints.push(Constraint::Length(highlight_symbol_width));
} }
for (i, constraint) in self.widths.iter().enumerate() { for constraint in self.widths {
ccs.push(variables[i] | GE(WEAK) | 0.); constraints.push(*constraint);
ccs.push(match *constraint { constraints.push(Constraint::Length(self.column_spacing));
Constraint::Length(v) => variables[i] | EQ(MEDIUM) | f64::from(v),
Constraint::Percentage(v) => {
variables[i] | EQ(WEAK) | (f64::from(v * available_width) / 100.0)
}
Constraint::Ratio(n, d) => {
variables[i]
| EQ(WEAK)
| (f64::from(available_width) * f64::from(n) / f64::from(d))
}
Constraint::Min(v) => variables[i] | GE(WEAK) | f64::from(v),
Constraint::Max(v) => variables[i] | LE(WEAK) | f64::from(v),
})
} }
solver if !self.widths.is_empty() {
.add_constraint( constraints.pop();
variables
.iter()
.fold(Expression::from_constant(0.), |acc, v| acc + *v)
| LE(REQUIRED)
| f64::from(available_width),
)
.unwrap();
solver.add_constraints(&ccs).unwrap();
let mut widths = vec![0; variables.len()];
for &(var, value) in solver.fetch_changes() {
let index = var_indices[&var];
let value = if value.is_sign_negative() {
0
} else {
value.round() as u16
};
widths[index] = value;
} }
// Cassowary could still return columns widths greater than the max width when there are let mut chunks = crate::layout::Layout::default()
// fixed length constraints that cannot be satisfied. Therefore, we clamp the widths from .direction(crate::layout::Direction::Horizontal)
// left to right. .constraints(constraints)
let mut available_width = max_width; .split(Rect {
for w in &mut widths { x: 0,
*w = available_width.min(*w); y: 0,
available_width = available_width width: max_width,
.saturating_sub(*w) height: 1,
.saturating_sub(self.column_spacing); });
if has_selection {
chunks.remove(0);
} }
widths chunks.iter().step_by(2).map(|c| c.width).collect()
} }
fn get_row_bounds( fn get_row_bounds(

Loading…
Cancel
Save