line struct ha

parse-tree
Jordan Orelli 2 years ago
parent ef41b94d2d
commit d1c379c755

@ -0,0 +1,36 @@
use crate::{error::Error, stdout_handle};
use anyhow::Result;
use windows::Win32::System::Console;
pub struct Line {
chars: Vec<char>,
}
impl Line {
pub fn new() -> Self {
Self { chars: Vec::new() }
}
/// adds a character to the end of the line
pub fn append(&mut self, c: char) {
self.chars.push(c)
}
pub fn clear(&mut self) {
self.chars.clear()
}
pub fn print(&self) -> Result<()> {
let s: String = self.chars.iter().collect();
unsafe {
Error::check(Console::WriteConsoleA(
stdout_handle()?,
s.as_bytes(),
None,
None,
))?;
}
Ok(())
}
}

@ -1,9 +1,9 @@
mod error; mod error;
mod line;
mod log; mod log;
use std::ffi::OsString; use line::Line;
use windows::Win32::{Foundation::HANDLE, System::Console}; use windows::Win32::{Foundation::HANDLE, System::Console};
use windows::{h, s, w};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
@ -186,7 +186,7 @@ fn stdin_handle() -> Result<HANDLE> {
} }
} }
fn stdout_handle() -> Result<HANDLE> { pub fn stdout_handle() -> Result<HANDLE> {
unsafe { unsafe {
let handle = Console::GetStdHandle(Console::STD_OUTPUT_HANDLE) let handle = Console::GetStdHandle(Console::STD_OUTPUT_HANDLE)
.context("unable to get stdin handle")?; .context("unable to get stdin handle")?;
@ -249,7 +249,7 @@ fn main() -> Result<()> {
let stdin = stdin_handle()?; let stdin = stdin_handle()?;
let stdout = stdout_handle()?; let stdout = stdout_handle()?;
let mut buf = [Console::INPUT_RECORD::default(); 100]; let mut buf = [Console::INPUT_RECORD::default(); 100];
let mut line = String::new(); let mut line = Line::new();
loop { loop {
let mut n: u32 = 0; let mut n: u32 = 0;
@ -306,19 +306,14 @@ fn main() -> Result<()> {
None, None,
None, None,
))?; ))?;
Error::check(Console::WriteConsoleA( line.print()?;
stdout,
line.as_bytes(),
None,
None,
))?;
Error::check(Console::WriteConsoleA( Error::check(Console::WriteConsoleA(
stdout, stdout,
"\r\n".as_bytes(), "\r\n".as_bytes(),
None, None,
None, None,
))?; ))?;
line = String::new(); line.clear();
} }
} else if key_code == 74 } else if key_code == 74
&& (modifiers & Console::LEFT_CTRL_PRESSED && (modifiers & Console::LEFT_CTRL_PRESSED
@ -336,7 +331,7 @@ fn main() -> Result<()> {
if !c.is_control() { if !c.is_control() {
let mut buf = [0 as u8; 8]; let mut buf = [0 as u8; 8];
let s = c.encode_utf8(&mut buf); let s = c.encode_utf8(&mut buf);
line.push_str(s); s.chars().for_each(|c| line.append(c));
Error::check(Console::WriteConsoleW( Error::check(Console::WriteConsoleW(
stdout, stdout,
s.as_bytes(), s.as_bytes(),

Loading…
Cancel
Save