Skip to content

Commit 1ac62ef

Browse files
tobiasbrunnerlucasdemarchi
authored andcommitted
Show and return words in the proper case in interactive mode.
1 parent 9964ed2 commit 1ac62ef

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

codespell.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,24 @@ def istextfile(filename):
183183

184184
return True
185185

186+
def fix_case(word, fixword):
187+
if word == word.capitalize():
188+
return fixword.capitalize()
189+
elif word == word.upper():
190+
return fixword.upper()
191+
# they are both lower case
192+
# or we don't have any idea
193+
return fixword
194+
186195
def ask_for_word_fix(line, wrongword, misspelling, interactivity):
187196
if interactivity <= 0:
188-
return misspelling.fix, misspelling.data
197+
return misspelling.fix, fix_case(wrongword, misspelling.data)
189198

190199
if misspelling.fix and interactivity & 1:
191200
r = ''
201+
fixword = fix_case(wrongword, misspelling.data)
192202
while not r:
193-
print("%s\t%s ==> %s (Y/n) " % (line, wrongword,
194-
misspelling.data), end='')
203+
print("%s\t%s ==> %s (Y/n) " % (line, wrongword, fixword), end='')
195204
r = sys.stdin.readline().strip().upper()
196205
if not r: r = 'Y'
197206
if r != 'Y' and r != 'N':
@@ -211,7 +220,8 @@ def ask_for_word_fix(line, wrongword, misspelling, interactivity):
211220
while not r:
212221
print("%s Choose an option (blank for none): " % line, end='')
213222
for i in range(len(opt)):
214-
print(" %d) %s" % (i, opt[i]), end='')
223+
fixword = fix_case(wrongword, opt[i])
224+
print(" %d) %s" % (i, fixword), end='')
215225
print(": ", end='')
216226
sys.stdout.flush()
217227

@@ -229,7 +239,7 @@ def ask_for_word_fix(line, wrongword, misspelling, interactivity):
229239
misspelling.fix = True
230240
misspelling.data = r
231241

232-
return misspelling.fix, misspelling.data
242+
return misspelling.fix, fix_case(wrongword, misspelling.data)
233243

234244
def parse_file(filename, colors, summary):
235245
lines = None
@@ -293,15 +303,7 @@ def parse_file(filename, colors, summary):
293303
lword = word.lower()
294304
if lword in misspellings:
295305
fix = misspellings[lword].fix
296-
297-
if word == word.capitalize():
298-
fixword = misspellings[lword].data.capitalize()
299-
elif word == word.upper():
300-
fixword = misspellings[lword].data.upper()
301-
else:
302-
# even they are the same lower case or
303-
# or we don't have any idea
304-
fixword = misspellings[lword].data
306+
fixword = fix_case(word, misspellings[lword].data)
305307

306308
if options.interactive and not lword in fixed_words:
307309
fix, fixword = ask_for_word_fix(lines[i - 1], word,

0 commit comments

Comments
 (0)