Skip to content

Commit bdd27b4

Browse files
committed
feat: Develop the paint method that merge all styles into String
1 parent c5f297f commit bdd27b4

File tree

1 file changed

+41
-47
lines changed

1 file changed

+41
-47
lines changed

src/styled_text.rs

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
use crate::colors::{
2-
PaintType,
3-
PaletteColor,
4-
Styles,
5-
Stylify,
6-
BLACK,
7-
BLUE,
8-
BRIGHT_BLUE,
9-
BRIGHT_CYAN,
10-
BRIGHT_GREEN,
11-
BRIGHT_MAGENTA,
12-
BRIGHT_RED,
13-
BRIGHT_WHITE,
14-
BRIGHT_YELLOW,
15-
CYAN,
16-
GRAY,
17-
GREEN,
18-
MAGENTA,
19-
RED,
20-
Rgb,
21-
WHITE,
22-
YELLOW,
1+
use crate::{
2+
ansi_escape_code::ANSIEscapeCode,
3+
colors::{
4+
PaintType,
5+
PaletteColor,
6+
Rgb,
7+
Styles,
8+
Stylify,
9+
BLACK,
10+
BLUE,
11+
BRIGHT_BLUE,
12+
BRIGHT_CYAN,
13+
BRIGHT_GREEN,
14+
BRIGHT_MAGENTA,
15+
BRIGHT_RED,
16+
BRIGHT_WHITE,
17+
BRIGHT_YELLOW,
18+
CYAN,
19+
GRAY,
20+
GREEN,
21+
MAGENTA,
22+
RED,
23+
WHITE,
24+
YELLOW,
25+
},
2326
};
2427

2528
// =======================================================================
@@ -49,36 +52,27 @@ impl StyledText {
4952

5053
pub fn paint(&mut self) -> String {
5154
println!("{:?}", self.start_styles);
52-
let mut default_paint_type = Styles::StylePaintType(PaintType::FG);
53-
let indexes: Vec<usize> = self.start_styles
55+
let mut default_paint_type = PaintType::FG;
56+
57+
let start_codes_list: Vec<String> = self.start_styles
5458
.iter()
55-
.enumerate()
56-
.filter_map(|(index, style)| {
57-
match style {
58-
Styles::StylePaintType(_) => Some(index),
59-
_ => None,
59+
.rev()
60+
.filter_map(|s| {
61+
if let Styles::StylePaintType(p) = s {
62+
default_paint_type = p.clone();
63+
return None;
6064
}
65+
let t = s.make_styles(Some(&default_paint_type));
66+
Some(ANSIEscapeCode::new(t.as_str()).code())
6167
})
68+
.collect::<Vec<_>>()
69+
.into_iter()
70+
.rev()
6271
.collect();
72+
let start_codes = start_codes_list.join("");
73+
let end_codes = ANSIEscapeCode::new("0").code();
6374

64-
if indexes.len() == 1 {
65-
// We sure about 1 element exist on i
66-
default_paint_type = self.start_styles
67-
.get(indexes[0])
68-
.unwrap_or(&default_paint_type)
69-
.clone();
70-
71-
// * call for make style on start_styles
72-
}
73-
if indexes.len() > 1 {
74-
// * each slice get it's own paint type!
75-
}
76-
77-
println!("{:?}", indexes);
78-
// Add formatter reset in the end
79-
// self.end_styles.push();
80-
// format!()
81-
String::new()
75+
format!("{}{}{}", start_codes, self.text, end_codes)
8276
}
8377

8478
pub fn fg(&mut self) -> &mut Self {

0 commit comments

Comments
 (0)