All style definitions in one place

newfloat
Mark 2023-08-20 16:49:35 -07:00
parent 3ae5383eed
commit 77c357c2f3
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 37 additions and 34 deletions

View File

@ -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(());
}

View File

@ -22,7 +22,8 @@ impl ToString for FormattedText {
fn format_map_none(c: char) -> Option<String> {
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<String> {
'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<String> {
'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<String> {
format!("{}{}", color::Fg(color::Green), style::Bold)
},
_ => { return None }
})
}
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 newline(stdout: &mut RawTerminal<std::io::Stdout>) -> 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(());
}
}