From e0f36ce1120740ae459137e2e69721a68a545c98 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 7 Apr 2023 20:36:55 -0700 Subject: [PATCH] Added conditional compilation for os type --- Cargo.lock | 7 ++ Cargo.toml | 6 +- src/entry/mod.rs | 12 ++ src/entry/unix/mod.rs | 3 + src/{ => entry/unix}/promptbuffer.rs | 0 src/entry/unix/unix.rs | 158 +++++++++++++++++++++++++++ src/main.rs | 155 ++------------------------ 7 files changed, 192 insertions(+), 149 deletions(-) create mode 100644 src/entry/mod.rs create mode 100644 src/entry/unix/mod.rs rename src/{ => entry/unix}/promptbuffer.rs (100%) create mode 100644 src/entry/unix/unix.rs diff --git a/Cargo.lock b/Cargo.lock index fdfb18e..d813e68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,10 +14,17 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "daisy" version = "0.0.1" dependencies = [ + "cfg-if", "rug", "termion", ] diff --git a/Cargo.toml b/Cargo.toml index 347cdc5..946f89a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,11 @@ lto = "fat" codegen-units = 1 panic = "abort" + [dependencies] -signal-hook = "0.3.15" +rug = "1.19.2" +cfg-if = "1.0.0" + +[target.'cfg(target_family = "unix")'.dependencies] termion = "2.0.1" rug = "1.19.2" \ No newline at end of file diff --git a/src/entry/mod.rs b/src/entry/mod.rs new file mode 100644 index 0000000..798a76c --- /dev/null +++ b/src/entry/mod.rs @@ -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(()) + } + } +} \ No newline at end of file diff --git a/src/entry/unix/mod.rs b/src/entry/unix/mod.rs new file mode 100644 index 0000000..0f4aa89 --- /dev/null +++ b/src/entry/unix/mod.rs @@ -0,0 +1,3 @@ +mod unix; +pub(in crate::entry::unix) mod promptbuffer; +pub use crate::entry::unix::unix::main; \ No newline at end of file diff --git a/src/promptbuffer.rs b/src/entry/unix/promptbuffer.rs similarity index 100% rename from src/promptbuffer.rs rename to src/entry/unix/promptbuffer.rs diff --git a/src/entry/unix/unix.rs b/src/entry/unix/unix.rs new file mode 100644 index 0000000..56a8ead --- /dev/null +++ b/src/entry/unix/unix.rs @@ -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) -> 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(()); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a92633c..9f95eae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,16 @@ -use std::io::{Write, stdout, stdin}; - -use termion::event::Key; -use termion::input::TermRead; -use termion::raw::IntoRawMode; -use termion::raw::RawTerminal; -use termion::{color, style}; - pub mod tokens; pub mod parser; pub mod evaluate; pub mod quantity; -mod promptbuffer; - - -use crate::promptbuffer::PromptBuffer; - //use crate::tokens::Token; //use crate::parser::ParserError; //use crate::parser::LineLocation; /* + Greeter ascii art: + ###### @@@@@@ # ##@@ @ ## #@ @@ @@ -30,146 +19,16 @@ use crate::promptbuffer::PromptBuffer; @ @@## # @@@@@@ ###### - Daisy 0.0.1 + Daisy 0.0.0 */ -#[inline(always)] -fn draw_greeter(stdout: &mut RawTerminal) -> 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(()); -} +mod entry; +use crate::entry::main_e; 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(()); + return main_e(); } + #[cfg(test)] mod tests; \ No newline at end of file