Skip to content

Commit

Permalink
Add support for Windows terminals
Browse files Browse the repository at this point in the history
  • Loading branch information
Piturnah committed Aug 19, 2022
1 parent f0bfa93 commit eb14c96
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 65 deletions.
205 changes: 191 additions & 14 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
termion = "*"
crossterm = "0.25"
93 changes: 51 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::fs;
use std::io;
use std::fmt;

use termion::raw::IntoRawMode;
use termion::input::TermRead;
use termion::event::Key;
use crossterm::terminal;
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
use crossterm::cursor::MoveToColumn;

#[macro_use]
mod lexer;
Expand Down Expand Up @@ -1000,48 +1000,57 @@ fn start_new_cool_repl() {
// TODO: check if the stdin is tty
// If it is not maybe switch to the old/simplified REPL
let prompt = "new> ";
let mut stdout = stdout().into_raw_mode().unwrap();
let stdin = stdin();
write!(stdout, "{}", prompt).unwrap();
stdout.flush().unwrap();
terminal::enable_raw_mode().expect("failed to enable raw mode");
print!("{}", prompt);
stdout().flush().unwrap();

let mut new_cool_repl: NewCoolRepl = Default::default();

for key in stdin.keys() {
match key.unwrap() {
Key::Char('\n') => {
write!(stdout, "\r\n").unwrap();
if &new_cool_repl.take() == "quit" {
break
}
}
Key::Ctrl('a') | Key::Home => new_cool_repl.home(),
Key::Ctrl('e') | Key::End => new_cool_repl.end(),
Key::Ctrl('b') | Key::Left => new_cool_repl.left_char(),
Key::Ctrl('f') | Key::Right => new_cool_repl.right_char(),
Key::Ctrl('n') | Key::Down => new_cool_repl.down(),
Key::Ctrl('p') | Key::Up => new_cool_repl.up(),
Key::Ctrl('c') => {
write!(stdout, "^C\r\n").unwrap();
break;
}
Key::Alt('b') => new_cool_repl.left_word(),
Key::Alt('f') => new_cool_repl.right_word(),
Key::Char(key) => {
new_cool_repl.insert_char(key);
new_cool_repl.popup.clear();
if let Ok((head, body)) = parse_match(&mut Lexer::new(new_cool_repl.buffer.clone(), None)) {
let subexprs = find_all_subexprs(&head, &body);
for subexpr in subexprs {
new_cool_repl.popup.push(format!("{}", HighlightedSubexpr{expr: &body, subexpr}));
}
}
},
Key::Backspace => new_cool_repl.backspace(),
_ => {},
}
new_cool_repl.render(prompt, &mut stdout).unwrap();
stdout.flush().unwrap();

while let Ok(Event::Key(key)) = event::read() {
match key.modifiers {
KeyModifiers::CONTROL => match key.code {
KeyCode::Char('a') | KeyCode::Home => new_cool_repl.home(),
KeyCode::Char('e') | KeyCode::End => new_cool_repl.end(),
KeyCode::Char('b') | KeyCode::Left => new_cool_repl.left_char(),
KeyCode::Char('f') | KeyCode::Right => new_cool_repl.right_char(),
KeyCode::Char('n') | KeyCode::Down => new_cool_repl.down(),
KeyCode::Char('p') | KeyCode::Up => new_cool_repl.up(),
KeyCode::Char('c') => {
print!("^C\r\n{}", MoveToColumn(0));
break;
}
_ => {}
}
KeyModifiers::ALT => match key.code {
KeyCode::Char('b') => new_cool_repl.left_word(),
KeyCode::Char('f') => new_cool_repl.right_word(),
_ => {}
}
KeyModifiers::NONE => match key.code {
KeyCode::Char('\n') => {
print!("\r\n{}", MoveToColumn(0));
if &new_cool_repl.take() == "quit" {
break
}
}
KeyCode::Char(key) => {
new_cool_repl.insert_char(key);
new_cool_repl.popup.clear();
if let Ok((head, body)) = parse_match(&mut Lexer::new(new_cool_repl.buffer.clone(), None)) {
let subexprs = find_all_subexprs(&head, &body);
for subexpr in subexprs {
new_cool_repl.popup.push(format!("{}", HighlightedSubexpr{expr: &body, subexpr}));
}
}
},
KeyCode::Backspace => new_cool_repl.backspace(),
_ => {}
}
_ => {},
}
new_cool_repl.render(prompt, &mut stdout().lock()).unwrap();
stdout().flush().unwrap();
}
}

Expand Down
Loading

0 comments on commit eb14c96

Please sign in to comment.