color_span/view/mod.rs
1use std::fmt::{Debug, Formatter};
2
3use internship::IStr;
4use serde::{Deserialize, Serialize};
5
6use code_span::CodeView;
7
8mod convert;
9mod der;
10pub mod iter;
11mod ser;
12
13/// Write color palette into html
14///
15/// **Support 255 color at most**.
16///
17/// # Arguments
18///
19/// * `w`:
20///
21/// returns: Result<(), Error>
22///
23/// # Examples
24///
25/// ```
26/// use color_span::ColorClass;
27/// ```
28#[derive(Clone, Eq, PartialEq)]
29pub struct ColorView {
30 span: CodeView<IStr>,
31}
32
33/// # Arguments
34///
35/// * `text`:
36///
37/// returns: TextColorView
38///
39/// # Examples
40///
41/// ```
42/// use color_span::ColorView;
43/// ```
44#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
45pub struct ColorSpan {
46 /// content of the code span
47 pub text: String,
48 /// color class of the code span
49 pub color: IStr,
50}
51
52impl ColorView {
53 /// # Arguments
54 ///
55 /// * `text`:
56 ///
57 /// returns: TextColorView
58 ///
59 /// # Examples
60 ///
61 /// ```
62 /// use color_span::ColorView;
63 /// ```
64 #[inline]
65 pub fn new(text: impl Into<String>) -> ColorView {
66 Self { span: CodeView::blank(text) }
67 }
68 /// Color the text in the range of `start`..`end` to given color name
69 ///
70 /// # Arguments
71 ///
72 /// * `start`: start offset
73 /// * `end`: end offset
74 /// * `color`: color name
75 ///
76 /// # Examples
77 ///
78 /// ```
79 /// use color_span::ColorView;
80 /// ```
81 pub fn text(&self) -> &str {
82 self.span.get_text()
83 }
84 /// Color the text in the range of `start`..`end` to given color name
85 ///
86 /// # Arguments
87 ///
88 /// * `start`: start offset
89 /// * `end`: end offset
90 /// * `color`: color name
91 ///
92 /// # Examples
93 ///
94 /// ```
95 /// use color_span::ColorView;
96 /// ```
97 pub fn dye_position(&mut self, start: usize, end: usize, color: &str) {
98 let color = match color {
99 "" => None,
100 _ => Some(IStr::new(color)),
101 };
102 self.span.mark_position(start, end, color)
103 }
104 /// Color the text in the range of `start`..`end` to given color name
105 ///
106 /// # Arguments
107 ///
108 /// * `start`: start offset
109 /// * `end`: end offset
110 /// * `color`: color name
111 ///
112 /// # Examples
113 ///
114 /// ```
115 /// use color_span::ColorView;
116 /// ```
117 pub fn dye_offset(&mut self, start: usize, end: usize, color: &str) {
118 let color = match color {
119 "" => None,
120 _ => Some(IStr::new(color)),
121 };
122 self.span.mark_offset(start, end, color)
123 }
124}