Added conditional compilation for os type

This commit is contained in:
2023-04-07 20:36:55 -07:00
parent f8e2c32f9a
commit e0f36ce112
7 changed files with 192 additions and 149 deletions

12
src/entry/mod.rs Normal file
View File

@ -0,0 +1,12 @@
cfg_if::cfg_if! {
if #[cfg(target_family = "unix")] {
mod unix;
pub use unix::main as main_e;
} else {
pub fn main_e () -> Result<(), std::io::Error> {
println!("Not yet implemented.");
Ok(())
}
}
}

3
src/entry/unix/mod.rs Normal file
View File

@ -0,0 +1,3 @@
mod unix;
pub(in crate::entry::unix) mod promptbuffer;
pub use crate::entry::unix::unix::main;

View File

@ -0,0 +1,185 @@
use std::collections::VecDeque;
use std::io::Write;
use termion::raw::RawTerminal;
use termion::color;
use termion::style;
use crate::parser::substitute;
#[derive(Debug)]
pub struct PromptBuffer {
// History
hist: VecDeque<String>,
hist_maxlen: usize,
// Counts from back of hist.
// 0 means "not on history",
// 1 means "on last item of history"
hist_cursor: usize,
buffer: String,
buffer_changed: bool,
cursor: usize,
last_print_len: usize
}
impl PromptBuffer {
pub fn new(maxlen: usize) -> PromptBuffer {
return PromptBuffer {
hist: VecDeque::with_capacity(maxlen/2),
hist_maxlen: maxlen,
hist_cursor: 0,
buffer: String::with_capacity(64),
buffer_changed: false,
cursor: 0,
last_print_len: 0,
};
}
pub fn write_prompt(&mut self, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
let l = self.buffer.chars().count();
let i = if l == 0 {0} else {l - self.cursor};
// Draw prettyprinted expression
let (display_cursor, s) = substitute(&self.get_contents(), i);
write!(
stdout, "\r{}{}==>{}{} {}",
style::Bold,
color::Fg(color::Blue),
color::Fg(color::Reset),
style::Reset,
s
)?;
// If this string is shorter, clear the remaining old one.
if s.chars().count() < self.last_print_len {
write!(
stdout, "{}{}",
" ".repeat(self.last_print_len - s.chars().count()),
termion::cursor::Left((self.last_print_len - s.chars().count()) as u16)
)?;
}
// Move cursor to correct position
if display_cursor != 0 {
write!(
stdout, "{}",
termion::cursor::Left(display_cursor as u16)
)?;
stdout.flush()?;
}
self.last_print_len = s.chars().count();
stdout.flush()?;
return Ok(());
}
// Prompt methods
pub fn get_contents(&self) -> &String {&self.buffer}
pub fn enter(&mut self) -> String{
let s = String::from(self.buffer.trim());
self.buffer.clear();
self.hist_cursor = 0;
self.cursor = 0;
self.buffer_changed = false;
if s != "" { self.hist.push_back(s.clone()); }
while self.hist.len() > self.hist_maxlen {
self.hist.pop_front();
}
return s;
}
// Buffer manipulation
pub fn add_char(&mut self, c: char) {
self.buffer_changed = true;
if self.cursor == 0 {
self.buffer.push(c);
} else {
let l = self.buffer.chars().count();
let i = l - self.cursor;
self.buffer.insert(i, c);
}
}
pub fn backspace(&mut self) {
if self.buffer.len() == 0 { return }
self.buffer_changed = true;
let l = self.buffer.chars().count();
if self.cursor == 0 {
self.buffer.pop();
} else if self.cursor != l {
let i = l - self.cursor;
self.buffer.remove(i-1);
if self.cursor >= l {
self.cursor = l-1;
}
}
}
pub fn delete(&mut self) {
if self.cursor != 0 {
self.cursor -= 1;
self.backspace();
}
}
// Cursor manipulation
pub fn cursor_left(&mut self) {
let l = self.buffer.chars().count();
if self.cursor < l {
self.cursor += 1;
}
}
pub fn cursor_right(&mut self) {
if self.cursor > 0 {
self.cursor -= 1;
}
}
// History manipulation
pub fn hist_up(&mut self) {
if self.buffer_changed && self.buffer.len() != 0 { return; }
if self.hist_cursor < self.hist.len() {
if self.buffer.len() != 0 || !self.buffer_changed {
self.hist_cursor += 1;
}
self.buffer_changed = false;
if self.hist_cursor == 0 {
self.buffer.clear();
} else {
self.buffer = self.hist[self.hist.len() - self.hist_cursor].clone();
}
}
}
pub fn hist_down(&mut self) {
if self.buffer_changed && self.buffer.len() != 0 { return; }
if self.hist_cursor > 0 {
self.hist_cursor -= 1;
self.buffer_changed = false;
if self.hist_cursor == 0 {
self.buffer.clear();
} else {
self.buffer = self.hist[self.hist.len() - self.hist_cursor].clone();
}
} else {
self.buffer.clear();
}
}
}

158
src/entry/unix/unix.rs Normal file
View File

@ -0,0 +1,158 @@
use std::io::Write;
use std::io::stdout;
use std::io::stdin;
use termion::{
event::Key,
input::TermRead,
raw::IntoRawMode,
raw::RawTerminal,
color,
style,
};
use super::promptbuffer::PromptBuffer;
use crate::parser;
use crate::evaluate;
#[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),
// Icon colors
a = color::Fg(color::Magenta),
b = color::Fg(color::White),
// Title format
t = format!("{}{}", color::Fg(color::White), style::Bold),
// Version
v = format!("{}{}", color::Fg(color::White), style::Italic),
ver = env!("CARGO_PKG_VERSION"),
)?;
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();
let mut pb: PromptBuffer = PromptBuffer::new(64);
'outer: loop {
pb.write_prompt(&mut stdout)?;
let stdin = stdin();
for c in stdin.keys() {
if let Key::Char(q) = c.as_ref().unwrap() {
match q {
'\n' => {
let in_str = pb.enter();
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.print();
let g = evaluate::evaluate(g);
#[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.print(),
color::Fg(color::Reset)
)?;
},
Err(_) => {
write!(
stdout, "\n {}{}Mathematical Error: {}Failed to evaluate expression.{}\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),
)?;
}
};
break;
},
_ => { pb.add_char(*q); }
};
} else {
match c.unwrap() {
Key::Backspace => { pb.backspace(); },
Key::Delete => { pb.delete(); },
Key::Left => { pb.cursor_left(); },
Key::Right => { pb.cursor_right(); },
Key::Up => { pb.hist_up(); },
Key::Down => { pb.hist_down(); },
Key::Ctrl('d') |
Key::Ctrl('c') => { break 'outer; },
_ => {}
};
};
pb.write_prompt(&mut stdout)?;
}
}
write!(stdout, "\r\n")?;
return Ok(());
}