mirror of https://github.com/rm-dr/daisy
All style definitions in one place
parent
3ae5383eed
commit
77c357c2f3
|
@ -1,11 +1,12 @@
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use termion::raw::RawTerminal;
|
use termion::raw::RawTerminal;
|
||||||
use termion::color;
|
use crate::formattedtext;
|
||||||
use termion::style;
|
|
||||||
use crate::parser::substitute_cursor;
|
use crate::parser::substitute_cursor;
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
|
|
||||||
|
const PROMPT_STR: &str = "==> ";
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PromptBuffer {
|
pub struct PromptBuffer {
|
||||||
// History
|
// History
|
||||||
|
@ -49,18 +50,13 @@ impl PromptBuffer {
|
||||||
let l = self.buffer.chars().count();
|
let l = self.buffer.chars().count();
|
||||||
let i = if l == 0 {0} else {l - self.cursor};
|
let i = if l == 0 {0} else {l - self.cursor};
|
||||||
|
|
||||||
|
|
||||||
//println!("{i} {}", self.cursor);
|
|
||||||
|
|
||||||
// Draw prettyprinted expression
|
// Draw prettyprinted expression
|
||||||
let (display_c, s) = substitute_cursor(context, &self.get_contents(), i);
|
let (display_c, s) = substitute_cursor(context, &self.get_contents(), i);
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
stdout, "\r{}{}==>{}{} {}",
|
stdout, "\r{}{PROMPT_STR}{}{}",
|
||||||
style::Bold,
|
formattedtext::format_map('p', context).unwrap(),
|
||||||
color::Fg(color::Blue),
|
formattedtext::format_map('n', context).unwrap(),
|
||||||
color::Fg(color::Reset),
|
|
||||||
style::Reset,
|
|
||||||
s
|
s
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
@ -72,15 +68,13 @@ impl PromptBuffer {
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
stdout, "\r{}",
|
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()?;
|
stdout.flush()?;
|
||||||
|
self.last_print_len = s.chars().count();
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,8 @@ impl ToString for FormattedText {
|
||||||
fn format_map_none(c: char) -> Option<String> {
|
fn format_map_none(c: char) -> Option<String> {
|
||||||
Some(match c {
|
Some(match c {
|
||||||
'n'|'i'|'t'|'a'|
|
'n'|'i'|'t'|'a'|
|
||||||
'e'|'c'|'s'|'r'
|
'e'|'c'|'s'|'r'|
|
||||||
|
'p'
|
||||||
=> { "".to_string() },
|
=> { "".to_string() },
|
||||||
_ => { return None }
|
_ => { return None }
|
||||||
})
|
})
|
||||||
|
@ -37,24 +38,27 @@ fn format_map_ansi(c: char) -> Option<String> {
|
||||||
'i' => { // Normal italic text
|
'i' => { // Normal italic text
|
||||||
format!("{}{}", color::Fg(color::Reset), color::Bg(color::Reset))
|
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))
|
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))
|
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))
|
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)))
|
format!("{}{}", color::Fg(color::AnsiValue(0)), color::Bg(color::AnsiValue(7)))
|
||||||
},
|
},
|
||||||
's' => { // Repeat prompt (how => is styled)
|
'p' => { // Input prompt (how ==> is styled) (should be blue)
|
||||||
format!("{}{}", color::Fg(color::AnsiValue(2)), color::Bg(color::Reset))
|
|
||||||
},
|
|
||||||
'r' => { // Result prompt (how = is styled)
|
|
||||||
format!("{}{}", color::Fg(color::AnsiValue(4)), color::Bg(color::Reset))
|
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 }
|
_ => { return None }
|
||||||
})
|
})
|
||||||
|
@ -83,6 +87,9 @@ fn format_map_full(c: char) -> Option<String> {
|
||||||
'c' => { // Console text
|
'c' => { // Console text
|
||||||
format!("{}{}", color::Fg(color::LightBlack), style::Italic)
|
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)
|
's' => { // Repeat prompt (how => is styled)
|
||||||
format!("{}{}", color::Fg(color::Magenta), style::Bold)
|
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)
|
format!("{}{}", color::Fg(color::Green), style::Bold)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
_ => { return None }
|
_ => { 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 {
|
impl FormattedText {
|
||||||
pub fn newline(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
pub fn newline(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
|
||||||
write!(
|
write!(stdout, "\n")?;
|
||||||
stdout,
|
|
||||||
"\r\n",
|
|
||||||
)?;
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -147,12 +161,7 @@ impl FormattedText {
|
||||||
match (a, b) {
|
match (a, b) {
|
||||||
(c, ']') => { // Normal text
|
(c, ']') => { // Normal text
|
||||||
|
|
||||||
let q = match context.config.term_color_type {
|
let q = format_map(c, context);
|
||||||
0 => format_map_none(c),
|
|
||||||
1 => format_map_ansi(c),
|
|
||||||
2 => format_map_full(c),
|
|
||||||
_ => unreachable!("Invalid term_color_type")
|
|
||||||
};
|
|
||||||
|
|
||||||
if q.is_some() {
|
if q.is_some() {
|
||||||
s.push_str(&q.unwrap());
|
s.push_str(&q.unwrap());
|
||||||
|
@ -175,7 +184,7 @@ impl FormattedText {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(stdout, "{}", s)?;
|
write!(stdout, "\r{}", s)?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue