Skip to content

Commit

Permalink
More consistent code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
MisanthropicBit committed Feb 22, 2021
1 parent a9955bf commit c7a7e3d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 32 deletions.
17 changes: 9 additions & 8 deletions examples/highlight_differences.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@ def colordiff(s1, s2, color):
print(s1, end='')
cprint_missing(ls1, ls2, color, [Attr.Bold])

colorise.highlight(s2, indices=diff_indices, attributes=[Attr.Bold],
fg=color, end='')
colorise.highlight(
s2, indices=diff_indices, attributes=[Attr.Bold], fg=color, end=''
)

cprint_missing(ls1, ls2, color, [Attr.Bold])


if __name__ == '__main__':
colordiff('Highlight the differences',
'Hightight tke sifferenc',
'red')
colordiff('Highlight the differences', 'Hightight tke sifferenc', 'red')

print()

colordiff('I wonder if these strings are any different?',
'A wonder iF thzse xxxings are xxy different???',
'red')
colordiff(
'I wonder if these strings are any different?',
'A wonder iF thzse xxxings are xxy different???',
'red'
)
10 changes: 6 additions & 4 deletions examples/humpty.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import colorise

if __name__ == '__main__':
humpty = ["Humpty Dumpty sat on the wall",
"Humpty Dumpty had a great fall",
"All the king's horses and all the king's men",
"Couldn't put Humpty together again"]
humpty = [
'Humpty Dumpty sat on the wall',
'Humpty Dumpty had a great fall',
"All the king's horses and all the king's men",
"Couldn't put Humpty together again",
]

# Set colors manually
colorise.set_color('red')
Expand Down
4 changes: 2 additions & 2 deletions examples/lorem.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
# All text print after the above call will be in that color...
print(IPSUM)

# ...until colorise.set_color is called with no argument or colorise
# is quit as it will automatically reset colorise
# ...until colorise.reset_color is called or colorise is quit as it will
# automatically reset colorise
25 changes: 15 additions & 10 deletions src/colorise/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,20 @@ def _parse_color_format(self, colors):
index = 0 if color[0] == 'f' else 2

if result[index]:
raise ValueError('Duplicate {0}ground color format'
.format('back' if index > 0 else 'fore'))
raise ValueError(
'Duplicate {0}ground color format'
.format('back' if index > 0 else 'fore')
)

result[index] = color[3:]
is_fg = index == 0
elif color in self._attribute_names:
# This is an attribute
result[1 if is_fg else 3].append(Attr.from_name(color))
else:
raise ValueError("Unknown color format or attribute '{0}'"
.format(color))
raise ValueError(
"Unknown color format or attribute '{0}'".format(color)
)

return result

Expand Down Expand Up @@ -169,16 +172,18 @@ def _vformat(self, format_string, args, kwargs, used_args,
# Handle arg indexing when empty field_names are given
if field_name == '':
if auto_arg_index is False:
raise ValueError('cannot switch from manual field '
'specification to automatic field '
'numbering')
raise ValueError(
'cannot switch from manual field specification to '
'automatic field numbering'
)
field_name = str(auto_arg_index)
auto_arg_index += 1
elif field_name.isdigit():
if auto_arg_index:
raise ValueError('cannot switch from manual field '
'specification to automatic field '
'numbering')
raise ValueError(
'cannot switch from manual field specification to '
'automatic field numbering'
)
# Disable auto arg incrementing, if it gets used later on,
# then an exception will be raised
auto_arg_index = False
Expand Down
10 changes: 6 additions & 4 deletions src/colorise/nix/color_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ def set_color(

for colorspec, isbg in ((fg, False), (bg, True)):
if colorspec:
prefix, color = get_color(colorspec, color_count,
colorise.nix.cluts, isbg)
prefix, color = get_color(
colorspec, color_count, colorise.nix.cluts, isbg
)
codes.append(prefix.format(color))

if codes:
Expand All @@ -105,5 +106,6 @@ def set_color(

def redefine_colors(color_map, file=sys.stdout):
"""Redefine the base console colors with a new mapping."""
raise colorise.error.NotSupportedError('Cannot redefine colors on nix '
'systems')
raise colorise.error.NotSupportedError(
'Cannot redefine colors on nix systems'
)
6 changes: 4 additions & 2 deletions src/colorise/win/win32_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,10 @@ def set_console_text_attribute(handle, flags):
if not handle or handle == WinHandle.INVALID:
raise ValueError('Invalid handle')

if kernel32.SetConsoleTextAttribute(handle.value,
wintypes.WORD(flags)) == 0:
if kernel32.SetConsoleTextAttribute(
handle.value,
wintypes.WORD(flags)
) == 0:
raise WinError()


Expand Down
5 changes: 3 additions & 2 deletions src/colorise/win/winhandle.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,6 @@ def console_mode(self, value):
self._console_mode = value

def __str__(self): # noqa: D105
return "{0}({1}, {2})".format(self.__class__.__name__,
self.fg, self.bg)
return "{0}({1}, {2})".format(
self.__class__.__name__, self.fg, self.bg
)

0 comments on commit c7a7e3d

Please sign in to comment.