mirror of
https://github.com/MeexReay/bRAC.git
synced 2025-05-06 21:48:03 +03:00
simplify
This commit is contained in:
parent
9bc1cdcba7
commit
8399453be9
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +0,0 @@
|
|||||||
/target
|
|
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -1,7 +0,0 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 4
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "bRAC"
|
|
||||||
version = "0.1.0"
|
|
@ -1,6 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "bRAC"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
@ -1,2 +1,9 @@
|
|||||||
# bRAC
|
# bRAC
|
||||||
better RAC client
|
better RAC client
|
||||||
|
|
||||||
|
## how to use
|
||||||
|
|
||||||
|
```bash
|
||||||
|
rustc main.rs -o main # build
|
||||||
|
./main # run
|
||||||
|
```
|
||||||
|
@ -1,32 +1,16 @@
|
|||||||
use std::{error::Error, io::{stdin, stdout, BufRead, Read, Write}, net::TcpStream, thread};
|
use std::{error::Error, io::{stdin, stdout, BufRead, Read, Write}, net::TcpStream, thread};
|
||||||
|
|
||||||
const MAX_MESSAGES: usize = 100;
|
const MAX_MESSAGES: usize = 100;
|
||||||
|
const DEFAULT_HOST: &str = "meex.lol:11234";
|
||||||
|
|
||||||
type E = Box<dyn Error>;
|
fn send_message(host: &str, message: &str) -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
fn send_message(host: &str, message: &str) -> Result<(), E> {
|
|
||||||
let mut stream = TcpStream::connect(host)?;
|
let mut stream = TcpStream::connect(host)?;
|
||||||
stream.write_all(&[0x01])?;
|
stream.write_all(&[0x01])?;
|
||||||
stream.write_all(message.as_bytes())?;
|
stream.write_all(message.as_bytes())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sanitize_string(s: &str, sanitize_newlines: bool) -> String {
|
fn read_messages(host: &str) -> Result<Vec<String>, Box<dyn Error>> {
|
||||||
let mut sanitized = s.replace(&['\x08', '\x0D', '\x1B'][..], "");
|
|
||||||
|
|
||||||
if sanitize_newlines {
|
|
||||||
sanitized = sanitized.replace("\n", "\\\\n");
|
|
||||||
if !s.ends_with('\n') {
|
|
||||||
sanitized.push('\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sanitized
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// max messages count: 100
|
|
||||||
fn read_messages(host: &str) -> Result<Vec<String>, E> {
|
|
||||||
let mut stream = TcpStream::connect(host)?;
|
let mut stream = TcpStream::connect(host)?;
|
||||||
stream.write_all(&[0x00])?;
|
stream.write_all(&[0x00])?;
|
||||||
let packet_size = {
|
let packet_size = {
|
||||||
@ -44,7 +28,6 @@ fn read_messages(host: &str) -> Result<Vec<String>, E> {
|
|||||||
stream.read_exact(&mut buf)?;
|
stream.read_exact(&mut buf)?;
|
||||||
format!("{}{}", &buf_str, String::from_utf8_lossy(&buf).to_string())
|
format!("{}{}", &buf_str, String::from_utf8_lossy(&buf).to_string())
|
||||||
};
|
};
|
||||||
let packet_data = sanitize_string(&packet_data, false);
|
|
||||||
let mut lines: Vec<String> = packet_data.split("\n").map(|o| o.to_string()).collect();
|
let mut lines: Vec<String> = packet_data.split("\n").map(|o| o.to_string()).collect();
|
||||||
lines.reverse();
|
lines.reverse();
|
||||||
lines.truncate(MAX_MESSAGES);
|
lines.truncate(MAX_MESSAGES);
|
||||||
@ -52,15 +35,15 @@ fn read_messages(host: &str) -> Result<Vec<String>, E> {
|
|||||||
Ok(lines)
|
Ok(lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_console(messages: Vec<String>) -> Result<(), E> {
|
fn print_console(messages: Vec<String>) -> Result<(), Box<dyn Error>> {
|
||||||
let mut out = stdout().lock();
|
let mut out = stdout().lock();
|
||||||
let text = format!("{}\n> ", messages.join("\n"));
|
let text = format!("{}{}\n> ", "\n".repeat(MAX_MESSAGES - messages.len()), messages.join("\n"));
|
||||||
out.write_all(text.as_bytes())?;
|
out.write_all(text.as_bytes())?;
|
||||||
out.flush()?;
|
out.flush()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn recv_loop(host: &str) -> Result<(), E> {
|
fn recv_loop(host: &str) -> Result<(), Box<dyn Error>> {
|
||||||
let mut cache = Vec::new();
|
let mut cache = Vec::new();
|
||||||
while let Ok(messages) = read_messages(host) {
|
while let Ok(messages) = read_messages(host) {
|
||||||
if cache == messages { continue }
|
if cache == messages { continue }
|
||||||
@ -85,12 +68,12 @@ fn main() {
|
|||||||
|
|
||||||
let host = if let Some(host) = &host {
|
let host = if let Some(host) = &host {
|
||||||
if host.is_empty() {
|
if host.is_empty() {
|
||||||
"meex.lol:11234"
|
DEFAULT_HOST
|
||||||
} else {
|
} else {
|
||||||
host.as_str()
|
host
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
"meex.lol:11234"
|
DEFAULT_HOST
|
||||||
}.to_string();
|
}.to_string();
|
||||||
|
|
||||||
thread::spawn({
|
thread::spawn({
|
Loading…
Reference in New Issue
Block a user