Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Added interactive test #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
members = [
"examples",
"first-depth-search",
"snake"
"snake",
"interactive-test"
]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ $ cargo run --bin alternate_screen

## License

This project is licensed under the MIT License - see the [LICENSE.md](./LICENSE) file for details.
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE) file for details.

[s2]: https://img.shields.io/badge/license-MIT-blue.svg
[l2]: ./LICENSE
[l2]: LICENSE

[s5]: https://img.shields.io/discord/560857607196377088.svg?logo=discord
[l5]: https://discord.gg/K4nyTDB
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/command_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{thread, time};
use crossterm::cursor::{Hide, Show};
use crossterm::{
cursor::MoveTo,
input::{input, InputEvent, KeyEvent},
event::{input, InputEvent, KeyEvent},
screen::RawScreen,
terminal::{self, Clear, ClearType},
ExecutableCommand, Output, Result,
Expand Down
1 change: 1 addition & 0 deletions examples/src/bin/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 1 addition & 1 deletion examples/src/bin/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crossterm::input::input;
use crossterm::event::input;

fn read_char() {
let input = input();
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/key_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::{thread, time::Duration};

use crossterm::{
input::{input, InputEvent, KeyEvent, MouseButton, MouseEvent},
event::{input, InputEvent, KeyEvent, MouseButton, MouseEvent},
screen::RawScreen,
Result,
};
Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/stderr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::io::{stderr, Write};

use crossterm::{
cursor::{Hide, MoveTo, Show},
input::input,
event::input,
queue,
screen::{EnterAlternateScreen, LeaveAlternateScreen, RawScreen},
Output, Result,
Expand Down
14 changes: 14 additions & 0 deletions interactive-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "crossterm-interactive-test"
version = "0.0.1"
authors = ["T. Post", "Robert Vojta <[email protected]>"]
edition = "2018"
description = "Interactive test for crossterm."
license = "MIT"
exclude = ["target", "Cargo.lock"]
readme = "README.md"
publish = false

[dependencies]
crossterm = { git = "https://github.com/crossterm-rs/crossterm" }
anes = "0.1.4" # needed till `MoveCursorToNextLine`, `MoveCursorToColumn`, `MoveCursorToPreviousLine` are implemented for crossterm.
29 changes: 29 additions & 0 deletions interactive-test/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
macro_rules! run_tests {
(
$dst:expr,
$(
$testfn:ident
),*
$(,)?
) => {
use crossterm::{queue, style, terminal, cursor};
$(
queue!(
$dst,
style::ResetColor,
terminal::Clear(terminal::ClearType::All),
cursor::MoveTo(1, 1),
cursor::Show,
cursor::EnableBlinking
)?;

$testfn($dst)?;

match $crate::read_char() {
Ok('q') => return Ok(()),
Err(e) => return Err(e),
_ => { },
};
)*
}
}
139 changes: 139 additions & 0 deletions interactive-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#![allow(clippy::cognitive_complexity)]

use std::io::{self, Write};

use crate::event::KeyEvent;
use crossterm::event::KeyCode;
pub use crossterm::{
cursor,
event::{self, Event},
execute, queue, screen, style,
terminal::{self, ClearType},
Command, Result,
};

#[macro_use]
mod macros;
mod test;

struct MoveCursorToNextLine(u16);

impl Command for MoveCursorToNextLine {
type AnsiType = String;

fn ansi_code(&self) -> Self::AnsiType {
format!("{}", anes::MoveCursorToNextLine(self.0))
}

fn execute_winapi(&self) -> Result<()> {
unimplemented!()
}
}

struct MoveCursorToPreviousLine(u16);

impl Command for MoveCursorToPreviousLine {
type AnsiType = String;

fn ansi_code(&self) -> Self::AnsiType {
format!("{}", anes::MoveCursorToPreviousLine(self.0))
}

fn execute_winapi(&self) -> Result<()> {
unimplemented!()
}
}

struct MoveCursorToColumn(u16);

impl Command for MoveCursorToColumn {
type AnsiType = String;

fn ansi_code(&self) -> Self::AnsiType {
format!("{}", anes::MoveCursorToColumn(self.0))
}

fn execute_winapi(&self) -> Result<()> {
unimplemented!()
}
}

const MENU: &str = r#"Crossterm interactive test

Controls:

- 'q' - quit interactive test (or return to this menu)
- any other key - continue with next step

Available tests:

1. cursor
2. color (foreground, background)
3. attributes (bold, italic, ...)
4. input

Select test to run ('1', '2', ...) or hit 'q' to quit.
"#;

fn run<W>(w: &mut W) -> Result<()>
where
W: Write,
{
execute!(w, screen::EnterAlternateScreen)?;

let _raw = screen::RawScreen::into_raw_mode()?;

loop {
queue!(
w,
style::ResetColor,
terminal::Clear(ClearType::All),
cursor::Hide,
cursor::MoveTo(1, 1)
)?;

for line in MENU.split('\n') {
queue!(w, style::Print(line), MoveCursorToNextLine(1))?;
}

w.flush()?;

match read_char()? {
'1' => test::cursor::run(w)?,
'2' => test::color::run(w)?,
'3' => test::attribute::run(w)?,
'4' => test::event::run(w)?,
'q' => break,
_ => {}
};
}

execute!(
w,
style::ResetColor,
cursor::Show,
screen::LeaveAlternateScreen
)?;
Ok(())
}

pub fn read_char() -> Result<char> {
loop {
if let Ok(Event::Key(KeyEvent {
code: KeyCode::Char(c),
..
})) = event::read()
{
return Ok(c);
}
}
}

pub fn buffer_size() -> Result<(u16, u16)> {
terminal::size()
}

fn main() -> Result<()> {
let mut stderr = io::stdout();
run(&mut stderr)
}
4 changes: 4 additions & 0 deletions interactive-test/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod attribute;
pub mod color;
pub mod cursor;
pub mod event;
52 changes: 52 additions & 0 deletions interactive-test/src/test/attribute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#![allow(clippy::cognitive_complexity)]

use crate::{MoveCursorToNextLine, Result};
use crossterm::{queue, style};
use std::io::Write;

const ATTRIBUTES: [(style::Attribute, style::Attribute); 6] = [
(style::Attribute::Bold, style::Attribute::NoBold),
(style::Attribute::Italic, style::Attribute::NoItalic),
(style::Attribute::Underlined, style::Attribute::NoUnderline),
(style::Attribute::Reverse, style::Attribute::NoReverse),
(
style::Attribute::CrossedOut,
style::Attribute::NotCrossedOut,
),
(style::Attribute::SlowBlink, style::Attribute::NoBlink),
];

fn test_set_display_attributes<W>(w: &mut W) -> Result<()>
where
W: Write,
{
queue!(
w,
style::Print("Display attributes"),
MoveCursorToNextLine(2)
)?;

for (on, off) in &ATTRIBUTES {
queue!(
w,
style::SetAttribute(*on),
style::Print(format!("{:>width$} ", format!("{:?}", on), width = 35)),
style::SetAttribute(*off),
style::Print(format!("{:>width$}", format!("{:?}", off), width = 35)),
style::ResetColor,
MoveCursorToNextLine(1)
)?;
}

w.flush()?;

Ok(())
}

pub fn run<W>(w: &mut W) -> Result<()>
where
W: Write,
{
run_tests!(w, test_set_display_attributes,);
Ok(())
}
Loading