extern crate ansi_term;
extern crate nix;
use ansi_term::Colour;
use std::env;
use std::path::Path;
use std::thread::sleep;
use std::time::{Duration};
use crate::config;
use crate::shell::proc::ShellState;
use crate::shell::{Shell};
use crate::translator::ioprocessor::IOProcessor;
use crate::utils::async_stdin;
use crate::utils::file;
pub fn run_interactive(processor: IOProcessor, config: &config::Config, shell: Option<String>) -> u8 {
let (shell, args): (String, Vec<String>) = match shell {
Some(sh) => (sh, vec![]),
None => (config.shell_config.exec.clone(), config.shell_config.args.clone()) };
let mut shell: Shell = match Shell::start(shell, args, &config.prompt_config) {
Ok(sh) => sh,
Err(err) => {
print_err(
String::from(format!("Could not start shell: {}", err)),
config.output_config.translate_output,
&processor,
);
return 255;
}
};
let mut last_state: ShellState = ShellState::Unknown;
let mut state_changed: bool = true; while last_state != ShellState::Terminated {
let current_state: ShellState = shell.get_state();
if current_state != last_state {
state_changed = true;
last_state = current_state;
}
if state_changed && current_state == ShellState::Idle {
shell.refresh_env();
shell.pprompt(&processor);
state_changed = false; } else if state_changed {
state_changed = false; }
if async_stdin::is_ready() { let input: String = async_stdin::read();
if input.trim().len() == 0 {
if last_state == ShellState::Idle {
shell.pprompt(&processor);
}
} else {
let input: String = match last_state {
ShellState::Idle => {
let mut argv: Vec<String> = Vec::with_capacity(input.matches(" ").count() + 1);
for arg in input.split_whitespace() {
argv.push(String::from(arg));
}
resolve_command(&mut argv, &config);
let input: String = argv.join(" ") + "\n";
match processor.expression_to_latin(input) {
Ok(ex) => ex,
Err(err) => {
print_err(String::from(format!("Input error: {:?}", err)), config.output_config.translate_output, &processor);
continue;
}
}
},
ShellState::SubprocessRunning => processor.text_to_latin(input),
_ => continue
};
if let Err(err) = shell.write(input) {
print_err(
String::from(err.to_string()),
config.output_config.translate_output,
&processor,
);
}
let new_state = shell.get_state(); if new_state != last_state {
last_state = new_state;
state_changed = true;
}
}
}
if let Ok((out, err)) = shell.read() {
if out.is_some() {
print_out(out.unwrap(), config.output_config.translate_output, &processor);
}
if err.is_some() {
print_err(err.unwrap().to_string(), config.output_config.translate_output, &processor);
}
}
sleep(Duration::from_nanos(100)); } match shell.stop() {
Ok(rc) => rc,
Err(err) => {
print_err(format!("Could not stop shell: {}", err), config.output_config.translate_output, &processor);
255
}
}
}
pub fn run_command(mut command: String, processor: IOProcessor, config: &config::Config, shell: Option<String>) -> u8 {
let (shell, args): (String, Vec<String>) = match shell {
Some(sh) => (sh, vec![]),
None => (config.shell_config.exec.clone(), config.shell_config.args.clone()) };
let mut shell: Shell = match Shell::start(shell, args, &config.prompt_config) {
Ok(sh) => sh,
Err(err) => {
print_err(
String::from(format!("Could not start shell: {}", err)),
config.output_config.translate_output,
&processor,
);
return 255;
}
};
while command.ends_with('\n') {
command.pop();
}
while command.ends_with(';') {
command.pop();
}
command.push_str("; exit $?\n");
if let Err(err) = shell.write(command) {
print_err(
String::from(format!("Could not start shell: {}", err)),
config.output_config.translate_output,
&processor,
);
return 255;
}
let _ = shell.write(String::from("\n"));
loop { if async_stdin::is_ready() { let input: String = async_stdin::read();
if input.trim().len() > 0 {
let input: String = processor.text_to_latin(input);
if let Err(err) = shell.write(input) {
print_err(
String::from(err.to_string()),
config.output_config.translate_output,
&processor,
);
}
}
}
if let Ok((out, err)) = shell.read() {
if out.is_some() {
print_out(out.unwrap(), config.output_config.translate_output, &processor);
}
if err.is_some() {
print_err(err.unwrap().to_string(), config.output_config.translate_output, &processor);
}
}
if shell.get_state() == ShellState::Terminated {
break;
}
sleep(Duration::from_nanos(100)); } match shell.stop() {
Ok(rc) => rc,
Err(err) => {
print_err(format!("Could not stop shell: {}", err), config.output_config.translate_output, &processor);
255
}
}
}
pub fn run_file(file: String, processor: IOProcessor, config: &config::Config, shell: Option<String>) -> u8 {
let file_path: &Path = Path::new(file.as_str());
let lines: Vec<String> = match file::read_lines(file_path) {
Ok(lines) => lines,
Err(_) => {
print_err(format!("{}: No such file or directory", file), config.output_config.translate_output, &processor);
return 255
}
};
let mut command: String = String::new();
for line in lines.iter() {
if line.starts_with("#") {
continue;
}
if line.len() == 0 {
continue;
}
command.push_str(line);
command.push(';');
}
run_command(command, processor, config, shell)
}
#[allow(dead_code)]
fn get_shell_from_env() -> Result<String, ()> {
if let Ok(val) = env::var("SHELL") {
Ok(val)
} else {
Err(())
}
}
fn resolve_command(argv: &mut Vec<String>, config: &config::Config) {
match config.get_alias(&argv[0]) {
Some(resolved) => argv[0] = resolved,
None => {}
};
}
fn print_err(err: String, to_cyrillic: bool, processor: &IOProcessor) {
match to_cyrillic {
true => eprintln!("{}", Colour::Red.paint(processor.text_to_cyrillic(err))),
false => eprintln!("{}", Colour::Red.paint(err)),
};
}
fn print_out(out: String, to_cyrillic: bool, processor: &IOProcessor) {
match to_cyrillic {
true => print!("{}", processor.text_to_cyrillic(out)),
false => print!("{}", out),
};
}