moved read function to mindus.rs and removed ansi encoding

This commit is contained in:
poslop
2022-12-04 20:26:08 -06:00
parent 95f377e2ee
commit deab97b46f
3 changed files with 28 additions and 18 deletions

View File

@@ -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::<TcpSock>().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(())
}

View File

@@ -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,