Skip to content

Commit

Permalink
Merge pull request #140 from ZapAnton/fix_clippy_warnings
Browse files Browse the repository at this point in the history
Fixed several clippy warnings
  • Loading branch information
o2sh authored Oct 31, 2019
2 parents 0815e8a + 615eecd commit 1e10bd2
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 87 deletions.
30 changes: 16 additions & 14 deletions src/ascii_art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ impl<'a> AsciiArt<'a> {

AsciiArt {
content: Box::new(lines.into_iter()),
colors: colors,
bold: bold,
start: start,
end: end,
colors,
bold,
start,
end,
}
}
pub fn width(&self) -> usize {
Expand Down Expand Up @@ -155,7 +155,7 @@ impl<'a> Tokens<'a> {
})
}
/// render a truncated line of tokens.
fn render(self, colors: &Vec<Color>, start: usize, end: usize, bold: bool) -> String {
fn render(self, colors: &[Color], start: usize, end: usize, bold: bool) -> String {
assert!(start <= end);
let mut width = end - start;
let mut colored_segment = String::new();
Expand All @@ -169,7 +169,7 @@ impl<'a> Tokens<'a> {
colored_segment.push(chr);
}
Token::Color(col) => {
add_colored_segment(&mut whole_string, &colored_segment, color, bold);
add_colored_segment(&mut whole_string, &colored_segment, *color, bold);
colored_segment = String::new();
color = colors.get(col as usize).unwrap_or(&Color::White);
}
Expand All @@ -180,7 +180,7 @@ impl<'a> Tokens<'a> {
};
});

add_colored_segment(&mut whole_string, &colored_segment, color, bold);
add_colored_segment(&mut whole_string, &colored_segment, *color, bold);
(0..width).for_each(|_| whole_string.push(' '));
whole_string
}
Expand All @@ -198,8 +198,8 @@ fn succeed_when<I>(predicate: impl FnOnce(I) -> bool) -> impl FnOnce(I) -> Optio
}
}

fn add_colored_segment(base: &mut String, segment: &String, color: &Color, bold: bool) {
let mut colored_segment = segment.color(*color);
fn add_colored_segment(base: &mut String, segment: &str, color: Color, bold: bool) {
let mut colored_segment = segment.color(color);
if bold {
colored_segment = colored_segment.bold();
}
Expand All @@ -210,7 +210,7 @@ fn add_colored_segment(base: &mut String, segment: &String, color: &Color, bold:

type ParseResult<'a, R> = Option<(&'a str, R)>;

fn token<'a, R>(s: &'a str, predicate: impl FnOnce(char) -> Option<R>) -> ParseResult<'a, R> {
fn token<R>(s: &str, predicate: impl FnOnce(char) -> Option<R>) -> ParseResult<R> {
let token = s.chars().next()?;
let result = predicate(token)?;
Some((s.get(1..).unwrap(), result))
Expand Down Expand Up @@ -267,7 +267,7 @@ mod test {
let colors_shim = Vec::new();

assert_eq!(
Tokens("").render(&colors_shim, 0, 0, true),
Tokens("").render(&colors_shim, 0, 0, true),
"\u{1b}[1;37m\u{1b}[0m"
);

Expand Down Expand Up @@ -316,11 +316,11 @@ mod test {
"\u{1b}[37m \u{1b}[0m"
);
}

#[test]
fn truncate() {
assert_eq!(
Tokens("").truncate(0, 0).collect::<Vec<_>>(),
Tokens("").truncate(0, 0).collect::<Vec<_>>(),
Tokens("").collect::<Vec<_>>()
);

Expand Down Expand Up @@ -350,7 +350,9 @@ mod test {
);

assert_eq!(
Tokens(" {1} {5} {9} a").truncate(4, 10).collect::<Vec<_>>(),
Tokens(" {1} {5} {9} a")
.truncate(4, 10)
.collect::<Vec<_>>(),
Tokens("{1}{5} {9} a").collect::<Vec<_>>()
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commit_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl CommitInfo {
impl std::fmt::Display for CommitInfo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let short_commit = self.commit.to_string().chars().take(7).collect::<String>();
if self.refs.len() > 0 {
if !self.refs.is_empty() {
let refs_str = self
.refs
.iter()
Expand Down
20 changes: 9 additions & 11 deletions src/image_backends/kitty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl KittyBackend {
let mut old_attributes: termios = std::mem::zeroed();
tcgetattr(STDIN_FILENO, &mut old_attributes);

let mut new_attributes = old_attributes.clone();
let mut new_attributes = old_attributes;
new_attributes.c_lflag &= !ICANON;
new_attributes.c_lflag &= !ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &new_attributes);
Expand All @@ -46,15 +46,13 @@ impl KittyBackend {
let (stop_sender, stop_receiver) = mpsc::channel::<()>();
std::thread::spawn(move || {
let mut buf = Vec::<u8>::new();
let allowed_bytes = [0x1B, '_' as u8, 'G' as u8, '\\' as u8];
let allowed_bytes = [0x1B, b'_', b'G', b'\\'];
for byte in std::io::stdin().lock().bytes() {
let byte = byte.unwrap();
if allowed_bytes.contains(&byte) {
buf.push(byte);
}
if buf.starts_with(&[0x1B, '_' as u8, 'G' as u8])
&& buf.ends_with(&[0x1B, '\\' as u8])
{
if buf.starts_with(&[0x1B, b'_', b'G']) && buf.ends_with(&[0x1B, b'\\']) {
sender.send(()).unwrap();
return;
}
Expand All @@ -64,7 +62,7 @@ impl KittyBackend {
}
}
});
if let Ok(_) = receiver.recv_timeout(Duration::from_millis(50)) {
if receiver.recv_timeout(Duration::from_millis(50)).is_ok() {
unsafe {
tcsetattr(STDIN_FILENO, TCSANOW, &old_attributes);
}
Expand All @@ -86,17 +84,17 @@ impl super::ImageBackend for KittyBackend {
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_size);
tty_size
};
let width_ratio = tty_size.ws_col as f64 / tty_size.ws_xpixel as f64;
let height_ratio = tty_size.ws_row as f64 / tty_size.ws_ypixel as f64;
let width_ratio = f64::from(tty_size.ws_col) / f64::from(tty_size.ws_xpixel);
let height_ratio = f64::from(tty_size.ws_row) / f64::from(tty_size.ws_ypixel);

// resize image to fit the text height with the Lanczos3 algorithm
let image = image.resize(
u32::max_value(),
(lines.len() as f64 / height_ratio) as u32,
FilterType::Lanczos3,
);
let _image_columns = width_ratio * image.width() as f64;
let image_rows = height_ratio * image.height() as f64;
let _image_columns = width_ratio * f64::from(image.width());
let image_rows = height_ratio * f64::from(image.height());

// convert the image to rgba samples
let rgba_image = image.to_rgba();
Expand All @@ -108,7 +106,7 @@ impl super::ImageBackend for KittyBackend {
image.width() as usize * image.height() as usize * 4,
raw_image.len()
);

let encoded_image = base64::encode(&raw_image); // image data is base64 encoded
let mut image_data = Vec::<u8>::new();
for chunk in encoded_image.as_bytes().chunks(4096) {
Expand Down
Loading

0 comments on commit 1e10bd2

Please sign in to comment.