Skip to content

Commit

Permalink
Prepare from release
Browse files Browse the repository at this point in the history
  • Loading branch information
uttarayan21 committed Apr 30, 2021
1 parent a4e50fa commit 8ad6643
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 68 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Rust Build Test

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
41 changes: 0 additions & 41 deletions .github/workflows/docs.yaml

This file was deleted.

12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
[package]
name = "ansi-to-tui"
version = "0.1.6"
authors = ["Uttarayan Mondal <uttarayan21@gmail.com>"]
edition = "2018"
description = "A library to convert ansi color coded text into tui::text::Text type from tui-rs library"
keywords = ["ansi", "ascii", "tui", "parser"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/uttarayan21/ansi-to-tui"
exclude = [
".github/*",
"ascii",
"tests",
]

[dependencies]
simdutf8 = { version = "0.1.1", optional = true }
tui = { version = "0.14.0", default-features = false }

[features]
simd = ["simdutf8"]

7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2021 Uttarayan Mondal

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
# ansi-to-tui

[![Documentation](https://github.com/uttarayan21/ansi-to-tui/actions/workflows/docs.yaml/badge.svg)](https://uttarayan21.github.io/ansi-to-tui/ansi_to_tui/index.html)
[![Build & Tests](https://github.com/uttarayan21/ansi-to-tui/actions/workflows/build.yaml/badge.svg)](https://github.com/uttarayan21/ansi-to-tui)

Parse text with ansi color codes and turn them into [`tui::text::Text`](https://docs.rs/tui/0.14.0/tui/text/struct.Text.html).

Supports TrueColor ( RGB ) ( `\x1b[38;2;<r>;<g>;<b>m`)
Supports 8 - Bit Color ( 0..256 ) ( `\x1b[38;5;<n>m` )
Supports 4 - Bit Color Pallete ( `\x1b[30..37;40..47m` )

A naive implementation, relatively fast.
Only dependency is the tui crate.
Example

```rust
use ansi_to_tui::ansi_to_text;
use std::io::Read;

let file = std::fs::File::open("text.ascii");
let mut buffer: Vec<u8> = Vec::new();
let text = ansi_to_text(buffer);
```

A naive implementation, relatively fast.
Only dependency is the tui crate.
Lots of room for improvement.
40 changes: 22 additions & 18 deletions src/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ use tui::{
/// let bytes : Vec<u8> = vec![b'\x1b', b'[', b'3', b'1', b'm', b'A', b'A', b'A', b'\x1b', b'[', b'0'];
/// let text = ansi_to_text(bytes);
/// ```
/// Example parsing from a file.
/// ```rust
/// use ansi_to_tui::ansi_to_text;
/// use std::io::Read;
///
/// let file = std::fs::File::open("text.ascii");
/// let mut buffer: Vec<u8> = Vec::new();
/// let text = ansi_to_text(buffer);
/// ```
///
pub fn ansi_to_text<'t, B: IntoIterator<Item = u8>>(bytes: B) -> Result<Text<'t>, Error> {
// let reader = bytes.as_ref().iter().copied(); // copies the whole buffer to memory
Expand Down Expand Up @@ -54,21 +63,19 @@ pub fn ansi_to_text<'t, B: IntoIterator<Item = u8>>(bytes: B) -> Result<Text<'t>
// \e[31mHELLO\e[0m\e[31mTEST -> \e[31mHELLOTEST

// if we find a byte after the new stack has been parsed we do
// if style_stack.last().unwrap() == &style {}
if line_styled_buffer.is_empty() {
// if !line_buffer.is_empty() {
if style_stack.last().unwrap() != &style && !line_buffer.is_empty() {
// } else {
span_buffer.push(Span::styled(
#[cfg(feature = "simd")]
from_utf8(&line_buffer)?.to_owned(),
#[cfg(not(feature = "simd"))]
String::from_utf8(line_buffer.clone())?,
style_stack.pop().unwrap(),
));
line_buffer.clear();
style_stack.push(style);
}
if line_styled_buffer.is_empty()
&& style_stack.last().unwrap() != &style
&& !line_buffer.is_empty()
{
span_buffer.push(Span::styled(
#[cfg(feature = "simd")]
from_utf8(&line_buffer)?.to_owned(),
#[cfg(not(feature = "simd"))]
String::from_utf8(line_buffer.clone())?,
style_stack.pop().unwrap(),
));
line_buffer.clear();
style_stack.push(style);
}
line_styled_buffer.push(byte);
} else {
Expand All @@ -82,11 +89,8 @@ pub fn ansi_to_text<'t, B: IntoIterator<Item = u8>>(bytes: B) -> Result<Text<'t>
} // this clears the stack

b'\n' => {
// println!("span_buffer {:#?}", span_buffer);
// println!("line_buffer {:#?}", line_buffer);
// If line buffer is not empty when a newline is detected push the line_buffer
// to the span_buffer since we need the spans.
// if style_stack.last().unwrap() == &stack {}
if !line_styled_buffer.is_empty() {
line_buffer.append(&mut line_styled_buffer);
line_styled_buffer.clear();
Expand Down
14 changes: 8 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
//! let bytes = b"\x1b[38;2;225;192;203mAAAAA\x1b[0m".to_owned().to_vec();
//! let text = ansi_to_text(bytes).unwrap();
//! ```
//! You can use this text in a [tui](https://docs.rs/tui/) application.
//!
//! ## Cargo.toml
//! Example parsing from a file.
//! ```rust
//! use ansi_to_tui::ansi_to_text;
//! use std::io::Read;
//!
//! ```toml
//! [dependencies]
//! ansi_to_tui = { git = "https://github.com/uttarayan21/ansi-to-tui" }
//! let file = std::fs::File::open("text.ascii");
//! let mut buffer: Vec<u8> = Vec::new();
//! let text = ansi_to_text(buffer);
//! ```
//!
//! If you want to use [`simdutf8`](https://github.com/rusticstuff/simdutf8) instead of `String::from_utf8()`
//! for parsing UTF-8 then enable optional feature `simd`

Expand Down

0 comments on commit 8ad6643

Please sign in to comment.