1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use std::io::{self, Error, Result};
use std::iter;
use std::slice;
use std::str;
use winapi::ctypes::c_void;
use winapi::shared::minwindef::DWORD;
use winapi::shared::ntdef::NULL;
use winapi::um::consoleapi::{GetNumberOfConsoleInputEvents, ReadConsoleInputW, WriteConsoleW};
use winapi::um::wincon::{
FillConsoleOutputAttribute, FillConsoleOutputCharacterA, GetLargestConsoleWindowSize,
SetConsoleTextAttribute, SetConsoleWindowInfo, COORD, INPUT_RECORD, SMALL_RECT,
};
use super::{is_true, Coord, Handle, HandleType, InputRecord, WindowPositions};
#[derive(Debug, Clone)]
pub struct Console {
handle: Handle,
}
impl Console {
pub fn output() -> Result<Console> {
Ok(Console {
handle: Handle::new(HandleType::OutputHandle)?,
})
}
pub fn set_text_attribute(&self, value: u16) -> Result<()> {
unsafe {
if !is_true(SetConsoleTextAttribute(*self.handle, value)) {
return Err(Error::last_os_error());
}
}
Ok(())
}
pub fn set_console_info(&self, absolute: bool, rect: WindowPositions) -> Result<()> {
let absolute = match absolute {
true => 1,
false => 0,
};
let a = SMALL_RECT::from(rect);
unsafe {
if !is_true(SetConsoleWindowInfo(*self.handle, absolute, &a)) {
return Err(Error::last_os_error());
}
}
Ok(())
}
pub fn fill_whit_character(
&self,
start_location: Coord,
cells_to_write: u32,
filling_char: char,
) -> Result<u32> {
let mut chars_written = 0;
unsafe {
if !is_true(FillConsoleOutputCharacterA(
*self.handle,
filling_char as i8,
cells_to_write,
COORD::from(start_location),
&mut chars_written,
)) {
return Err(Error::last_os_error());
}
Ok(chars_written)
}
}
pub fn fill_whit_attribute(
&self,
start_location: Coord,
cells_to_write: u32,
dw_attribute: u16,
) -> Result<u32> {
let mut cells_written = 0;
unsafe {
if !is_true(FillConsoleOutputAttribute(
*self.handle,
dw_attribute,
cells_to_write,
COORD::from(start_location),
&mut cells_written,
)) {
return Err(Error::last_os_error());
}
}
Ok(cells_written)
}
pub fn largest_window_size(&self) -> Coord {
Coord::from(unsafe { GetLargestConsoleWindowSize(*self.handle) })
}
pub fn write_char_buffer(&self, buf: &[u8]) -> Result<usize> {
let utf8 = match str::from_utf8(buf) {
Ok(string) => string,
Err(_) => {
return Err(io::Error::new(
io::ErrorKind::Other,
"Could not parse to utf8 string",
));
}
};
let utf16: Vec<u16> = utf8.encode_utf16().collect();
let utf16_ptr: *const c_void = utf16.as_ptr() as *const _ as *const c_void;
let mut cells_written: u32 = 0;
unsafe {
if !is_true(WriteConsoleW(
*self.handle,
utf16_ptr,
utf16.len() as u32,
&mut cells_written,
NULL,
)) {
return Err(io::Error::last_os_error());
}
}
Ok(utf8.as_bytes().len())
}
pub fn read_single_input_event(&self) -> Result<InputRecord> {
let mut record: INPUT_RECORD = INPUT_RECORD::default();
{
let buf = slice::from_mut(&mut record);
let num_read = self.read_input(buf)?;
debug_assert!(num_read == 1);
}
Ok(record.into())
}
pub fn read_console_input(&self) -> Result<Vec<InputRecord>> {
let buf_len = self.number_of_console_input_events()?;
if buf_len == 0 {
return Ok(vec![]);
}
let mut buf: Vec<INPUT_RECORD> = iter::repeat_with(INPUT_RECORD::default)
.take(buf_len as usize)
.collect();
let num_read = self.read_input(buf.as_mut_slice())?;
Ok(buf
.into_iter()
.take(num_read)
.map(InputRecord::from)
.collect())
}
pub fn number_of_console_input_events(&self) -> Result<u32> {
let mut buf_len: DWORD = 0;
if is_true(unsafe { GetNumberOfConsoleInputEvents(*self.handle, &mut buf_len) }) {
Ok(buf_len)
} else {
Err(Error::last_os_error())
}
}
fn read_input(&self, buf: &mut [INPUT_RECORD]) -> Result<usize> {
let mut num_records = 0;
debug_assert!(buf.len() < std::u32::MAX as usize);
if !is_true(unsafe {
ReadConsoleInputW(
*self.handle,
buf.as_mut_ptr(),
buf.len() as u32,
&mut num_records,
)
}) {
Err(Error::last_os_error())
} else {
Ok(num_records as usize)
}
}
}
impl From<Handle> for Console {
fn from(handle: Handle) -> Self {
Console { handle }
}
}