mirror of
https://github.com/rm-dr/daisy
synced 2025-07-01 14:43:30 -07:00
Merge branch 'wasm' into dev
This commit is contained in:
@ -1,201 +0,0 @@
|
||||
use std::io::Write;
|
||||
use termion::raw::RawTerminal;
|
||||
use termion::color;
|
||||
use termion::style;
|
||||
use termion::clear;
|
||||
use termion::cursor;
|
||||
use std::ops::Add;
|
||||
use crate::context::Context;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone)]
|
||||
pub struct FormattedText {
|
||||
text: String
|
||||
}
|
||||
|
||||
impl ToString for FormattedText {
|
||||
fn to_string(&self) -> String { return self.text.clone(); }
|
||||
}
|
||||
|
||||
|
||||
fn format_map_none(c: char) -> Option<String> {
|
||||
Some(match c {
|
||||
'n'|'i'|'t'|'a'|
|
||||
'e'|'c'|'s'|'r'|
|
||||
'p'
|
||||
=> { "".to_string() },
|
||||
_ => { return None }
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
fn format_map_ansi(c: char) -> Option<String> {
|
||||
Some(match c {
|
||||
'n' => { // Normal text
|
||||
format!("{}{}", color::Fg(color::Reset), color::Bg(color::Reset))
|
||||
},
|
||||
'i' => { // Normal italic text
|
||||
format!("{}{}", color::Fg(color::Reset), color::Bg(color::Reset))
|
||||
},
|
||||
't' => { // Title text (should be cyan)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(6)), color::Bg(color::Reset))
|
||||
},
|
||||
'a' => { // Colored text (should be pink)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(5)), color::Bg(color::Reset))
|
||||
},
|
||||
'e' => { // Error titles (should be red)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(1)), color::Bg(color::Reset))
|
||||
},
|
||||
'c' => { // Console text (inverted black on white)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(0)), color::Bg(color::AnsiValue(7)))
|
||||
},
|
||||
'p' => { // Input prompt (how ==> is styled) (should be blue)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(4)), color::Bg(color::Reset))
|
||||
},
|
||||
's' => { // Repeat prompt (how => is styled) (should be pink)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(5)), color::Bg(color::Reset))
|
||||
},
|
||||
'r' => { // Result prompt (how = is styled) (should be green)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(2)), color::Bg(color::Reset))
|
||||
},
|
||||
|
||||
_ => { return None }
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// style::reset also resets color.
|
||||
// Make sure color comes AFTER style reset.
|
||||
fn format_map_full(c: char) -> Option<String> {
|
||||
Some(match c {
|
||||
'n' => { // Normal text
|
||||
format!("{}{}", style::Reset, color::Fg(color::Reset))
|
||||
},
|
||||
'i' => { // Normal italic text
|
||||
format!("{}{}", color::Fg(color::Reset), style::Italic)
|
||||
},
|
||||
't' => { // Title text
|
||||
format!("{}{}", color::Fg(color::Magenta), style::Bold)
|
||||
},
|
||||
'a' => { // Colored text
|
||||
format!("{}{}", style::Reset, color::Fg(color::Magenta))
|
||||
},
|
||||
'e' => { // Error titles
|
||||
format!("{}{}", color::Fg(color::Red), style::Bold)
|
||||
},
|
||||
'c' => { // Console text
|
||||
format!("{}{}", color::Fg(color::LightBlack), style::Italic)
|
||||
},
|
||||
'p' => { // Input prompt (how ==> is styled)
|
||||
format!("{}{}", color::Fg(color::Blue), style::Bold)
|
||||
},
|
||||
's' => { // Repeat prompt (how => is styled)
|
||||
format!("{}{}", color::Fg(color::Magenta), style::Bold)
|
||||
},
|
||||
'r' => { // Result prompt (how = is styled)
|
||||
format!("{}{}", color::Fg(color::Green), style::Bold)
|
||||
},
|
||||
|
||||
|
||||
_ => { return None }
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
impl FormattedText {
|
||||
pub fn newline(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||
write!(stdout, "\n")?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
pub fn format_map(c: char, context: &Context) -> Option<String> {
|
||||
match context.config.term_color_type {
|
||||
0 => format_map_none(c),
|
||||
1 => format_map_ansi(c),
|
||||
2 => format_map_full(c),
|
||||
_ => unreachable!("Invalid term_color_type")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl FormattedText {
|
||||
pub fn new(s: String) -> FormattedText {
|
||||
return FormattedText {
|
||||
text: s
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, s: &str) {
|
||||
self.text.push_str(s);
|
||||
}
|
||||
|
||||
|
||||
pub fn write(&self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||
|
||||
if self.text == "[clear]" {
|
||||
write!(
|
||||
stdout,
|
||||
"{}{}",
|
||||
clear::All,
|
||||
cursor::Goto(1, 1)
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
|
||||
let mut s = String::new();
|
||||
let mut chars = self.text.chars();
|
||||
|
||||
while let Some(c) = chars.next() {
|
||||
match c {
|
||||
'[' => {
|
||||
let a = chars.next().unwrap();
|
||||
|
||||
// Handle double [[ as escaped [
|
||||
if a == '[' { s.push('['); }
|
||||
|
||||
let b = chars.next().unwrap();
|
||||
|
||||
match (a, b) {
|
||||
(c, ']') => { // Normal text
|
||||
|
||||
let q = Self::format_map(c, context);
|
||||
|
||||
if q.is_some() {
|
||||
s.push_str(&q.unwrap());
|
||||
} else {
|
||||
s.push('[');
|
||||
s.push(a);
|
||||
s.push(b);
|
||||
}
|
||||
},
|
||||
|
||||
_ => {
|
||||
s.push('[');
|
||||
s.push(a);
|
||||
s.push(b);
|
||||
}
|
||||
}
|
||||
},
|
||||
'\n' => { s.push_str("\r\n") },
|
||||
_ => s.push(c)
|
||||
}
|
||||
}
|
||||
|
||||
write!(stdout, "\r{}", s)?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Add for FormattedText {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self::Output {
|
||||
return FormattedText::new(format!("{}{}", self.text, other.text));
|
||||
}
|
||||
}
|
40
src/formattedtext/formattedtext.rs
Normal file
40
src/formattedtext/formattedtext.rs
Normal file
@ -0,0 +1,40 @@
|
||||
use std::ops::Add;
|
||||
use std::ops::AddAssign;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone)]
|
||||
pub struct FormattedText {
|
||||
pub(super) text: String
|
||||
}
|
||||
|
||||
impl ToString for FormattedText {
|
||||
fn to_string(&self) -> String { return self.text.clone(); }
|
||||
}
|
||||
|
||||
impl FormattedText {
|
||||
pub fn new(s: String) -> FormattedText {
|
||||
return FormattedText {
|
||||
text: s
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, s: &str) {
|
||||
self.text.push_str(s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Add for FormattedText {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: Self) -> Self::Output {
|
||||
return FormattedText::new(format!("{}{}", self.text, other.text));
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign for FormattedText where {
|
||||
fn add_assign(&mut self, other: Self) {
|
||||
self.text.push_str(&other.text);
|
||||
}
|
||||
}
|
12
src/formattedtext/mod.rs
Normal file
12
src/formattedtext/mod.rs
Normal file
@ -0,0 +1,12 @@
|
||||
mod formattedtext;
|
||||
pub use formattedtext::FormattedText;
|
||||
|
||||
|
||||
// Select write implementation by target system
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_family = "unix")] {
|
||||
mod unix_backend;
|
||||
} else if #[cfg(target_arch = "wasm32")] {
|
||||
mod wasm_backend;
|
||||
}
|
||||
}
|
179
src/formattedtext/unix_backend.rs
Normal file
179
src/formattedtext/unix_backend.rs
Normal file
@ -0,0 +1,179 @@
|
||||
use super::FormattedText;
|
||||
use std::io::Write;
|
||||
use crate::context::Context;
|
||||
|
||||
use termion::raw::RawTerminal;
|
||||
use termion::color;
|
||||
use termion::style;
|
||||
use termion::clear;
|
||||
use termion::cursor;
|
||||
|
||||
fn format_map_ansi(s: &str) -> Option<String> {
|
||||
Some(match s {
|
||||
"n" => { // Normal text
|
||||
format!("{}{}", color::Fg(color::Reset), color::Bg(color::Reset))
|
||||
},
|
||||
"i" => { // Normal italic text
|
||||
format!("{}{}", color::Fg(color::Reset), color::Bg(color::Reset))
|
||||
},
|
||||
"t" => { // Title text (should be cyan)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(6)), color::Bg(color::Reset))
|
||||
},
|
||||
"a" => { // Colored text (should be pink)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(5)), color::Bg(color::Reset))
|
||||
},
|
||||
"e" => { // Error titles (should be red)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(1)), color::Bg(color::Reset))
|
||||
},
|
||||
"c" => { // Console text (inverted black on white)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(0)), color::Bg(color::AnsiValue(7)))
|
||||
},
|
||||
"p" => { // Input prompt (how ==> is styled) (should be blue)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(4)), color::Bg(color::Reset))
|
||||
},
|
||||
"s" => { // Repeat prompt (how => is styled) (should be pink)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(5)), color::Bg(color::Reset))
|
||||
},
|
||||
"r" => { // Result prompt (how = is styled) (should be green)
|
||||
format!("{}{}", color::Fg(color::AnsiValue(2)), color::Bg(color::Reset))
|
||||
},
|
||||
|
||||
_ => { return None }
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn format_map_none(s: &str) -> Option<String> {
|
||||
Some(match s {
|
||||
"n"|"i"|"t"|"a"|
|
||||
"e"|"c"|"s"|"r"|
|
||||
"p"
|
||||
=> { "".to_string() },
|
||||
_ => { return None }
|
||||
})
|
||||
}
|
||||
|
||||
// style::reset also resets color.
|
||||
// Make sure color comes AFTER style reset.
|
||||
fn format_map_full(s: &str) -> Option<String> {
|
||||
Some(match s {
|
||||
"n" => { // Normal text
|
||||
format!("{}{}", style::Reset, color::Fg(color::Reset))
|
||||
},
|
||||
"i" => { // Normal italic text
|
||||
format!("{}{}", color::Fg(color::Reset), style::Italic)
|
||||
},
|
||||
"t" => { // Title text
|
||||
format!("{}{}", color::Fg(color::Magenta), style::Bold)
|
||||
},
|
||||
"a" => { // Colored text
|
||||
format!("{}{}", style::Reset, color::Fg(color::Magenta))
|
||||
},
|
||||
"e" => { // Error titles
|
||||
format!("{}{}", color::Fg(color::Red), style::Bold)
|
||||
},
|
||||
"c" => { // Console text
|
||||
format!("{}{}", color::Fg(color::LightBlack), style::Italic)
|
||||
},
|
||||
"p" => { // Input prompt (how ==> is styled)
|
||||
format!("{}{}", color::Fg(color::Blue), style::Bold)
|
||||
},
|
||||
"s" => { // Repeat prompt (how => is styled)
|
||||
format!("{}{}", color::Fg(color::Magenta), style::Bold)
|
||||
},
|
||||
"r" => { // Result prompt (how = is styled)
|
||||
format!("{}{}", color::Fg(color::Green), style::Bold)
|
||||
},
|
||||
|
||||
|
||||
_ => { return None }
|
||||
})
|
||||
}
|
||||
|
||||
impl FormattedText {
|
||||
pub fn newline(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||
write!(stdout, "\n")?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
pub fn format_map(s: &str, context: &Context) -> Option<String> {
|
||||
match context.config.term_color_type {
|
||||
0 => format_map_none(s),
|
||||
1 => format_map_ansi(s),
|
||||
2 => format_map_full(s),
|
||||
_ => unreachable!("Invalid term_color_type")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write(&self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||
|
||||
let mut word = String::new();
|
||||
let mut reading = false; // are we reading a word?
|
||||
let mut chars = self.text.chars();
|
||||
let mut out = String::new();
|
||||
|
||||
while let Some(c) = chars.next() {
|
||||
|
||||
match c {
|
||||
'[' => {
|
||||
if reading {
|
||||
// Discard old word, start reading again.
|
||||
out.push_str(&word);
|
||||
word.clear();
|
||||
}
|
||||
|
||||
// Start reading a new word
|
||||
reading = true;
|
||||
word.push(c);
|
||||
},
|
||||
|
||||
']' => {
|
||||
if !reading {
|
||||
out.push(c);
|
||||
} else {
|
||||
word.push(c);
|
||||
|
||||
|
||||
let f = Self::format_map(&word[1..word.len()-1], context);
|
||||
|
||||
if f.is_some() {
|
||||
out.push_str(&f.unwrap());
|
||||
} else if word == "[clear]" {
|
||||
out.push_str(&format!(
|
||||
"{}{}",
|
||||
clear::All,
|
||||
cursor::Goto(1, 1)
|
||||
));
|
||||
} else if word.starts_with("[cursorright") {
|
||||
let n: u16 = word[12..word.len()-1].parse().unwrap();
|
||||
out.push_str(&format!(
|
||||
"{}",
|
||||
cursor::Right(n),
|
||||
));
|
||||
} else {
|
||||
out.push_str(&word);
|
||||
}
|
||||
|
||||
reading = false;
|
||||
word.clear();
|
||||
}
|
||||
},
|
||||
|
||||
'\n' => {
|
||||
if reading { word.push_str("\r\n"); }
|
||||
else { out.push_str("\r\n"); }
|
||||
},
|
||||
|
||||
_ => {
|
||||
if reading { word.push(c); }
|
||||
else { out.push(c); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
write!(stdout, "\r{}", out)?;
|
||||
stdout.flush()?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
80
src/formattedtext/wasm_backend.rs
Normal file
80
src/formattedtext/wasm_backend.rs
Normal file
@ -0,0 +1,80 @@
|
||||
use super::FormattedText;
|
||||
|
||||
fn format_map(s: &str) -> Option<String> {
|
||||
Some(match s {
|
||||
"n" => {"\x1B[0m"},
|
||||
"i" => {"\x1B[3m"},
|
||||
"t" => {"\x1B[1;35m"},
|
||||
"a" => {"\x1B[0;35m"},
|
||||
"e" => {"\x1B[1;31m"},
|
||||
"c" => {"\x1B[3;90m"},
|
||||
"p" => {"\x1B[1;34m"},
|
||||
"s" => {"\x1B[1;35m"},
|
||||
"r" => {"\x1B[1;32m"},
|
||||
_ => { return None }
|
||||
}.to_string())
|
||||
}
|
||||
|
||||
|
||||
impl FormattedText {
|
||||
pub fn write(&self) -> String {
|
||||
|
||||
let mut word = String::new();
|
||||
let mut reading = false; // are we reading a word?
|
||||
let mut chars = self.text.chars();
|
||||
let mut out = String::new();
|
||||
|
||||
while let Some(c) = chars.next() {
|
||||
|
||||
match c {
|
||||
'[' => {
|
||||
if reading {
|
||||
// Discard old word, start reading again.
|
||||
out.push_str(&word);
|
||||
word.clear();
|
||||
}
|
||||
|
||||
// Start reading a new word
|
||||
reading = true;
|
||||
word.push(c);
|
||||
},
|
||||
|
||||
']' => {
|
||||
if !reading {
|
||||
out.push(c);
|
||||
} else {
|
||||
word.push(c);
|
||||
|
||||
let f = format_map(&word[1..word.len()-1]);
|
||||
|
||||
if f.is_some() {
|
||||
out.push_str(&f.unwrap());
|
||||
} else if word == "[clear]" {
|
||||
out.push_str(&format!("\x1B[2J\x1B[H"));
|
||||
} else if word.starts_with("[cursorright") {
|
||||
let n: u16 = word[12..word.len()-1].parse().unwrap();
|
||||
out.push_str(&format!("\x1B[{n}C"));
|
||||
} else {
|
||||
out.push_str(&word);
|
||||
}
|
||||
|
||||
reading = false;
|
||||
word.clear();
|
||||
}
|
||||
},
|
||||
|
||||
'\n' => {
|
||||
if reading { word.push_str("\r\n"); }
|
||||
else { out.push_str("\r\n"); }
|
||||
},
|
||||
|
||||
_ => {
|
||||
if reading { word.push(c); }
|
||||
else { out.push(c); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
}
|
82
src/lib.rs
82
src/lib.rs
@ -10,14 +10,96 @@ mod context;
|
||||
mod formattedtext;
|
||||
mod errors;
|
||||
mod evaluate;
|
||||
mod promptbuffer;
|
||||
|
||||
pub use crate::formattedtext::FormattedText;
|
||||
pub use crate::context::Context;
|
||||
pub use crate::errors::DaisyError;
|
||||
pub use crate::evaluate::evaluate;
|
||||
pub use crate::promptbuffer::PromptBuffer;
|
||||
|
||||
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_arch = "wasm32")] {
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct State {
|
||||
pub context: Context,
|
||||
pub promptbuffer: PromptBuffer
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub extern fn daisy_init() -> *mut State {
|
||||
Box::into_raw(Box::new(State {
|
||||
context: Context::new(),
|
||||
promptbuffer: PromptBuffer::new(64)
|
||||
}))
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub extern fn daisy_free(state: *mut State) {
|
||||
unsafe { drop(Box::from_raw(state)) };
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn daisy_prompt(state: *mut State) -> String {
|
||||
let t = unsafe { (*state).promptbuffer.write_prompt(&mut (*state).context) };
|
||||
return t.write();
|
||||
}
|
||||
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn daisy_char(state: *mut State, s: String) -> String {
|
||||
let mut out = FormattedText::new("".to_string());
|
||||
|
||||
match &s[..] {
|
||||
"\r" => {
|
||||
// Print again without cursor, in case we pressed enter
|
||||
// while inside a substitution
|
||||
let t = unsafe { (*state).promptbuffer.write_prompt_nocursor(&mut (*state).context) };
|
||||
out += t;
|
||||
|
||||
|
||||
let in_str = unsafe { (*state).promptbuffer.enter() };
|
||||
out += FormattedText::new("\n".to_string());
|
||||
if in_str == "" {
|
||||
return format!("\r\n{}", daisy_prompt(state));
|
||||
}
|
||||
|
||||
if in_str.trim() == "quit" {
|
||||
return "[quit]".to_string();
|
||||
} else {
|
||||
let r = crate::do_string( unsafe { &mut (*state).context }, &in_str);
|
||||
|
||||
match r {
|
||||
Ok(t) | Err(t) => {
|
||||
out += t;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
"\x7F" => { unsafe { (*state).promptbuffer.backspace(); } },
|
||||
"\x1B[3~" => { unsafe { (*state).promptbuffer.delete(); } },
|
||||
"\x1B[D" => { unsafe { (*state).promptbuffer.cursor_left(); } },
|
||||
"\x1B[C" => { unsafe { (*state).promptbuffer.cursor_right(); } },
|
||||
"\x1B[A" => { unsafe { (*state).promptbuffer.hist_up(); } },
|
||||
"\x1B[B" => { unsafe { (*state).promptbuffer.hist_down(); } },
|
||||
|
||||
//'\x04' | '\x03'
|
||||
//=> { break 'outer; },
|
||||
|
||||
_ => { unsafe { (*state).promptbuffer.add_char(s.chars().next().unwrap()); } },
|
||||
};
|
||||
|
||||
let t = unsafe { (*state).promptbuffer.write_prompt(&mut (*state).context) };
|
||||
return (out + t).write();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn do_string(
|
||||
context: &mut Context,
|
||||
|
16
src/main.rs
16
src/main.rs
@ -1,5 +1,3 @@
|
||||
pub mod promptbuffer;
|
||||
|
||||
use std::io::stdout;
|
||||
use std::io::stdin;
|
||||
use std::env;
|
||||
@ -11,11 +9,10 @@ use termion::{
|
||||
color::DetectColors
|
||||
};
|
||||
|
||||
use promptbuffer::PromptBuffer;
|
||||
use daisycalc::PromptBuffer;
|
||||
use daisycalc::command;
|
||||
use daisycalc::Context;
|
||||
use daisycalc::FormattedText;
|
||||
|
||||
use daisycalc::do_string;
|
||||
|
||||
#[cfg(test)]
|
||||
@ -87,7 +84,8 @@ pub fn main() -> Result<(), std::io::Error> {
|
||||
|
||||
'outer: loop {
|
||||
|
||||
pb.write_prompt(&mut context, &mut stdout)?;
|
||||
let t = pb.write_prompt(&mut context);
|
||||
t.write(&context, &mut stdout)?;
|
||||
|
||||
let stdin = stdin();
|
||||
for c in stdin.keys() {
|
||||
@ -96,7 +94,10 @@ pub fn main() -> Result<(), std::io::Error> {
|
||||
'\n' => {
|
||||
// Print again without cursor, in case we pressed enter
|
||||
// while inside a substitution
|
||||
pb.write_prompt_nocursor(&mut context, &mut stdout)?;
|
||||
let t = pb.write_prompt_nocursor(&mut context);
|
||||
t.write(&context, &mut stdout)?;
|
||||
|
||||
|
||||
let in_str = pb.enter();
|
||||
FormattedText::newline(&mut stdout)?;
|
||||
if in_str == "" { break; }
|
||||
@ -140,7 +141,8 @@ pub fn main() -> Result<(), std::io::Error> {
|
||||
};
|
||||
};
|
||||
|
||||
pb.write_prompt(&mut context, &mut stdout)?;
|
||||
let t = pb.write_prompt(&mut context);
|
||||
t.write(&context, &mut stdout)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
use std::collections::VecDeque;
|
||||
use std::io::Write;
|
||||
use termion::raw::RawTerminal;
|
||||
use daisycalc::FormattedText;
|
||||
use daisycalc::parser::substitute_cursor;
|
||||
use daisycalc::Context;
|
||||
use crate::FormattedText;
|
||||
use crate::parser::substitute_cursor;
|
||||
use crate::Context;
|
||||
|
||||
const PROMPT_STR: &str = "==> ";
|
||||
|
||||
@ -18,12 +16,49 @@ pub struct PromptBuffer {
|
||||
// 1 means "on last item of history"
|
||||
hist_cursor: usize,
|
||||
|
||||
buffer: String,
|
||||
pub buffer: String,
|
||||
buffer_changed: bool,
|
||||
cursor: usize,
|
||||
last_print_len: usize
|
||||
}
|
||||
|
||||
impl PromptBuffer {
|
||||
// Same as write_primpt, but pretends there is no cursor
|
||||
pub fn write_prompt_nocursor(&mut self, context: &Context) -> FormattedText {
|
||||
let tmp = self.cursor;
|
||||
self.cursor = 0;
|
||||
let r = self.write_prompt(context);
|
||||
self.cursor = tmp;
|
||||
return r;
|
||||
}
|
||||
|
||||
pub fn write_prompt(&mut self, context: &Context) -> FormattedText {
|
||||
let l = self.buffer.chars().count();
|
||||
let i = if l == 0 {0} else {l - self.cursor};
|
||||
|
||||
// Draw prettyprinted expression
|
||||
let (display_c, s) = substitute_cursor(context, &self.get_contents(), i);
|
||||
|
||||
let mut tx = FormattedText::new("".to_string());
|
||||
|
||||
tx.push(&format!("\r[p]{PROMPT_STR}[n]{s}"));
|
||||
|
||||
|
||||
// If this string is shorter, clear the remaining old one.
|
||||
if s.chars().count() < self.last_print_len {
|
||||
tx.push(&" ".repeat(self.last_print_len - s.chars().count()));
|
||||
}
|
||||
|
||||
let q = (display_c + PROMPT_STR.chars().count()) as u16;
|
||||
tx.push(&format!("\r[cursorright{q}]"));
|
||||
|
||||
self.last_print_len = s.chars().count();
|
||||
|
||||
return tx;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl PromptBuffer {
|
||||
pub fn new(maxlen: usize) -> PromptBuffer {
|
||||
return PromptBuffer {
|
||||
@ -37,48 +72,6 @@ impl PromptBuffer {
|
||||
};
|
||||
}
|
||||
|
||||
// Same as write_primpt, but pretends there is no cursor
|
||||
pub fn write_prompt_nocursor(&mut self, context: &Context, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||
let tmp = self.cursor;
|
||||
self.cursor = 0;
|
||||
let r = self.write_prompt(context, stdout);
|
||||
self.cursor = tmp;
|
||||
return r;
|
||||
}
|
||||
|
||||
pub fn write_prompt(&mut self, context: &Context, 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_c, s) = substitute_cursor(context, &self.get_contents(), i);
|
||||
|
||||
write!(
|
||||
stdout, "\r{}{PROMPT_STR}{}{}",
|
||||
FormattedText::format_map('p', context).unwrap(),
|
||||
FormattedText::format_map('n', context).unwrap(),
|
||||
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()),
|
||||
)?;
|
||||
}
|
||||
|
||||
write!(
|
||||
stdout, "\r{}",
|
||||
termion::cursor::Right((display_c + PROMPT_STR.chars().count()) as u16)
|
||||
)?;
|
||||
|
||||
stdout.flush()?;
|
||||
self.last_print_len = s.chars().count();
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Prompt methods
|
||||
pub fn get_contents(&self) -> &String {&self.buffer}
|
||||
|
||||
|
Reference in New Issue
Block a user