added display implementation for all the ANSI Sequences
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
[](https://gitlab.com/davidbittner/ansi-parser/commits/master)
|
||||

|
||||
# Ansi Escape Sequence Parser
|
||||
|
54
src/enums.rs
54
src/enums.rs
@@ -1,7 +1,10 @@
|
||||
use num_derive::FromPrimitive;
|
||||
use num_derive::ToPrimitive;
|
||||
|
||||
use num_traits::ToPrimitive;
|
||||
|
||||
///A list of available text attributes.
|
||||
#[derive(Debug, PartialEq, FromPrimitive)]
|
||||
#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)]
|
||||
pub enum TextAttribute {
|
||||
Off = 0,
|
||||
Bold = 1,
|
||||
@@ -11,8 +14,14 @@ pub enum TextAttribute {
|
||||
Concealed = 8
|
||||
}
|
||||
|
||||
impl Display for TextAttribute {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(formatter, "{}", self.to_u8().unwrap_or(0))
|
||||
}
|
||||
}
|
||||
|
||||
///The basic ANSI colors.
|
||||
#[derive(Debug, PartialEq, FromPrimitive)]
|
||||
#[derive(Debug, PartialEq, FromPrimitive, ToPrimitive)]
|
||||
pub enum Color {
|
||||
Black = 30,
|
||||
Red = 31,
|
||||
@@ -24,6 +33,12 @@ pub enum Color {
|
||||
White = 37
|
||||
}
|
||||
|
||||
impl Display for Color {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(formatter, "{}", self.to_u8().unwrap_or(0))
|
||||
}
|
||||
}
|
||||
|
||||
///The following are the implemented ANSI escape sequences. More to be added.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum AnsiSequence {
|
||||
@@ -45,6 +60,41 @@ pub enum AnsiSequence {
|
||||
ResetMode(u8),
|
||||
}
|
||||
|
||||
use std::fmt::Display;
|
||||
impl Display for AnsiSequence {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(formatter, "\x27[")?;
|
||||
|
||||
use AnsiSequence::*;
|
||||
match self {
|
||||
CursorPos(line, col)
|
||||
=> write!(formatter, "{};{}H", line, col),
|
||||
CursorUp(amt)
|
||||
=> write!(formatter, "{}A", amt),
|
||||
CursorDown(amt)
|
||||
=> write!(formatter, "{}B", amt),
|
||||
CursorForward(amt)
|
||||
=> write!(formatter, "{}C", amt),
|
||||
CursorBackward(amt)
|
||||
=> write!(formatter, "{}D", amt),
|
||||
CursorSave
|
||||
=> write!(formatter, "s"),
|
||||
CursorRestore
|
||||
=> write!(formatter, "u"),
|
||||
EraseDisplay
|
||||
=> write!(formatter, "2J"),
|
||||
EraseLine
|
||||
=> write!(formatter, "K"),
|
||||
SetGraphicsMode{ta, fg, bg}
|
||||
=> write!(formatter, "{};{};{}m", ta, fg, bg),
|
||||
SetMode(mode)
|
||||
=> write!(formatter, "={}h", mode),
|
||||
ResetMode(mode)
|
||||
=> write!(formatter, "={}l", mode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///This is what is outputted by the parsing iterator.
|
||||
///Each block contains either straight-up text, or simply
|
||||
///an ANSI escape sequence.
|
||||
|
Reference in New Issue
Block a user