Merge pull request #75 from kdheepak/kd/fix-spacing

This commit is contained in:
Dheepak Krishnamurthy 2021-02-08 05:40:44 -07:00 committed by GitHub
commit def8c9f282
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 10 deletions

View file

@ -434,9 +434,9 @@ impl TTApp {
vertical: 0, horizontal: 0, vertical: 0, horizontal: 0,
})); }));
let maximum_column_width = area.width;
let (contexts, headers) = self.get_all_contexts(); let (contexts, headers) = self.get_all_contexts();
let maximum_column_width = area.width;
let widths = self.calculate_widths(&contexts, &headers, maximum_column_width); let widths = self.calculate_widths(&contexts, &headers, maximum_column_width);
let selected = self.context_table_state.selected().unwrap_or_default(); let selected = self.context_table_state.selected().unwrap_or_default();
@ -566,7 +566,7 @@ impl TTApp {
for row in tasks.iter() { for row in tasks.iter() {
for (i, cell) in row.iter().enumerate() { for (i, cell) in row.iter().enumerate() {
widths[i] = std::cmp::max(cell.len() + 2, widths[i]); widths[i] = std::cmp::max(cell.len(), widths[i]);
} }
} }
@ -577,9 +577,15 @@ impl TTApp {
break break
} }
} }
for (i, header) in headers.iter().enumerate() {
if header == "ID" {
// always give ID a couple of extra for indicator
widths[i] += self.config.uda_selection_indicator.as_str().graphemes(true).count();
}
}
// now start trimming // now start trimming
while (widths.iter().sum::<usize>() as u16) >= maximum_column_width - 5 { while (widths.iter().sum::<usize>() as u16) >= maximum_column_width - (headers.len()) as u16 {
let index = widths.iter().position(|i| i == widths.iter().max().unwrap()).unwrap(); let index = widths.iter().position(|i| i == widths.iter().max().unwrap()).unwrap();
if widths[index] == 1 { if widths[index] == 1 {
break break
@ -617,9 +623,7 @@ impl TTApp {
for (i, header) in headers.iter().enumerate() { for (i, header) in headers.iter().enumerate() {
if header == "Description" || header == "Definition" { if header == "Description" || header == "Definition" {
if widths[i] > headers.iter().len() { self.task_report_table.description_width = widths[i] - 1;
self.task_report_table.description_width = widths[i] - 1;
}
break break
} }
} }

View file

@ -15,6 +15,8 @@ use tui::{
widgets::{Block, StatefulWidget, Widget}, widgets::{Block, StatefulWidget, Widget},
}; };
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use unicode_segmentation::Graphemes;
use unicode_segmentation::UnicodeSegmentation;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TableState { pub struct TableState {
@ -272,10 +274,18 @@ where
let mut x = table_area.left(); let mut x = table_area.left();
// Draw header // Draw header
let mut header_index = usize::MAX;
let mut index = 0;
if y < table_area.bottom() { if y < table_area.bottom() {
for (w, t) in solved_widths.iter().zip(self.header.by_ref()) { for (w, t) in solved_widths.iter().zip(self.header.by_ref()) {
buf.set_stringn(x, y, format!("{}", t), *w as usize, self.header_style); if t.to_string() == "ID" {
buf.set_stringn(x, y, format!("{symbol:>width$}", symbol=t, width=*w as usize), *w as usize, self.header_style);
header_index = index;
} else {
buf.set_stringn(x, y, format!("{}", t), *w as usize, self.header_style);
}
x += *w + self.column_spacing; x += *w + self.column_spacing;
index += 1;
} }
} }
y += 1 + self.header_gap; y += 1 + self.header_gap;
@ -323,7 +333,11 @@ where
*w as usize, *w as usize,
style, style,
); );
format!("{}{}", symbol, elt) if c == header_index {
format!("{symbol}{elt:>width$}", symbol = symbol, elt = elt, width = *w as usize - symbol.to_string().graphemes(true).count())
} else {
format!("{symbol}{elt:<width$}", symbol = symbol, elt = elt, width = *w as usize - symbol.to_string().graphemes(true).count())
}
} else { } else {
buf.set_stringn( buf.set_stringn(
x - 1, x - 1,
@ -332,7 +346,11 @@ where
*w as usize + 1, *w as usize + 1,
style, style,
); );
format!("{}", elt) if c == header_index {
format!("{elt:>width$}", elt = elt, width = *w as usize)
} else {
format!("{elt:<width$}", elt = elt, width = *w as usize)
}
}; };
buf.set_stringn(x, y + i as u16, s, *w as usize, style); buf.set_stringn(x, y + i as u16, s, *w as usize, style);
x += *w + self.column_spacing; x += *w + self.column_spacing;