@@ -6,7 +6,6 @@ use anstyle::{AnsiColor, Effects, Style};
6
6
use crate :: markdown:: { MdStream , MdTree } ;
7
7
8
8
const DEFAULT_COLUMN_WIDTH : usize = 140 ;
9
- const RESET : anstyle:: Reset = anstyle:: Reset ;
10
9
11
10
thread_local ! {
12
11
/// Track the position of viewable characters in our buffer
@@ -31,53 +30,52 @@ fn write_stream(
31
30
default : Option < Style > ,
32
31
indent : usize ,
33
32
) -> io:: Result < ( ) > {
34
- match default {
35
- Some ( c) => write ! ( buf, "{c:#}{c}" ) ?,
36
- None => write ! ( buf, "{RESET}" ) ?,
37
- }
38
-
39
33
for tt in stream {
40
34
write_tt ( tt, buf, default, indent) ?;
41
- if let Some ( c) = default {
42
- write ! ( buf, "{c:#}{c}" ) ?;
43
- }
44
35
}
36
+ reset_opt_style ( buf, default) ?;
45
37
46
- write ! ( buf, "{RESET}" ) ?;
47
38
Ok ( ( ) )
48
39
}
49
40
50
41
fn write_tt (
51
42
tt : & MdTree < ' _ > ,
52
43
buf : & mut Vec < u8 > ,
53
- _default : Option < Style > ,
44
+ default : Option < Style > ,
54
45
indent : usize ,
55
46
) -> io:: Result < ( ) > {
56
47
match tt {
57
48
MdTree :: CodeBlock { txt, lang : _ } => {
58
- write ! ( buf, "{RESET}" ) ?;
49
+ reset_opt_style ( buf, default ) ?;
59
50
let style = Style :: new ( ) . effects ( Effects :: DIMMED ) ;
60
- write ! ( buf, "{style}{txt}" ) ?;
51
+ write ! ( buf, "{style}{txt}{style:#}" ) ?;
52
+ render_opt_style ( buf, default) ?;
61
53
}
62
54
MdTree :: CodeInline ( txt) => {
63
- write ! ( buf, "{RESET}" ) ?;
64
- let style = Style :: new ( ) . effects ( Effects :: DIMMED ) ;
65
- write_wrapping ( buf, txt , indent , None , Some ( style ) ) ?;
55
+ reset_opt_style ( buf, default ) ?;
56
+ write_wrapping ( buf , txt , indent , None , Some ( Style :: new ( ) . effects ( Effects :: DIMMED ) ) ) ? ;
57
+ render_opt_style ( buf, default ) ?;
66
58
}
67
59
MdTree :: Strong ( txt) => {
68
- write ! ( buf, "{RESET}" ) ?;
69
- let style = Style :: new ( ) . effects ( Effects :: BOLD ) ;
70
- write_wrapping ( buf, txt , indent , None , Some ( style ) ) ?;
60
+ reset_opt_style ( buf, default ) ?;
61
+ write_wrapping ( buf , txt , indent , None , Some ( Style :: new ( ) . effects ( Effects :: BOLD ) ) ) ? ;
62
+ render_opt_style ( buf, default ) ?;
71
63
}
72
64
MdTree :: Emphasis ( txt) => {
73
- write ! ( buf, "{RESET}" ) ?;
74
- let style = Style :: new ( ) . effects ( Effects :: ITALIC ) ;
75
- write_wrapping ( buf, txt , indent , None , Some ( style ) ) ?;
65
+ reset_opt_style ( buf, default ) ?;
66
+ write_wrapping ( buf , txt , indent , None , Some ( Style :: new ( ) . effects ( Effects :: ITALIC ) ) ) ? ;
67
+ render_opt_style ( buf, default ) ?;
76
68
}
77
69
MdTree :: Strikethrough ( txt) => {
78
- write ! ( buf, "{RESET}" ) ?;
79
- let style = Style :: new ( ) . effects ( Effects :: STRIKETHROUGH ) ;
80
- write_wrapping ( buf, txt, indent, None , Some ( style) ) ?;
70
+ reset_opt_style ( buf, default) ?;
71
+ write_wrapping (
72
+ buf,
73
+ txt,
74
+ indent,
75
+ None ,
76
+ Some ( Style :: new ( ) . effects ( Effects :: STRIKETHROUGH ) ) ,
77
+ ) ?;
78
+ render_opt_style ( buf, default) ?;
81
79
}
82
80
MdTree :: PlainText ( txt) => {
83
81
write_wrapping ( buf, txt, indent, None , None ) ?;
@@ -105,7 +103,11 @@ fn write_tt(
105
103
4 .. => AnsiColor :: Cyan . on_default ( ) . effects ( Effects :: UNDERLINE | Effects :: ITALIC ) ,
106
104
0 => unreachable ! ( ) ,
107
105
} ;
106
+ reset_opt_style ( buf, default) ?;
107
+ write ! ( buf, "{cs}" ) ?;
108
108
write_stream ( stream, buf, Some ( cs) , 0 ) ?;
109
+ write ! ( buf, "{cs:#}" ) ?;
110
+ render_opt_style ( buf, default) ?;
109
111
buf. write_all ( b"\n " ) ?;
110
112
}
111
113
MdTree :: OrderedListItem ( n, stream) => {
@@ -122,8 +124,20 @@ fn write_tt(
122
124
MdTree :: Comment ( _) | MdTree :: LinkDef { .. } | MdTree :: RefLink { .. } => unreachable ! ( ) ,
123
125
}
124
126
125
- write ! ( buf, "{RESET}" ) ?;
127
+ Ok ( ( ) )
128
+ }
126
129
130
+ fn render_opt_style ( buf : & mut Vec < u8 > , style : Option < Style > ) -> io:: Result < ( ) > {
131
+ if let Some ( style) = & style {
132
+ write ! ( buf, "{style}" ) ?;
133
+ }
134
+ Ok ( ( ) )
135
+ }
136
+
137
+ fn reset_opt_style ( buf : & mut Vec < u8 > , style : Option < Style > ) -> io:: Result < ( ) > {
138
+ if let Some ( style) = & style {
139
+ write ! ( buf, "{style:#}" ) ?;
140
+ }
127
141
Ok ( ( ) )
128
142
}
129
143
@@ -141,9 +155,8 @@ fn write_wrapping(
141
155
link_url : Option < & str > ,
142
156
style : Option < Style > ,
143
157
) -> io:: Result < ( ) > {
144
- if let Some ( style) = & style {
145
- write ! ( buf, "{style}" ) ?;
146
- }
158
+ render_opt_style ( buf, style) ?;
159
+
147
160
let ind_ws = & b" " [ ..indent] ;
148
161
let mut to_write = text;
149
162
if let Some ( url) = link_url {
@@ -191,6 +204,7 @@ fn write_wrapping(
191
204
if link_url. is_some ( ) {
192
205
buf. write_all ( b"\x1b ]8;;\x1b \\ " ) ?;
193
206
}
207
+ reset_opt_style ( buf, style) ?;
194
208
Ok ( ( ) )
195
209
} )
196
210
}
0 commit comments