2424CONSOLE = Console ()
2525
2626
27- def get_syntax (code : str , syntax : str ) -> str :
28- """Produces highlighted text based on the syntax"""
27+ def conceal_text (segment : str ) -> str :
28+ """Produce the segment concealed like a password."""
29+ return "•" * len (segment )
30+
31+
32+ def syntax_highlight_text (code : str , syntax : str ) -> Text :
33+ """Produces highlighted text based on the syntax."""
2934 syntax_obj = Syntax (code , syntax )
3035 with CONSOLE .capture () as capture :
3136 CONSOLE .print (syntax_obj )
32- return capture .get ()
37+ return Text . from_ansi ( capture .get () )
3338
3439
3540class TextInput (Widget ):
@@ -171,7 +176,7 @@ def render(self) -> RenderableType:
171176 else :
172177 segments = [self .placeholder ]
173178 else :
174- segments = [self ._conceal_or_reveal (self .value )]
179+ segments = [self ._modify_text (self .value )]
175180
176181 text = Text .assemble (* segments )
177182
@@ -195,37 +200,26 @@ def render(self) -> RenderableType:
195200 box = rich .box .DOUBLE if self .has_focus else rich .box .SQUARE ,
196201 )
197202
198- def _conceal_or_reveal (self , segment : str ) -> Union [str , Text ]:
203+ def _modify_text (self , segment : str ) -> Union [str , Text ]:
199204 """
200- Produce the segment concealed like a password, as it was passed,
201- or syntax highlighted.
205+ Produces the text with modifications, such as password concealing.
202206 """
203207 if self .has_password :
204- return "•" * len (segment )
208+ return conceal_text (segment )
205209 if self .syntax :
206- return Text . from_ansi ( get_syntax ( segment , self .syntax ) )
210+ return syntax_highlight_text ( segment , self .syntax )
207211 return segment
208212
209- def _render_text_with_cursor (self ) -> List [Union [str , Tuple [str , Style ]]]:
213+ def _render_text_with_cursor (self ) -> List [Union [str , Text , Tuple [str , Style ]]]:
210214 """
211215 Produces the renderable Text object combining value and cursor
212216 """
213- if len (self .value ) == 0 :
214- segments = [self .cursor ]
215- else :
216- text = self ._conceal_or_reveal (self .value )
217- if self ._cursor_position == 0 :
218- segments = [self .cursor , text ]
219- elif self ._cursor_position == len (self .value ):
220- segments = [text , self .cursor ]
221- else :
222- segments = [
223- text [: self ._cursor_position ],
224- self .cursor ,
225- text [self ._cursor_position :],
226- ]
227-
228- return segments
217+ text = self ._modify_text (self .value )
218+ return [
219+ text [: self ._cursor_position ],
220+ self .cursor ,
221+ text [self ._cursor_position :],
222+ ]
229223
230224 async def on_focus (self , event : events .Focus ) -> None :
231225 self ._has_focus = True
0 commit comments