feat: remove unecessary borrows of Style

pull/71/head
Florian Dehau 6 years ago
parent d0cee47e22
commit 6c69160d6b

@ -19,7 +19,7 @@ impl<'a> Default for Label<'a> {
impl<'a> Widget for Label<'a> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
buf.set_string(area.left(), area.top(), self.text, &Style::default());
buf.set_string(area.left(), area.top(), self.text, Style::default());
}
}

@ -367,10 +367,10 @@ fn draw_charts(f: &mut Frame<MouseBackend>, app: &App, area: Rect) {
Item::StyledData(
format!("{}: {}", level, evt),
match level {
"ERROR" => &error_style,
"CRITICAL" => &critical_style,
"WARNING" => &warning_style,
_ => &info_style,
"ERROR" => error_style,
"CRITICAL" => critical_style,
"WARNING" => warning_style,
_ => info_style,
},
)
});
@ -477,9 +477,9 @@ fn draw_second_tab(f: &mut Frame<MouseBackend>, app: &App, area: Rect) {
["Server", "Location", "Status"].into_iter(),
app.servers.iter().map(|s| {
let style = if s.status == "Up" {
&up_style
up_style
} else {
&failure_style
failure_style
};
Row::StyledData(vec![s.name, s.location, s.status].into_iter(), style)
}),

@ -191,10 +191,10 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
Item::StyledData(
format!("{}: {}", level, evt),
match level {
"ERROR" => &app.error_style,
"CRITICAL" => &app.critical_style,
"WARNING" => &app.warning_style,
_ => &app.info_style,
"ERROR" => app.error_style,
"CRITICAL" => app.critical_style,
"WARNING" => app.warning_style,
_ => app.info_style,
},
)
});

@ -90,9 +90,9 @@ fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
let header = ["Header1", "Header2", "Header3"];
let rows = app.items.iter().enumerate().map(|(i, item)| {
if i == app.selected {
Row::StyledData(item.into_iter(), &selected_style)
Row::StyledData(item.into_iter(), selected_style)
} else {
Row::StyledData(item.into_iter(), &normal_style)
Row::StyledData(item.into_iter(), normal_style)
}
});

@ -81,7 +81,7 @@ impl Default for Cell {
/// let mut buf = Buffer::empty(Rect{x: 0, y: 0, width: 10, height: 5});
/// buf.get_mut(0, 2).set_symbol("x");
/// assert_eq!(buf.get(0, 2).symbol, "x");
/// buf.set_string(3, 0, "string", &Style::default().fg(Color::Red).bg(Color::White));
/// buf.set_string(3, 0, "string", Style::default().fg(Color::Red).bg(Color::White));
/// assert_eq!(buf.get(5, 0), &Cell{
/// symbol: String::from("r"),
/// style: Style {
@ -233,20 +233,26 @@ impl Buffer {
}
/// Print a string, starting at the position (x, y)
pub fn set_string(&mut self, x: u16, y: u16, string: &str, style: &Style) {
pub fn set_string<S>(&mut self, x: u16, y: u16, string: S, style: Style)
where
S: AsRef<str>,
{
self.set_stringn(x, y, string, usize::MAX, style);
}
/// Print at most the first n characters of a string if enough space is available
/// until the end of the line
pub fn set_stringn(&mut self, x: u16, y: u16, string: &str, limit: usize, style: &Style) {
pub fn set_stringn<S>(&mut self, x: u16, y: u16, string: S, limit: usize, style: Style)
where
S: AsRef<str>,
{
let mut index = self.index_of(x, y);
let graphemes = UnicodeSegmentation::graphemes(string, true).collect::<Vec<&str>>();
let graphemes = UnicodeSegmentation::graphemes(string.as_ref(), true);
let max_index = min((self.area.right() - x) as usize, limit);
for s in graphemes.into_iter().take(max_index) {
for s in graphemes.take(max_index) {
self.content[index].symbol.clear();
self.content[index].symbol.push_str(s);
self.content[index].style = *style;
self.content[index].style = style;
index += 1;
}
}

@ -177,7 +177,7 @@ impl<'a> Widget for BarChart<'a> {
+ (self.bar_width - width) / 2,
chart_area.bottom() - 2,
value_label,
&self.value_style,
self.value_style,
);
}
}
@ -186,7 +186,7 @@ impl<'a> Widget for BarChart<'a> {
chart_area.bottom() - 1,
label,
self.bar_width as usize,
&self.label_style,
self.label_style,
);
}
}

@ -178,7 +178,7 @@ impl<'a> Widget for Block<'a> {
area.top(),
title,
width as usize,
&self.title_style,
self.title_style,
);
}
}

@ -285,7 +285,7 @@ where
dx + canvas_area.left(),
dy + canvas_area.top(),
label.text,
&style.fg(label.color),
style.fg(label.color),
);
}
}

@ -361,12 +361,12 @@ where
if let Some((x, y)) = layout.title_x {
let title = self.x_axis.title.unwrap();
buf.set_string(x, y, title, &self.x_axis.style);
buf.set_string(x, y, title, self.x_axis.style);
}
if let Some((x, y)) = layout.title_y {
let title = self.y_axis.title.unwrap();
buf.set_string(x, y, title, &self.y_axis.style);
buf.set_string(x, y, title, self.y_axis.style);
}
if let Some(y) = layout.label_x {
@ -380,7 +380,7 @@ where
- label.as_ref().width() as u16,
y,
label.as_ref(),
&self.x_axis.labels_style,
self.x_axis.labels_style,
);
}
}
@ -396,7 +396,7 @@ where
x,
graph_area.bottom() - 1 - dy,
label.as_ref(),
&self.y_axis.labels_style,
self.y_axis.labels_style,
);
}
}
@ -471,7 +471,7 @@ where
legend_area.x + 1,
legend_area.y + 1 + i as u16,
dataset.name,
&dataset.style,
dataset.style,
);
}
}

@ -92,7 +92,7 @@ impl<'a> Widget for Gauge<'a> {
let label = self.label.unwrap_or(&precent_label);
let label_width = label.width() as u16;
let middle = (gauge_area.width - label_width) / 2 + gauge_area.left();
buf.set_string(middle, y, label, &self.style);
buf.set_string(middle, y, label, self.style);
}
// Fix colors

@ -9,14 +9,14 @@ use layout::{Corner, Rect};
use style::Style;
use widgets::{Block, Widget};
pub enum Item<'i, D: 'i> {
pub enum Item<D> {
Data(D),
StyledData(D, &'i Style),
StyledData(D, Style),
}
pub struct List<'b, 'i, L, D: 'i>
pub struct List<'b, L, D>
where
L: Iterator<Item = Item<'i, D>>,
L: Iterator<Item = Item<D>>,
{
block: Option<Block<'b>>,
items: L,
@ -24,11 +24,11 @@ where
start_corner: Corner,
}
impl<'b, 'i, L, D> Default for List<'b, 'i, L, D>
impl<'b, L, D> Default for List<'b, L, D>
where
L: Iterator<Item = Item<'i, D>> + Default,
L: Iterator<Item = Item<D>> + Default,
{
fn default() -> List<'b, 'i, L, D> {
fn default() -> List<'b, L, D> {
List {
block: None,
items: L::default(),
@ -38,11 +38,11 @@ where
}
}
impl<'b, 'i, L, D> List<'b, 'i, L, D>
impl<'b, L, D> List<'b, L, D>
where
L: Iterator<Item = Item<'i, D>>,
L: Iterator<Item = Item<D>>,
{
pub fn new(items: L) -> List<'b, 'i, L, D> {
pub fn new(items: L) -> List<'b, L, D> {
List {
block: None,
items,
@ -51,33 +51,33 @@ where
}
}
pub fn block(mut self, block: Block<'b>) -> List<'b, 'i, L, D> {
pub fn block(mut self, block: Block<'b>) -> List<'b, L, D> {
self.block = Some(block);
self
}
pub fn items<I>(mut self, items: I) -> List<'b, 'i, L, D>
pub fn items<I>(mut self, items: I) -> List<'b, L, D>
where
I: IntoIterator<Item = Item<'i, D>, IntoIter = L>,
I: IntoIterator<Item = Item<D>, IntoIter = L>,
{
self.items = items.into_iter();
self
}
pub fn style(mut self, style: Style) -> List<'b, 'i, L, D> {
pub fn style(mut self, style: Style) -> List<'b, L, D> {
self.style = style;
self
}
pub fn start_corner(mut self, corner: Corner) -> List<'b, 'i, L, D> {
pub fn start_corner(mut self, corner: Corner) -> List<'b, L, D> {
self.start_corner = corner;
self
}
}
impl<'b, 'i, L, D> Widget for List<'b, 'i, L, D>
impl<'b, L, D> Widget for List<'b, L, D>
where
L: Iterator<Item = Item<'i, D>>,
L: Iterator<Item = Item<D>>,
D: Display,
{
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
@ -112,13 +112,13 @@ where
buf.set_stringn(
x,
y,
&format!("{}", v),
format!("{}", v),
list_area.width as usize,
&Style::default(),
Style::default(),
);
}
Item::StyledData(ref v, s) => {
buf.set_stringn(x, y, &format!("{}", v), list_area.width as usize, s);
buf.set_stringn(x, y, format!("{}", v), list_area.width as usize, s);
}
};
}
@ -216,8 +216,8 @@ impl<'b> Widget for SelectableList<'b> {
// Use highlight_style only if something is selected
let (selected, highlight_style) = match self.selected {
Some(i) => (Some(i), &self.highlight_style),
None => (None, &self.style),
Some(i) => (Some(i), self.highlight_style),
None => (None, self.style),
};
let highlight_symbol = self.highlight_symbol.unwrap_or("");
let blank_symbol = iter::repeat(" ")
@ -244,10 +244,10 @@ impl<'b> Widget for SelectableList<'b> {
if i == s {
Item::StyledData(format!("{} {}", highlight_symbol, item), highlight_style)
} else {
Item::StyledData(format!("{} {}", blank_symbol, item), &self.style)
Item::StyledData(format!("{} {}", blank_symbol, item), self.style)
}
} else {
Item::StyledData(item.to_string(), &self.style)
Item::StyledData(item.to_string(), self.style)
}
})
.skip(offset as usize);

@ -7,13 +7,13 @@ use style::Style;
use widgets::{Block, Widget};
/// Holds data to be displayed in a Table widget
pub enum Row<'i, D, I>
pub enum Row<D, I>
where
D: Iterator<Item = I>,
I: Display,
{
Data(D),
StyledData(D, &'i Style),
StyledData(D, Style),
}
/// A widget to display data in formatted columns
@ -28,9 +28,9 @@ where
/// Table::new(
/// ["Col1", "Col2", "Col3"].into_iter(),
/// vec![
/// Row::StyledData(["Row11", "Row12", "Row13"].into_iter(), &row_style),
/// Row::StyledData(["Row21", "Row22", "Row23"].into_iter(), &row_style),
/// Row::StyledData(["Row31", "Row32", "Row33"].into_iter(), &row_style),
/// Row::StyledData(["Row11", "Row12", "Row13"].into_iter(), row_style),
/// Row::StyledData(["Row21", "Row22", "Row23"].into_iter(), row_style),
/// Row::StyledData(["Row31", "Row32", "Row33"].into_iter(), row_style),
/// Row::Data(["Row41", "Row42", "Row43"].into_iter())
/// ].into_iter()
/// )
@ -41,13 +41,13 @@ where
/// .column_spacing(1);
/// # }
/// ```
pub struct Table<'a, 'i, T, H, I, D, R>
pub struct Table<'a, T, H, I, D, R>
where
T: Display,
H: Iterator<Item = T>,
I: Display,
D: Iterator<Item = I>,
R: Iterator<Item = Row<'i, D, I>>,
R: Iterator<Item = Row<D, I>>,
{
/// A block to wrap the widget in
block: Option<Block<'a>>,
@ -66,15 +66,15 @@ where
rows: R,
}
impl<'a, 'i, T, H, I, D, R> Default for Table<'a, 'i, T, H, I, D, R>
impl<'a, T, H, I, D, R> Default for Table<'a, T, H, I, D, R>
where
T: Display,
H: Iterator<Item = T> + Default,
I: Display,
D: Iterator<Item = I>,
R: Iterator<Item = Row<'i, D, I>> + Default,
R: Iterator<Item = Row<D, I>> + Default,
{
fn default() -> Table<'a, 'i, T, H, I, D, R> {
fn default() -> Table<'a, T, H, I, D, R> {
Table {
block: None,
style: Style::default(),
@ -87,15 +87,15 @@ where
}
}
impl<'a, 'i, T, H, I, D, R> Table<'a, 'i, T, H, I, D, R>
impl<'a, T, H, I, D, R> Table<'a, T, H, I, D, R>
where
T: Display,
H: Iterator<Item = T>,
I: Display,
D: Iterator<Item = I>,
R: Iterator<Item = Row<'i, D, I>>,
R: Iterator<Item = Row<D, I>>,
{
pub fn new(header: H, rows: R) -> Table<'a, 'i, T, H, I, D, R> {
pub fn new(header: H, rows: R) -> Table<'a, T, H, I, D, R> {
Table {
block: None,
style: Style::default(),
@ -106,12 +106,12 @@ where
column_spacing: 1,
}
}
pub fn block(mut self, block: Block<'a>) -> Table<'a, 'i, T, H, I, D, R> {
pub fn block(mut self, block: Block<'a>) -> Table<'a, T, H, I, D, R> {
self.block = Some(block);
self
}
pub fn header<II>(mut self, header: II) -> Table<'a, 'i, T, H, I, D, R>
pub fn header<II>(mut self, header: II) -> Table<'a, T, H, I, D, R>
where
II: IntoIterator<Item = T, IntoIter = H>,
{
@ -119,42 +119,42 @@ where
self
}
pub fn header_style(mut self, style: Style) -> Table<'a, 'i, T, H, I, D, R> {
pub fn header_style(mut self, style: Style) -> Table<'a, T, H, I, D, R> {
self.header_style = style;
self
}
pub fn widths(mut self, widths: &'a [u16]) -> Table<'a, 'i, T, H, I, D, R> {
pub fn widths(mut self, widths: &'a [u16]) -> Table<'a, T, H, I, D, R> {
self.widths = widths;
self
}
pub fn rows<II>(mut self, rows: II) -> Table<'a, 'i, T, H, I, D, R>
pub fn rows<II>(mut self, rows: II) -> Table<'a, T, H, I, D, R>
where
II: IntoIterator<Item = Row<'i, D, I>, IntoIter = R>,
II: IntoIterator<Item = Row<D, I>, IntoIter = R>,
{
self.rows = rows.into_iter();
self
}
pub fn style(mut self, style: Style) -> Table<'a, 'i, T, H, I, D, R> {
pub fn style(mut self, style: Style) -> Table<'a, T, H, I, D, R> {
self.style = style;
self
}
pub fn column_spacing(mut self, spacing: u16) -> Table<'a, 'i, T, H, I, D, R> {
pub fn column_spacing(mut self, spacing: u16) -> Table<'a, T, H, I, D, R> {
self.column_spacing = spacing;
self
}
}
impl<'a, 'i, T, H, I, D, R> Widget for Table<'a, 'i, T, H, I, D, R>
impl<'a, T, H, I, D, R> Widget for Table<'a, T, H, I, D, R>
where
T: Display,
H: Iterator<Item = T>,
I: Display,
D: Iterator<Item = I>,
R: Iterator<Item = Row<'i, D, I>>,
R: Iterator<Item = Row<D, I>>,
{
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
// Render block if necessary and get the drawing area
@ -185,7 +185,7 @@ where
if y < table_area.bottom() {
x = table_area.left();
for (w, t) in widths.iter().zip(self.header.by_ref()) {
buf.set_string(x, y, &format!("{}", t), &self.header_style);
buf.set_string(x, y, format!("{}", t), self.header_style);
x += *w + self.column_spacing;
}
}
@ -197,12 +197,12 @@ where
let remaining = (table_area.bottom() - y) as usize;
for (i, row) in self.rows.by_ref().take(remaining).enumerate() {
let (data, style) = match row {
Row::Data(d) => (d, &default_style),
Row::Data(d) => (d, default_style),
Row::StyledData(d, s) => (d, s),
};
x = table_area.left();
for (w, elt) in widths.iter().zip(data) {
buf.set_stringn(x, y + i as u16, &format!("{}", elt), *w as usize, style);
buf.set_stringn(x, y + i as u16, format!("{}", elt), *w as usize, style);
x += *w + self.column_spacing;
}
}

@ -105,9 +105,9 @@ where
let mut x = tabs_area.left();
for (title, style) in self.titles.iter().enumerate().map(|(i, t)| {
if i == self.selected {
(t, &self.highlight_style)
(t, self.highlight_style)
} else {
(t, &self.style)
(t, self.style)
}
}) {
x += 1;

Loading…
Cancel
Save