Fix cargo fmt

This commit is contained in:
Maxim Zhiburt
2022-01-31 19:12:01 +03:00
parent 9d45910632
commit 92658d5486
6 changed files with 141 additions and 193 deletions

View File

@@ -1,7 +1,7 @@
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
use heapless::{Vec, consts::U5}; use heapless::{consts::U5, Vec};
///The following are the implemented ANSI escape sequences. More to be added. ///The following are the implemented ANSI escape sequences. More to be added.
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
@@ -64,114 +64,66 @@ impl Display for AnsiSequence {
use AnsiSequence::*; use AnsiSequence::*;
match self { match self {
Escape => Escape => write!(formatter, "\u{1b}"),
write!(formatter, "\u{1b}"), CursorPos(line, col) => write!(formatter, "[{};{}H", line, col),
CursorPos(line, col) CursorUp(amt) => write!(formatter, "[{}A", amt),
=> write!(formatter, "[{};{}H", line, col), CursorDown(amt) => write!(formatter, "[{}B", amt),
CursorUp(amt) CursorForward(amt) => write!(formatter, "[{}C", amt),
=> write!(formatter, "[{}A", amt), CursorBackward(amt) => write!(formatter, "[{}D", amt),
CursorDown(amt) CursorSave => write!(formatter, "[s"),
=> write!(formatter, "[{}B", amt), CursorRestore => write!(formatter, "[u"),
CursorForward(amt) EraseDisplay => write!(formatter, "[2J"),
=> write!(formatter, "[{}C", amt), EraseLine => write!(formatter, "[K"),
CursorBackward(amt) SetGraphicsMode(vec) => match vec.len() {
=> write!(formatter, "[{}D", amt),
CursorSave
=> write!(formatter, "[s"),
CursorRestore
=> write!(formatter, "[u"),
EraseDisplay
=> write!(formatter, "[2J"),
EraseLine
=> write!(formatter, "[K"),
SetGraphicsMode(vec)
=> {
match vec.len() {
0 => write!(formatter, "[m"), 0 => write!(formatter, "[m"),
1 => write!(formatter, "[{}m", vec[0]), 1 => write!(formatter, "[{}m", vec[0]),
2 => write!(formatter, "[{};{}m", vec[0], vec[1]), 2 => write!(formatter, "[{};{}m", vec[0], vec[1]),
3 => write!(formatter, "[{};{};{}m", vec[0], vec[1], vec[2]), 3 => write!(formatter, "[{};{};{}m", vec[0], vec[1], vec[2]),
5 => write!(formatter, "[{};{};{};{};{}m", vec[0], vec[1], 5 => write!(
vec[2], vec[3], vec[4]), formatter,
_ => unreachable!() "[{};{};{};{};{}m",
} vec[0], vec[1], vec[2], vec[3], vec[4]
),
_ => unreachable!(),
}, },
SetMode(mode) SetMode(mode) => write!(formatter, "[={}h", mode),
=> write!(formatter, "[={}h", mode), ResetMode(mode) => write!(formatter, "[={}l", mode),
ResetMode(mode) ShowCursor => write!(formatter, "[?25h"),
=> write!(formatter, "[={}l", mode), HideCursor => write!(formatter, "[?25l"),
ShowCursor CursorToApp => write!(formatter, "[?1h"),
=> write!(formatter, "[?25h"), SetNewLineMode => write!(formatter, "[20h"),
HideCursor SetCol132 => write!(formatter, "[?3h"),
=> write!(formatter, "[?25l"), SetSmoothScroll => write!(formatter, "[?4h"),
CursorToApp SetReverseVideo => write!(formatter, "[?5h"),
=> write!(formatter, "[?1h"), SetOriginRelative => write!(formatter, "[?6h"),
SetNewLineMode SetAutoWrap => write!(formatter, "[?7h"),
=> write!(formatter, "[20h"), SetAutoRepeat => write!(formatter, "[?8h"),
SetCol132 SetInterlacing => write!(formatter, "[?9h"),
=> write!(formatter, "[?3h"), SetLineFeedMode => write!(formatter, "[20l"),
SetSmoothScroll SetCursorKeyToCursor => write!(formatter, "[?1l"),
=> write!(formatter, "[?4h"), SetVT52 => write!(formatter, "[?2l"),
SetReverseVideo SetCol80 => write!(formatter, "[?3l"),
=> write!(formatter, "[?5h"), SetJumpScrolling => write!(formatter, "[?4l"),
SetOriginRelative SetNormalVideo => write!(formatter, "[?5l"),
=> write!(formatter, "[?6h"), SetOriginAbsolute => write!(formatter, "[?6l"),
SetAutoWrap ResetAutoWrap => write!(formatter, "[?7l"),
=> write!(formatter, "[?7h"), ResetAutoRepeat => write!(formatter, "[?8l"),
SetAutoRepeat ResetInterlacing => write!(formatter, "[?9l"),
=> write!(formatter, "[?8h"), SetAlternateKeypad => write!(formatter, "="),
SetInterlacing SetNumericKeypad => write!(formatter, ">"),
=> write!(formatter, "[?9h"), SetUKG0 => write!(formatter, "(A"),
SetLineFeedMode SetUKG1 => write!(formatter, ")A"),
=> write!(formatter, "[20l"), SetUSG0 => write!(formatter, "(B"),
SetCursorKeyToCursor SetUSG1 => write!(formatter, ")B"),
=> write!(formatter, "[?1l"), SetG0SpecialChars => write!(formatter, "(0"),
SetVT52 SetG1SpecialChars => write!(formatter, ")0"),
=> write!(formatter, "[?2l"), SetG0AlternateChar => write!(formatter, "(1"),
SetCol80 SetG1AlternateChar => write!(formatter, ")1"),
=> write!(formatter, "[?3l"), SetG0AltAndSpecialGraph => write!(formatter, "(2"),
SetJumpScrolling SetG1AltAndSpecialGraph => write!(formatter, ")2"),
=> write!(formatter, "[?4l"), SetSingleShift2 => write!(formatter, "N"),
SetNormalVideo SetSingleShift3 => write!(formatter, "O"),
=> write!(formatter, "[?5l"), SetTopAndBottom(x, y) => write!(formatter, "{};{}r", x, y),
SetOriginAbsolute
=> write!(formatter, "[?6l"),
ResetAutoWrap
=> write!(formatter, "[?7l"),
ResetAutoRepeat
=> write!(formatter, "[?8l"),
ResetInterlacing
=> write!(formatter, "[?9l"),
SetAlternateKeypad
=> write!(formatter, "="),
SetNumericKeypad
=> write!(formatter, ">"),
SetUKG0
=> write!(formatter, "(A"),
SetUKG1
=> write!(formatter, ")A"),
SetUSG0
=> write!(formatter, "(B"),
SetUSG1
=> write!(formatter, ")B"),
SetG0SpecialChars
=> write!(formatter, "(0"),
SetG1SpecialChars
=> write!(formatter, ")0"),
SetG0AlternateChar
=> write!(formatter, "(1"),
SetG1AlternateChar
=> write!(formatter, ")1"),
SetG0AltAndSpecialGraph
=> write!(formatter, "(2"),
SetG1AltAndSpecialGraph
=> write!(formatter, ")2"),
SetSingleShift2
=> write!(formatter, "N"),
SetSingleShift3
=> write!(formatter, "O"),
SetTopAndBottom(x, y)
=> write!(formatter, "{};{}r", x, y)
} }
} }
} }
@@ -182,7 +134,7 @@ impl Display for AnsiSequence {
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Output<'a> { pub enum Output<'a> {
TextBlock(&'a str), TextBlock(&'a str),
Escape(AnsiSequence) Escape(AnsiSequence),
} }
impl<'a> Display for Output<'a> { impl<'a> Display for Output<'a> {
@@ -190,7 +142,7 @@ impl<'a> Display for Output<'a> {
use Output::*; use Output::*;
match self { match self {
TextBlock(txt) => write!(formatter, "{}", txt), TextBlock(txt) => write!(formatter, "{}", txt),
Escape(seq) => write!(formatter, "{}", seq) Escape(seq) => write!(formatter, "{}", seq),
} }
} }
} }

View File

@@ -7,8 +7,7 @@ fn test_cursor_pos() {
let pos = AnsiSequence::CursorPos(5, 20); let pos = AnsiSequence::CursorPos(5, 20);
let mut buff = String::new(); let mut buff = String::new();
write!(&mut buff, "{}", pos) write!(&mut buff, "{}", pos).expect("failed to write");
.expect("failed to write");
assert_eq!(buff, "\x1b[5;20H"); assert_eq!(buff, "\x1b[5;20H");
} }

View File

@@ -18,7 +18,6 @@ mod traits;
/// This is done through a pulldown type parser, where an iterator is exposed. This essentially /// This is done through a pulldown type parser, where an iterator is exposed. This essentially
/// turns all of the ANSI sequences into enums and splits the string at every location that there /// turns all of the ANSI sequences into enums and splits the string at every location that there
/// was an ANSI Sequence. /// was an ANSI Sequence.
pub use enums::*; pub use enums::*;
pub use traits::*;
pub use parsers::parse_escape; pub use parsers::parse_escape;
pub use traits::*;

View File

@@ -190,7 +190,6 @@ named!(
) )
); );
named!( named!(
graphics_mode<&str, AnsiSequence>, graphics_mode<&str, AnsiSequence>,
alt!( alt!(

View File

@@ -16,12 +16,11 @@ macro_rules! test_parser {
assert!(ret.is_ok()); assert!(ret.is_ok());
let ret = ret.unwrap().1; let ret = ret.unwrap().1;
write!(&mut buff, "{}", ret) write!(&mut buff, "{}", ret).unwrap();
.unwrap();
assert_eq!(buff, $string); assert_eq!(buff, $string);
} }
} };
} }
macro_rules! test_def_val_parser { macro_rules! test_def_val_parser {
@@ -34,8 +33,7 @@ macro_rules! test_def_val_parser {
assert!(ret.is_ok()); assert!(ret.is_ok());
let ret = ret.unwrap().1; let ret = ret.unwrap().1;
write!(&mut buff, "{}", ret) write!(&mut buff, "{}", ret).unwrap();
.unwrap();
let ret2 = parse_escape(&buff); let ret2 = parse_escape(&buff);
assert!(ret2.is_ok()); assert!(ret2.is_ok());
@@ -43,7 +41,7 @@ macro_rules! test_def_val_parser {
let ret2 = ret2.unwrap().1; let ret2 = ret2.unwrap().1;
assert_eq!(ret, ret2); assert_eq!(ret, ret2);
} }
} };
} }
test_def_val_parser!(cursor_pos_default, "\u{1b}[H"); test_def_val_parser!(cursor_pos_default, "\u{1b}[H");
@@ -132,8 +130,14 @@ fn test_default_value() {
.collect(); .collect();
assert_eq!(strings.len(), 5); assert_eq!(strings.len(), 5);
assert_eq!(strings[0], Output::Escape(AnsiSequence::CursorPos(1, 1))); assert_eq!(strings[0], Output::Escape(AnsiSequence::CursorPos(1, 1)));
assert_eq!(strings[1], Output::Escape(AnsiSequence::CursorPos(123456,1))); assert_eq!(
assert_eq!(strings[2], Output::Escape(AnsiSequence::CursorPos(1,123456))); strings[1],
Output::Escape(AnsiSequence::CursorPos(123456, 1))
);
assert_eq!(
strings[2],
Output::Escape(AnsiSequence::CursorPos(1, 123456))
);
assert_eq!(strings[3], Output::TextBlock("\x1b[7asd;1234H")); assert_eq!(strings[3], Output::TextBlock("\x1b[7asd;1234H"));
assert_eq!(strings[4], Output::TextBlock("\x1b[a;sd7H")); assert_eq!(strings[4], Output::TextBlock("\x1b[a;sd7H"));
} }

View File

@@ -1,4 +1,4 @@
use crate::enums::{Output}; use crate::enums::Output;
use crate::parsers::parse_escape; use crate::parsers::parse_escape;
pub trait AnsiParser { pub trait AnsiParser {
@@ -7,18 +7,14 @@ pub trait AnsiParser {
impl AnsiParser for str { impl AnsiParser for str {
fn ansi_parse(&self) -> AnsiParseIterator<'_> { fn ansi_parse(&self) -> AnsiParseIterator<'_> {
AnsiParseIterator { AnsiParseIterator { dat: self }
dat: self
}
} }
} }
#[cfg(any(feature = "std", test))] #[cfg(any(feature = "std", test))]
impl AnsiParser for String { impl AnsiParser for String {
fn ansi_parse(&self) -> AnsiParseIterator<'_> { fn ansi_parse(&self) -> AnsiParseIterator<'_> {
AnsiParseIterator { AnsiParseIterator { dat: self }
dat: self
}
} }
} }
@@ -59,7 +55,6 @@ impl<'a> Iterator for AnsiParseIterator<'a> {
Some(Output::TextBlock(temp)) Some(Output::TextBlock(temp))
} }
} }
} else { } else {
let temp = &self.dat[..loc]; let temp = &self.dat[..loc];
self.dat = &self.dat[loc..]; self.dat = &self.dat[loc..];