From 77c357c2f316045a36f01f3ae27812d74add56ab Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 20 Aug 2023 16:49:35 -0700 Subject: [PATCH] All style definitions in one place --- src/entrypoint/unix/promptbuffer.rs | 22 +++++-------- src/formattedtext.rs | 49 +++++++++++++++++------------ 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/entrypoint/unix/promptbuffer.rs b/src/entrypoint/unix/promptbuffer.rs index 7077bca..619f759 100644 --- a/src/entrypoint/unix/promptbuffer.rs +++ b/src/entrypoint/unix/promptbuffer.rs @@ -1,11 +1,12 @@ use std::collections::VecDeque; use std::io::Write; use termion::raw::RawTerminal; -use termion::color; -use termion::style; +use crate::formattedtext; use crate::parser::substitute_cursor; use crate::context::Context; +const PROMPT_STR: &str = "==> "; + #[derive(Debug)] pub struct PromptBuffer { // History @@ -49,18 +50,13 @@ impl PromptBuffer { let l = self.buffer.chars().count(); let i = if l == 0 {0} else {l - self.cursor}; - - //println!("{i} {}", self.cursor); - // Draw prettyprinted expression let (display_c, s) = substitute_cursor(context, &self.get_contents(), i); write!( - stdout, "\r{}{}==>{}{} {}", - style::Bold, - color::Fg(color::Blue), - color::Fg(color::Reset), - style::Reset, + stdout, "\r{}{PROMPT_STR}{}{}", + formattedtext::format_map('p', context).unwrap(), + formattedtext::format_map('n', context).unwrap(), s )?; @@ -72,15 +68,13 @@ impl PromptBuffer { )?; } - write!( stdout, "\r{}", - termion::cursor::Right((display_c + 4) as u16) + termion::cursor::Right((display_c + PROMPT_STR.chars().count()) as u16) )?; - stdout.flush()?; - self.last_print_len = s.chars().count(); stdout.flush()?; + self.last_print_len = s.chars().count(); return Ok(()); } diff --git a/src/formattedtext.rs b/src/formattedtext.rs index 68de1ef..dfcfc55 100644 --- a/src/formattedtext.rs +++ b/src/formattedtext.rs @@ -22,7 +22,8 @@ impl ToString for FormattedText { fn format_map_none(c: char) -> Option { Some(match c { 'n'|'i'|'t'|'a'| - 'e'|'c'|'s'|'r' + 'e'|'c'|'s'|'r'| + 'p' => { "".to_string() }, _ => { return None } }) @@ -37,24 +38,27 @@ fn format_map_ansi(c: char) -> Option { 'i' => { // Normal italic text format!("{}{}", color::Fg(color::Reset), color::Bg(color::Reset)) }, - 't' => { // Title text + 't' => { // Title text (should be cyan) format!("{}{}", color::Fg(color::AnsiValue(6)), color::Bg(color::Reset)) }, - 'a' => { // Colored text + 'a' => { // Colored text (should be pink) format!("{}{}", color::Fg(color::AnsiValue(5)), color::Bg(color::Reset)) }, - 'e' => { // Error titles + 'e' => { // Error titles (should be red) format!("{}{}", color::Fg(color::AnsiValue(1)), color::Bg(color::Reset)) }, - 'c' => { // Console text + 'c' => { // Console text (inverted black on white) format!("{}{}", color::Fg(color::AnsiValue(0)), color::Bg(color::AnsiValue(7))) }, - 's' => { // Repeat prompt (how => is styled) - format!("{}{}", color::Fg(color::AnsiValue(2)), color::Bg(color::Reset)) - }, - 'r' => { // Result prompt (how = is styled) + '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 } }) @@ -83,6 +87,9 @@ fn format_map_full(c: char) -> Option { '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) }, @@ -90,17 +97,24 @@ fn format_map_full(c: char) -> Option { format!("{}{}", color::Fg(color::Green), style::Bold) }, + _ => { return None } }) } +pub fn format_map(c: char, context: &Context) -> Option { + 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 newline(stdout: &mut RawTerminal) -> Result<(), std::io::Error> { - write!( - stdout, - "\r\n", - )?; + write!(stdout, "\n")?; return Ok(()); } } @@ -147,12 +161,7 @@ impl FormattedText { match (a, b) { (c, ']') => { // Normal text - let q = 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") - }; + let q = format_map(c, context); if q.is_some() { s.push_str(&q.unwrap()); @@ -175,7 +184,7 @@ impl FormattedText { } } - write!(stdout, "{}", s)?; + write!(stdout, "\r{}", s)?; return Ok(()); } }