jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1107961?usp=email )
Change subject: [IMPR] Simplify colorTagR ......................................................................
[IMPR] Simplify colorTagR
only include the outer colorTagR regex group and hide the separate groups fg_col and bg_col which were never used.
Use batched instead of text_parts slice to process a line.
Change-Id: Ie2bfec424af6360278cb41d7512638d5dd099caf --- M pywikibot/userinterfaces/terminal_interface_base.py 1 file changed, 8 insertions(+), 11 deletions(-)
Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
diff --git a/pywikibot/userinterfaces/terminal_interface_base.py b/pywikibot/userinterfaces/terminal_interface_base.py index c3afee5..c3cc545 100644 --- a/pywikibot/userinterfaces/terminal_interface_base.py +++ b/pywikibot/userinterfaces/terminal_interface_base.py @@ -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, @@ -53,7 +53,7 @@ ]
colorTagR = re.compile( - '<<((:?{0});?(:?{0})?)>>'.format('|'.join([*colors, 'previous']))) + '<<((?:{0})(?:;(?:{0}))?)>>'.format('|'.join([*colors, 'previous'])))
class UI(ABUIC): @@ -198,16 +198,13 @@ # 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'))
- # 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])): + text_parts.append('default') + + 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
pywikibot-commits@lists.wikimedia.org