File: strip-ansi-escapes.rs

package info (click to toggle)
rust-termwiz 0.22.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,036 kB
  • sloc: sh: 10,450; makefile: 18
file content (30 lines) | stat: -rw-r--r-- 891 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//! This is a little utility that strips escape sequences from
//! stdin and prints the result on stdout.
//! It preserves only printable characters and CR, LF and HT.
use std::io::{Read, Result};
use termwiz::escape::parser::Parser;
use termwiz::escape::{Action, ControlCode};

fn main() -> Result<()> {
    let mut buf = [0u8; 4096];

    let mut parser = Parser::new();

    loop {
        let len = std::io::stdin().read(&mut buf)?;
        if len == 0 {
            return Ok(());
        }

        parser.parse(&buf[0..len], |action| match action {
            Action::Print(c) => print!("{}", c),
            Action::Control(c) => match c {
                ControlCode::HorizontalTab
                | ControlCode::LineFeed
                | ControlCode::CarriageReturn => print!("{}", c as u8 as char),
                _ => {}
            },
            _ => {}
        });
    }
}