Added simple commands

This commit is contained in:
2023-06-07 14:38:22 -07:00
parent 156b94c937
commit c73bec0c13
3 changed files with 222 additions and 116 deletions

View File

@ -2,63 +2,104 @@ use std::io::Write;
use std::io::stdout;
use std::io::stdin;
// We only need this for debug
#[cfg(debug_assertions)]
use termion::raw::RawTerminal;
use termion::{
event::Key,
input::TermRead,
raw::IntoRawMode,
raw::RawTerminal,
color,
style,
};
use crate::tokens::EvalError;
use super::promptbuffer::PromptBuffer;
use crate::tokens::EvalError;
use crate::parser;
use crate::command;
/*
#[inline(always)]
fn draw_greeter(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
write!(
stdout,
"\n \
{a} ###### {b} @@@@@@\r\n \
{a}# ##{b}@@ @\r\n \
{a}## #{b}@ @@\r\n \
{a} {b}@@@@@@@@@@@@@{a}\r\n \
{b}@@ @{a}# ##\r\n \
{b}@ @@{a}## #\r\n \
{b} @@@@@@ {a} ###### {r}\r\n \
\n {t}Daisy{r} {v}v{ver}{r}\r\n\n",
r = format!("{}{}", color::Fg(color::Reset), style::Reset),
fn do_expression(
stdout: &mut RawTerminal<std::io::Stdout>,
s: &String
) -> Result<(), std::io::Error> {
#[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout)?;
let g = parser::parse(&s);
#[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout)?;
// Icon colors
a = color::Fg(color::Magenta),
b = color::Fg(color::White),
match g {
Ok(g) => {
#[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout)?;
let out_str = g.to_string();
let g = g.evaluate();
#[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout)?;
// Title format
t = format!("{}{}", color::Fg(color::White), style::Bold),
write!(
stdout, " {}{}=>{}{} {}\r\n",
style::Bold, color::Fg(color::Magenta),
style::Reset, color::Fg(color::Reset),
out_str
)?;
// Version
v = format!("{}{}", color::Fg(color::White), style::Italic),
ver = env!("CARGO_PKG_VERSION"),
)?;
match g {
Ok(q) => {
write!(
stdout, "\n {}{}={} {}{}\r\n\n",
style::Bold,
color::Fg(color::Green),
style::Reset,
q.to_string_outer(),
color::Fg(color::Reset)
)?;
},
Err(EvalError::BadMath) => {
write!(
stdout, "\n {}{}Mathematical Error: {}Failed to evaluate expression{}\r\n\n",
style::Bold,
color::Fg(color::Red),
style::Reset,
color::Fg(color::Reset),
)?;
},
Err(EvalError::IncompatibleUnit) => {
write!(
stdout, "\n {}{}Evaluation Error: {}Incompatible units{}\r\n\n",
style::Bold,
color::Fg(color::Red),
style::Reset,
color::Fg(color::Reset),
)?;
}
}
},
// Show parse error
Err((l, e)) => {
write!(
stdout, "{}{}{} {}{}\r\n",
color::Fg(color::Red),
" ".repeat(l.pos + 4),
"^".repeat(l.len),
e.to_message(),
color::Fg(color::Reset),
)?;
}
};
return Ok(());
}
*/
#[inline(always)]
pub fn main() -> Result<(), std::io::Error> {
let mut stdout = stdout().into_raw_mode().unwrap();
//draw_greeter(&mut stdout)?;
//let size = termion::terminal_size().unwrap();
//write!(stdout, "{:?}", size).unwrap();
@ -77,74 +118,13 @@ pub fn main() -> Result<(), std::io::Error> {
write!(stdout, "\r\n")?;
if in_str == "" { break; }
#[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout)?;
let g = parser::parse(&in_str);
#[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout)?;
match g {
Ok(g) => {
#[cfg(debug_assertions)]
RawTerminal::suspend_raw_mode(&stdout)?;
let out_str = g.to_string();
let g = g.evaluate();
#[cfg(debug_assertions)]
RawTerminal::activate_raw_mode(&stdout)?;
write!(
stdout, " {}{}=>{}{} {}\r\n",
style::Bold, color::Fg(color::Magenta),
style::Reset, color::Fg(color::Reset),
out_str
)?;
match g {
Ok(q) => {
write!(
stdout, "\n {}{}={} {}{}\r\n\n",
style::Bold,
color::Fg(color::Green),
style::Reset,
q.to_string_outer(),
color::Fg(color::Reset)
)?;
},
Err(EvalError::BadMath) => {
write!(
stdout, "\n {}{}Mathematical Error: {}Failed to evaluate expression{}\r\n\n",
style::Bold,
color::Fg(color::Red),
style::Reset,
color::Fg(color::Reset),
)?;
},
Err(EvalError::IncompatibleUnit) => {
write!(
stdout, "\n {}{}Evaluation Error: {}Incompatible units{}\r\n\n",
style::Bold,
color::Fg(color::Red),
style::Reset,
color::Fg(color::Reset),
)?;
}
}
},
// Show parse error
Err((l, e)) => {
write!(
stdout, "{}{}{} {}{}\r\n",
color::Fg(color::Red),
" ".repeat(l.pos + 4),
"^".repeat(l.len),
e.to_message(),
color::Fg(color::Reset),
)?;
}
};
if in_str.trim() == "quit" {
break 'outer;
} else if command::is_command(&in_str) {
command::do_command(&mut stdout, &in_str)?;
} else {
do_expression(&mut stdout, &in_str)?;
}
break;
},