Warnings cleanup (#198)

This commit is contained in:
Zrzka 2019-09-05 16:13:23 +02:00 committed by Timon
parent ec44719d2c
commit 68346699e1
8 changed files with 76 additions and 72 deletions

View File

@ -53,7 +53,7 @@ impl TerminalInput {
pub fn read_line(&self) -> io::Result<String> {
let mut rv = String::new();
io::stdin().read_line(&mut rv)?;
let len = rv.trim_right_matches(&['\r', '\n'][..]).len();
let len = rv.trim_end_matches(&['\r', '\n'][..]).len();
rv.truncate(len);
Ok(rv)
}

View File

@ -100,7 +100,7 @@ pub struct AsyncReader {
impl AsyncReader {
/// Construct a new instance of the `AsyncReader`.
/// The reading will immediately start when calling this function.
pub fn new(function: Box<Fn(&Sender<u8>, &Arc<AtomicBool>) + Send>) -> AsyncReader {
pub fn new(function: Box<dyn Fn(&Sender<u8>, &Arc<AtomicBool>) + Send>) -> AsyncReader {
let shutdown_handle = Arc::new(AtomicBool::new(false));
let (event_tx, event_rx) = mpsc::channel();
@ -242,7 +242,7 @@ where
Some(b'O') => {
match iter.next() {
// F1-F4
Some(val @ b'P'...b'S') => {
Some(val @ b'P'..=b'S') => {
InputEvent::Keyboard(KeyEvent::F(1 + val - b'P'))
}
_ => return Err(error),
@ -263,10 +263,10 @@ where
b'\n' | b'\r' => InputEvent::Keyboard(KeyEvent::Char('\n')),
b'\t' => InputEvent::Keyboard(KeyEvent::Char('\t')),
b'\x7F' => InputEvent::Keyboard(KeyEvent::Backspace),
c @ b'\x01'...b'\x1A' => {
c @ b'\x01'..=b'\x1A' => {
InputEvent::Keyboard(KeyEvent::Ctrl((c as u8 - 0x1 + b'a') as char))
}
c @ b'\x1C'...b'\x1F' => {
c @ b'\x1C'..=b'\x1F' => {
InputEvent::Keyboard(KeyEvent::Ctrl((c as u8 - 0x1C + b'4') as char))
}
b'\0' => InputEvent::Keyboard(KeyEvent::Null),
@ -290,7 +290,7 @@ where
Some(b'[') => match iter.next() {
// NOTE (@imdaveho): cannot find when this occurs;
// having another '[' after ESC[ not a likely scenario
Some(val @ b'A'...b'E') => InputEvent::Keyboard(KeyEvent::F(1 + val - b'A')),
Some(val @ b'A'..=b'E') => InputEvent::Keyboard(KeyEvent::F(1 + val - b'A')),
_ => InputEvent::Unknown,
},
Some(b'D') => InputEvent::Keyboard(KeyEvent::Left),
@ -350,7 +350,7 @@ where
let cy = nums.next().unwrap().parse::<u16>().unwrap();
match cb {
0...2 | 64...65 => {
0..=2 | 64..=65 => {
let button = match cb {
0 => MouseButton::Left,
1 => MouseButton::Middle,
@ -370,7 +370,7 @@ where
_ => InputEvent::Unknown,
}
}
Some(c @ b'0'...b'9') => {
Some(c @ b'0'..=b'9') => {
// Numbered escape code.
let mut buf = Vec::new();
buf.push(c);
@ -430,9 +430,9 @@ where
4 | 8 => InputEvent::Keyboard(KeyEvent::End),
5 => InputEvent::Keyboard(KeyEvent::PageUp),
6 => InputEvent::Keyboard(KeyEvent::PageDown),
v @ 11...15 => InputEvent::Keyboard(KeyEvent::F(v - 10)),
v @ 17...21 => InputEvent::Keyboard(KeyEvent::F(v - 11)),
v @ 23...24 => InputEvent::Keyboard(KeyEvent::F(v - 12)),
v @ 11..=15 => InputEvent::Keyboard(KeyEvent::F(v - 10)),
v @ 17..=21 => InputEvent::Keyboard(KeyEvent::F(v - 11)),
v @ 23..=24 => InputEvent::Keyboard(KeyEvent::F(v - 12)),
_ => InputEvent::Unknown,
}
}

View File

@ -1,4 +1,6 @@
use crate::{execute, impl_display, queue, write_cout, Result};
#[cfg(windows)]
use crate::Result;
use crate::{execute, impl_display, queue, write_cout};
use std::fmt::Display;
use std::io::Write;
@ -31,13 +33,13 @@ pub trait Command {
/// This can be used in order to get more performance.
pub trait QueueableCommand<T: Display> {
/// Queues the given command for later execution.
fn queue(mut self, command: impl Command<AnsiType = T>) -> Self;
fn queue(self, command: impl Command<AnsiType = T>) -> Self;
}
/// A trait that defines behaviour for a command that will be executed immediately.
pub trait ExecutableCommand<T: Display> {
/// Execute the given command directly.
fn execute(mut self, command: impl Command<AnsiType = T>) -> Self;
fn execute(self, command: impl Command<AnsiType = T>) -> Self;
}
impl<T, A> QueueableCommand<A> for T
@ -66,7 +68,7 @@ where
/// Because of that there is no difference between `execute` and `queue` for those windows versions.
/// - Queuing might sound that there is some scheduling going on, however, this means that we write to the stdout without flushing which will cause commands to be stored in the buffer without them being written to the terminal.
fn queue(mut self, command: impl Command<AnsiType = A>) -> Self {
queue!(self, command);
let _ = queue!(self, command);
self
}
}
@ -90,7 +92,7 @@ where
/// This is happening because Windows versions lower then 10 do not support ANSI codes, and thus they can't be written to the given buffer.
/// Because of that there is no difference between `execute` and `queue` for those windows versions.
fn execute(mut self, command: impl Command<AnsiType = A>) -> Self {
execute!(self, command);
let _ = execute!(self, command);
self
}
}

View File

@ -1,5 +1,4 @@
/// Append a the first few characters of an ANSI escape code to the given string.
#[macro_export]
macro_rules! csi {
($( $l:expr ),*) => { concat!("\x1B[", $( $l ),*) };
@ -66,42 +65,43 @@ macro_rules! write_cout {
/// - Queuing might sound that there is some scheduling going on, however, this means that we write to the stdout without flushing which will cause commands to be stored in the buffer without them being written to the terminal.
#[macro_export]
macro_rules! queue {
($write:expr, $($command:expr), *) =>
{{
($write:expr, $($command:expr), *) => {{
// Silent warning when the macro is used inside the `command` module
#[allow(unused_imports)]
use $crate::Command;
let mut error = None;
$(
#[cfg(windows)]
{
{
if $crate::supports_ansi() {
match write!($write, "{}",$command.get_ansi_code()) {
match write!($write, "{}", $command.get_ansi_code()) {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
};
} else {
match $command.execute_winapi() {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
match $command.execute_winapi() {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
};
}
#[cfg(unix)]
match write!($write, "{}",$command.get_ansi_code()) {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
#[cfg(unix)]
match write!($write, "{}", $command.get_ansi_code()) {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
)*
if let Some(error) = error {
error
}else {
} else {
Ok(())
}
}}
@ -137,42 +137,43 @@ macro_rules! queue {
/// Because of that there is no difference between `execute` and `queue` for those windows versions.
#[macro_export]
macro_rules! execute {
($write:expr, $($command:expr), *) =>
{{
use $crate::{Command, write_cout};
($write:expr, $($command:expr), *) => {{
// Silent warning when the macro is used inside the `command` module
#[allow(unused_imports)]
use $crate::{Command, write_cout};
let mut error = None;
$(
#[cfg(windows)]
{
{
if $crate::supports_ansi() {
match write_cout!($write, $command.get_ansi_code()) {
match write_cout!($write, $command.get_ansi_code()) {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
};
} else {
match $command.execute_winapi() {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
match $command.execute_winapi() {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
};
}
#[cfg(unix)]
match write_cout!($write, $command.get_ansi_code()) {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
#[cfg(unix)]
match write_cout!($write, $command.get_ansi_code()) {
Err(e) => {
error = Some(Err($crate::ErrorKind::from(e)));
}
_ => {}
};
)*
if let Some(error) = error {
error
}else {
} else {
Ok(())
}
}}

View File

@ -1,19 +1,19 @@
#![allow(dead_code)]
extern crate crossterm;
use crossterm::{
execute, queue, Clear, ClearType, Color, Colorize, ExecutableCommand, Goto, Output,
PrintStyledFont, QueueableCommand,
execute, queue, Clear, ClearType, ExecutableCommand, Goto, Output, QueueableCommand,
};
use std::fmt::Display;
use std::io::{stdout, Stdout, Write};
use std::io::{stdout, Write};
/// execute commands by using normal functions
fn execute_command_directly_using_functions() {
// single command
stdout().execute(Output("Text1 ".to_string()));
let _ = stdout().execute(Output("Text1 ".to_string()));
// multiple commands
stdout()
let _ = stdout()
.execute(Output("Text2 ".to_string()))
.execute(Output("Text3 ".to_string()));
}
@ -21,10 +21,10 @@ fn execute_command_directly_using_functions() {
/// execute commands by using macro's
fn execute_command_directly_using_macros() {
// single command
execute!(stdout(), Output("Text1 ".to_string()));
let _ = execute!(stdout(), Output("Text1 ".to_string()));
// multiple commands
execute!(
let _ = execute!(
stdout(),
Output("Text2 ".to_string()),
Output("Text 3".to_string())
@ -49,7 +49,7 @@ fn later_execution_command_using_functions() {
::std::thread::sleep(std::time::Duration::from_millis(2000));
// when you call this all commands will be executed
sdout.flush();
let _ = sdout.flush();
}
/// queue commands without executing them directly by using macro's
@ -57,10 +57,10 @@ fn later_execution_command_directly_using_macros() {
let mut stdout = stdout();
// single command
queue!(stdout, Output("Text1 ".to_string()));
let _ = queue!(stdout, Output("Text1 ".to_string()));
// multiple commands
queue!(
let _ = queue!(
stdout,
Clear(ClearType::All),
Goto(5, 5),
@ -70,7 +70,7 @@ fn later_execution_command_directly_using_macros() {
::std::thread::sleep(std::time::Duration::from_millis(2000));
// when you call this all commands will be executed
stdout.flush();
let _ = stdout.flush();
}
fn main() {}

View File

@ -1,3 +1,6 @@
// Remove once the TODO below is fixed
#![allow(unused_variables)]
extern crate crossterm;
use crossterm::{Color, Crossterm};

View File

@ -4,7 +4,6 @@
extern crate crossterm;
use self::crossterm::{color, Attribute, Color, Colored, Colorize, Styler};
use std::io::stdout;
/// print some red text | demonstration.
pub fn paint_foreground() {
@ -412,6 +411,5 @@ pub fn reset_fg_and_bg() {
}
fn main() {
use std::io::Write;
print_all_background_colors_with_method()
}

View File

@ -136,5 +136,5 @@ pub fn exit() {
}
fn main() {
scroll_down();
let _ = scroll_down();
}