1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! Structures for working with styled text.

use std::ops::Deref;

use tui::text::{Span as TuiSpan, Spans as TuiSpans};

use crate::style::Style;

/// Text with a specific style.
///
/// As `Span` can only hold a single style, components typically accept [`Spans`]
/// or [`Lines`] when accepting text.
///
/// `Span` also implements `From<S: Into<String>>`, making it easy to pass
/// in string-like values to components accepting [`Span`], [`Spans`], and [`Lines`].
///
/// [`Lines`]: struct.Lines.html
/// [`Spans`]: struct.Spans.html
#[derive(Default, Clone)]
pub struct Span {
  pub text: String,
  pub style: Style,
}

impl Span {
  pub fn new<Str: Into<String>, Sty: Into<Style>>(text: Str, style: Sty) -> Self {
    Self {
      text: text.into(),
      style: style.into(),
    }
  }

  pub fn len(&self) -> usize {
    self.text.len()
  }
}

impl<S: Into<String>> From<S> for Span {
  fn from(s: S) -> Self {
    Self {
      text: s.into(),
      style: Style::default(),
    }
  }
}

impl<'a> From<&'a Span> for TuiSpan<'a> {
  fn from(span: &'a Span) -> Self {
    Self {
      content: (&span.text).into(),
      style: span.style.into(),
    }
  }
}

impl From<Span> for TuiSpan<'_> {
  fn from(span: Span) -> Self {
    Self {
      content: span.text.into(),
      style: span.style.into(),
    }
  }
}

/// A single line of text with a variety of styles.
///
/// Components that accept multiple lines of text should accept [`Lines`].
/// Components that accept single lines of text should accept `Spans`.
///
/// `Spans` implement `From<S: Into<Span>>`, making it easy to pass values of
/// many types such as [`String`], [`Span`] and [`&str`].
///
/// [`Lines`]: struct.Lines.html
/// [`Span`]: struct.Span.html
/// [`&str`]: https://doc.rust-lang.org/std/primitive.str.html
/// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
/// [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html
#[derive(Default, Clone)]
pub struct Spans(pub Vec<Span>);

impl Spans {
  pub fn new(spans: Vec<Span>) -> Self {
    Self(spans)
  }

  pub fn len(&self) -> usize {
    self.0.iter().map(|span| span.len()).sum()
  }
}

impl<S: Into<Span>> From<S> for Spans {
  fn from(s: S) -> Self {
    Spans(vec![s.into()])
  }
}

impl<'a> From<&'a Spans> for Spans {
  fn from(spans: &'a Spans) -> Self {
    spans.clone()
  }
}

impl<'a> From<&'a Spans> for TuiSpans<'a> {
  fn from(spans: &'a Spans) -> Self {
    TuiSpans(spans.0.iter().map(TuiSpan::from).collect())
  }
}

impl From<Spans> for TuiSpans<'_> {
  fn from(spans: Spans) -> Self {
    TuiSpans(spans.0.into_iter().map(TuiSpan::from).collect())
  }
}

impl Deref for Spans {
  type Target = Vec<Span>;

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

#[derive(Clone, Default)]
/// Multiple lines of text with a variety of styles.
///
/// Each [`Spans`] element in the inner [`Vec`] is considered a line. Components that
/// accept multiple lines of text should accept `Lines`. Components that accept single
/// lines of text should accept [`Spans`].
///
/// `Lines` implement `From<S: Into<Spans>>`, making it easy to pass values of
/// many types such as [`Spans`], [`Span`], [`String`] and [`&str`].
///
/// **Note**: The implementation of `From<S: Into<Spans>>` for `Lines` automatically
/// splits on newlines (`\n`). If you do not want this behavior, then construct
/// `Lines` directly.
///
/// [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html
/// [`Spans`]: struct.Spans.html
/// [`Span`]: struct.Span.html
/// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
pub struct Lines(pub Vec<Spans>);

impl<S: Into<Spans>> From<S> for Lines {
  fn from(spans: S) -> Self {
    let mut expanded = Vec::new();

    for span in spans.into().0 {
      let lines: Vec<&str> = span.text.split('\n').collect();
      expanded.push(Some(Span::new(lines[0], span.style)));

      for line in &lines[1..] {
        expanded.push(None);
        expanded.push(Some(Span::new(*line, span.style)));
      }
    }

    let split = expanded
      .split(|span| span.is_none())
      .map(|spans| Spans::new(spans.iter().flatten().cloned().collect::<Vec<Span>>()))
      .collect();

    Lines(split)
  }
}