|
|
|
@ -1,30 +1,38 @@
|
|
|
|
|
use either::Either;
|
|
|
|
|
use itertools::{multipeek, MultiPeek};
|
|
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
|
|
|
|
use unicode_width::UnicodeWidthStr;
|
|
|
|
|
|
|
|
|
|
use buffer::Buffer;
|
|
|
|
|
use layout::Rect;
|
|
|
|
|
use style::{Alignment, Color, Modifier, Style};
|
|
|
|
|
use layout::{Alignment, Rect};
|
|
|
|
|
use style::Style;
|
|
|
|
|
use widgets::{Block, Widget};
|
|
|
|
|
|
|
|
|
|
/// A widget to display some text. You can specify colors using commands embedded in
|
|
|
|
|
/// the text such as "{[color] [text]}".
|
|
|
|
|
/// A widget to display some text.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # extern crate tui;
|
|
|
|
|
/// # use tui::widgets::{Block, Borders, Paragraph};
|
|
|
|
|
/// # use tui::widgets::{Block, Borders, Paragraph, Text};
|
|
|
|
|
/// # use tui::style::{Style, Color};
|
|
|
|
|
/// # use tui::layout::{Alignment};
|
|
|
|
|
/// # fn main() {
|
|
|
|
|
/// Paragraph::default()
|
|
|
|
|
/// let text = [
|
|
|
|
|
/// Text::Data("First line\n"),
|
|
|
|
|
/// Text::StyledData("Second line\n", Style::default().fg(Color::Red))
|
|
|
|
|
/// ];
|
|
|
|
|
/// Paragraph::new(text.iter())
|
|
|
|
|
/// .block(Block::default().title("Paragraph").borders(Borders::ALL))
|
|
|
|
|
/// .style(Style::default().fg(Color::White).bg(Color::Black))
|
|
|
|
|
/// .wrap(true)
|
|
|
|
|
/// .text("First line\nSecond line\n{red Colored text}.");
|
|
|
|
|
/// .alignment(Alignment::Center)
|
|
|
|
|
/// .wrap(true);
|
|
|
|
|
/// # }
|
|
|
|
|
/// ```
|
|
|
|
|
pub struct Paragraph<'a> {
|
|
|
|
|
pub struct Paragraph<'a, 't, T>
|
|
|
|
|
where
|
|
|
|
|
T: Iterator<Item = &'t Text<'t>>,
|
|
|
|
|
{
|
|
|
|
|
/// A block to wrap the widget in
|
|
|
|
|
block: Option<Block<'a>>,
|
|
|
|
|
/// Widget style
|
|
|
|
@ -32,7 +40,7 @@ pub struct Paragraph<'a> {
|
|
|
|
|
/// Wrap the text or not
|
|
|
|
|
wrapping: bool,
|
|
|
|
|
/// The text to display
|
|
|
|
|
text: &'a str,
|
|
|
|
|
text: T,
|
|
|
|
|
/// Should we parse the text for embedded commands
|
|
|
|
|
raw: bool,
|
|
|
|
|
/// Scroll
|
|
|
|
@ -41,195 +49,62 @@ pub struct Paragraph<'a> {
|
|
|
|
|
alignment: Alignment,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Default for Paragraph<'a> {
|
|
|
|
|
fn default() -> Paragraph<'a> {
|
|
|
|
|
pub enum Text<'b> {
|
|
|
|
|
Data(&'b str),
|
|
|
|
|
StyledData(&'b str, Style),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 't, T> Paragraph<'a, 't, T>
|
|
|
|
|
where
|
|
|
|
|
T: Iterator<Item = &'t Text<'t>>,
|
|
|
|
|
{
|
|
|
|
|
pub fn new(text: T) -> Paragraph<'a, 't, T> {
|
|
|
|
|
Paragraph {
|
|
|
|
|
block: None,
|
|
|
|
|
style: Default::default(),
|
|
|
|
|
wrapping: false,
|
|
|
|
|
raw: false,
|
|
|
|
|
text: "",
|
|
|
|
|
text,
|
|
|
|
|
scroll: 0,
|
|
|
|
|
alignment: Alignment::Left,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Paragraph<'a> {
|
|
|
|
|
pub fn block(&'a mut self, block: Block<'a>) -> &mut Paragraph<'a> {
|
|
|
|
|
pub fn block(&'a mut self, block: Block<'a>) -> &mut Paragraph<'a, 't, T> {
|
|
|
|
|
self.block = Some(block);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn text(&mut self, text: &'a str) -> &mut Paragraph<'a> {
|
|
|
|
|
self.text = text;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn style(&mut self, style: Style) -> &mut Paragraph<'a> {
|
|
|
|
|
pub fn style(&mut self, style: Style) -> &mut Paragraph<'a, 't, T> {
|
|
|
|
|
self.style = style;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn wrap(&mut self, flag: bool) -> &mut Paragraph<'a> {
|
|
|
|
|
pub fn wrap(&mut self, flag: bool) -> &mut Paragraph<'a, 't, T> {
|
|
|
|
|
self.wrapping = flag;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn raw(&mut self, flag: bool) -> &mut Paragraph<'a> {
|
|
|
|
|
pub fn raw(&mut self, flag: bool) -> &mut Paragraph<'a, 't, T> {
|
|
|
|
|
self.raw = flag;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn scroll(&mut self, offset: u16) -> &mut Paragraph<'a> {
|
|
|
|
|
pub fn scroll(&mut self, offset: u16) -> &mut Paragraph<'a, 't, T> {
|
|
|
|
|
self.scroll = offset;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn alignment(&mut self, alignment: Alignment) -> &mut Paragraph<'a> {
|
|
|
|
|
pub fn alignment(&mut self, alignment: Alignment) -> &mut Paragraph<'a, 't, T> {
|
|
|
|
|
self.alignment = alignment;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Parser<'a, T>
|
|
|
|
|
where
|
|
|
|
|
T: Iterator<Item = &'a str>,
|
|
|
|
|
{
|
|
|
|
|
text: T,
|
|
|
|
|
mark: bool,
|
|
|
|
|
cmd_string: String,
|
|
|
|
|
style: Style,
|
|
|
|
|
base_style: Style,
|
|
|
|
|
escaping: bool,
|
|
|
|
|
styling: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, T> Parser<'a, T>
|
|
|
|
|
where
|
|
|
|
|
T: Iterator<Item = &'a str>,
|
|
|
|
|
{
|
|
|
|
|
fn new(text: T, base_style: Style) -> Parser<'a, T> {
|
|
|
|
|
Parser {
|
|
|
|
|
text: text,
|
|
|
|
|
mark: false,
|
|
|
|
|
cmd_string: String::from(""),
|
|
|
|
|
style: base_style,
|
|
|
|
|
base_style: base_style,
|
|
|
|
|
escaping: false,
|
|
|
|
|
styling: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_style(&mut self) {
|
|
|
|
|
for cmd in self.cmd_string.split(';') {
|
|
|
|
|
let args = cmd.split('=').collect::<Vec<&str>>();
|
|
|
|
|
if let Some(first) = args.get(0) {
|
|
|
|
|
match *first {
|
|
|
|
|
"fg" => if let Some(snd) = args.get(1) {
|
|
|
|
|
self.style.fg = Parser::<T>::str_to_color(snd);
|
|
|
|
|
},
|
|
|
|
|
"bg" => if let Some(snd) = args.get(1) {
|
|
|
|
|
self.style.bg = Parser::<T>::str_to_color(snd);
|
|
|
|
|
},
|
|
|
|
|
"mod" => if let Some(snd) = args.get(1) {
|
|
|
|
|
self.style.modifier = Parser::<T>::str_to_modifier(snd);
|
|
|
|
|
},
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn str_to_color(string: &str) -> Color {
|
|
|
|
|
match string {
|
|
|
|
|
"black" => Color::Black,
|
|
|
|
|
"red" => Color::Red,
|
|
|
|
|
"green" => Color::Green,
|
|
|
|
|
"yellow" => Color::Yellow,
|
|
|
|
|
"blue" => Color::Blue,
|
|
|
|
|
"magenta" => Color::Magenta,
|
|
|
|
|
"cyan" => Color::Cyan,
|
|
|
|
|
"gray" => Color::Gray,
|
|
|
|
|
"dark_gray" => Color::DarkGray,
|
|
|
|
|
"light_red" => Color::LightRed,
|
|
|
|
|
"light_green" => Color::LightGreen,
|
|
|
|
|
"light_blue" => Color::LightBlue,
|
|
|
|
|
"light_yellow" => Color::LightYellow,
|
|
|
|
|
"light_magenta" => Color::LightMagenta,
|
|
|
|
|
"light_cyan" => Color::LightCyan,
|
|
|
|
|
"white" => Color::White,
|
|
|
|
|
_ => Color::Reset,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn str_to_modifier(string: &str) -> Modifier {
|
|
|
|
|
match string {
|
|
|
|
|
"bold" => Modifier::Bold,
|
|
|
|
|
"italic" => Modifier::Italic,
|
|
|
|
|
"underline" => Modifier::Underline,
|
|
|
|
|
"invert" => Modifier::Invert,
|
|
|
|
|
"crossed_out" => Modifier::CrossedOut,
|
|
|
|
|
_ => Modifier::Reset,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn reset(&mut self) {
|
|
|
|
|
self.styling = false;
|
|
|
|
|
self.mark = false;
|
|
|
|
|
self.style = self.base_style;
|
|
|
|
|
self.cmd_string.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, T> Iterator for Parser<'a, T>
|
|
|
|
|
impl<'a, 't, T> Widget for Paragraph<'a, 't, T>
|
|
|
|
|
where
|
|
|
|
|
T: Iterator<Item = &'a str>,
|
|
|
|
|
T: Iterator<Item = &'t Text<'t>>,
|
|
|
|
|
{
|
|
|
|
|
type Item = (&'a str, Style);
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
|
match self.text.next() {
|
|
|
|
|
Some(s) => if s == "\\" {
|
|
|
|
|
if self.escaping {
|
|
|
|
|
Some((s, self.style))
|
|
|
|
|
} else {
|
|
|
|
|
self.escaping = true;
|
|
|
|
|
self.next()
|
|
|
|
|
}
|
|
|
|
|
} else if s == "{" {
|
|
|
|
|
if self.escaping {
|
|
|
|
|
self.escaping = false;
|
|
|
|
|
Some((s, self.style))
|
|
|
|
|
} else if self.mark {
|
|
|
|
|
Some((s, self.style))
|
|
|
|
|
} else {
|
|
|
|
|
self.style = self.base_style;
|
|
|
|
|
self.mark = true;
|
|
|
|
|
self.next()
|
|
|
|
|
}
|
|
|
|
|
} else if s == "}" && self.mark {
|
|
|
|
|
self.reset();
|
|
|
|
|
self.next()
|
|
|
|
|
} else if s == " " && self.mark {
|
|
|
|
|
if self.styling {
|
|
|
|
|
Some((s, self.style))
|
|
|
|
|
} else {
|
|
|
|
|
self.styling = true;
|
|
|
|
|
self.update_style();
|
|
|
|
|
self.next()
|
|
|
|
|
}
|
|
|
|
|
} else if self.mark && !self.styling {
|
|
|
|
|
self.cmd_string.push_str(s);
|
|
|
|
|
self.next()
|
|
|
|
|
} else {
|
|
|
|
|
Some((s, self.style))
|
|
|
|
|
},
|
|
|
|
|
None => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Widget for Paragraph<'a> {
|
|
|
|
|
fn draw(&mut self, area: &Rect, buf: &mut Buffer) {
|
|
|
|
|
let text_area = match self.block {
|
|
|
|
|
Some(ref mut b) => {
|
|
|
|
@ -245,13 +120,15 @@ impl<'a> Widget for Paragraph<'a> {
|
|
|
|
|
|
|
|
|
|
self.background(&text_area, buf, self.style.bg);
|
|
|
|
|
|
|
|
|
|
let graphemes = UnicodeSegmentation::graphemes(self.text, true);
|
|
|
|
|
let styled: Box<Iterator<Item = (&str, Style)>> = if self.raw {
|
|
|
|
|
Box::new(graphemes.map(|g| (g, self.style)))
|
|
|
|
|
} else {
|
|
|
|
|
Box::new(Parser::new(graphemes, self.style))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let style = self.style;
|
|
|
|
|
let styled = self.text.by_ref().flat_map(|t| match t {
|
|
|
|
|
&Text::Data(d) => {
|
|
|
|
|
Either::Left(UnicodeSegmentation::graphemes(d, true).map(|g| (g, style)))
|
|
|
|
|
}
|
|
|
|
|
&Text::StyledData(d, s) => {
|
|
|
|
|
Either::Right(UnicodeSegmentation::graphemes(d, true).map(move |g| (g, s)))
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
let mut styled = multipeek(styled);
|
|
|
|
|
|
|
|
|
|
fn get_cur_line_len<'a, I: Iterator<Item = (&'a str, Style)>>(
|
|
|
|
|