Skip to content

Commit 18106f8

Browse files
committed
find: fix bolding when replacing inside a previous replacement
Perform the replacement with an unformatted version of the substitute string, then create a display version with bolding added only after the matching line (if any) is found. Updated the corresponding test case. Also renamed some local vars in the `find` plugin file to make a bit more sense (but not too dramatic).
1 parent 04a5b43 commit 18106f8

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

sopel/builtins/find.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def findandreplace(bot, trigger):
170170

171171
# only clean/format the new string if it's non-empty
172172
if new:
173-
new = bold(escape_sequence_pattern.sub(decode_escape, new))
173+
new = escape_sequence_pattern.sub(decode_escape, new)
174174

175175
# If g flag is given, replace all. Otherwise, replace once.
176176
if 'g' in flags:
@@ -183,42 +183,43 @@ def findandreplace(bot, trigger):
183183
if 'i' in flags:
184184
regex = re.compile(re.escape(old), re.U | re.I)
185185

186-
def repl(s):
187-
return re.sub(regex, new, s, count == 1)
186+
def repl(line, subst):
187+
return re.sub(regex, subst, line, count == 1)
188188
else:
189-
def repl(s):
190-
return s.replace(old, new, count)
189+
def repl(line, subst):
190+
return line.replace(old, subst, count)
191191

192192
# Look back through the user's lines in the channel until you find a line
193193
# where the replacement works
194-
new_phrase = None
194+
new_line = new_display = None
195195
for line in history:
196196
if line.startswith("\x01ACTION"):
197197
me = True # /me command
198198
line = line[8:]
199199
else:
200200
me = False
201-
replaced = repl(line)
201+
replaced = repl(line, new)
202202
if replaced != line: # we are done
203-
new_phrase = replaced
203+
new_line = replaced
204+
new_display = repl(line, bold(new))
204205
break
205206

206-
if not new_phrase:
207+
if not new_line:
207208
return # Didn't find anything
208209

209210
# Save the new "edited" message.
210211
action = (me and '\x01ACTION ') or '' # If /me message, prepend \x01ACTION
211-
history.appendleft(action + new_phrase) # history is in most-recent-first order
212+
history.appendleft(action + new_line) # history is in most-recent-first order
212213

213214
# output
214215
if not me:
215-
new_phrase = 'meant to say: %s' % new_phrase
216+
new_display = 'meant to say: %s' % new_display
216217
if trigger.group(1):
217-
phrase = '%s thinks %s %s' % (trigger.nick, rnick, new_phrase)
218+
msg = '%s thinks %s %s' % (trigger.nick, rnick, new_display)
218219
else:
219-
phrase = '%s %s' % (trigger.nick, new_phrase)
220+
msg = '%s %s' % (trigger.nick, new_display)
220221

221-
bot.say(phrase)
222+
bot.say(msg)
222223

223224

224225
def decode_escape(match):

test/builtins/test_builtins_find.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ def test_replace_the_replacement(bot, irc, user, channel):
102102
channel, user.nick, bold('eggs'),
103103
),
104104
"PRIVMSG %s :%s meant to say: %s" % (
105-
channel, user.nick, bold(bold('bacon')),
106-
# the test is accurate, even though the behavior here (doubled bold
107-
# control characters) is less than ideal
105+
channel, user.nick, bold('bacon'),
108106
),
109107
)

0 commit comments

Comments
 (0)