mirror of
https://github.com/Xyverle/neutuino.git
synced 2026-08-29 07:40:03 -04:00
reorganize some things
This commit is contained in:
@@ -2,10 +2,16 @@
|
||||
//!
|
||||
//! These should work on *most* terminals (i.e. Xterm compatible terminals)
|
||||
//!
|
||||
//! For these to work on Windows you need to run the `enable_ansi` function in the os module
|
||||
//! For these to work on Windows you need to run the `enable_ansi` function inside this module
|
||||
|
||||
use std::io::{self, Write};
|
||||
|
||||
#[cfg(unix)]
|
||||
pub use crate::unix::enable_ansi;
|
||||
|
||||
#[cfg(windows)]
|
||||
pub use crate::windows::enable_ansi;
|
||||
|
||||
/// Sets the terminal to an arbitrary 12-bit/truecolor color in the foreground when printed
|
||||
#[must_use]
|
||||
pub fn rgb_color_code_fg(red: u8, green: u8, blue: u8) -> String {
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
use std::io;
|
||||
|
||||
#[cfg(unix)]
|
||||
pub use crate::unix::os::*;
|
||||
pub use crate::unix::{disable_raw_mode, enable_raw_mode, get_terminal_size};
|
||||
|
||||
#[cfg(windows)]
|
||||
pub use crate::windows::os::*;
|
||||
pub use crate::windows::{disable_raw_mode, enable_raw_mode, get_terminal_size};
|
||||
|
||||
/// Struct that calls `enable_raw_mode` on construction
|
||||
/// and `disable_raw_mode` on destruction
|
||||
@@ -114,7 +114,7 @@ pub enum KeyType {
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
pub use crate::unix::input::*;
|
||||
pub use crate::unix::poll_input;
|
||||
|
||||
#[cfg(windows)]
|
||||
pub use crate::windows::input::*;
|
||||
pub use crate::windows::poll_input;
|
||||
|
||||
@@ -47,12 +47,12 @@ mod unix;
|
||||
mod windows;
|
||||
|
||||
pub mod ansi;
|
||||
pub mod control;
|
||||
pub mod input;
|
||||
pub mod os;
|
||||
|
||||
pub mod prelude {
|
||||
//! Covenience re-export of common members
|
||||
pub use crate::ansi::*;
|
||||
pub use crate::control::*;
|
||||
pub use crate::input::*;
|
||||
pub use crate::os::*;
|
||||
}
|
||||
|
||||
44
src/unix.rs
44
src/unix.rs
@@ -1,5 +1,10 @@
|
||||
use std::ffi::{c_int, c_short, c_uint, c_ulong, c_ushort};
|
||||
use std::ffi::{c_int, c_uint, c_ulong, c_ushort};
|
||||
use std::io;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use crate::input::{Event, Key, KeyModifiers, KeyType};
|
||||
use std::ffi::{c_short, c_void};
|
||||
use std::time::Duration;
|
||||
|
||||
unsafe extern "C" {
|
||||
fn ioctl(fd: c_int, request: c_ulong, argp: *mut u8) -> c_int;
|
||||
@@ -55,20 +60,6 @@ fn set_attributes(fd: c_int, termios: &mut Termios) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn make_raw(termios: &mut Termios) {
|
||||
unsafe {
|
||||
cfmakeraw(termios);
|
||||
}
|
||||
// termios.iflag |= !(ICRNL);
|
||||
}
|
||||
|
||||
pub mod os {
|
||||
use super::{STDIN_FILENO, STDOUT_FILENO, TIOCGWINSZ};
|
||||
use super::{Termios, Winsize};
|
||||
use super::{get_attributes, ioctl, make_raw, set_attributes};
|
||||
use std::io;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
static TERMIOS: LazyLock<Option<Termios>> = LazyLock::new(|| {
|
||||
let mut orig_termios = Termios::default();
|
||||
get_attributes(STDIN_FILENO, &mut orig_termios).ok()?;
|
||||
@@ -83,9 +74,10 @@ pub mod os {
|
||||
/// stdin is not a tty,
|
||||
/// or it fails to change terminal settings
|
||||
pub fn enable_raw_mode() -> io::Result<()> {
|
||||
let mut termios =
|
||||
(*TERMIOS).ok_or(io::Error::other("Failed to get terminal properties"))?;
|
||||
make_raw(&mut termios);
|
||||
let mut termios = (*TERMIOS).ok_or(io::Error::other("Failed to get terminal properties"))?;
|
||||
unsafe {
|
||||
cfmakeraw(&mut termios);
|
||||
}
|
||||
set_attributes(STDIN_FILENO, &mut termios)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -98,8 +90,7 @@ pub mod os {
|
||||
/// stdin is not a tty,
|
||||
/// or it fails to change terminal settings
|
||||
pub fn disable_raw_mode() -> io::Result<()> {
|
||||
let mut termios =
|
||||
(*TERMIOS).ok_or(io::Error::other("Failed to get terminal properties"))?;
|
||||
let mut termios = (*TERMIOS).ok_or(io::Error::other("Failed to get terminal properties"))?;
|
||||
set_attributes(STDIN_FILENO, &mut termios)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -115,6 +106,7 @@ pub mod os {
|
||||
/// If There is no stdout,
|
||||
/// if stdout isn't a TTY, or
|
||||
/// if it cannot change terminal properties on Windows
|
||||
#[cfg(unix)]
|
||||
pub fn enable_ansi() -> io::Result<()> {
|
||||
// ANSI is on by default on unix platforms
|
||||
// This is here for compatibility with the windows version of this API
|
||||
@@ -132,8 +124,7 @@ pub mod os {
|
||||
/// if it fails to retrieve the terminal size
|
||||
pub fn get_terminal_size() -> io::Result<(u16, u16)> {
|
||||
let mut winsize = Winsize::default();
|
||||
let ioctl_result =
|
||||
unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ, (&raw mut winsize).cast::<u8>()) };
|
||||
let ioctl_result = unsafe { ioctl(STDOUT_FILENO, TIOCGWINSZ, (&raw mut winsize).cast::<u8>()) };
|
||||
|
||||
if ioctl_result == 0 {
|
||||
Ok((winsize.col, winsize.row))
|
||||
@@ -141,14 +132,6 @@ pub mod os {
|
||||
Err(io::Error::last_os_error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod input {
|
||||
use super::{POLLIN, STDIN_FILENO};
|
||||
use crate::input::{Event, Key, KeyModifiers, KeyType};
|
||||
use std::ffi::{c_int, c_short, c_ulong, c_void};
|
||||
use std::io;
|
||||
use std::time::Duration;
|
||||
|
||||
unsafe extern "C" {
|
||||
fn poll(fds: *mut PollFD, nfds: c_ulong, timeout: c_int) -> c_int;
|
||||
@@ -348,4 +331,3 @@ pub mod input {
|
||||
assert!(c == character);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::io;
|
||||
use crate::input::{Event, Key, KeyModifiers, KeyType};
|
||||
use std::os::windows::raw::HANDLE;
|
||||
use std::{io, mem, time::Duration};
|
||||
|
||||
#[link(name = "kernel32")]
|
||||
unsafe extern "system" {
|
||||
@@ -62,17 +63,6 @@ fn get_console_mode(handle: HANDLE, mode: &mut u32) -> io::Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod os {
|
||||
use super::{
|
||||
ConsoleScreenBufferInfo, GetConsoleScreenBufferInfo, get_console_mode, get_stdin_handle,
|
||||
get_stdout_handle, set_console_mode,
|
||||
};
|
||||
use super::{
|
||||
ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT,
|
||||
ENABLE_VIRTUAL_TERMINAL_PROCESSING,
|
||||
};
|
||||
use std::io;
|
||||
|
||||
/// Enables raw mode, which disables line buffering, input echoing, and output canonicalization
|
||||
///
|
||||
/// # Errors
|
||||
@@ -144,14 +134,6 @@ pub mod os {
|
||||
}
|
||||
Err(io::Error::last_os_error())
|
||||
}
|
||||
}
|
||||
|
||||
pub mod input {
|
||||
use super::get_stdin_handle;
|
||||
use crate::input::{Event, Key, KeyModifiers, KeyType};
|
||||
|
||||
use std::os::windows::raw::HANDLE;
|
||||
use std::{io, mem, time::Duration};
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone)]
|
||||
@@ -283,8 +265,8 @@ pub mod input {
|
||||
KeyModifiers::none(),
|
||||
), // F1-F24
|
||||
_ => {
|
||||
let c =
|
||||
char::from_u32(u32::from(unsafe { event.u_char.unicode_char })).unwrap_or(' ');
|
||||
let num = u32::from(unsafe { event.u_char.unicode_char });
|
||||
let c = char::from_u32(num).unwrap_or(' ');
|
||||
if ctrl && c.is_ascii_alphabetic() {
|
||||
Event::Key(Key::Char(c), KeyType::Press, KeyModifiers::none().ctrl())
|
||||
} else {
|
||||
@@ -293,4 +275,3 @@ pub mod input {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user