resolved conflicts

This commit is contained in:
David Bittner
2020-10-05 14:43:45 -04:00
8 changed files with 167 additions and 34 deletions

View File

@@ -1,8 +1,10 @@
#[cfg(test)]
mod tests;
use heapless::{Vec, consts::U5};
///The following are the implemented ANSI escape sequences. More to be added.
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub enum AnsiSequence {
Escape,
CursorPos(u32, u32),
@@ -14,7 +16,7 @@ pub enum AnsiSequence {
CursorRestore,
EraseDisplay,
EraseLine,
SetGraphicsMode(Vec<u32>),
SetGraphicsMode(Vec<u8, U5>),
SetMode(u8),
ResetMode(u8),
HideCursor,
@@ -55,14 +57,15 @@ pub enum AnsiSequence {
SetTopAndBottom(u32, u32),
}
use std::fmt::Display;
use core::fmt::{Display, Formatter, Result as DisplayResult};
impl Display for AnsiSequence {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, formatter: &mut Formatter) -> DisplayResult {
write!(formatter, "\u{1b}")?;
use AnsiSequence::*;
match self {
Escape => write!(formatter, "\u{1b}"),
Escape =>
write!(formatter, "\u{1b}"),
CursorPos(line, col)
=> write!(formatter, "[{};{}H", line, col),
CursorUp(amt)
@@ -88,6 +91,8 @@ impl Display for AnsiSequence {
1 => write!(formatter, "[{}m", vec[0]),
2 => write!(formatter, "[{};{}m", vec[0], vec[1]),
3 => write!(formatter, "[{};{};{}m", vec[0], vec[1], vec[2]),
5 => write!(formatter, "[{};{};{};{};{}m", vec[0], vec[1],
vec[2], vec[3], vec[4]),
_ => unreachable!()
}
},
@@ -181,7 +186,7 @@ pub enum Output<'a> {
}
impl<'a> Display for Output<'a> {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
fn fmt(&self, formatter: &mut Formatter) -> DisplayResult {
use Output::*;
match self {
TextBlock(txt) => write!(formatter, "{}", txt),

View File

@@ -1,4 +1,5 @@
#![recursion_limit="256"]
#![cfg_attr(not(any(feature = "std", test)), no_std)]
mod enums;
mod parsers;
@@ -20,3 +21,4 @@ mod traits;
pub use enums::*;
pub use traits::*;
pub use parsers::parse_escape;

View File

@@ -1,9 +1,10 @@
#[cfg(test)]
mod tests;
use crate::{AnsiSequence, Output};
use crate::AnsiSequence;
use std::convert::TryInto;
use core::convert::TryInto;
use heapless::Vec;
use nom::*;
macro_rules! tag_parser {
@@ -105,7 +106,9 @@ named!(
tag!("[") >>
val: parse_int >>
tag!("m") >>
(AnsiSequence::SetGraphicsMode(vec![val]))
val: expr_res!(val.try_into()) >>
conv: expr_res!(Vec::from_slice(&[val])) >>
(AnsiSequence::SetGraphicsMode(conv))
)
);
@@ -117,7 +120,13 @@ named!(
tag!(";") >>
val2: parse_int >>
tag!("m") >>
(AnsiSequence::SetGraphicsMode(vec![val1, val2]))
val1: expr_res!(val1.try_into()) >>
val2: expr_res!(val2.try_into()) >>
conv: expr_res!(Vec::from_slice(&[
val1,
val2,
])) >>
(AnsiSequence::SetGraphicsMode(conv))
)
);
@@ -131,7 +140,15 @@ named!(
tag!(";") >>
val3: parse_int >>
tag!("m") >>
(AnsiSequence::SetGraphicsMode(vec![val1, val2, val3]))
val1: expr_res!(val1.try_into()) >>
val2: expr_res!(val2.try_into()) >>
val3: expr_res!(val3.try_into()) >>
conv: expr_res!(Vec::from_slice(&[
val1,
val2,
val3,
])) >>
(AnsiSequence::SetGraphicsMode(conv))
)
);
@@ -139,11 +156,10 @@ named!(
graphics_mode4<&str, AnsiSequence>,
do_parse!(
tag!("[m") >>
(AnsiSequence::SetGraphicsMode(vec![]))
(AnsiSequence::SetGraphicsMode(Vec::new()))
)
);
named!(
graphics_mode5<&str, AnsiSequence>,
do_parse!(
@@ -158,7 +174,19 @@ named!(
tag!(";") >>
val5: parse_int >>
tag!("m") >>
(AnsiSequence::SetGraphicsMode(vec![val1, val2, val3, val4, val5]))
val1: expr_res!(val1.try_into()) >>
val2: expr_res!(val2.try_into()) >>
val3: expr_res!(val3.try_into()) >>
val4: expr_res!(val4.try_into()) >>
val5: expr_res!(val5.try_into()) >>
conv: expr_res!(Vec::from_slice(&[
val1,
val2,
val3,
val4,
val5,
])) >>
(AnsiSequence::SetGraphicsMode(conv))
)
);
@@ -177,7 +205,7 @@ named!(
named!(
set_mode<&str, AnsiSequence>,
do_parse!(
tag_s!("[=") >>
tag!("[=") >>
mode: parse_int >>
conv: expr_res!(mode.try_into()) >>
tag!("h") >>
@@ -188,7 +216,7 @@ named!(
named!(
reset_mode<&str, AnsiSequence>,
do_parse!(
tag_s!("[=") >>
tag!("[=") >>
mode: parse_int >>
conv: expr_res!(mode.try_into()) >>
tag!("l") >>
@@ -305,10 +333,10 @@ named!(
);
named!(
pub parse_escape<&str, Output>,
pub parse_escape<&str, AnsiSequence>,
do_parse!(
tag_s!("\u{1b}") >>
tag!("\u{1b}") >>
seq: combined >>
(Output::Escape(seq))
(seq)
)
);

View File

@@ -1,5 +1,8 @@
use super::*;
use crate::*;
use crate::{
enums::{AnsiSequence, Output},
parsers::parse_escape,
traits::AnsiParser,
};
use std::fmt::Write;
@@ -59,6 +62,7 @@ test_parser!(erase_line, "\u{1b}[K");
test_parser!(set_video_mode_a, "\u{1b}[4m");
test_parser!(set_video_mode_b, "\u{1b}[4;42m");
test_parser!(set_video_mode_c, "\u{1b}[4;31;42m");
test_parser!(set_video_mode_d, "\u{1b}[4;31;42;42;42m");
test_parser!(reset_mode, "\u{1b}[=13l");
test_parser!(set_mode, "\u{1b}[=7h");

View File

@@ -2,19 +2,20 @@ use crate::enums::{Output};
use crate::parsers::parse_escape;
pub trait AnsiParser {
fn ansi_parse<'a>(&'a self) -> AnsiParseIterator<'a>;
fn ansi_parse(&self) -> AnsiParseIterator<'_>;
}
impl AnsiParser for str {
fn ansi_parse<'a>(&'a self) -> AnsiParseIterator<'a> {
fn ansi_parse(&self) -> AnsiParseIterator<'_> {
AnsiParseIterator {
dat: self
}
}
}
#[cfg(any(feature = "std", test))]
impl AnsiParser for String {
fn ansi_parse<'a>(&'a self) -> AnsiParseIterator<'a> {
fn ansi_parse(&self) -> AnsiParseIterator<'_> {
AnsiParseIterator {
dat: self
}
@@ -40,7 +41,7 @@ impl<'a> Iterator for AnsiParseIterator<'a> {
if let Ok(ret) = res {
self.dat = &ret.0;
Some(ret.1)
Some(Output::Escape(ret.1))
}else{
let pos = self.dat[(loc+1)..].find('\u{1b}');
if let Some(loc) = pos {