diff options
Diffstat (limited to 'vendor/anes/examples')
-rw-r--r-- | vendor/anes/examples/execute.rs | 15 | ||||
-rw-r--r-- | vendor/anes/examples/parser.rs | 106 | ||||
-rw-r--r-- | vendor/anes/examples/queue.rs | 18 | ||||
-rw-r--r-- | vendor/anes/examples/sequence.rs | 42 | ||||
-rw-r--r-- | vendor/anes/examples/stdout.rs | 12 | ||||
-rw-r--r-- | vendor/anes/examples/string.rs | 7 |
6 files changed, 200 insertions, 0 deletions
diff --git a/vendor/anes/examples/execute.rs b/vendor/anes/examples/execute.rs new file mode 100644 index 000000000..df781fc74 --- /dev/null +++ b/vendor/anes/examples/execute.rs @@ -0,0 +1,15 @@ +/// An example how to execute the ANSI escape sequence. +use std::io::{Result, Write}; + +use anes::execute; + +fn main() -> Result<()> { + let mut stdout = std::io::stdout(); + execute!( + &mut stdout, + anes::SaveCursorPosition, + anes::MoveCursorTo(10, 10), + anes::RestoreCursorPosition + )?; + Ok(()) +} diff --git a/vendor/anes/examples/parser.rs b/vendor/anes/examples/parser.rs new file mode 100644 index 000000000..a5ec5b6dd --- /dev/null +++ b/vendor/anes/examples/parser.rs @@ -0,0 +1,106 @@ +/// An example how to use the ANSI escape sequence parser. +use std::io::{Read, Result, Write}; + +use anes::{ + self, execute, + parser::{KeyCode, Parser, Sequence}, + queue, +}; +use libc::termios as Termios; + +const HELP: &str = r#"ANES parser example + +* Hit `Esc` to quit +* Hit 'c' to ask for cursor position +* Use your mouse or type anything +"#; + +fn main() -> Result<()> { + let mut w = std::io::stdout(); + queue!( + w, + anes::SwitchBufferToAlternate, + anes::HideCursor, + anes::EnableMouseEvents + )?; + for line in HELP.split('\n') { + queue!(w, line, anes::MoveCursorToNextLine(1))?; + } + w.flush()?; + + let saved_attributes = get_termios()?; + let mut attributes = saved_attributes; + make_raw(&mut attributes); + set_termios(attributes)?; + + let mut stdin = std::io::stdin(); + let mut stdin_buffer = [0u8; 1024]; + let mut parser = Parser::default(); + + loop { + if let Ok(size) = stdin.read(&mut stdin_buffer) { + parser.advance(&stdin_buffer[..size], false); + + let mut break_outer_loop = false; + + while let Some(sequence) = parser.next() { + match sequence { + Sequence::Key(KeyCode::Esc, _) => { + break_outer_loop = true; + break; + } + Sequence::Key(KeyCode::Char('c'), _) => { + execute!(w, anes::ReportCursorPosition)? + } + _ => execute!( + w, + anes::ClearLine::Left, + anes::MoveCursorToColumn(1), + format!("{:?}", sequence), + )?, + } + } + + if break_outer_loop { + break; + } + } + } + + set_termios(saved_attributes)?; + + execute!( + w, + anes::DisableMouseEvents, + anes::ShowCursor, + anes::SwitchBufferToNormal + )?; + Ok(()) +} + +// +// RAW mode +// + +fn get_termios() -> Result<Termios> { + unsafe { + let mut termios = std::mem::zeroed(); + if libc::tcgetattr(libc::STDIN_FILENO, &mut termios) != -1 { + Ok(termios) + } else { + Err(std::io::Error::last_os_error()) + } + } +} + +fn set_termios(termios: Termios) -> Result<()> { + if unsafe { libc::tcsetattr(libc::STDIN_FILENO, libc::TCSANOW, &termios) } != -1 { + Ok(()) + } else { + Err(std::io::Error::last_os_error()) + } +} + +fn make_raw(termios: &mut Termios) { + unsafe { libc::cfmakeraw(termios) } +} diff --git a/vendor/anes/examples/queue.rs b/vendor/anes/examples/queue.rs new file mode 100644 index 000000000..0b2c7120d --- /dev/null +++ b/vendor/anes/examples/queue.rs @@ -0,0 +1,18 @@ +/// An example how to queue & flush the ANSI escape sequence. +use std::io::{Result, Write}; + +use anes::queue; + +fn main() -> Result<()> { + let mut stdout = std::io::stdout(); + queue!( + &mut stdout, + anes::SaveCursorPosition, + anes::MoveCursorTo(10, 10) + )?; + + queue!(&mut stdout, anes::RestoreCursorPosition,)?; + + // ANSI sequences are not executed until you flush it! + stdout.flush() +} diff --git a/vendor/anes/examples/sequence.rs b/vendor/anes/examples/sequence.rs new file mode 100644 index 000000000..d038f5851 --- /dev/null +++ b/vendor/anes/examples/sequence.rs @@ -0,0 +1,42 @@ +/// An example how to create custom ANSI sequences. +use anes::{csi, esc, sequence}; + +fn static_unit_struct() { + sequence!( + /// Documentation string is also supported. + struct Foo => csi!("foo") + ); + + assert_eq!(&format!("{}", Foo), "\x1B[foo"); +} + +fn dynamic_struct() { + sequence!( + /// Documentation string is also supported. + struct Foo(u16, u16) => + |this, f| write!(f, esc!("{};{}"), this.0, this.1) + ); + + assert_eq!(&format!("{}", Foo(5, 10)), "\x1B5;10"); +} + +fn static_enum() { + sequence!( + /// Documentation string is also supported. + enum Foo { + /// Documentation string is also supported. + Bar => esc!("bar"), + /// Documentation string is also supported. + Baz => csi!("baz"), + } + ); + + assert_eq!(&format!("{}", Foo::Bar), "\x1Bbar"); + assert_eq!(&format!("{}", Foo::Baz), "\x1B[baz"); +} + +fn main() { + static_unit_struct(); + dynamic_struct(); + static_enum(); +} diff --git a/vendor/anes/examples/stdout.rs b/vendor/anes/examples/stdout.rs new file mode 100644 index 000000000..9b5be7257 --- /dev/null +++ b/vendor/anes/examples/stdout.rs @@ -0,0 +1,12 @@ +/// An example how to use the ANSI escape sequence. +use std::io::{Result, Write}; + +use anes; + +fn main() -> Result<()> { + let mut stdout = std::io::stdout(); + write!(stdout, "{}", anes::SaveCursorPosition)?; + write!(stdout, "{}", anes::RestoreCursorPosition)?; + stdout.flush()?; + Ok(()) +} diff --git a/vendor/anes/examples/string.rs b/vendor/anes/examples/string.rs new file mode 100644 index 000000000..bd81c368e --- /dev/null +++ b/vendor/anes/examples/string.rs @@ -0,0 +1,7 @@ +//! An example how to retrieve the ANSI escape sequence as a `String`. +use anes::SaveCursorPosition; + +fn main() { + let string = format!("{}", SaveCursorPosition); + assert_eq!(&string, "\x1B7"); +} |