-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from jnises/strip-incoming-escapes
Strip incoming escapes
- Loading branch information
Showing
5 changed files
with
396 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
[package] | ||
name = "colight" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = "1.0.72" | ||
clap = { version = "4.4.8", features = ["derive"] } | ||
colorous = "1.0.12" | ||
scopeguard = "1.2.0" | ||
strip-ansi-escapes = "0.2.0" | ||
termcolor = "1.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/// TODO: there is a better standard way of doing this right? | ||
use std::{ | ||
cell::RefCell, | ||
collections::VecDeque, | ||
io::{Read, Write}, | ||
rc::Rc, | ||
}; | ||
|
||
struct ReadHalf(Rc<RefCell<VecDeque<u8>>>); | ||
|
||
impl Read for ReadHalf { | ||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | ||
self.0.borrow_mut().read(buf) | ||
} | ||
} | ||
|
||
struct WriteHalf(Rc<RefCell<VecDeque<u8>>>); | ||
|
||
impl Write for WriteHalf { | ||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | ||
self.0.borrow_mut().extend(buf); | ||
Ok(buf.len()) | ||
} | ||
|
||
fn flush(&mut self) -> std::io::Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn make_read_writer() -> (ReadHalf, WriteHalf) { | ||
let buf = Rc::new(RefCell::new(VecDeque::new())); | ||
(ReadHalf(buf.clone()), WriteHalf(buf)) | ||
} | ||
|
||
pub(crate) struct AnsiStripReader<R> { | ||
input: R, | ||
buf: ReadHalf, | ||
stripper: strip_ansi_escapes::Writer<WriteHalf>, | ||
} | ||
|
||
impl<R> AnsiStripReader<R> { | ||
pub(crate) fn new(input: R) -> Self { | ||
let (buf, writer) = make_read_writer(); | ||
let stripper = strip_ansi_escapes::Writer::new(writer); | ||
Self { | ||
input, | ||
buf, | ||
stripper, | ||
} | ||
} | ||
} | ||
|
||
impl<R> Read for AnsiStripReader<R> | ||
where | ||
R: Read, | ||
{ | ||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | ||
loop { | ||
match self.buf.read(buf)? { | ||
0 => { | ||
let mut input_buf = [0; 64]; | ||
match self.input.read(&mut input_buf)? { | ||
0 => return Ok(0), | ||
n => { | ||
self.stripper.write_all(&input_buf[..n])?; | ||
} | ||
} | ||
} | ||
n => return Ok(n), | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.