add convenience constants & example

This commit is contained in:
2025-04-01 23:29:14 -04:00
parent 190c7d090e
commit e6e7e3e835
4 changed files with 106 additions and 15 deletions

View File

@@ -208,3 +208,39 @@ pub const COLOR_WHITE_BG: &str = "\x1b[47m";
pub const COLOR_DEFAULT_FG: &str = "\x1b[39m";
/// Makes characters sent to the screen have a default background
pub const COLOR_DEFAULT_BG: &str = "\x1b[49m";
pub const COLORS_FG: [&str; 9] = [
COLOR_BLACK_FG,
COLOR_RED_FG,
COLOR_GREEN_FG,
COLOR_YELLOW_FG,
COLOR_BLUE_FG,
COLOR_MAGENTA_FG,
COLOR_CYAN_FG,
COLOR_WHITE_FG,
COLOR_DEFAULT_FG,
];
pub const COLORS_BG: [&str; 9] = [
COLOR_BLACK_BG,
COLOR_RED_BG,
COLOR_GREEN_BG,
COLOR_YELLOW_BG,
COLOR_BLUE_BG,
COLOR_MAGENTA_BG,
COLOR_CYAN_BG,
COLOR_WHITE_BG,
COLOR_DEFAULT_BG,
];
pub const COLORS: [(&str, &str); 9] = [
(COLOR_BLACK_FG, COLOR_BLACK_BG),
(COLOR_RED_FG, COLOR_RED_BG),
(COLOR_GREEN_FG, COLOR_GREEN_BG),
(COLOR_YELLOW_FG, COLOR_YELLOW_BG),
(COLOR_BLUE_FG, COLOR_BLUE_BG),
(COLOR_MAGENTA_FG, COLOR_MAGENTA_BG),
(COLOR_CYAN_FG, COLOR_CYAN_BG),
(COLOR_WHITE_FG, COLOR_WHITE_BG),
(COLOR_DEFAULT_FG, COLOR_DEFAULT_BG),
];

View File

@@ -16,6 +16,7 @@ mod windows_input;
#[cfg(windows)]
pub use windows_input::*;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Event {
Key(KeyEvent),
Mouse(MouseEvent),
@@ -23,6 +24,7 @@ pub enum Event {
FocusLost,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum KeyEvent {
Backspace,
Up,
@@ -44,12 +46,14 @@ pub enum KeyEvent {
Null,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MouseEvent {
Press(MouseButton, u16, u16),
Release(u16, u16),
Hold(u16, u16),
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum MouseButton {
Left,
Right,

View File

@@ -32,18 +32,21 @@ impl Iterator for ReadIterator {
type Item = io::Result<u8>;
fn next(&mut self) -> Option<Self::Item> {
let bytes_read = unsafe { read(self.fd, &raw mut self.buf as *mut c_void, 1) };
let bytes_read = unsafe { read(self.fd, (&raw mut self.buf).cast::<c_void>(), 1) };
if bytes_read > 0 {
Some(Ok(self.buf))
} else if bytes_read == 0 {
None
} else {
Some(Err(io::Error::last_os_error()))
match bytes_read {
1.. => Some(Ok(self.buf)),
0 => None,
_ => Some(Err(io::Error::last_os_error())),
}
}
}
/// Attempts to fetch input from stdin
///
/// # Errors
/// If the timeout has expired or
/// there was an error getting the data
pub fn poll_input(timeout: Duration) -> io::Result<Event> {
let mut fds = [PollFD {
fd: STDIN_FILENO,
@@ -51,6 +54,7 @@ pub fn poll_input(timeout: Duration) -> io::Result<Event> {
revents: 0,
}];
let result = unsafe {
#[allow(clippy::cast_possible_truncation)]
poll(
fds.as_mut_ptr(),
fds.len() as c_ulong,
@@ -59,14 +63,15 @@ pub fn poll_input(timeout: Duration) -> io::Result<Event> {
};
let mut read_iter = ReadIterator::new(STDIN_FILENO);
if result > 0 {
let item = read_iter.next().ok_or(io::ErrorKind::InvalidData)??;
try_parse_event(item, &mut read_iter)
} else if result == 0 {
// The function timed out.
Err(io::ErrorKind::TimedOut.into())
} else {
Err(io::Error::last_os_error())
let timed_out: io::Error = io::ErrorKind::TimedOut.into();
match result {
1.. => {
let item = read_iter.next().ok_or(timed_out)??;
try_parse_event(item, &mut read_iter)
}
0 => Err(timed_out),
_ => Err(io::Error::last_os_error()),
}
}