update doorlib
This commit is contained in:
parent
9ab25ca63b
commit
0d9a00d2e9
70
src/lib.rs
70
src/lib.rs
@ -17,6 +17,19 @@ macro_rules! esc {
|
|||||||
($($arg:expr),*) => { concat!("\x1B", $($arg),*) };
|
($($arg:expr),*) => { concat!("\x1B", $($arg),*) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! reset {
|
||||||
|
() => {
|
||||||
|
esc!("[0m")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! emph {
|
||||||
|
() => {
|
||||||
|
esc!("[1;31m")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum ConnType {
|
enum ConnType {
|
||||||
Local,
|
Local,
|
||||||
@ -140,9 +153,7 @@ impl NetUser {
|
|||||||
fn maybe_timeout(&mut self) {
|
fn maybe_timeout(&mut self) {
|
||||||
const TIME_OUT: u64 = 300;
|
const TIME_OUT: u64 = 300;
|
||||||
if self.info.timeout.elapsed().as_secs() >= TIME_OUT {
|
if self.info.timeout.elapsed().as_secs() >= TIME_OUT {
|
||||||
let e = esc!("[1;31m");
|
self.write_ln(concat!("\r\n\r\n", emph!(), "Timeout!", reset!()))
|
||||||
let r = esc!("[0m");
|
|
||||||
self.write_ln(format!("\r\n\r\n{e}Timeout!{r}").as_str())
|
|
||||||
.expect("write");
|
.expect("write");
|
||||||
process::exit(0);
|
process::exit(0);
|
||||||
}
|
}
|
||||||
@ -152,10 +163,14 @@ impl NetUser {
|
|||||||
let start_time = self.info.starttime;
|
let start_time = self.info.starttime;
|
||||||
let time_left = self.info.timeleft;
|
let time_left = self.info.timeleft;
|
||||||
if start_time.elapsed().as_secs() >= time_left {
|
if start_time.elapsed().as_secs() >= time_left {
|
||||||
let e = esc!("[1;31m");
|
self.write_ln(concat!(
|
||||||
let r = esc!("[0m");
|
"\r\n\r\n",
|
||||||
self.write_ln(format!("\r\n\r\n{e}You're out of time!{r}\r\n").as_str())
|
emph!(),
|
||||||
.expect("write");
|
"You're out of time!",
|
||||||
|
reset!(),
|
||||||
|
"\r\n"
|
||||||
|
))
|
||||||
|
.expect("write");
|
||||||
process::exit(0);
|
process::exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -224,23 +239,26 @@ impl Conn for User {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn door_clear_screen(user: &mut User) -> Result<()> {
|
pub mod screen {
|
||||||
user.write_str(
|
use crate::{esc, Conn, Result, User};
|
||||||
format!("{}{}", esc!("[2J"), esc!("[1;1H"))
|
const CLEAR: &'static str = concat!(esc!("[2J"), esc!("H"));
|
||||||
.to_string()
|
pub fn clear(user: &mut User) -> Result<()> {
|
||||||
.as_str(),
|
user.write_str(CLEAR)
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn door_display_file(user: &mut User, path: &str) -> Result<()> {
|
|
||||||
if let Ok(file) = std::fs::read(path) {
|
|
||||||
user.write(&file)?;
|
|
||||||
user.write_str(esc!("[0m").to_string().as_str())?;
|
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn door_read_string(user: &mut impl Conn, len: usize) -> Result<String> {
|
pub mod file {
|
||||||
|
use crate::{Conn, Result, User};
|
||||||
|
pub fn display(user: &mut User, path: &str) -> Result<()> {
|
||||||
|
if let Ok(file) = std::fs::read(path) {
|
||||||
|
user.write(&file)?;
|
||||||
|
user.write_str(reset!())?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_string(user: &mut impl Conn, len: usize) -> Result<String> {
|
||||||
const BACKSPACE: [u8; 3] = *b"\x08\x20\x08";
|
const BACKSPACE: [u8; 3] = *b"\x08\x20\x08";
|
||||||
|
|
||||||
user.write_str(esc!("[s"))?;
|
user.write_str(esc!("[s"))?;
|
||||||
@ -259,17 +277,17 @@ pub fn door_read_string(user: &mut impl Conn, len: usize) -> Result<String> {
|
|||||||
127 | 8 => {
|
127 | 8 => {
|
||||||
if !received.is_empty() {
|
if !received.is_empty() {
|
||||||
user.write(&BACKSPACE)?;
|
user.write(&BACKSPACE)?;
|
||||||
received.truncate(received.len() - 1);
|
received.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
user.write(std::slice::from_ref(&ch))?;
|
user.write(std::slice::from_ref(&ch))?;
|
||||||
received.extend_from_slice(&[ch]);
|
received.push(ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let s = std::str::from_utf8(&received).unwrap().to_string();
|
let s = String::from_utf8(received).unwrap();
|
||||||
user.write_ln(esc!("[0m").to_string().as_str())?;
|
user.write_ln(reset!())?;
|
||||||
Ok(s)
|
Ok(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -323,7 +341,7 @@ fn convert_socket(sock: i64) -> Result<TcpStream> {
|
|||||||
Ok(stream)
|
Ok(stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn door_init() -> Result<User> {
|
pub fn init() -> Result<User> {
|
||||||
let args = env::args().collect::<Vec<_>>();
|
let args = env::args().collect::<Vec<_>>();
|
||||||
if args.len() < 2 || 3 < args.len() {
|
if args.len() < 2 || 3 < args.len() {
|
||||||
panic!("Usage: door.exe door32.sys [socket]");
|
panic!("Usage: door.exe door32.sys [socket]");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user