From cd494d0f769ec1dab7613ef14bc95c0d7d247747 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Mon, 13 Mar 2023 08:23:43 -0500 Subject: [PATCH] is this macro at all necessary probably not --- .gitignore | 2 +- Cargo.lock | 5 +++++ Cargo.toml | 7 ++++++- macros/Cargo.lock | 7 +++++++ macros/Cargo.toml | 7 +++++++ macros/src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ src/input.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 macros/Cargo.lock create mode 100644 macros/Cargo.toml create mode 100644 macros/src/lib.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..eb5a316 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -/target +target diff --git a/Cargo.lock b/Cargo.lock index ad2612e..0f381d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,10 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "macros" +version = "0.0.0" + [[package]] name = "proc-macro2" version = "1.0.51" @@ -84,6 +88,7 @@ version = "0.1.0" dependencies = [ "anyhow", "log", + "macros", "thiserror", "windows", ] diff --git a/Cargo.toml b/Cargo.toml index 0082b59..9ea573a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,12 +2,17 @@ name = "wash" version = "0.1.0" edition = "2021" - # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +# [workspace] +# members = [ +# "macros" +# ] + [dependencies] anyhow = "1.0" thiserror = "1.0" +macros = { path = "macros" } log = "0.4" diff --git a/macros/Cargo.lock b/macros/Cargo.lock new file mode 100644 index 0000000..7afdb76 --- /dev/null +++ b/macros/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "macros" +version = "0.0.0" diff --git a/macros/Cargo.toml b/macros/Cargo.toml new file mode 100644 index 0000000..afa8efb --- /dev/null +++ b/macros/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "macros" +version = "0.0.0" +edition = "2021" + +[lib] +proc-macro = true diff --git a/macros/src/lib.rs b/macros/src/lib.rs new file mode 100644 index 0000000..73d0ea6 --- /dev/null +++ b/macros/src/lib.rs @@ -0,0 +1,36 @@ +use proc_macro::{ + TokenStream, + TokenTree::{self, Ident, Literal}, +}; + +fn next_pair>(tokens: &mut I) -> Option<(TokenTree, TokenTree)> { + match (tokens.next(), tokens.next()) { + (Some(a), Some(b)) => Some((a, b)), + _ => None, + } +} + +struct Escape { + sequence: String, + name: String, +} + +#[proc_macro] +pub fn escapes(tokens: TokenStream) -> TokenStream { + let mut it = tokens.into_iter(); + let mut pairs: Vec = Vec::new(); + + while let Some(pair) = next_pair(&mut it) { + match (pair.0, pair.1) { + (Literal(seq), Ident(ident)) => { + pairs.push(Escape{ + sequence: seq.to_string(), + name: ident.to_string(), + }) + }, + _ => panic!("bad escapes"), + } + } + + TokenStream::new() +} diff --git a/src/input.rs b/src/input.rs index 007dcca..5e86dd5 100644 --- a/src/input.rs +++ b/src/input.rs @@ -2,6 +2,7 @@ use crate::{error::Error, key, log::*}; use anyhow::{Context, Result}; use windows::Win32::Foundation::HANDLE; use windows::Win32::System::Console; +use macros::escapes; #[allow(dead_code)] fn log_input_mode(mode: Console::CONSOLE_MODE) { @@ -102,6 +103,8 @@ pub struct Reader { buf_len: u32, // the position of our current indexer into the input record buffer buf_idx: usize, + + ctrl: bool, } impl Reader { @@ -111,6 +114,7 @@ impl Reader { buf_len: 0, buf_idx: 0, input: stdin_handle()?, + ctrl: false, }; v.reset()?; Ok(v) @@ -155,6 +159,10 @@ impl Reader { if event.wVirtualKeyCode == 0 && event.uChar.UnicodeChar == 27 { return Ok(self.next_escape_sequence()?); } + if event.wVirtualKeyCode == 17 { + self.ctrl = event.bKeyDown.as_bool(); + debug!("ctrl {}", event.bKeyDown.as_bool()); + } } } Ok(rec.into()) @@ -372,3 +380,41 @@ impl From for Event { } } } + +escapes! { + "[A" Up + "[B" Down + "[C" Right + "[D" Left + "[H" Home + "[F" End + "[3~" Delete + "[5~" PageUp + "[6~" PageDown + + "[13;2u" Shift_Enter + "[13;5u" Ctrl_Enter + "[13;6u" Ctrl_Shift_Enter + + "[1;2P" Shift_F1 + "[1;2Q" Shift_F2 + "[1;5P" Ctrl_F1 + "[1;6P" Ctrl_Shift_F1 + "[1;3P" Alt_F1 + "[1;4P" Shift_Alt_F1 + + "OP" F1 + "OQ" F2 + "OR" F3 + "OS" F4 + + "[15~" F5 + "[15;2~" Shift_F5 + "[17~" F6 + "[18~" F7 + "[19~" F8 + "[24~" F12 + "[53;5u" Ctrl_Shift_5 +} +/* +*/