Skip to content

Commit 4009a85

Browse files
bpo-38041: Refine IDLE Shell restart lines. (GH-15709)
Restart lines now always start with '=' and never end with ' ' and fill the width of the window unless that would require ending with ' ', which could be wrapped by itself and possible confusing the user. (cherry picked from commit 38da805) Co-authored-by: Terry Jan Reedy <[email protected]>
1 parent 4d1abed commit 4009a85

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

Lib/idlelib/NEWS.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Released on 2019-10-20?
33
======================================
44

55

6+
bpo-38401: Shell restart lines now fill the window width, always start
7+
with '=', and avoid wrapping unnecessarily. The line will still wrap
8+
if the included file name is long relative to the width.
9+
610
bpo-37092: Add mousewheel scrolling for IDLE module, path, and stack
711
browsers. Patch by George Zhang.
812

Lib/idlelib/idle_test/test_pyshell.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@
77
from tkinter import Tk
88

99

10+
class FunctionTest(unittest.TestCase):
11+
# Test stand-alone module level non-gui functions.
12+
13+
def test_restart_line_wide(self):
14+
eq = self.assertEqual
15+
for file, mul, extra in (('', 22, ''), ('finame', 21, '=')):
16+
width = 60
17+
bar = mul * '='
18+
with self.subTest(file=file, bar=bar):
19+
file = file or 'Shell'
20+
line = pyshell.restart_line(width, file)
21+
eq(len(line), width)
22+
eq(line, f"{bar+extra} RESTART: {file} {bar}")
23+
24+
def test_restart_line_narrow(self):
25+
expect, taglen = "= RESTART: Shell", 16
26+
for width in (taglen-1, taglen, taglen+1):
27+
with self.subTest(width=width):
28+
self.assertEqual(pyshell.restart_line(width, ''), expect)
29+
self.assertEqual(pyshell.restart_line(taglen+2, ''), expect+' =')
30+
31+
1032
class PyShellFileListTest(unittest.TestCase):
1133

1234
@classmethod

Lib/idlelib/pyshell.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,19 @@ def handle_EOF(self):
387387
"Override the base class - just re-raise EOFError"
388388
raise EOFError
389389

390+
def restart_line(width, filename): # See bpo-38141.
391+
"""Return width long restart line formatted with filename.
392+
393+
Fill line with balanced '='s, with any extras and at least one at
394+
the beginning. Do not end with a trailing space.
395+
"""
396+
tag = f"= RESTART: {filename or 'Shell'} ="
397+
if width >= len(tag):
398+
div, mod = divmod((width -len(tag)), 2)
399+
return f"{(div+mod)*'='}{tag}{div*'='}"
400+
else:
401+
return tag[:-2] # Remove ' ='.
402+
390403

391404
class ModifiedInterpreter(InteractiveInterpreter):
392405

@@ -491,9 +504,8 @@ def restart_subprocess(self, with_cwd=False, filename=''):
491504
console.stop_readline()
492505
# annotate restart in shell window and mark it
493506
console.text.delete("iomark", "end-1c")
494-
tag = 'RESTART: ' + (filename if filename else 'Shell')
495-
halfbar = ((int(console.width) -len(tag) - 4) // 2) * '='
496-
console.write("\n{0} {1} {0}".format(halfbar, tag))
507+
console.write('\n')
508+
console.write(restart_line(console.width, filename))
497509
console.text.mark_set("restart", "end-1c")
498510
console.text.mark_gravity("restart", "left")
499511
if not filename:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Shell restart lines now fill the window width, always start with '=',
2+
and avoid wrapping unnecessarily. The line will still wrap if the
3+
included file name is long relative to the width.

0 commit comments

Comments
 (0)