Skip to content

Commit

Permalink
Allow the fill character to be colon by looking for expected directiv…
Browse files Browse the repository at this point in the history
…es in each split
  • Loading branch information
James Smith authored and James Smith committed Nov 24, 2024
1 parent b82ac25 commit d7f7bd4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/ansi_string/ansi_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
)
from .ansi_parsing import ParsedAnsiControlSequenceString, parse_graphic_sequence, settings_to_dict

__version__ = '1.1.8'
__version__ = '1.1.9'
PACKAGE_NAME = 'ansi_string'

# Constant: all characters considered to be whitespaces - this is used in strip functionality
Expand Down Expand Up @@ -748,7 +748,23 @@ def to_str(self, __format_spec:str=None, optimize:bool=True, reset_start:bool=Fa
# Make a copy
obj = self.copy()

format_parts = __format_spec.split(':', 1)
format_parts = __format_spec.split(':', 2)

# If more than 2 colons found, its meaning can be determined by ansi_format spec - if it starts with [
if len(format_parts) > 2:
if format_parts[1].endswith('['):
# Third colon needs to be within ansi_format
format_parts[1] = ':'.join(format_parts[1:])
else:
# Second colon should be treated as fill character for string_format
format_parts[0] = ':'.join(format_parts[0:2])
format_parts[1] = format_parts[2]
del format_parts[2]
# Allow for fill char to be colon when no ansi_format is specified by looking for justification char which
# shouldn't exist in ansi_format spec unless it starts with [
elif len(format_parts) > 1 and re.match(r'^[-\+]?[<>\^]', format_parts[1]) is not None:
format_parts[0] = ':'.join(format_parts[0:])
del format_parts[1]

if len(format_parts) > 1:
# ANSI color/style formatting
Expand Down
28 changes: 28 additions & 0 deletions tests/test_ansi_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,34 @@ def test_format_right_only(self):
'###############################################################This has no ANSI formatting'
)

def test_format_colon_fillchar(self):
s = AnsiString('This has no ANSI formatting')
self.assertEqual(
f'{s::>90}',
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::This has no ANSI formatting'
)

def test_format_colon_fillchar2(self):
s = AnsiString('This has no ANSI formatting')
self.assertEqual(
f'{s::->90}',
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::This has no ANSI formatting'
)

def test_format_colon_fillchar3(self):
s = AnsiString('This has no ANSI formatting')
self.assertEqual(
f'{s::>90:}',
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::This has no ANSI formatting'
)

def test_format_colon_in_ansi_settings(self):
s = AnsiString('ANSI string')
self.assertEqual(
f'{s::[:}',
'\x1b[:mANSI string\x1b[m'
)

def test_format_right_justify_and_int(self):
s = AnsiString('This string will be formatted bold and red, right justify')
self.assertEqual(
Expand Down

0 comments on commit d7f7bd4

Please sign in to comment.