Make ID right align along with header

This commit is contained in:
Dheepak Krishnamurthy 2021-02-08 05:16:23 -07:00
parent 7f2ea5ab78
commit bbfba5a5e7
2 changed files with 26 additions and 17 deletions

View file

@ -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::<usize>() 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
}

View file

@ -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::<String>();
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$}", symbol = symbol, elt = elt, width = *w as usize - symbol.to_string().graphemes(true).count())
}
} else {
buf.set_stringn(
x - 1,
@ -333,14 +344,13 @@ where
*w as usize + 1,
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)
}
};
let cell = if c == header_index {
format!("{symbol:>width$}", symbol = s, width = *w as usize)
} else {
format!("{symbol:<width$}", symbol = s, width = *w as usize)
};
buf.set_stringn(x, y + i as u16, cell, *w as usize, style);
buf.set_stringn(x, y + i as u16, s, *w as usize, style);
x += *w + self.column_spacing;
}
}