From 6ed405d71be1f0887101f4a444b0307c2ca74e56 Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Tue, 18 Jun 2019 16:24:57 +0530 Subject: add imports --- src/main.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index ffdb690..63e886e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ // std use std::f64; +use std::borrow::Cow::{self, Borrowed, Owned}; // modules mod lex; @@ -19,10 +20,12 @@ use crate::format::*; // extern crates use rustyline::error::ReadlineError; -use rustyline::Editor; +use rustyline::{ Editor, Context, Helper }; use rustyline::config::{ Builder, ColorMode, EditMode, CompletionType }; +use rustyline::hint::Hinter; +use rustyline::completion::{ FilenameCompleter, Completer, Pair }; use rustyline::highlight::{ Highlighter, MatchingBracketHighlighter }; -use rustyline::hint::{ Hinter, HistoryHinter }; + use clap::{Arg, App}; use lazy_static::lazy_static; -- cgit v1.2.3 From d31247992286bef8e808a8777c2c8b9bdb23a783 Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Tue, 18 Jun 2019 16:25:01 +0530 Subject: add hinter, completer, matching bracket highlighter --- src/main.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 75 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 63e886e..1a05904 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,67 @@ use rustyline::highlight::{ Highlighter, MatchingBracketHighlighter }; use clap::{Arg, App}; use lazy_static::lazy_static; -struct LineHelper (MatchingBracketHighlighter, HistoryHinter); + +struct RLHelper { + completer: FilenameCompleter, + highlighter: MatchingBracketHighlighter, + hinter: AnswerHinter, +} + +struct AnswerHinter { } + +impl Hinter for AnswerHinter { + fn hint(&self, line: &str, _: usize, _: &Context) -> Option { + let input = line.trim(); + let input = input.replace(" ", ""); + if input.len() == 0 { + return Some("".into()) + } + let dry_run = eval_math_expression(&input); + match dry_run { + Ok(ans) => return Some(format!("\x1b[90m = {}\x1b[0m", ans)), + Err(_) => return Some(format!("")) + }; + } +} + +impl Highlighter for RLHelper { + fn highlight_prompt<'p>(&self, prompt: &'p str) -> Cow<'p, str> { + Owned(String::from(prompt)) + } + + fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { + Owned("\x1b[1m".to_owned() + hint + "\x1b[m") + } + + fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> { + self.highlighter.highlight(line, pos) + } + + fn highlight_char(&self, line: &str, pos: usize) -> bool { + self.highlighter.highlight_char(line, pos) + } +} + +impl Completer for RLHelper { + type Candidate = Pair; + fn complete( + &self, + line: &str, + pos: usize, + ctx: &Context<'_>, + ) -> Result<(usize, Vec), ReadlineError> { + self.completer.complete(line, pos, ctx) + } +} + +impl Hinter for RLHelper { + fn hint(&self, line: &str, a: usize, b: &Context) -> Option { + self.hinter.hint(line, a, b) + } +} + +impl Helper for RLHelper {} struct Configuration { radian_mode: bool, @@ -60,10 +120,16 @@ fn main() { .completion_type(CompletionType::Circular) .max_history_size(1000) .build(); - let mut rl = Editor::<()>::with_config(config); + let mut rl = Editor::with_config(config); + let h = RLHelper { + completer: FilenameCompleter::new(), + highlighter: MatchingBracketHighlighter::new(), + hinter: AnswerHinter {} + }; + rl.set_helper(Some(h)); if rl.load_history("history.txt").is_err() { - println!("No previous history."); - } + println!("No previous history.") + }; loop { let readline = rl.readline("> "); @@ -130,11 +196,11 @@ fn parse_arguments() -> Configuration { .unwrap_or("10") .parse() .unwrap(), - base: config.value_of("base") - .unwrap_or("10") - .parse() - .unwrap(), - input, + base: config.value_of("base") + .unwrap_or("10") + .parse() + .unwrap(), + input, } } -- cgit v1.2.3 From ede1886262030d445ebf33e6093b5b282f0895dc Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Thu, 20 Jun 2019 14:18:07 +0530 Subject: quicc fix --- src/format/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/format/mod.rs b/src/format/mod.rs index 210b0cb..baae301 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -35,7 +35,7 @@ fn radix_fmt(number: f64, obase: usize) -> Result { let table: Vec = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars().collect(); // format integral part of float - let mut integral = number.trunc() as i64; + let mut integral = number.abs().trunc() as i64; let mut obase_int = String::new(); while integral >= obase as i64 { obase_int.push(table[(integral % obase as i64) as usize]); -- cgit v1.2.3 From f98a2796b7c1379b204a24586e598d7f8da9da9f Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Thu, 20 Jun 2019 14:26:39 +0530 Subject: fixes for line length --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1a05904..8a66c4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,7 @@ impl Hinter for AnswerHinter { } let dry_run = eval_math_expression(&input); match dry_run { - Ok(ans) => return Some(format!("\x1b[90m = {}\x1b[0m", ans)), + Ok(ans) => return Some(format!(" = {}", ans)), Err(_) => return Some(format!("")) }; } @@ -59,7 +59,7 @@ impl Highlighter for RLHelper { } fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { - Owned("\x1b[1m".to_owned() + hint + "\x1b[m") + Owned("\x1b[90m".to_owned() + hint + "\x1b[0m") } fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> { -- cgit v1.2.3 From 8c0cef894f00008ff1fe3e76588fca111776f68e Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Thu, 20 Jun 2019 16:04:49 +0530 Subject: add basic syntax highlighting --- src/main.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8a66c4d..a8fbcf2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,12 +32,11 @@ use lazy_static::lazy_static; struct RLHelper { completer: FilenameCompleter, - highlighter: MatchingBracketHighlighter, + highlighter: LineHighlighter, hinter: AnswerHinter, } struct AnswerHinter { } - impl Hinter for AnswerHinter { fn hint(&self, line: &str, _: usize, _: &Context) -> Option { let input = line.trim(); @@ -53,22 +52,27 @@ impl Hinter for AnswerHinter { } } -impl Highlighter for RLHelper { - fn highlight_prompt<'p>(&self, prompt: &'p str) -> Cow<'p, str> { - Owned(String::from(prompt)) +struct LineHighlighter { } +impl Highlighter for LineHighlighter { + fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { + Owned(format!("\x1b[90m{}\x1b[0m", hint)) + } + fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { + let op = eval_math_expression(line); + match op { + Ok(_) => Owned(line.into()), + Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line)) + } } +} +impl Highlighter for RLHelper { fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { - Owned("\x1b[90m".to_owned() + hint + "\x1b[0m") + self.highlighter.highlight_hint(hint) } - fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> { self.highlighter.highlight(line, pos) } - - fn highlight_char(&self, line: &str, pos: usize) -> bool { - self.highlighter.highlight_char(line, pos) - } } impl Completer for RLHelper { @@ -123,7 +127,7 @@ fn main() { let mut rl = Editor::with_config(config); let h = RLHelper { completer: FilenameCompleter::new(), - highlighter: MatchingBracketHighlighter::new(), + highlighter: LineHighlighter {}, hinter: AnswerHinter {} }; rl.set_helper(Some(h)); -- cgit v1.2.3