From dac571f7e72f7f46b6c9ee061b8c15bd8136a107 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Mon, 8 Feb 2021 03:51:05 -0700 Subject: [PATCH 1/4] Fix spacing for all columns --- src/app.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/app.rs b/src/app.rs index 42926c8..6ed6d89 100644 --- a/src/app.rs +++ b/src/app.rs @@ -434,9 +434,9 @@ impl TTApp { vertical: 0, horizontal: 0, })); - let maximum_column_width = area.width; - let (contexts, headers) = self.get_all_contexts(); + + let maximum_column_width = area.width - 2; let widths = self.calculate_widths(&contexts, &headers, maximum_column_width); let selected = self.context_table_state.selected().unwrap_or_default(); @@ -566,7 +566,7 @@ impl TTApp { for row in tasks.iter() { 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]); } } @@ -579,7 +579,7 @@ impl TTApp { } // now start trimming - while (widths.iter().sum::() as u16) >= maximum_column_width - 5 { + while (widths.iter().sum::() as u16) >= maximum_column_width - (headers.len()) as u16 { let index = widths.iter().position(|i| i == widths.iter().max().unwrap()).unwrap(); if widths[index] == 1 { break @@ -587,6 +587,13 @@ impl TTApp { widths[index] -= 1; } + for (i, header) in headers.iter().enumerate() { + if header == "ID" { + // always give ID a couple of extra for indicator + widths[i] += 2; + break + } + } return widths } @@ -612,14 +619,12 @@ impl TTApp { return; } - let maximum_column_width = rect.width; + let maximum_column_width = rect.width - 2; let widths = self.calculate_widths(&tasks, &headers, maximum_column_width); for (i, header) in headers.iter().enumerate() { 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 } } From 7f2ea5ab78f352a04a08ad44c55e35b03b458ff7 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Mon, 8 Feb 2021 04:19:12 -0700 Subject: [PATCH 2/4] Make ID right align --- src/table.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/table.rs b/src/table.rs index 22c755d..986747e 100644 --- a/src/table.rs +++ b/src/table.rs @@ -305,6 +305,7 @@ where } else { 0 }; + let header_index = self.header.by_ref().into_iter().position(|r| r.to_string() == "ID").unwrap_or_else(|| 0); for (i, row) in self.rows.skip(state.offset).take(remaining).enumerate() { let (data, style, symbol) = match row { Row::Data(d) | Row::StyledData(d, _) if Some(i) == state.selected.map(|s| s - state.offset) => { @@ -334,7 +335,12 @@ where ); format!("{}", elt) }; - buf.set_stringn(x, y + i as u16, s, *w as usize, style); + let cell = if c == header_index { + format!("{symbol:>width$}", symbol = s, width = *w as usize) + } else { + format!("{symbol: Date: Mon, 8 Feb 2021 05:16:23 -0700 Subject: [PATCH 3/4] Make ID right align along with header --- src/app.rs | 13 ++++++------- src/table.rs | 30 ++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/app.rs b/src/app.rs index 6ed6d89..446a55d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -577,6 +577,12 @@ impl TTApp { 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 while (widths.iter().sum::() as u16) >= maximum_column_width - (headers.len()) as u16 { @@ -587,13 +593,6 @@ impl TTApp { widths[index] -= 1; } - for (i, header) in headers.iter().enumerate() { - if header == "ID" { - // always give ID a couple of extra for indicator - widths[i] += 2; - break - } - } return widths } diff --git a/src/table.rs b/src/table.rs index 986747e..42b9b12 100644 --- a/src/table.rs +++ b/src/table.rs @@ -15,6 +15,8 @@ use tui::{ widgets::{Block, StatefulWidget, Widget}, }; use unicode_width::UnicodeWidthStr; +use unicode_segmentation::Graphemes; +use unicode_segmentation::UnicodeSegmentation; #[derive(Debug, Clone)] pub struct TableState { @@ -274,7 +276,11 @@ where // Draw header if y < table_area.bottom() { 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); + } else { + buf.set_stringn(x, y, format!("{}", t), *w as usize, self.header_style); + } x += *w + self.column_spacing; } } @@ -288,6 +294,8 @@ where let highlight_symbol = self.highlight_symbol.unwrap_or(""); let blank_symbol = iter::repeat(" ").take(highlight_symbol.width()).collect::(); + let header_index = self.header.by_ref().into_iter().position(|r| r.to_string() == "ID").unwrap_or_else(|| 0); + // Draw rows let default_style = Style::default(); if y < table_area.bottom() { @@ -305,7 +313,6 @@ where } else { 0 }; - let header_index = self.header.by_ref().into_iter().position(|r| r.to_string() == "ID").unwrap_or_else(|| 0); for (i, row) in self.rows.skip(state.offset).take(remaining).enumerate() { let (data, style, symbol) = match row { Row::Data(d) | Row::StyledData(d, _) if Some(i) == state.selected.map(|s| s - state.offset) => { @@ -324,7 +331,11 @@ where *w as usize, 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$}", elt = elt, width = *w as usize) + } else { + format!("{elt:width$}", symbol = s, width = *w as usize) - } else { - format!("{symbol: Date: Mon, 8 Feb 2021 05:34:14 -0700 Subject: [PATCH 4/4] Fix spacing at column end and for first column --- src/app.rs | 4 ++-- src/table.rs | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/app.rs b/src/app.rs index 446a55d..4e0c87f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -436,7 +436,7 @@ impl TTApp { let (contexts, headers) = self.get_all_contexts(); - let maximum_column_width = area.width - 2; + let maximum_column_width = area.width; let widths = self.calculate_widths(&contexts, &headers, maximum_column_width); let selected = self.context_table_state.selected().unwrap_or_default(); @@ -618,7 +618,7 @@ impl TTApp { return; } - let maximum_column_width = rect.width - 2; + let maximum_column_width = rect.width; let widths = self.calculate_widths(&tasks, &headers, maximum_column_width); for (i, header) in headers.iter().enumerate() { diff --git a/src/table.rs b/src/table.rs index 42b9b12..02cdd55 100644 --- a/src/table.rs +++ b/src/table.rs @@ -274,14 +274,18 @@ where let mut x = table_area.left(); // Draw header + let mut header_index = usize::MAX; + let mut index = 0; if y < table_area.bottom() { for (w, t) in solved_widths.iter().zip(self.header.by_ref()) { 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; + index += 1; } } y += 1 + self.header_gap; @@ -294,8 +298,6 @@ where let highlight_symbol = self.highlight_symbol.unwrap_or(""); let blank_symbol = iter::repeat(" ").take(highlight_symbol.width()).collect::(); - let header_index = self.header.by_ref().into_iter().position(|r| r.to_string() == "ID").unwrap_or_else(|| 0); - // Draw rows let default_style = Style::default(); if y < table_area.bottom() {