Skip to content

Commit

Permalink
Ensure ANSI works in Windows when built
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Izaguirre committed May 6, 2020
1 parent 9af76a5 commit ee23d95
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/console_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::time::Duration;

use crossterm::terminal;
use crossterm::event;
use crossterm::queue;
use crossterm::style;

use super::translate::Adm3aToAnsi;

Expand All @@ -14,7 +16,7 @@ pub struct Console {
impl Console {
pub fn new() -> Console {
terminal::enable_raw_mode().unwrap();

Console {
next_char: None,
translator: Adm3aToAnsi::new(),
Expand All @@ -30,7 +32,7 @@ impl Console {
loop {
if event::poll(Duration::from_nanos(100)).unwrap() {
let event = event::read().unwrap();
let some_ch = event_to_chat(event);
let some_ch = event_to_char(event);
if let Some(ch) = some_ch {
self.next_char = Some(ch);
break true
Expand All @@ -53,7 +55,7 @@ impl Console {
None => {
loop {
let event = event::read().unwrap();
let some_ch = event_to_chat(event);
let some_ch = event_to_char(event);
if let Some(ch) = some_ch {
break ch;
}
Expand All @@ -65,7 +67,7 @@ impl Console {

pub fn put(&mut self, ch: u8) {
if let Some(sequence) = self.translator.translate(ch) {
print!("{}", sequence);
queue!(stdout(), style::Print(sequence)).unwrap();
stdout().flush().unwrap();
}
}
Expand All @@ -77,9 +79,7 @@ impl Drop for Console {
}
}

fn event_to_chat(event: event::Event) -> Option<u8> {
//println!("Event::{:?}\r", event);

fn event_to_char(event: event::Event) -> Option<u8> {
let a = match event {
event::Event::Key(k) => match k.code {
event::KeyCode::Char(c) => {
Expand Down Expand Up @@ -116,6 +116,5 @@ fn event_to_chat(event: event::Event) -> Option<u8> {
_ => None, // Not a keyboard event, ignore.
};

//println!("Ascci: {:?}", a);
a
}

0 comments on commit ee23d95

Please sign in to comment.