Skip to content

Commit

Permalink
Merge "[IMPR] Simplify colorTagR"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Jan 9, 2025
2 parents f342be6 + 4074b6e commit de7d5ff
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions pywikibot/userinterfaces/terminal_interface_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import pywikibot
from pywikibot import config
from pywikibot.backports import Iterable, Sequence, removeprefix
from pywikibot.backports import Iterable, Sequence, batched, removeprefix
from pywikibot.bot_choice import (
ChoiceException,
Option,
Expand Down Expand Up @@ -53,7 +53,7 @@
]

colorTagR = re.compile(
'<<((:?{0});?(:?{0})?)>>'.format('|'.join([*colors, 'previous'])))
'<<((?:{0})(?:;(?:{0}))?)>>'.format('|'.join([*colors, 'previous'])))


class UI(ABUIC):
Expand Down Expand Up @@ -198,16 +198,13 @@ def _print(self, text, target_stream) -> None:
# Add default before the last linefeed
if text.endswith('\n'):
text_parts[-1] = re.sub(r'\r?\n\Z', '', text_parts[-1])
text_parts.extend(('default', None, None,
'\n', 'default', None, None))
else:
text_parts.append('default')
text_parts.extend(('default', '\n'))

text_parts.append('default')

# match.split() includes every regex group; for each matched color
# fg_col:b_col, fg_col and bg_col are added to the resulting list.
len_text_parts = len(text_parts[::4])
for index, (txt, next_color) in enumerate(zip(text_parts[::4],
text_parts[1::4])):
len_text_parts = len(text_parts) // 2
for index, (txt, next_color) in enumerate(batched(text_parts, 2,
strict=True)):
current_color = color_stack[-1]
if next_color == 'previous':
if len(color_stack) > 1: # keep the last element in the stack
Expand Down

0 comments on commit de7d5ff

Please sign in to comment.