@@ -33,6 +33,7 @@ class Syntax:
3333 highlight_lines (Set[int]): A set of line numbers to highlight.
3434 code_width: Width of code to render (not including line numbers), or ``None`` to use all available width.
3535 tab_size (int, optional): Size of tabs. Defaults to 4.
36+ word_wrap (bool, optional): Enable word wrapping.
3637 """
3738
3839 def __init__ (
@@ -48,6 +49,7 @@ def __init__(
4849 highlight_lines : Set [int ] = None ,
4950 code_width : Optional [int ] = None ,
5051 tab_size : int = 4 ,
52+ word_wrap : bool = False
5153 ) -> None :
5254 self .code = code
5355 self .lexer_name = lexer_name
@@ -58,6 +60,7 @@ def __init__(
5860 self .highlight_lines = highlight_lines or set ()
5961 self .code_width = code_width
6062 self .tab_size = tab_size
63+ self .word_wrap = word_wrap
6164
6265 self ._style_cache : Dict [Any , Style ] = {}
6366 if not isinstance (theme , str ) and issubclass (theme , PygmentsStyle ):
@@ -82,6 +85,7 @@ def from_path(
8285 highlight_lines : Set [int ] = None ,
8386 code_width : Optional [int ] = None ,
8487 tab_size : int = 4 ,
88+ word_wrap : bool = False ,
8589 ) -> "Syntax" :
8690 """Construct a Syntax object from a file.
8791
@@ -97,6 +101,7 @@ def from_path(
97101 highlight_lines (Set[int]): A set of line numbers to highlight.
98102 code_width: Width of code to render (not including line numbers), or ``None`` to use all available width.
99103 tab_size (int, optional): Size of tabs. Defaults to 4.
104+ word_wrap (bool, optional): Enable word wrapping of code.
100105
101106 Returns:
102107 [Syntax]: A Syntax object that may be printed to the console
@@ -118,6 +123,7 @@ def from_path(
118123 start_line = start_line ,
119124 highlight_lines = highlight_lines ,
120125 code_width = code_width ,
126+ word_wrap = word_wrap ,
121127 )
122128
123129 def _get_theme_style (self , token_type ) -> Style :
@@ -208,7 +214,11 @@ def __measure__(self, console: "Console", max_width: int) -> "Measurement":
208214 return Measurement (max_width , max_width )
209215
210216 def __console__ (self , console : Console , options : ConsoleOptions ) -> RenderResult :
211- code_width = options .max_width if self .code_width is None else self .code_width
217+ code_width = (
218+ (options .max_width - self ._numbers_column_width - 1 )
219+ if self .code_width is None
220+ else self .code_width
221+ )
212222 code = self .code
213223 if self .dedent :
214224 code = textwrap .dedent (code )
@@ -231,7 +241,7 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult
231241 lines = lines [line_offset :end_line ]
232242
233243 numbers_column_width = self ._numbers_column_width
234- render_options = options .update (width = code_width + numbers_column_width )
244+ render_options = options .update (width = code_width )
235245
236246 (
237247 background_style ,
@@ -241,15 +251,18 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult
241251
242252 highlight_line = self .highlight_lines .__contains__
243253 _Segment = Segment
244- padding = _Segment (" " * numbers_column_width , background_style )
254+ padding = _Segment (" " * numbers_column_width + " " , background_style )
245255 new_line = _Segment ("\n " )
246256
247257 line_pointer = "❱ "
248258
249259 for line_no , line in enumerate (lines , self .start_line + line_offset ):
250- wrapped_lines = console .render_lines (
251- line , render_options , style = background_style
252- )
260+ if self .word_wrap :
261+ wrapped_lines = console .render_lines (
262+ line , render_options , style = background_style
263+ )
264+ else :
265+ wrapped_lines = [list (line .render (console , render_options , end = "" ))]
253266 for first , wrapped_line in loop_first (wrapped_lines ):
254267 if first :
255268 line_column = str (line_no ).rjust (numbers_column_width - 2 ) + " "
@@ -276,5 +289,5 @@ def __console__(self, console: Console, options: ConsoleOptions) -> RenderResult
276289
277290 console = Console ()
278291
279- syntax = Syntax .from_path (sys .argv [1 ], line_numbers = False )
292+ syntax = Syntax .from_path (sys .argv [1 ], line_numbers = True , word_wrap = True )
280293 console .print (syntax )
0 commit comments