Fix clippy warnings

This commit is contained in:
Alexandre Bury 2019-04-17 11:47:01 -07:00
parent 34ecb67f1b
commit 26ea559479
9 changed files with 142 additions and 115 deletions

View File

@ -101,7 +101,7 @@ impl Backend {
offset: Vec2::zero(), offset: Vec2::zero(),
} }
}) })
.unwrap_or(Event::Unknown(vec![])) .unwrap_or_else(|| Event::Unknown(vec![]))
} }
BltEvent::ShiftReleased | BltEvent::ControlReleased => { BltEvent::ShiftReleased | BltEvent::ControlReleased => {
Event::Refresh Event::Refresh
@ -162,7 +162,7 @@ impl Backend {
offset: Vec2::zero(), offset: Vec2::zero(),
} }
}) })
.unwrap_or(Event::Unknown(vec![])), .unwrap_or_else(|| Event::Unknown(vec![])),
KeyCode::A KeyCode::A
| KeyCode::B | KeyCode::B
| KeyCode::C | KeyCode::C
@ -237,6 +237,10 @@ impl Backend {
} }
impl backend::Backend for Backend { impl backend::Backend for Backend {
fn name(&self) -> &str {
"bear-lib-terminal"
}
fn finish(&mut self) { fn finish(&mut self) {
terminal::close(); terminal::close();
} }
@ -353,9 +357,9 @@ fn colour_to_blt_colour(clr: Color, role: ColorRole) -> BltColor {
Color::Rgb(r, g, b) => (r, g, b), Color::Rgb(r, g, b) => (r, g, b),
Color::RgbLowRes(r, g, b) => ( Color::RgbLowRes(r, g, b) => (
(r as f32 / 5.0 * 255.0) as u8, (f32::from(r) / 5.0 * 255.0) as u8,
(g as f32 / 5.0 * 255.0) as u8, (f32::from(g) / 5.0 * 255.0) as u8,
(b as f32 / 5.0 * 255.0) as u8, (f32::from(b) / 5.0 * 255.0) as u8,
), ),
}; };
BltColor::from_rgb(r, g, b) BltColor::from_rgb(r, g, b)

View File

@ -23,7 +23,7 @@ pub struct Backend {
last_button: Option<MouseButton>, last_button: Option<MouseButton>,
// reader to read user input async. // reader to read user input async.
async_reader: AsyncReader, async_reader: AsyncReader,
alternate_screen: AlternateScreen, _alternate_screen: AlternateScreen,
stdout: RefCell<Stdout>, stdout: RefCell<Stdout>,
cursor: TerminalCursor, cursor: TerminalCursor,
terminal: Terminal, terminal: Terminal,
@ -35,19 +35,19 @@ impl Backend {
where where
Self: Sized, Self: Sized,
{ {
let alternate_screen = AlternateScreen::to_alternate(true)?; let _alternate_screen = AlternateScreen::to_alternate(true)?;
let input = input(); let input = input();
let async_reader = input.read_async(); let async_reader = input.read_async();
input.enable_mouse_mode().unwrap(); input.enable_mouse_mode().unwrap();
cursor().hide(); cursor().hide()?;
Ok(Box::new(Backend { Ok(Box::new(Backend {
current_style: Cell::new(theme::ColorPair::from_256colors(0, 0)), current_style: Cell::new(theme::ColorPair::from_256colors(0, 0)),
last_button: None, last_button: None,
async_reader, async_reader,
alternate_screen, _alternate_screen,
stdout: RefCell::new(io::stdout()), stdout: RefCell::new(io::stdout()),
terminal: terminal(), terminal: terminal(),
cursor: cursor(), cursor: cursor(),
@ -55,8 +55,8 @@ impl Backend {
} }
fn apply_colors(&self, colors: theme::ColorPair) { fn apply_colors(&self, colors: theme::ColorPair) {
with_color(&colors.front, |c| self.write(Colored::Fg(*c))); with_color(colors.front, |c| self.write(Colored::Fg(*c)));
with_color(&colors.back, |c| self.write(Colored::Bg(*c))); with_color(colors.back, |c| self.write(Colored::Bg(*c)));
} }
fn write<T>(&self, content: T) fn write<T>(&self, content: T)
@ -68,8 +68,7 @@ impl Backend {
fn map_key(&mut self, event: CInputEvent) -> Event { fn map_key(&mut self, event: CInputEvent) -> Event {
match event { match event {
CInputEvent::Keyboard(key_event) => { CInputEvent::Keyboard(key_event) => match key_event {
return match key_event {
CKeyEvent::Esc => Event::Key(Key::Esc), CKeyEvent::Esc => Event::Key(Key::Esc),
CKeyEvent::Backspace => Event::Key(Key::Backspace), CKeyEvent::Backspace => Event::Key(Key::Backspace),
CKeyEvent::Left => Event::Key(Key::Left), CKeyEvent::Left => Event::Key(Key::Left),
@ -90,10 +89,8 @@ impl Backend {
CKeyEvent::Ctrl(c) => Event::CtrlChar(c), CKeyEvent::Ctrl(c) => Event::CtrlChar(c),
CKeyEvent::Alt(c) => Event::AltChar(c), CKeyEvent::Alt(c) => Event::AltChar(c),
_ => Event::Unknown(vec![]), _ => Event::Unknown(vec![]),
}; },
} CInputEvent::Mouse(mouse_event) => match mouse_event {
CInputEvent::Mouse(mouse_event) => {
return match mouse_event {
CMouseEvent::Press(btn, x, y) => { CMouseEvent::Press(btn, x, y) => {
let position = (x - 1, y - 1).into(); let position = (x - 1, y - 1).into();
@ -115,35 +112,31 @@ impl Backend {
self.last_button = Some(btn); self.last_button = Some(btn);
} }
return Event::Mouse { Event::Mouse {
event, event,
position, position,
offset: Vec2::zero(), offset: Vec2::zero(),
};
} }
CMouseEvent::Release(x, y) }
if self.last_button.is_some() => CMouseEvent::Release(x, y) if self.last_button.is_some() => {
{ let event = MouseEvent::Release(self.last_button.unwrap());
let event =
MouseEvent::Release(self.last_button.unwrap());
let position = (x - 1, y - 1).into(); let position = (x - 1, y - 1).into();
return Event::Mouse { Event::Mouse {
event, event,
position, position,
offset: Vec2::zero(), offset: Vec2::zero(),
}; }
} }
CMouseEvent::Hold(x, y) if self.last_button.is_some() => { CMouseEvent::Hold(x, y) if self.last_button.is_some() => {
let event = let event = MouseEvent::Hold(self.last_button.unwrap());
MouseEvent::Hold(self.last_button.unwrap());
let position = (x - 1, y - 1).into(); let position = (x - 1, y - 1).into();
return Event::Mouse { Event::Mouse {
event, event,
position, position,
offset: Vec2::zero(), offset: Vec2::zero(),
}; }
} }
_ => { _ => {
log::warn!( log::warn!(
@ -152,8 +145,7 @@ impl Backend {
); );
Event::Unknown(vec![]) Event::Unknown(vec![])
} }
}; },
}
_ => { _ => {
log::warn!("Unknown mouse event {:?}!", event); log::warn!("Unknown mouse event {:?}!", event);
Event::Unknown(vec![]) Event::Unknown(vec![])
@ -163,16 +155,20 @@ impl Backend {
} }
impl backend::Backend for Backend { impl backend::Backend for Backend {
fn name(&self) -> &str {
"crossterm"
}
fn poll_event(&mut self) -> Option<Event> { fn poll_event(&mut self) -> Option<Event> {
self.async_reader.next().map(|event| self.map_key(event)) self.async_reader.next().map(|event| self.map_key(event))
} }
fn finish(&mut self) { fn finish(&mut self) {
self.cursor.goto(1, 1); self.cursor.goto(1, 1).unwrap();
self.terminal.clear(ClearType::All); self.terminal.clear(ClearType::All).unwrap();
self.write(Attribute::Reset); self.write(Attribute::Reset);
input().disable_mouse_mode(); input().disable_mouse_mode().unwrap();
cursor().show(); cursor().show().unwrap();
} }
fn refresh(&mut self) { fn refresh(&mut self) {
@ -186,11 +182,11 @@ impl backend::Backend for Backend {
fn screen_size(&self) -> Vec2 { fn screen_size(&self) -> Vec2 {
let size = self.terminal.terminal_size(); let size = self.terminal.terminal_size();
return Vec2::new(size.0 as usize + 1, size.1 as usize + 1); Vec2::from(size) + (1, 1)
} }
fn print_at(&self, pos: Vec2, text: &str) { fn print_at(&self, pos: Vec2, text: &str) {
self.cursor.goto(pos.x as u16, pos.y as u16); self.cursor.goto(pos.x as u16, pos.y as u16).unwrap();
self.write(text); self.write(text);
} }
@ -198,15 +194,15 @@ impl backend::Backend for Backend {
if repetitions > 0 { if repetitions > 0 {
let mut out = self.stdout.borrow_mut(); let mut out = self.stdout.borrow_mut();
self.cursor.goto(pos.x as u16, pos.y as u16); self.cursor.goto(pos.x as u16, pos.y as u16).unwrap();
// as I (Timon) wrote this I figured out that calling `write_str` for unix was flushing the stdout. // as I (Timon) wrote this I figured out that calling `write_str` for unix was flushing the stdout.
// Current work aground is writing bytes instead of a string to the terminal. // Current work aground is writing bytes instead of a string to the terminal.
out.write(text.as_bytes()).unwrap(); out.write_all(text.as_bytes()).unwrap();
let mut dupes_left = repetitions - 1; let mut dupes_left = repetitions - 1;
while dupes_left > 0 { while dupes_left > 0 {
out.write(text.as_bytes()).unwrap(); out.write_all(text.as_bytes()).unwrap();
dupes_left -= 1; dupes_left -= 1;
} }
} }
@ -218,7 +214,7 @@ impl backend::Backend for Backend {
back: color, back: color,
}); });
self.terminal.clear(ClearType::All); self.terminal.clear(ClearType::All).unwrap();
} }
fn set_color(&self, color: theme::ColorPair) -> theme::ColorPair { fn set_color(&self, color: theme::ColorPair) -> theme::ColorPair {
@ -229,7 +225,7 @@ impl backend::Backend for Backend {
self.current_style.set(color); self.current_style.set(color);
} }
return current_style; current_style
} }
fn set_effect(&self, effect: theme::Effect) { fn set_effect(&self, effect: theme::Effect) {
@ -253,11 +249,11 @@ impl backend::Backend for Backend {
} }
} }
fn with_color<F, R>(clr: &theme::Color, f: F) -> R fn with_color<F, R>(clr: theme::Color, f: F) -> R
where where
F: FnOnce(&Color) -> R, F: FnOnce(&Color) -> R,
{ {
match *clr { match clr {
theme::Color::Dark(theme::BaseColor::Black) => f(&Color::Black), theme::Color::Dark(theme::BaseColor::Black) => f(&Color::Black),
theme::Color::Dark(theme::BaseColor::Red) => f(&Color::DarkRed), theme::Color::Dark(theme::BaseColor::Red) => f(&Color::DarkRed),
theme::Color::Dark(theme::BaseColor::Green) => f(&Color::DarkGreen), theme::Color::Dark(theme::BaseColor::Green) => f(&Color::DarkGreen),

View File

@ -303,6 +303,10 @@ impl Backend {
} }
impl backend::Backend for Backend { impl backend::Backend for Backend {
fn name(&self) -> &str {
"ncurses"
}
fn screen_size(&self) -> Vec2 { fn screen_size(&self) -> Vec2 {
let mut x: i32 = 0; let mut x: i32 = 0;
let mut y: i32 = 0; let mut y: i32 = 0;

View File

@ -133,7 +133,7 @@ impl Backend {
return Some(event); return Some(event);
} }
let event = if let Some(ev) = self.window.getch() { if let Some(ev) = self.window.getch() {
Some(match ev { Some(match ev {
pancurses::Input::Character('\n') => Event::Key(Key::Enter), pancurses::Input::Character('\n') => Event::Key(Key::Enter),
// TODO: wait for a very short delay. If more keys are // TODO: wait for a very short delay. If more keys are
@ -286,8 +286,7 @@ impl Backend {
}) })
} else { } else {
None None
}; }
event
} }
fn parse_mouse_event(&mut self) -> Event { fn parse_mouse_event(&mut self) -> Event {
@ -307,7 +306,7 @@ impl Backend {
let make_event = |event| Event::Mouse { let make_event = |event| Event::Mouse {
offset: Vec2::zero(), offset: Vec2::zero(),
position: Vec2::new(mevent.x as usize, mevent.y as usize), position: Vec2::new(mevent.x as usize, mevent.y as usize),
event: event, event,
}; };
if mevent.bstate == pancurses::REPORT_MOUSE_POSITION as mmask_t { if mevent.bstate == pancurses::REPORT_MOUSE_POSITION as mmask_t {
@ -352,6 +351,10 @@ impl Backend {
} }
impl backend::Backend for Backend { impl backend::Backend for Backend {
fn name(&self) -> &str {
"pancurses"
}
fn screen_size(&self) -> Vec2 { fn screen_size(&self) -> Vec2 {
// Coordinates are reversed here // Coordinates are reversed here
let (y, x) = self.window.get_max_yx(); let (y, x) = self.window.get_max_yx();

View File

@ -21,6 +21,10 @@ impl Backend {
} }
impl backend::Backend for Backend { impl backend::Backend for Backend {
fn name(&self) -> &str {
"dummy"
}
fn finish(&mut self) {} fn finish(&mut self) {}
fn refresh(&mut self) {} fn refresh(&mut self) {}

View File

@ -101,4 +101,11 @@ pub trait Backend {
/// Disables the given effect. /// Disables the given effect.
fn unset_effect(&self, effect: theme::Effect); fn unset_effect(&self, effect: theme::Effect);
/// Returns a name to identify the backend.
///
/// Mostly used for debugging.
fn name(&self) -> &str {
"unknown"
}
} }

View File

@ -96,8 +96,8 @@ impl Backend {
} }
fn apply_colors(&self, colors: theme::ColorPair) { fn apply_colors(&self, colors: theme::ColorPair) {
with_color(&colors.front, |c| self.write(tcolor::Fg(c))); with_color(colors.front, |c| self.write(tcolor::Fg(c)));
with_color(&colors.back, |c| self.write(tcolor::Bg(c))); with_color(colors.back, |c| self.write(tcolor::Bg(c)));
} }
fn map_key(&mut self, event: TEvent) -> Event { fn map_key(&mut self, event: TEvent) -> Event {
@ -183,6 +183,10 @@ impl Backend {
} }
impl backend::Backend for Backend { impl backend::Backend for Backend {
fn name(&self) -> &str {
"termion"
}
fn finish(&mut self) { fn finish(&mut self) {
write!( write!(
self.terminal.get_mut(), self.terminal.get_mut(),
@ -210,7 +214,7 @@ impl backend::Backend for Backend {
self.current_style.set(color); self.current_style.set(color);
} }
return current_style; current_style
} }
fn set_effect(&self, effect: theme::Effect) { fn set_effect(&self, effect: theme::Effect) {
@ -297,11 +301,11 @@ impl backend::Backend for Backend {
} }
} }
fn with_color<F, R>(clr: &theme::Color, f: F) -> R fn with_color<F, R>(clr: theme::Color, f: F) -> R
where where
F: FnOnce(&dyn tcolor::Color) -> R, F: FnOnce(&dyn tcolor::Color) -> R,
{ {
match *clr { match clr {
theme::Color::TerminalDefault => f(&tcolor::Reset), theme::Color::TerminalDefault => f(&tcolor::Reset),
theme::Color::Dark(theme::BaseColor::Black) => f(&tcolor::Black), theme::Color::Dark(theme::BaseColor::Black) => f(&tcolor::Black),
theme::Color::Dark(theme::BaseColor::Red) => f(&tcolor::Red), theme::Color::Dark(theme::BaseColor::Red) => f(&tcolor::Red),

View File

@ -96,8 +96,7 @@ cfg_if::cfg_if! {
Self::crossterm().unwrap() Self::crossterm().unwrap()
} }
} }
} } else if #[cfg(feature = "pancurses-backend")] {
else if #[cfg(feature = "pancurses-backend")] {
impl Default for Cursive { impl Default for Cursive {
fn default() -> Self { fn default() -> Self {
Self::pancurses().unwrap() Self::pancurses().unwrap()
@ -888,6 +887,13 @@ impl Cursive {
pub fn noop(&mut self) { pub fn noop(&mut self) {
// foo // foo
} }
/// Return the name of the backend used.
///
/// Mostly used for debugging.
pub fn backend_name(&self) -> &str {
self.backend.name()
}
} }
impl Drop for Cursive { impl Drop for Cursive {

View File

@ -134,9 +134,8 @@ impl<'a> Iterator for Parser<'a> {
/// Parse the given markdown text into a list of spans. /// Parse the given markdown text into a list of spans.
/// ///
/// This is a shortcut for `Parser::new(input).collect()`. /// This is a shortcut for `Parser::new(input).collect()`.
pub fn parse_spans<'a>(input: &'a str) -> Vec<StyledIndexedSpan> { pub fn parse_spans(input: &str) -> Vec<StyledIndexedSpan> {
Parser::new(input).collect() Parser::new(input).collect()
// Parser::new(input).inspect(|span| eprintln!("{:?}", span)).collect()
} }
#[cfg(test)] #[cfg(test)]