You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am developing a TUI program and one of the features is that I can open vim for any file. The problem is that vim outputs some ANSI escape codes that I would like to strip out (the enable and disable alternative screen codes). I'm trying to use combine to do so but am running into some difficulty and was wondering if I could get some guidance.
Here is what I current have:
I spawn vim as a std::process::Child
I wrap the child's stdout in a std::io::BufReader
I read from the buffer one byte at a time and create a ParitalStream with it
Reading one byte at a time probably isn't the best way to do this?
I then use parse_with_state on my parser
I can't seem to get this approach to compile b/c of some lifetime errors. Is this the right approach or is there a better way to do this w/ combine?
Here is a snippet of the code I described above:
let command_stdout:ChildStdout = child.stdout.take().unwrap();letmut reader:BufReader<ChildStdout> = BufReader::new(command_stdout);letmut buffer:[u8;1] = [0;1];letmut stdout = io::stdout().lock();letmut parser = any_partial_state(ansi_escaped_text::parser());letmut parser_state = AnyPartialState::default();letmut length:usize;loop{
length = {
reader.read(&mut buffer).unwrap()};if length == 0{break;}#[cfg(feature = "logging")]debug!("Got output {:?} from vim.", &buffer[..length]);// Parse the text so that we can strip out the ANSI escape codes for enabling and// disabling the alternative screen.letmut stream = PartialStream(&buffer[..]);let parsed_text = parser.parse_with_state(&mut stream,&mut parser_state);match parsed_text {Ok(ansi_escaped_text) => {match ansi_escaped_text {ANSIEscapedText::ANSIEscapeCode(ansi_escape_code) => {match ansi_escape_code {ANSIEscapeCode::EnableAlternativeScreen => {#[cfg(feature = "logging")]debug!("Stripping enable alternative screen ANSI escape code from vim's output.");},ANSIEscapeCode::DisableAlternativeScreen => {#[cfg(feature = "logging")]debug!("Stripping disable alternative screen ANSI escape code from vim's output.");}}},ANSIEscapedText::Character(character) => {
stdout.write(&[character]).unwrap();
stdout.flush().unwrap();}};},#[cfg(feature = "logging")]Err(error) => {error!("Failed to parse ANSI escaped text: {:?}", error);},#[cfg(not(feature = "logging"))]Err(_) => {},};}
The text was updated successfully, but these errors were encountered:
Hi,
I am developing a TUI program and one of the features is that I can open
vim
for any file. The problem is that vim outputs some ANSI escape codes that I would like to strip out (the enable and disable alternative screen codes). I'm trying to usecombine
to do so but am running into some difficulty and was wondering if I could get some guidance.Here is what I current have:
std::process::Child
std::io::BufReader
ParitalStream
with itparse_with_state
on my parserI can't seem to get this approach to compile b/c of some lifetime errors. Is this the right approach or is there a better way to do this w/
combine
?Here is a snippet of the code I described above:
The text was updated successfully, but these errors were encountered: