refactor: remove pointless default on internal structs (#980)

See #978

Also remove other derives. They are unused and just slow down
compilation.
This commit is contained in:
EdJoPaTo 2024-03-12 08:33:30 +01:00 committed by GitHub
parent 541f0f9953
commit 8e68db9e2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 53 additions and 43 deletions

View File

@ -304,7 +304,6 @@ impl From<CColor> for Color {
/// The `ModifierDiff` struct is used to calculate the difference between two `Modifier` /// The `ModifierDiff` struct is used to calculate the difference between two `Modifier`
/// values. This is useful when updating the terminal display, as it allows for more /// values. This is useful when updating the terminal display, as it allows for more
/// efficient updates by only sending the necessary changes. /// efficient updates by only sending the necessary changes.
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
struct ModifierDiff { struct ModifierDiff {
pub from: Modifier, pub from: Modifier,
pub to: Modifier, pub to: Modifier,

View File

@ -209,16 +209,13 @@ where
self.writer.flush() self.writer.flush()
} }
} }
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
struct Fg(Color); struct Fg(Color);
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
struct Bg(Color); struct Bg(Color);
/// The `ModifierDiff` struct is used to calculate the difference between two `Modifier` /// The `ModifierDiff` struct is used to calculate the difference between two `Modifier`
/// values. This is useful when updating the terminal display, as it allows for more /// values. This is useful when updating the terminal display, as it allows for more
/// efficient updates by only sending the necessary changes. /// efficient updates by only sending the necessary changes.
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
struct ModifierDiff { struct ModifierDiff {
from: Modifier, from: Modifier,
to: Modifier, to: Modifier,

View File

@ -55,7 +55,7 @@ pub struct Label<'a> {
/// ///
/// This allows the canvas to be drawn in multiple layers. This is useful if you want to draw /// This allows the canvas to be drawn in multiple layers. This is useful if you want to draw
/// multiple shapes on the canvas in specific order. /// multiple shapes on the canvas in specific order.
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] #[derive(Debug)]
struct Layer { struct Layer {
// A string of characters representing the grid. This will be wrapped to the width of the grid // A string of characters representing the grid. This will be wrapped to the width of the grid
// when rendering // when rendering
@ -97,7 +97,7 @@ trait Grid: Debug {
/// ///
/// This grid type only supports a single foreground color for each 2x4 dots cell. There is no way /// This grid type only supports a single foreground color for each 2x4 dots cell. There is no way
/// to set the individual color of each dot in the braille pattern. /// to set the individual color of each dot in the braille pattern.
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] #[derive(Debug)]
struct BrailleGrid { struct BrailleGrid {
/// Width of the grid in number of terminal columns /// Width of the grid in number of terminal columns
width: u16, width: u16,
@ -160,7 +160,7 @@ impl Grid for BrailleGrid {
/// ///
/// This makes it possible to draw shapes with a resolution of 1x1 dots per cell. This is useful /// This makes it possible to draw shapes with a resolution of 1x1 dots per cell. This is useful
/// when you want to draw shapes with a low resolution. /// when you want to draw shapes with a low resolution.
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] #[derive(Debug)]
struct CharGrid { struct CharGrid {
/// Width of the grid in number of terminal columns /// Width of the grid in number of terminal columns
width: u16, width: u16,
@ -232,7 +232,7 @@ impl Grid for CharGrid {
/// This allows for more flexibility than the `BrailleGrid` which only supports a single /// This allows for more flexibility than the `BrailleGrid` which only supports a single
/// foreground color for each 2x4 dots cell, and the `CharGrid` which only supports a single /// foreground color for each 2x4 dots cell, and the `CharGrid` which only supports a single
/// character for each cell. /// character for each cell.
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] #[derive(Debug)]
struct HalfBlockGrid { struct HalfBlockGrid {
/// Width of the grid in number of terminal columns /// Width of the grid in number of terminal columns
width: u16, width: u16,

View File

@ -400,7 +400,6 @@ impl<'a> Dataset<'a> {
/// A container that holds all the infos about where to display each elements of the chart (axis, /// A container that holds all the infos about where to display each elements of the chart (axis,
/// labels, legend, ...). /// labels, legend, ...).
#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
struct ChartLayout { struct ChartLayout {
/// Location of the title of the x axis /// Location of the title of the x axis
title_x: Option<(u16, u16)>, title_x: Option<(u16, u16)>,
@ -691,50 +690,63 @@ impl<'a> Chart<'a> {
/// Compute the internal layout of the chart given the area. If the area is too small some /// Compute the internal layout of the chart given the area. If the area is too small some
/// elements may be automatically hidden /// elements may be automatically hidden
fn layout(&self, area: Rect) -> ChartLayout { fn layout(&self, area: Rect) -> Option<ChartLayout> {
let mut layout = ChartLayout::default();
if area.height == 0 || area.width == 0 { if area.height == 0 || area.width == 0 {
return layout; return None;
} }
let mut x = area.left(); let mut x = area.left();
let mut y = area.bottom() - 1; let mut y = area.bottom() - 1;
let mut label_x = None;
if self.x_axis.labels.is_some() && y > area.top() { if self.x_axis.labels.is_some() && y > area.top() {
layout.label_x = Some(y); label_x = Some(y);
y -= 1; y -= 1;
} }
layout.label_y = self.y_axis.labels.as_ref().and(Some(x)); let label_y = self.y_axis.labels.as_ref().and(Some(x));
x += self.max_width_of_labels_left_of_y_axis(area, self.y_axis.labels.is_some()); x += self.max_width_of_labels_left_of_y_axis(area, self.y_axis.labels.is_some());
let mut axis_x = None;
if self.x_axis.labels.is_some() && y > area.top() { if self.x_axis.labels.is_some() && y > area.top() {
layout.axis_x = Some(y); axis_x = Some(y);
y -= 1; y -= 1;
} }
let mut axis_y = None;
if self.y_axis.labels.is_some() && x + 1 < area.right() { if self.y_axis.labels.is_some() && x + 1 < area.right() {
layout.axis_y = Some(x); axis_y = Some(x);
x += 1; x += 1;
} }
if x < area.right() && y > 1 { let graph_width = area.right().saturating_sub(x);
layout.graph_area = Rect::new(x, area.top(), area.right() - x, y - area.top() + 1); let graph_height = y.saturating_sub(area.top()).saturating_add(1);
} debug_assert_ne!(
graph_width, 0,
"Axis and labels should have been hidden due to the small area"
);
debug_assert_ne!(
graph_height, 0,
"Axis and labels should have been hidden due to the small area"
);
let graph_area = Rect::new(x, area.top(), graph_width, graph_height);
let mut title_x = None;
if let Some(ref title) = self.x_axis.title { if let Some(ref title) = self.x_axis.title {
let w = title.width() as u16; let w = title.width() as u16;
if w < layout.graph_area.width && layout.graph_area.height > 2 { if w < graph_area.width && graph_area.height > 2 {
layout.title_x = Some((x + layout.graph_area.width - w, y)); title_x = Some((x + graph_area.width - w, y));
} }
} }
let mut title_y = None;
if let Some(ref title) = self.y_axis.title { if let Some(ref title) = self.y_axis.title {
let w = title.width() as u16; let w = title.width() as u16;
if w + 1 < layout.graph_area.width && layout.graph_area.height > 2 { if w + 1 < graph_area.width && graph_area.height > 2 {
layout.title_y = Some((x, area.top())); title_y = Some((x, area.top()));
} }
} }
let mut legend_area = None;
if let Some(legend_position) = self.legend_position { if let Some(legend_position) = self.legend_position {
let legends = self let legends = self
.datasets .datasets
@ -747,27 +759,25 @@ impl<'a> Chart<'a> {
let [max_legend_width] = Layout::horizontal([self.hidden_legend_constraints.0]) let [max_legend_width] = Layout::horizontal([self.hidden_legend_constraints.0])
.flex(Flex::Start) .flex(Flex::Start)
.areas(layout.graph_area); .areas(graph_area);
let [max_legend_height] = Layout::vertical([self.hidden_legend_constraints.1]) let [max_legend_height] = Layout::vertical([self.hidden_legend_constraints.1])
.flex(Flex::Start) .flex(Flex::Start)
.areas(layout.graph_area); .areas(graph_area);
if inner_width > 0 if inner_width > 0
&& legend_width <= max_legend_width.width && legend_width <= max_legend_width.width
&& legend_height <= max_legend_height.height && legend_height <= max_legend_height.height
{ {
layout.legend_area = legend_position.layout( legend_area = legend_position.layout(
layout.graph_area, graph_area,
legend_width, legend_width,
legend_height, legend_height,
layout title_x
.title_x
.and(self.x_axis.title.as_ref()) .and(self.x_axis.title.as_ref())
.map(|t| t.width() as u16) .map(|t| t.width() as u16)
.unwrap_or_default(), .unwrap_or_default(),
layout title_y
.title_y
.and(self.y_axis.title.as_ref()) .and(self.y_axis.title.as_ref())
.map(|t| t.width() as u16) .map(|t| t.width() as u16)
.unwrap_or_default(), .unwrap_or_default(),
@ -775,7 +785,16 @@ impl<'a> Chart<'a> {
} }
} }
} }
layout Some(ChartLayout {
title_x,
title_y,
label_x,
label_y,
axis_x,
axis_y,
legend_area,
graph_area,
})
} }
fn max_width_of_labels_left_of_y_axis(&self, area: Rect, has_y_axis: bool) -> u16 { fn max_width_of_labels_left_of_y_axis(&self, area: Rect, has_y_axis: bool) -> u16 {
@ -930,21 +949,16 @@ impl WidgetRef for Chart<'_> {
self.block.render_ref(area, buf); self.block.render_ref(area, buf);
let chart_area = self.block.inner_if_some(area); let chart_area = self.block.inner_if_some(area);
if chart_area.is_empty() { let Some(layout) = self.layout(chart_area) else {
return; return;
} };
let graph_area = layout.graph_area;
// Sample the style of the entire widget. This sample will be used to reset the style of // Sample the style of the entire widget. This sample will be used to reset the style of
// the cells that are part of the components put on top of the grah area (i.e legend and // the cells that are part of the components put on top of the grah area (i.e legend and
// axis names). // axis names).
let original_style = buf.get(area.left(), area.top()).style(); let original_style = buf.get(area.left(), area.top()).style();
let layout = self.layout(chart_area);
let graph_area = layout.graph_area;
if graph_area.width < 1 || graph_area.height < 1 {
return;
}
self.render_x_labels(buf, &layout, chart_area, graph_area); self.render_x_labels(buf, &layout, chart_area, graph_area);
self.render_y_labels(buf, &layout, chart_area, graph_area); self.render_y_labels(buf, &layout, chart_area, graph_area);
@ -1136,7 +1150,7 @@ mod tests {
.x_axis(Axis::default().title("X axis")) .x_axis(Axis::default().title("X axis"))
.y_axis(Axis::default().title("Y axis")) .y_axis(Axis::default().title("Y axis"))
.hidden_legend_constraints(case.hidden_legend_constraints); .hidden_legend_constraints(case.hidden_legend_constraints);
let layout = chart.layout(case.chart_area); let layout = chart.layout(case.chart_area).unwrap();
assert_eq!(layout.legend_area, case.legend_area); assert_eq!(layout.legend_area, case.legend_area);
} }
} }
@ -1208,7 +1222,7 @@ mod tests {
let data_unnamed = Dataset::default(); // must not occupy a row in legend let data_unnamed = Dataset::default(); // must not occupy a row in legend
let widget = Chart::new(vec![data_named_1, data_unnamed, data_named_2]); let widget = Chart::new(vec![data_named_1, data_unnamed, data_named_2]);
let buffer = Buffer::empty(Rect::new(0, 0, 50, 25)); let buffer = Buffer::empty(Rect::new(0, 0, 50, 25));
let layout = widget.layout(buffer.area); let layout = widget.layout(buffer.area).unwrap();
assert!(layout.legend_area.is_some()); assert!(layout.legend_area.is_some());
assert_eq!(layout.legend_area.unwrap().height, 4); // 2 for borders, 2 for rows assert_eq!(layout.legend_area.unwrap().height, 4); // 2 for borders, 2 for rows
@ -1219,7 +1233,7 @@ mod tests {
let dataset = Dataset::default(); let dataset = Dataset::default();
let widget = Chart::new(vec![dataset; 3]); let widget = Chart::new(vec![dataset; 3]);
let buffer = Buffer::empty(Rect::new(0, 0, 50, 25)); let buffer = Buffer::empty(Rect::new(0, 0, 50, 25));
let layout = widget.layout(buffer.area); let layout = widget.layout(buffer.area).unwrap();
assert!(layout.legend_area.is_none()); assert!(layout.legend_area.is_none());
} }