@@ -6,7 +6,6 @@ use anstyle::{AnsiColor, Effects, Style};
66use crate :: markdown:: { MdStream , MdTree } ;
77
88const DEFAULT_COLUMN_WIDTH : usize = 140 ;
9- const RESET : anstyle:: Reset = anstyle:: Reset ;
109
1110thread_local ! {
1211 /// Track the position of viewable characters in our buffer
@@ -31,53 +30,52 @@ fn write_stream(
3130 default : Option < Style > ,
3231 indent : usize ,
3332) -> io:: Result < ( ) > {
34- match default {
35- Some ( c) => write ! ( buf, "{c:#}{c}" ) ?,
36- None => write ! ( buf, "{RESET}" ) ?,
37- }
38-
3933 for tt in stream {
4034 write_tt ( tt, buf, default, indent) ?;
41- if let Some ( c) = default {
42- write ! ( buf, "{c:#}{c}" ) ?;
43- }
4435 }
36+ reset_opt_style ( buf, default) ?;
4537
46- write ! ( buf, "{RESET}" ) ?;
4738 Ok ( ( ) )
4839}
4940
5041fn write_tt (
5142 tt : & MdTree < ' _ > ,
5243 buf : & mut Vec < u8 > ,
53- _default : Option < Style > ,
44+ default : Option < Style > ,
5445 indent : usize ,
5546) -> io:: Result < ( ) > {
5647 match tt {
5748 MdTree :: CodeBlock { txt, lang : _ } => {
58- write ! ( buf, "{RESET}" ) ?;
49+ reset_opt_style ( buf, default ) ?;
5950 let style = Style :: new ( ) . effects ( Effects :: DIMMED ) ;
60- write ! ( buf, "{style}{txt}" ) ?;
51+ write ! ( buf, "{style}{txt}{style:#}" ) ?;
52+ render_opt_style ( buf, default) ?;
6153 }
6254 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 ) ?;
6658 }
6759 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 ) ?;
7163 }
7264 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 ) ?;
7668 }
7769 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) ?;
8179 }
8280 MdTree :: PlainText ( txt) => {
8381 write_wrapping ( buf, txt, indent, None , None ) ?;
@@ -105,7 +103,11 @@ fn write_tt(
105103 4 .. => AnsiColor :: Cyan . on_default ( ) . effects ( Effects :: UNDERLINE | Effects :: ITALIC ) ,
106104 0 => unreachable ! ( ) ,
107105 } ;
106+ reset_opt_style ( buf, default) ?;
107+ write ! ( buf, "{cs}" ) ?;
108108 write_stream ( stream, buf, Some ( cs) , 0 ) ?;
109+ write ! ( buf, "{cs:#}" ) ?;
110+ render_opt_style ( buf, default) ?;
109111 buf. write_all ( b"\n " ) ?;
110112 }
111113 MdTree :: OrderedListItem ( n, stream) => {
@@ -122,8 +124,20 @@ fn write_tt(
122124 MdTree :: Comment ( _) | MdTree :: LinkDef { .. } | MdTree :: RefLink { .. } => unreachable ! ( ) ,
123125 }
124126
125- write ! ( buf, "{RESET}" ) ?;
127+ Ok ( ( ) )
128+ }
126129
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+ }
127141 Ok ( ( ) )
128142}
129143
@@ -141,9 +155,8 @@ fn write_wrapping(
141155 link_url : Option < & str > ,
142156 style : Option < Style > ,
143157) -> io:: Result < ( ) > {
144- if let Some ( style) = & style {
145- write ! ( buf, "{style}" ) ?;
146- }
158+ render_opt_style ( buf, style) ?;
159+
147160 let ind_ws = & b" " [ ..indent] ;
148161 let mut to_write = text;
149162 if let Some ( url) = link_url {
@@ -191,6 +204,7 @@ fn write_wrapping(
191204 if link_url. is_some ( ) {
192205 buf. write_all ( b"\x1b ]8;;\x1b \\ " ) ?;
193206 }
207+ reset_opt_style ( buf, style) ?;
194208 Ok ( ( ) )
195209 } )
196210}
0 commit comments