From deab97b46fde2ae3b475a52380af59323d11de9c Mon Sep 17 00:00:00 2001 From: poslop Date: Sun, 4 Dec 2022 20:26:08 -0600 Subject: [PATCH] moved read function to mindus.rs and removed ansi encoding --- Cargo.toml | 3 +++ src/main.rs | 20 +++----------------- src/mindus.rs | 23 ++++++++++++++++++++++- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8c30699..4824656 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,8 @@ edition = "2021" [dependencies] dotenv = "0.15.0" +serde = "1.0.148" serenity = "0.11" +strip-ansi-escapes = "0.1.1" tokio = { version = "1.21.2", features = ["macros", "rt-multi-thread"] } +toml = "0.5.9" diff --git a/src/main.rs b/src/main.rs index 4bac934..ccfb570 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ mod mindus; use crate::mindus::*; -use std::io::{Write, BufRead}; use std::{env}; use serenity::async_trait; use serenity::prelude::*; @@ -67,30 +66,17 @@ async fn pong(ctx: &Context, msg: &Message) -> CommandResult { async fn console(ctx: &Context, msg: &Message) -> CommandResult { let input = msg.content.strip_prefix(";console ").to_owned(); - let output = &mut String::new(); + if input == Option::None { msg.reply(ctx, "Not enough Parameters").await?; return Ok(()); } let data = ctx.data.read().await; + let sock = data.get::().unwrap(); - - let mut writer = std::io::BufWriter::new(sock.stream.try_clone()?); - let mut reader = std::io::BufReader::new(sock.stream.try_clone()?); - - writer.write((input.unwrap().to_owned() + "\n").as_bytes())?; - writer.flush().expect("flush failed"); - - loop { - match reader.read_line(output) { - Ok(t) => t, - Err(_) => break(), - }; - } - println!("{}", output); - msg.reply(ctx, format!("```ansi\n{}\n```", output)).await?; + msg.reply(ctx, format!("```ansi\n{}\n```", cons_rw(sock, &input.unwrap()))).await?; Ok(()) } diff --git a/src/mindus.rs b/src/mindus.rs index b928895..1a99966 100644 --- a/src/mindus.rs +++ b/src/mindus.rs @@ -1,5 +1,5 @@ use std::fs::{File, OpenOptions}; -use std::io::{Read, Write, Seek}; +use std::io::{Read, Write, Seek, BufRead}; use std::time::Duration; use std::net::TcpStream; use serenity::prelude::TypeMapKey; @@ -22,6 +22,27 @@ impl TypeMapKey for TcpSock { type Value = TcpSock; } +pub fn cons_rw(sock: &TcpSock, input: &str) -> String { + + let mut output = String::new(); + + let mut writer = std::io::BufWriter::new(sock.stream.try_clone().unwrap()); + let mut reader = std::io::BufReader::new(sock.stream.try_clone().unwrap()); + + writer.write((input.to_owned() + "\n").as_bytes()).unwrap(); + writer.flush().expect("flush failed"); + + loop { + match reader.read_line(&mut output) { + Ok(t) => t, + Err(_) => break(), + }; + } + println!("{}", output); + output = String::from_utf8(strip_ansi_escapes::strip(&output).unwrap()).unwrap(); + output +} + #[derive(serde::Deserialize, serde::Serialize)] pub struct Config { pub ip: String,