daisy/src/main.rs

138 lines
3.0 KiB
Rust
Raw Normal View History

2023-03-23 22:10:53 -07:00
use std::io::{Write, stdout, stdin};
2023-03-24 19:42:23 -07:00
2023-03-23 22:10:53 -07:00
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use termion::raw::RawTerminal;
use termion::{color, style};
2023-03-18 22:16:26 -07:00
2023-03-19 20:32:49 -07:00
mod parser;
2023-03-24 19:42:23 -07:00
mod promptbuffer;
use crate::promptbuffer::PromptBuffer;
2023-03-24 13:28:14 -07:00
use crate::parser::Token;
//use crate::parser::ParserError;
use crate::parser::LineLocation;
2023-03-24 13:28:14 -07:00
use crate::parser::Eval;
2023-03-18 22:16:26 -07:00
2023-03-24 19:42:23 -07:00
fn draw_line(
stdout: &mut RawTerminal<std::io::Stdout>,
s: &String,
clear_len: usize
) -> Result<(), std::io::Error> {
2023-03-23 22:10:53 -07:00
write!(
2023-03-24 19:42:23 -07:00
stdout, "\r{}{}==>{}{} {}",
2023-03-23 22:10:53 -07:00
style::Bold,
color::Fg(color::Blue),
color::Fg(color::Reset),
style::Reset,
2023-03-24 19:42:23 -07:00
s
2023-03-23 22:10:53 -07:00
)?;
2023-03-24 19:42:23 -07:00
// If this string is shorter, clear the remaining old one.
if clear_len != 0 {
write!(
stdout, "{}{}",
" ".repeat(clear_len as usize),
termion::cursor::Left(clear_len as u16)
)?;
}
2023-03-23 22:10:53 -07:00
stdout.flush()?;
2023-03-18 22:16:26 -07:00
2023-03-23 22:10:53 -07:00
return Ok(());
2023-03-18 22:16:26 -07:00
}
fn main() -> Result<(), std::io::Error> {
2023-03-23 22:10:53 -07:00
let mut stdout = stdout().into_raw_mode().unwrap();
//let size = termion::terminal_size().unwrap();
//write!(stdout, "{:?}", size).unwrap();
2023-03-24 19:42:23 -07:00
let mut pb: PromptBuffer = PromptBuffer::new(64);
let mut last_len: usize = 0;
2023-03-23 22:10:53 -07:00
'outer: loop {
2023-03-24 19:42:23 -07:00
draw_line(
&mut stdout,
pb.get_contents(),
if pb.get_contents().len() >= last_len
{ 0 } else {last_len - pb.get_contents().len()}
)?;
last_len = pb.get_contents().len();
2023-03-23 22:10:53 -07:00
let stdin = stdin();
for c in stdin.keys() {
if let Key::Char(q) = c.as_ref().unwrap() {
match q {
'\n' => {
2023-03-24 19:42:23 -07:00
let s = pb.enter();
2023-03-23 22:10:53 -07:00
if s == "" { write!(stdout, "\r\n")?; break; }
RawTerminal::suspend_raw_mode(&stdout)?;
write!(stdout, "\n")?;
let g = parser::parse(&s);
RawTerminal::activate_raw_mode(&stdout)?;
match g {
2023-03-24 19:42:23 -07:00
Ok(g) => {
2023-03-24 13:28:14 -07:00
let n = g.eval();
if let Token::Number(_, v) = n {
write!(
stdout, "\r\n {}{}={} {v}{}\r\n\n",
style::Bold,
color::Fg(color::Green),
style::Reset,
color::Fg(color::Reset)
)?;
} else { panic!(); }
2023-03-23 22:10:53 -07:00
},
Err((l, e)) => {
let LineLocation{pos, len} = l;
write!(
2023-03-24 13:28:14 -07:00
stdout, "{}{}{} {e:?}{}\r\n",
2023-03-23 22:10:53 -07:00
color::Fg(color::Red),
" ".repeat(pos + 4),
"^".repeat(len),
color::Fg(color::Reset),
)?;
}
};
break;
},
2023-03-24 19:42:23 -07:00
'/' => { pb.add_char('÷'); },
'*' => { pb.add_char('×'); },
_ => { pb.add_char(*q); }
2023-03-23 22:10:53 -07:00
};
} else {
match c.unwrap() {
2023-03-24 19:42:23 -07:00
Key::Backspace => { pb.backspace(); },
Key::Delete => { pb.delete(); },
2023-03-23 22:10:53 -07:00
Key::Left => {},
Key::Right => {},
2023-03-24 19:42:23 -07:00
Key::Up => { pb.hist_up(); },
Key::Down => { pb.hist_down(); },
2023-03-23 22:10:53 -07:00
Key::Ctrl('d') |
Key::Ctrl('c') => { break 'outer; },
_ => {}
};
};
2023-03-24 19:42:23 -07:00
draw_line(
&mut stdout,
pb.get_contents(),
if pb.get_contents().len() >= last_len
{ 0 } else {last_len - pb.get_contents().len()}
)?;
last_len = pb.get_contents().len();
2023-03-18 22:16:26 -07:00
}
}
2023-03-23 22:10:53 -07:00
write!(stdout, "\r\n")?;
return Ok(());
2023-03-18 22:16:26 -07:00
}