Fix clippy warnings

This commit is contained in:
Alexandre Bury 2018-05-17 17:35:57 -07:00
parent 80f66262be
commit 755854963f
17 changed files with 57 additions and 92 deletions

View File

@ -12,7 +12,7 @@ pub struct Align {
impl Align { impl Align {
/// Creates a new Align object from the given alignments. /// Creates a new Align object from the given alignments.
pub fn new(h: HAlign, v: VAlign) -> Self { pub fn new(h: HAlign, v: VAlign) -> Self {
Align { h: h, v: v } Align { h, v }
} }
/// Creates a top-left alignment. /// Creates a top-left alignment.

View File

@ -95,7 +95,7 @@ fn find_closest(color: &Color, max_colors: i16) -> i16 {
// (r - 8) / 10 = n // (r - 8) / 10 = n
// //
let n = (r - 8) / 10; let n = (r - 8) / 10;
(232 + n) as i16 i16::from(232 + n)
} else { } else {
// Generic RGB // Generic RGB
let r = 6 * u16::from(r) / 256; let r = 6 * u16::from(r) / 256;

View File

@ -97,7 +97,7 @@ impl Cursive {
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
Cursive { Cursive {
theme: theme, theme,
screens: vec![views::StackView::new()], screens: vec![views::StackView::new()],
last_sizes: Vec::new(), last_sizes: Vec::new(),
global_callbacks: HashMap::new(), global_callbacks: HashMap::new(),
@ -106,7 +106,7 @@ impl Cursive {
running: true, running: true,
cb_source: rx, cb_source: rx,
cb_sink: tx, cb_sink: tx,
backend: backend, backend,
} }
} }

View File

@ -100,7 +100,7 @@ impl<'a> Printer<'a> {
// We accept requests between `content_offset` and // We accept requests between `content_offset` and
// `content_offset + size` // `content_offset + size`
if !(start < self.output_size + self.content_offset) { if start >= self.output_size + self.content_offset {
return; return;
} }

View File

@ -82,7 +82,7 @@ where
debug_assert!(current_width <= available_width + delimiter_width); debug_assert!(current_width <= available_width + delimiter_width);
Span { Span {
length: length, length,
width: current_width, width: current_width,
} }
} }

View File

@ -19,8 +19,8 @@ impl<R: Read> ProgressReader<R> {
/// many bytes should be received. /// many bytes should be received.
pub fn new(counter: Counter, reader: R) -> Self { pub fn new(counter: Counter, reader: R) -> Self {
ProgressReader { ProgressReader {
reader: reader, reader,
counter: counter, counter,
} }
} }

View File

@ -98,7 +98,7 @@ pub trait ViewWrapper: 'static {
/// Wraps the `important_area` method. /// Wraps the `important_area` method.
fn wrap_important_area(&self, size: Vec2) -> Rect { fn wrap_important_area(&self, size: Vec2) -> Rect {
self.with_view(|v| v.important_area(size)) self.with_view(|v| v.important_area(size))
.unwrap_or(Rect::from((0, 0))) .unwrap_or_else(|| Rect::from((0, 0)))
} }
} }

View File

@ -667,13 +667,13 @@ impl View for EditView {
offset, offset,
} if position.fits_in_rect(offset, (self.last_length, 1)) => } if position.fits_in_rect(offset, (self.last_length, 1)) =>
{ {
position.checked_sub(offset).map(|position| { if let Some(position) = position.checked_sub(offset) {
self.cursor = self.offset self.cursor = self.offset
+ simple_prefix( + simple_prefix(
&self.content[self.offset..], &self.content[self.offset..],
position.x, position.x,
).length; ).length;
}); }
} }
_ => return EventResult::Ignored, _ => return EventResult::Ignored,
} }

View File

@ -58,10 +58,7 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
where where
F: FnOnce(&mut Self::V) -> R, F: FnOnce(&mut Self::V) -> R,
{ {
self.view self.view.try_borrow_mut().ok().map(|mut v| f(&mut *v))
.try_borrow_mut()
.ok()
.map(|mut v| f(&mut *v))
} }
fn into_inner(mut self) -> Result<Self::V, Self> fn into_inner(mut self) -> Result<Self::V, Self>
@ -80,15 +77,14 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
// Some for<'b> weirdness here to please the borrow checker gods... // Some for<'b> weirdness here to please the borrow checker gods...
fn wrap_call_on_any<'a>( fn wrap_call_on_any<'a>(
&mut self, selector: &Selector, mut callback: BoxedCallback<'a> &mut self, selector: &Selector, mut callback: BoxedCallback<'a>,
) { ) {
match selector { match selector {
&Selector::Id(id) if id == self.id => callback(self), &Selector::Id(id) if id == self.id => callback(self),
s => { s => {
self.view if let Ok(mut v) = self.view.try_borrow_mut() {
.try_borrow_mut() v.deref_mut().call_on_any(s, callback);
.ok() }
.map(|mut v| v.deref_mut().call_on_any(s, callback));
} }
} }
} }

View File

@ -14,7 +14,7 @@ pub struct Layer<T: View> {
impl<T: View> Layer<T> { impl<T: View> Layer<T> {
/// Wraps the given view. /// Wraps the given view.
pub fn new(view: T) -> Self { pub fn new(view: T) -> Self {
Layer { view: view } Layer { view }
} }
inner_getters!(self.view: T); inner_getters!(self.view: T);

View File

@ -1,6 +1,3 @@
use Cursive;
use Printer;
use With;
use align::Align; use align::Align;
use event::{Callback, Event, EventResult, Key, MouseButton, MouseEvent}; use event::{Callback, Event, EventResult, Key, MouseButton, MouseEvent};
use menu::{MenuItem, MenuTree}; use menu::{MenuItem, MenuTree};
@ -11,6 +8,9 @@ use unicode_width::UnicodeWidthStr;
use vec::Vec2; use vec::Vec2;
use view::{Position, ScrollBase, View}; use view::{Position, ScrollBase, View};
use views::OnEventView; use views::OnEventView;
use Cursive;
use Printer;
use With;
/// Popup that shows a list of items. /// Popup that shows a list of items.
pub struct MenuPopup { pub struct MenuPopup {
@ -29,9 +29,7 @@ impl MenuPopup {
MenuPopup { MenuPopup {
menu, menu,
focus: 0, focus: 0,
scrollbase: ScrollBase::new() scrollbase: ScrollBase::new().scrollbar_offset(1).right_padding(0),
.scrollbar_offset(1)
.right_padding(0),
align: Align::top_left(), align: Align::top_left(),
on_dismiss: None, on_dismiss: None,
on_action: None, on_action: None,
@ -318,8 +316,7 @@ impl View for MenuPopup {
&& position && position
.checked_sub(offset + (0, 1)) .checked_sub(offset + (0, 1))
.map(|position| { .map(|position| {
self.scrollbase self.scrollbase.start_drag(position, self.last_size.x)
.start_drag(position, self.last_size.x)
}) })
.unwrap_or(false) => .unwrap_or(false) =>
{ {
@ -344,19 +341,16 @@ impl View for MenuPopup {
// eprintln!("Position: {:?} / {:?}", position, offset); // eprintln!("Position: {:?} / {:?}", position, offset);
// eprintln!("Last size: {:?}", self.last_size); // eprintln!("Last size: {:?}", self.last_size);
let inner_size = self.last_size.saturating_sub((2, 2)); let inner_size = self.last_size.saturating_sub((2, 2));
position.checked_sub(offset + (1, 1)).map( if let Some(position) = position.checked_sub(offset + (1, 1)) {
// `position` is not relative to the content // `position` is not relative to the content
// (It's inside the border) // (It's inside the border)
|position| {
if position < inner_size { if position < inner_size {
let focus = let focus = position.y + self.scrollbase.start_line;
position.y + self.scrollbase.start_line;
if !self.menu.children[focus].is_delimiter() { if !self.menu.children[focus].is_delimiter() {
self.focus = focus; self.focus = focus;
} }
} }
}, }
);
} }
Event::Mouse { Event::Mouse {
event: MouseEvent::Release(MouseButton::Left), event: MouseEvent::Release(MouseButton::Left),
@ -398,10 +392,8 @@ impl View for MenuPopup {
fn layout(&mut self, size: Vec2) { fn layout(&mut self, size: Vec2) {
self.last_size = size; self.last_size = size;
self.scrollbase.set_heights( self.scrollbase
size.y.saturating_sub(2), .set_heights(size.y.saturating_sub(2), self.menu.children.len());
self.menu.children.len(),
);
} }
fn important_area(&self, size: Vec2) -> Rect { fn important_area(&self, size: Vec2) -> Rect {

View File

@ -784,7 +784,7 @@ impl<T: 'static> View for SelectView<T> {
fn important_area(&self, size: Vec2) -> Rect { fn important_area(&self, size: Vec2) -> Rect {
self.selected_id() self.selected_id()
.map(|i| Rect::from_size((0, i), (size.x, 1))) .map(|i| Rect::from_size((0, i), (size.x, 1)))
.unwrap_or(Rect::from((0, 0))) .unwrap_or_else(|| Rect::from((0, 0)))
} }
} }
@ -796,7 +796,7 @@ struct Item<T> {
impl<T> Item<T> { impl<T> Item<T> {
fn new(label: String, value: T) -> Self { fn new(label: String, value: T) -> Self {
Item { Item {
label: label, label,
value: Rc::new(value), value: Rc::new(value),
} }
} }

View File

@ -17,7 +17,7 @@ impl<T: View> ShadowView<T> {
/// Wraps the given view. /// Wraps the given view.
pub fn new(view: T) -> Self { pub fn new(view: T) -> Self {
ShadowView { ShadowView {
view: view, view,
top_padding: true, top_padding: true,
left_padding: true, left_padding: true,
} }

View File

@ -15,7 +15,7 @@ impl<T> SizedView<T> {
/// Wraps the given view. /// Wraps the given view.
pub fn new(view: T) -> Self { pub fn new(view: T) -> Self {
SizedView { SizedView {
view: view, view,
size: Vec2::zero(), size: Vec2::zero(),
} }
} }

View File

@ -24,9 +24,9 @@ impl SliderView {
/// with one tick per block. /// with one tick per block.
pub fn new(orientation: Orientation, max_value: usize) -> Self { pub fn new(orientation: Orientation, max_value: usize) -> Self {
SliderView { SliderView {
orientation: orientation, orientation,
value: 0, value: 0,
max_value: max_value, max_value,
on_change: None, on_change: None,
on_enter: None, on_enter: None,
dragging: false, dragging: false,
@ -186,10 +186,10 @@ impl View for SliderView {
offset, offset,
} if position.fits_in_rect(offset, self.req_size()) => } if position.fits_in_rect(offset, self.req_size()) =>
{ {
position.checked_sub(offset).map(|position| { if let Some(position) = position.checked_sub(offset) {
self.dragging = true; self.dragging = true;
self.value = self.orientation.get(&position); self.value = self.orientation.get(&position);
}); }
self.get_change_result() self.get_change_result()
} }
Event::Mouse { Event::Mouse {

View File

@ -1,4 +1,3 @@
use {Printer, With, XY};
use direction::Direction; use direction::Direction;
use event::{Event, EventResult, Key, MouseButton, MouseEvent}; use event::{Event, EventResult, Key, MouseButton, MouseEvent};
use rect::Rect; use rect::Rect;
@ -9,6 +8,7 @@ use unicode_width::UnicodeWidthStr;
use utils::lines::simple::{prefix, simple_prefix, LinesIterator, Row}; use utils::lines::simple::{prefix, simple_prefix, LinesIterator, Row};
use vec::Vec2; use vec::Vec2;
use view::{ScrollBase, SizeCache, View}; use view::{ScrollBase, SizeCache, View};
use {Printer, With, XY};
/// Multi-lines text editor. /// Multi-lines text editor.
/// ///
@ -36,9 +36,7 @@ pub struct TextArea {
} }
fn make_rows(text: &str, width: usize) -> Vec<Row> { fn make_rows(text: &str, width: usize) -> Vec<Row> {
LinesIterator::new(text, width) LinesIterator::new(text, width).show_spaces().collect()
.show_spaces()
.collect()
} }
new_default!(TextArea); new_default!(TextArea);
@ -290,8 +288,7 @@ impl TextArea {
fn compute_rows(&mut self, size: Vec2) { fn compute_rows(&mut self, size: Vec2) {
self.soft_compute_rows(size); self.soft_compute_rows(size);
self.scrollbase self.scrollbase.set_heights(size.y, self.rows.len());
.set_heights(size.y, self.rows.len());
} }
fn backspace(&mut self) { fn backspace(&mut self) {
@ -388,16 +385,11 @@ impl TextArea {
let last_byte = self.content[self.cursor..] let last_byte = self.content[self.cursor..]
.find('\n') .find('\n')
.map(|i| 1 + i + self.cursor); .map(|i| 1 + i + self.cursor);
let last_row = last_byte.map_or(self.rows.len(), |last_byte| { let last_row = last_byte
self.row_at(last_byte) .map_or(self.rows.len(), |last_byte| self.row_at(last_byte));
});
let last_byte = last_byte.unwrap_or_else(|| self.content.len()); let last_byte = last_byte.unwrap_or_else(|| self.content.len());
debug!( debug!("Content: `{}` (len={})", self.content, self.content.len());
"Content: `{}` (len={})",
self.content,
self.content.len()
);
debug!("start/end: {}/{}", first_byte, last_byte); debug!("start/end: {}/{}", first_byte, last_byte);
debug!("start/end rows: {}/{}", first_row, last_row); debug!("start/end rows: {}/{}", first_row, last_row);
@ -431,14 +423,11 @@ impl TextArea {
// Otherwise, replace stuff. // Otherwise, replace stuff.
let affected_rows = first_row..last_row; let affected_rows = first_row..last_row;
let replacement_rows = new_rows let replacement_rows =
.into_iter() new_rows.into_iter().map(|row| row.shifted(first_byte));
.map(|row| row.shifted(first_byte)); self.rows.splice(affected_rows, replacement_rows);
self.rows
.splice(affected_rows, replacement_rows);
self.fix_ghost_row(); self.fix_ghost_row();
self.scrollbase self.scrollbase.set_heights(size.y, self.rows.len());
.set_heights(size.y, self.rows.len());
} }
} }
@ -451,18 +440,10 @@ impl View for TextArea {
// (we always keep a space at the end) // (we always keep a space at the end)
// And y = number of rows // And y = number of rows
debug!("{:?}", self.rows); debug!("{:?}", self.rows);
let scroll_width = if self.rows.len() > constraint.y { let scroll_width = if self.rows.len() > constraint.y { 1 } else { 0 };
1
} else {
0
};
Vec2::new( Vec2::new(
scroll_width + 1 scroll_width + 1
+ self.rows + self.rows.iter().map(|r| r.width).max().unwrap_or(1),
.iter()
.map(|r| r.width)
.max()
.unwrap_or(1),
self.rows.len(), self.rows.len(),
) )
} }
@ -573,8 +554,7 @@ impl View for TextArea {
} if position } if position
.checked_sub(offset) .checked_sub(offset)
.map(|position| { .map(|position| {
self.scrollbase self.scrollbase.start_drag(position, self.last_size.x)
.start_drag(position, self.last_size.x)
}) })
.unwrap_or(false) => .unwrap_or(false) =>
{ {
@ -593,13 +573,10 @@ impl View for TextArea {
event: MouseEvent::Press(_), event: MouseEvent::Press(_),
position, position,
offset, offset,
} if position.fits_in_rect(offset, self.last_size) => } if !self.rows.is_empty()
&& position.fits_in_rect(offset, self.last_size) =>
{ {
position.checked_sub(offset).map(|position| { if let Some(position) = position.checked_sub(offset) {
if self.rows.is_empty() {
return;
}
let y = position.y + self.scrollbase.start_line; let y = position.y + self.scrollbase.start_line;
let y = min(y, self.rows.len() - 1); let y = min(y, self.rows.len() - 1);
let x = position.x; let x = position.x;
@ -607,7 +584,7 @@ impl View for TextArea {
let content = &self.content[row.start..row.end]; let content = &self.content[row.start..row.end];
self.cursor = row.start + simple_prefix(content, x).length; self.cursor = row.start + simple_prefix(content, x).length;
}); }
} }
_ => return EventResult::Ignored, _ => return EventResult::Ignored,
} }

View File

@ -21,7 +21,7 @@ impl<T: View> TrackedView<T> {
/// Creates a new `TrackedView` around `view`. /// Creates a new `TrackedView` around `view`.
pub fn new(view: T) -> Self { pub fn new(view: T) -> Self {
TrackedView { TrackedView {
view: view, view,
offset: Cell::new(Vec2::zero()), offset: Cell::new(Vec2::zero()),
} }
} }