Skip to content

Commit

Permalink
give different colors to table columns (#5)
Browse files Browse the repository at this point in the history
this PR adds colors to the three line segments
- the name, data and shapes column have now different colors in the
"table" layout
- the "name: (shape) data" lines of the "compact" layout have different
colors, for the name, the shape and the data, just as in "table" layout
but compacted

these colors can be changed through config 👌
  • Loading branch information
amtoine authored Aug 21, 2023
1 parent 7ff3c12 commit 76f31a5
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ cargo doc --document-private-items --no-deps --open
## features
- [x] support non-character bindings
- [ ] when going into a file or URL, open it
- [ ] give different colors to names and type
- [x] give different colors to names and type
- [ ] show true tables as such
- [ ] get the config from `$env.config` => can parse configuration from CLI
- [ ] add check for the config to make sure it's valid
Expand Down
14 changes: 12 additions & 2 deletions examples/configuration/themes/dark.nuon
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
{
normal: { # the colors for a normal row
background: reset, # "black" is not pure *black*
foreground: white,
name: {
background: reset, # "black" is not pure *black*
foreground: green,
},
data: {
background: reset, # "black" is not pure *black*
foreground: white,
},
shape: {
background: reset, # "black" is not pure *black*
foreground: blue,
},
},
selected: { # the colors for the row under the cursor
background: white,
Expand Down
83 changes: 75 additions & 8 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,22 @@ pub(super) struct StatusBarColorConfig {
pub peek: BgFgColorConfig,
}

/// the configuration for a row of the data rendering table
#[derive(Clone, PartialEq, Debug)]
pub(super) struct TableRowColorConfig {
// the name of the data, i.e. the key on the left
pub name: BgFgColorConfig,
// the data itself,
pub data: BgFgColorConfig,
// the type of the data, e.g. `string` or `int`
pub shape: BgFgColorConfig,
}

/// the colors of the application
#[derive(Clone, PartialEq, Debug)]
pub(super) struct ColorConfig {
/// the color when a row is NOT selected
pub normal: BgFgColorConfig,
pub normal: TableRowColorConfig,
/// the color when a row is selected
pub selected: BgFgColorConfig,
/// the modifier to apply to the row under the cursor
Expand Down Expand Up @@ -108,9 +119,19 @@ impl Config {
show_cell_path: true,
layout: Layout::Table,
colors: ColorConfig {
normal: BgFgColorConfig {
background: Color::Reset, // "Black" is not pure *black*
foreground: Color::White,
normal: TableRowColorConfig {
name: BgFgColorConfig {
background: Color::Reset, // "Black" is not pure *black*
foreground: Color::Green,
},
data: BgFgColorConfig {
background: Color::Reset, // "Black" is not pure *black*
foreground: Color::White,
},
shape: BgFgColorConfig {
background: Color::Reset, // "Black" is not pure *black*
foreground: Color::Blue,
},
},
selected: BgFgColorConfig {
background: Color::White,
Expand Down Expand Up @@ -178,12 +199,58 @@ impl Config {
for column in columns {
match column.as_str() {
"normal" => {
if let Some(val) = try_fg_bg_colors(
let (columns, span) = match follow_cell_path(
&value,
&["colors", "normal"],
&config.colors.normal,
)? {
config.colors.normal = val
)
.unwrap()
{
Value::Record { cols, span, .. } => (cols, span),
x => {
return Err(invalid_type(
&x,
&["colors", "normal"],
"record",
))
}
};

for column in columns {
match column.as_str() {
"name" => {
if let Some(val) = try_fg_bg_colors(
&value,
&["colors", "normal", "name"],
&config.colors.normal.name,
)? {
config.colors.normal.name = val
}
}
"data" => {
if let Some(val) = try_fg_bg_colors(
&value,
&["colors", "normal", "data"],
&config.colors.normal.data,
)? {
config.colors.normal.data = val
}
}
"shape" => {
if let Some(val) = try_fg_bg_colors(
&value,
&["colors", "normal", "shape"],
&config.colors.normal.shape,
)? {
config.colors.normal.shape = val
}
}
x => {
return Err(invalid_field(
&["colors", "normal", x],
Some(span),
))
}
}
}
}
"selected" => {
Expand Down
51 changes: 45 additions & 6 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
use ratatui::{
prelude::{Alignment, Constraint, CrosstermBackend, Rect},
style::Style,
widgets::{Block, Borders, List, ListItem, ListState, Paragraph, Row, Table, TableState},
text::{Line, Span},
widgets::{Block, Borders, Cell, List, ListItem, ListState, Paragraph, Row, Table, TableState},
Frame,
};

Expand Down Expand Up @@ -195,9 +196,6 @@ fn render_data(
let mut data_path = state.cell_path.members.clone();
let current = if !state.bottom { data_path.pop() } else { None };

let normal_style = Style::default()
.fg(config.colors.normal.foreground)
.bg(config.colors.normal.background);
let highlight_style = Style::default()
.fg(config.colors.selected.foreground)
.bg(config.colors.selected.background)
Expand All @@ -221,7 +219,30 @@ fn render_data(
let items: Vec<ListItem> = repr_data(data, &data_path, config)[0]
.clone()
.iter()
.map(|line| ListItem::new(line.clone()).style(normal_style))
.map(|_line| {
ListItem::new(Line::from(vec![
Span::styled(
"name",
Style::default()
.fg(config.colors.normal.name.foreground)
.bg(config.colors.normal.name.background),
),
": (".into(),
Span::styled(
"shape",
Style::default()
.fg(config.colors.normal.shape.foreground)
.bg(config.colors.normal.shape.background),
),
") ".into(),
Span::styled(
"data",
Style::default()
.fg(config.colors.normal.data.foreground)
.bg(config.colors.normal.data.background),
),
]))
})
.collect();

let items = List::new(items)
Expand All @@ -237,7 +258,25 @@ fn render_data(
Layout::Table => {
let rows: Vec<Row> = repr_data(data, &data_path, config)
.iter()
.map(|row| Row::new(row.clone()).style(normal_style))
.map(|row| {
Row::new(vec![
Cell::from(row[0].clone()).style(
Style::default()
.fg(config.colors.normal.name.foreground)
.bg(config.colors.normal.name.background),
),
Cell::from(row[1].clone()).style(
Style::default()
.fg(config.colors.normal.data.foreground)
.bg(config.colors.normal.data.background),
),
Cell::from(row[2].clone()).style(
Style::default()
.fg(config.colors.normal.shape.foreground)
.bg(config.colors.normal.shape.background),
),
])
})
.collect();

let table = Table::new(rows)
Expand Down

0 comments on commit 76f31a5

Please sign in to comment.