Skip to content

Commit

Permalink
Renamed ansi-string to ansi_string; added is_formatting_parsable() an…
Browse files Browse the repository at this point in the history
…d is_formatting_valid() functions
  • Loading branch information
James Smith authored and James Smith committed Nov 20, 2024
1 parent 02e6f20 commit 6cbff11
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,10 @@ The first argument, `s`, is a string to be formatted. If this string contains AN
- Integer values which will be parsed in a similar way to above string ANSI directives
- The following setting types will be used verbatim as the ANSI graphics code and no exceptions will be thrown (handle with care)
- An `AnsiSetting` object generated outside of `AnsiFormat` function calls
- It is advised to check AnsiSetting.valid to ensure settings don't terminate the escape sequence
- It is advised to check `AnsiSetting.valid` to ensure settings don't terminate the escape sequence
- A string which starts with the character `"["` plus ANSI directives (ex: `"[38;5;214"`)

Hint: After creation, `is_optimizable()` can be called to determine if all settings are parsable. Call
`simplify()` in order to subsequently force invalid or redundant values to be thrown out.
Hint: After creation, `is_formatting_parsable()` can be called to determine if all settings are parsable. Call `simplify()` in order to force invalid or redundant values to be thrown out.

Examples:

Expand Down Expand Up @@ -359,9 +358,9 @@ ul_colors = [0xFF, 0x63, 0x47]
print(f"{ansi_str::rgb({fg_color});bg_rgb({bg_colors});ul_rgb({ul_colors})}")
```

#### is_optimizable
#### is_formatting_parsable

The methods `AnsiString.is_optimizable()` and `AnsiStr.is_optimizable()` are provided to determine if the provided formatting settings are considered valid and therefore optimizable.
The methods `AnsiString.is_formatting_parsable()` and `AnsiStr.is_formatting_parsable()` are provided to determine if the provided formatting settings are parsable into known ANSI codes.

#### simplify

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
long_description = fh.read()

setuptools.setup(
name='ansi-string',
name='ansi_string',
author='James Smith',
author_email='[email protected]',
description='ANSI String Formatter in Python for CLI Color and Style Formatting',
Expand Down
49 changes: 37 additions & 12 deletions src/ansi_string/ansi_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
)
from .ansi_parsing import ParsedAnsiControlSequenceString, parse_graphic_sequence, settings_to_dict

__version__ = '1.1.5'
PACKAGE_NAME = 'ansi-string'
__version__ = '1.1.6'
PACKAGE_NAME = 'ansi_string'

# Constant: all characters considered to be whitespaces - this is used in strip functionality
WHITESPACE_CHARS = ' \t\n\r\v\f'
Expand Down Expand Up @@ -176,8 +176,8 @@ def __init__(
- It is advised to check AnsiSetting.valid to ensure settings don't terminate the escape sequence
- A string which starts with the character "[" plus ANSI directives (ex: "[38;5;214")
Hint: After creation, is_optimizable() can be called to determine if all settings are parsable. Call
simplify() in order to subsequently force invalid or redundant values to be thrown out.
Hint: After creation, is_formatting_parsable() can be called to determine if all settings are parsable. Call
simplify() in order to force invalid or redundant values to be thrown out.
'''
# Key is the string index to make a color change at
self._fmts:Dict[int,'_AnsiSettingPoint'] = {}
Expand Down Expand Up @@ -678,19 +678,33 @@ def _apply_string_format(self, string_format:str, settings:Union[AnsiFormat, Ans

raise ValueError('Invalid format specifier')

def is_optimizable(self) -> bool:
def is_formatting_valid(self) -> bool:
'''
Returns True iff all of the following are true for this object:
- No verbatim setting strings (strings that starts with "[") were set in formatting
- All parsed settings are considered internally valid
Returns True iff all settings are valid. Validity only means that no settings will prematurely terminate the
escape code. Call is_formatting_parsable() to determine if all settings are meaningful.
'''
for fmt in self._fmts.values():
for setting in fmt.add:
if not setting.valid:
return False
return True

def is_formatting_parsable(self) -> bool:
'''
Returns True iff all settings are valid and parsable into known ANSI codes.
'''
# Check if optimization is possible
for fmt in self._fmts.values():
for setting in fmt.add:
if not setting.parsable:
return False
return True

def is_optimizable(self) -> bool:
'''
Returns True if optimization of formatting settings can be performed i.e. formatting is parsable.
'''
return self.is_formatting_parsable()

def to_str(self, __format_spec:str=None, optimize:bool=True) -> str:
'''
Returns an ANSI format string with both internal and given formatting spec set.
Expand Down Expand Up @@ -1823,11 +1837,22 @@ def __iadd__(self, value:Union[str,'AnsiString','AnsiStr']) -> 'AnsiStr':
# Can't add in place - always return a new instance
return (self + value)

def is_formatting_valid(self) -> bool:
'''
Returns True iff all settings are valid. Validity only means that no settings will prematurely terminate the
escape code. Call is_formatting_parsable() to determine if all settings are meaningful.
'''
return self._s.is_formatting_valid()

def is_formatting_parsable(self) -> bool:
'''
Returns True iff all settings are valid and parsable into known ANSI codes.
'''
return self._s.is_formatting_parsable()

def is_optimizable(self) -> bool:
'''
Returns True iff all of the following are true for this object:
- No verbatim setting strings (strings that starts with "[") were set in formatting
- All parsed settings are considered internally valid
Returns True if optimization of formatting settings can be performed i.e. formatting is parsable.
'''
return self._s.is_optimizable()

Expand Down

0 comments on commit 6cbff11

Please sign in to comment.