From 71ac03f287e4b3db12b3b90d92164aa697244ae7 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Fri, 5 Jul 2019 16:23:28 +1200 Subject: [PATCH] Improve ansi handling --- Cargo.toml | 4 ++-- src/plugins/binaryview.rs | 49 ++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index af69af47a..e1f5edb2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,8 @@ chrono-humanize = "0.0.11" byte-unit = "2.1.0" ordered-float = {version = "1.0.2", features = ["serde"]} prettyprint = "0.7.0" -futures-preview = { version = "0.3.0-alpha.16", features = ["compat", "io-compat"] } -futures-sink-preview = "0.3.0-alpha.16" +futures-preview = { version = "=0.3.0-alpha.16", features = ["compat", "io-compat"] } +futures-sink-preview = "=0.3.0-alpha.16" futures_codec = "0.2.2" term = "0.5.2" bytes = "0.4.12" diff --git a/src/plugins/binaryview.rs b/src/plugins/binaryview.rs index 5e0e85fc2..4f9c7bd95 100644 --- a/src/plugins/binaryview.rs +++ b/src/plugins/binaryview.rs @@ -1,4 +1,4 @@ -use crossterm::{cursor, terminal, Attribute, Color, Colored, RawScreen}; +use crossterm::{cursor, terminal, Attribute, RawScreen}; use indexmap::IndexMap; use nu::{serve_plugin, Args, CommandConfig, Plugin, ShellError, Value}; @@ -52,17 +52,6 @@ fn view_binary(b: &[u8]) -> Result<(), Box> { Ok(()) } -#[derive(PartialEq, Debug)] -pub enum JoyButton { - A = 0, - B = 1, - Select = 2, - Start = 3, - Up = 4, - Down = 5, - Left = 6, - Right = 7, -} pub struct Context { pub width: usize, pub height: usize, @@ -86,28 +75,40 @@ impl Context { let cursor = cursor(); cursor.goto(0, 0)?; - let mut prev_color = None; + let mut prev_color: Option<(u8,u8,u8)> = None; + let mut prev_count = 1; for pixel in &self.frame_buffer { match prev_color { Some(c) if c == pixel.1 => { - print!("{}", pixel.0); + prev_count += 1; + } + Some(c) => { + print!( + "{}", + ansi_term::Colour::RGB(c.0, c.1, c.2) + .paint((0..prev_count).map(|_| pixel.0).collect::()) + ); + prev_color = Some(pixel.1); + prev_count = 1; } _ => { - prev_color = Some(pixel.1); - print!( - "{}{}", - Colored::Fg(Color::Rgb { - r: (pixel.1).0, - g: (pixel.1).1, - b: (pixel.1).2 - }), - pixel.0 - ) + prev_color = Some(pixel.1); + prev_count = 1; } } } + if prev_count > 0 { + if let Some(color) = prev_color { + print!( + "{}", + ansi_term::Colour::RGB(color.0, color.1, color.2) + .paint((0..prev_count).map(|_| "@").collect::()) + ); + } + } + println!("{}", Attribute::Reset); Ok(())