Merge pull request #1 from OpenSauce/master

Try this
This commit is contained in:
WanderingPenwing 2024-01-25 20:30:10 +01:00 committed by GitHub
commit e96e45548a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,11 +1,9 @@
use crate::tools::format_path; use crate::tools::format_path;
use std::{process::Command, env, path::Path};
use std::io::BufReader;
use std::io::BufRead; use std::io::BufRead;
use std::io::BufReader;
use std::io::Read;
use std::process::Stdio; use std::process::Stdio;
use nix::fcntl::{fcntl, FcntlArg, OFlag}; use std::{env, path::Path, process::Command};
use std::os::unix::io::AsRawFd;
pub struct CommandEntry { pub struct CommandEntry {
pub env: String, pub env: String,
@ -16,7 +14,6 @@ pub struct CommandEntry {
pub error_buffer: BufReader<std::process::ChildStderr>, pub error_buffer: BufReader<std::process::ChildStderr>,
} }
impl CommandEntry { impl CommandEntry {
pub fn new(command: String) -> Self { pub fn new(command: String) -> Self {
let (stdout_reader, stderr_reader) = execute(command.clone()); let (stdout_reader, stderr_reader) = execute(command.clone());
@ -32,34 +29,35 @@ impl CommandEntry {
} }
pub fn update(&mut self) { pub fn update(&mut self) {
let mut output = String::new(); for line in self.output_buffer.by_ref().lines() {
let _ = self.output_buffer.read_line(&mut output); match line {
if !output.is_empty() { Ok(line) => self.output += &format!("{}\n", line),
self.output += &output; Err(_) => return,
}
} }
let mut error = String::new(); for line in self.error_buffer.by_ref().lines() {
let _ = self.error_buffer.read_line(&mut error); match line {
if !error.is_empty() { Ok(line) => self.error += &format!("{}\n", line),
self.error += &error; Err(_) => return,
}
} }
} }
} }
pub fn send_command(command: String) -> CommandEntry { pub fn send_command(command: String) -> CommandEntry {
if command.len() < 2 { if command.len() < 2 {
return CommandEntry::new(command); return CommandEntry::new(command);
} }
if &command[..2] != "cd" { if &command[..2] != "cd" {
return CommandEntry::new(command) return CommandEntry::new(command);
} }
if command.len() < 4 { if command.len() < 4 {
let mut entry = CommandEntry::new("echo Invalid cd, should provide path >&2".to_string()); let mut entry = CommandEntry::new("echo Invalid cd, should provide path >&2".to_string());
entry.command = command; entry.command = command;
return entry return entry;
} }
let path_append = command[3..].replace("~", "/home/penwing"); let path_append = command[3..].replace("~", "/home/penwing");
@ -68,22 +66,27 @@ pub fn send_command(command: String) -> CommandEntry {
if format!("{}", path.display()) == "/" { if format!("{}", path.display()) == "/" {
let mut entry = CommandEntry::new("echo Root access denied >&2".to_string()); let mut entry = CommandEntry::new("echo Root access denied >&2".to_string());
entry.command = command; entry.command = command;
return entry return entry;
} }
if env::set_current_dir(path).is_ok() { if env::set_current_dir(path).is_ok() {
let mut entry = CommandEntry::new(format!("echo Moved to : {}", path.display())); let mut entry = CommandEntry::new(format!("echo Moved to : {}", path.display()));
entry.command = command; entry.command = command;
return entry return entry;
} else { } else {
let mut entry = CommandEntry::new(format!("echo Could not find path : {} >&2", path.display())); let mut entry =
CommandEntry::new(format!("echo Could not find path : {} >&2", path.display()));
entry.command = command; entry.command = command;
return entry return entry;
} }
} }
pub fn execute(
pub fn execute(command: String) -> (BufReader<std::process::ChildStdout>, BufReader<std::process::ChildStderr>) { command: String,
) -> (
BufReader<std::process::ChildStdout>,
BufReader<std::process::ChildStderr>,
) {
let mut child = Command::new("sh") let mut child = Command::new("sh")
.arg("-c") .arg("-c")
.arg(command.clone()) .arg(command.clone())
@ -93,14 +96,7 @@ pub fn execute(command: String) -> (BufReader<std::process::ChildStdout>, BufRea
.expect("failed to execute process"); .expect("failed to execute process");
let stdout = child.stdout.take().unwrap(); let stdout = child.stdout.take().unwrap();
let stdout_fd = stdout.as_raw_fd();
fcntl(stdout_fd, FcntlArg::F_SETFL(OFlag::O_NONBLOCK)).expect("Failed to set non-blocking mode");
let stderr = child.stderr.take().unwrap(); let stderr = child.stderr.take().unwrap();
let stderr_fd = stderr.as_raw_fd();
fcntl(stderr_fd, FcntlArg::F_SETFL(OFlag::O_NONBLOCK)).expect("Failed to set non-blocking mode");
return (BufReader::new(stdout), BufReader::new(stderr)); return (BufReader::new(stdout), BufReader::new(stderr));
} }