squeezer.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. """An IDLE extension to avoid having very long texts printed in the shell.
  2. A common problem in IDLE's interactive shell is printing of large amounts of
  3. text into the shell. This makes looking at the previous history difficult.
  4. Worse, this can cause IDLE to become very slow, even to the point of being
  5. completely unusable.
  6. This extension will automatically replace long texts with a small button.
  7. Double-clicking this button will remove it and insert the original text instead.
  8. Middle-clicking will copy the text to the clipboard. Right-clicking will open
  9. the text in a separate viewing window.
  10. Additionally, any output can be manually "squeezed" by the user. This includes
  11. output written to the standard error stream ("stderr"), such as exception
  12. messages and their tracebacks.
  13. """
  14. import re
  15. import tkinter as tk
  16. from tkinter import messagebox
  17. from idlelib.config import idleConf
  18. from idlelib.textview import view_text
  19. from idlelib.tooltip import Hovertip
  20. from idlelib import macosx
  21. def count_lines_with_wrapping(s, linewidth=80):
  22. """Count the number of lines in a given string.
  23. Lines are counted as if the string was wrapped so that lines are never over
  24. linewidth characters long.
  25. Tabs are considered tabwidth characters long.
  26. """
  27. tabwidth = 8 # Currently always true in Shell.
  28. pos = 0
  29. linecount = 1
  30. current_column = 0
  31. for m in re.finditer(r"[\t\n]", s):
  32. # Process the normal chars up to tab or newline.
  33. numchars = m.start() - pos
  34. pos += numchars
  35. current_column += numchars
  36. # Deal with tab or newline.
  37. if s[pos] == '\n':
  38. # Avoid the `current_column == 0` edge-case, and while we're
  39. # at it, don't bother adding 0.
  40. if current_column > linewidth:
  41. # If the current column was exactly linewidth, divmod
  42. # would give (1,0), even though a new line hadn't yet
  43. # been started. The same is true if length is any exact
  44. # multiple of linewidth. Therefore, subtract 1 before
  45. # dividing a non-empty line.
  46. linecount += (current_column - 1) // linewidth
  47. linecount += 1
  48. current_column = 0
  49. else:
  50. assert s[pos] == '\t'
  51. current_column += tabwidth - (current_column % tabwidth)
  52. # If a tab passes the end of the line, consider the entire
  53. # tab as being on the next line.
  54. if current_column > linewidth:
  55. linecount += 1
  56. current_column = tabwidth
  57. pos += 1 # After the tab or newline.
  58. # Process remaining chars (no more tabs or newlines).
  59. current_column += len(s) - pos
  60. # Avoid divmod(-1, linewidth).
  61. if current_column > 0:
  62. linecount += (current_column - 1) // linewidth
  63. else:
  64. # Text ended with newline; don't count an extra line after it.
  65. linecount -= 1
  66. return linecount
  67. class ExpandingButton(tk.Button):
  68. """Class for the "squeezed" text buttons used by Squeezer
  69. These buttons are displayed inside a Tk Text widget in place of text. A
  70. user can then use the button to replace it with the original text, copy
  71. the original text to the clipboard or view the original text in a separate
  72. window.
  73. Each button is tied to a Squeezer instance, and it knows to update the
  74. Squeezer instance when it is expanded (and therefore removed).
  75. """
  76. def __init__(self, s, tags, numoflines, squeezer):
  77. self.s = s
  78. self.tags = tags
  79. self.numoflines = numoflines
  80. self.squeezer = squeezer
  81. self.editwin = editwin = squeezer.editwin
  82. self.text = text = editwin.text
  83. # The base Text widget is needed to change text before iomark.
  84. self.base_text = editwin.per.bottom
  85. line_plurality = "lines" if numoflines != 1 else "line"
  86. button_text = f"Squeezed text ({numoflines} {line_plurality})."
  87. tk.Button.__init__(self, text, text=button_text,
  88. background="#FFFFC0", activebackground="#FFFFE0")
  89. button_tooltip_text = (
  90. "Double-click to expand, right-click for more options."
  91. )
  92. Hovertip(self, button_tooltip_text, hover_delay=80)
  93. self.bind("<Double-Button-1>", self.expand)
  94. if macosx.isAquaTk():
  95. # AquaTk defines <2> as the right button, not <3>.
  96. self.bind("<Button-2>", self.context_menu_event)
  97. else:
  98. self.bind("<Button-3>", self.context_menu_event)
  99. self.selection_handle( # X windows only.
  100. lambda offset, length: s[int(offset):int(offset) + int(length)])
  101. self.is_dangerous = None
  102. self.after_idle(self.set_is_dangerous)
  103. def set_is_dangerous(self):
  104. dangerous_line_len = 50 * self.text.winfo_width()
  105. self.is_dangerous = (
  106. self.numoflines > 1000 or
  107. len(self.s) > 50000 or
  108. any(
  109. len(line_match.group(0)) >= dangerous_line_len
  110. for line_match in re.finditer(r'[^\n]+', self.s)
  111. )
  112. )
  113. def expand(self, event=None):
  114. """expand event handler
  115. This inserts the original text in place of the button in the Text
  116. widget, removes the button and updates the Squeezer instance.
  117. If the original text is dangerously long, i.e. expanding it could
  118. cause a performance degradation, ask the user for confirmation.
  119. """
  120. if self.is_dangerous is None:
  121. self.set_is_dangerous()
  122. if self.is_dangerous:
  123. confirm = messagebox.askokcancel(
  124. title="Expand huge output?",
  125. message="\n\n".join([
  126. "The squeezed output is very long: %d lines, %d chars.",
  127. "Expanding it could make IDLE slow or unresponsive.",
  128. "It is recommended to view or copy the output instead.",
  129. "Really expand?"
  130. ]) % (self.numoflines, len(self.s)),
  131. default=messagebox.CANCEL,
  132. parent=self.text)
  133. if not confirm:
  134. return "break"
  135. self.base_text.insert(self.text.index(self), self.s, self.tags)
  136. self.base_text.delete(self)
  137. self.squeezer.expandingbuttons.remove(self)
  138. def copy(self, event=None):
  139. """copy event handler
  140. Copy the original text to the clipboard.
  141. """
  142. self.clipboard_clear()
  143. self.clipboard_append(self.s)
  144. def view(self, event=None):
  145. """view event handler
  146. View the original text in a separate text viewer window.
  147. """
  148. view_text(self.text, "Squeezed Output Viewer", self.s,
  149. modal=False, wrap='none')
  150. rmenu_specs = (
  151. # Item structure: (label, method_name).
  152. ('copy', 'copy'),
  153. ('view', 'view'),
  154. )
  155. def context_menu_event(self, event):
  156. self.text.mark_set("insert", "@%d,%d" % (event.x, event.y))
  157. rmenu = tk.Menu(self.text, tearoff=0)
  158. for label, method_name in self.rmenu_specs:
  159. rmenu.add_command(label=label, command=getattr(self, method_name))
  160. rmenu.tk_popup(event.x_root, event.y_root)
  161. return "break"
  162. class Squeezer:
  163. """Replace long outputs in the shell with a simple button.
  164. This avoids IDLE's shell slowing down considerably, and even becoming
  165. completely unresponsive, when very long outputs are written.
  166. """
  167. @classmethod
  168. def reload(cls):
  169. """Load class variables from config."""
  170. cls.auto_squeeze_min_lines = idleConf.GetOption(
  171. "main", "PyShell", "auto-squeeze-min-lines",
  172. type="int", default=50,
  173. )
  174. def __init__(self, editwin):
  175. """Initialize settings for Squeezer.
  176. editwin is the shell's Editor window.
  177. self.text is the editor window text widget.
  178. self.base_test is the actual editor window Tk text widget, rather than
  179. EditorWindow's wrapper.
  180. self.expandingbuttons is the list of all buttons representing
  181. "squeezed" output.
  182. """
  183. self.editwin = editwin
  184. self.text = text = editwin.text
  185. # Get the base Text widget of the PyShell object, used to change
  186. # text before the iomark. PyShell deliberately disables changing
  187. # text before the iomark via its 'text' attribute, which is
  188. # actually a wrapper for the actual Text widget. Squeezer,
  189. # however, needs to make such changes.
  190. self.base_text = editwin.per.bottom
  191. # Twice the text widget's border width and internal padding;
  192. # pre-calculated here for the get_line_width() method.
  193. self.window_width_delta = 2 * (
  194. int(text.cget('border')) +
  195. int(text.cget('padx'))
  196. )
  197. self.expandingbuttons = []
  198. # Replace the PyShell instance's write method with a wrapper,
  199. # which inserts an ExpandingButton instead of a long text.
  200. def mywrite(s, tags=(), write=editwin.write):
  201. # Only auto-squeeze text which has just the "stdout" tag.
  202. if tags != "stdout":
  203. return write(s, tags)
  204. # Only auto-squeeze text with at least the minimum
  205. # configured number of lines.
  206. auto_squeeze_min_lines = self.auto_squeeze_min_lines
  207. # First, a very quick check to skip very short texts.
  208. if len(s) < auto_squeeze_min_lines:
  209. return write(s, tags)
  210. # Now the full line-count check.
  211. numoflines = self.count_lines(s)
  212. if numoflines < auto_squeeze_min_lines:
  213. return write(s, tags)
  214. # Create an ExpandingButton instance.
  215. expandingbutton = ExpandingButton(s, tags, numoflines, self)
  216. # Insert the ExpandingButton into the Text widget.
  217. text.mark_gravity("iomark", tk.RIGHT)
  218. text.window_create("iomark", window=expandingbutton,
  219. padx=3, pady=5)
  220. text.see("iomark")
  221. text.update()
  222. text.mark_gravity("iomark", tk.LEFT)
  223. # Add the ExpandingButton to the Squeezer's list.
  224. self.expandingbuttons.append(expandingbutton)
  225. editwin.write = mywrite
  226. def count_lines(self, s):
  227. """Count the number of lines in a given text.
  228. Before calculation, the tab width and line length of the text are
  229. fetched, so that up-to-date values are used.
  230. Lines are counted as if the string was wrapped so that lines are never
  231. over linewidth characters long.
  232. Tabs are considered tabwidth characters long.
  233. """
  234. return count_lines_with_wrapping(s, self.editwin.width)
  235. def squeeze_current_text_event(self, event):
  236. """squeeze-current-text event handler
  237. Squeeze the block of text inside which contains the "insert" cursor.
  238. If the insert cursor is not in a squeezable block of text, give the
  239. user a small warning and do nothing.
  240. """
  241. # Set tag_name to the first valid tag found on the "insert" cursor.
  242. tag_names = self.text.tag_names(tk.INSERT)
  243. for tag_name in ("stdout", "stderr"):
  244. if tag_name in tag_names:
  245. break
  246. else:
  247. # The insert cursor doesn't have a "stdout" or "stderr" tag.
  248. self.text.bell()
  249. return "break"
  250. # Find the range to squeeze.
  251. start, end = self.text.tag_prevrange(tag_name, tk.INSERT + "+1c")
  252. s = self.text.get(start, end)
  253. # If the last char is a newline, remove it from the range.
  254. if len(s) > 0 and s[-1] == '\n':
  255. end = self.text.index("%s-1c" % end)
  256. s = s[:-1]
  257. # Delete the text.
  258. self.base_text.delete(start, end)
  259. # Prepare an ExpandingButton.
  260. numoflines = self.count_lines(s)
  261. expandingbutton = ExpandingButton(s, tag_name, numoflines, self)
  262. # insert the ExpandingButton to the Text
  263. self.text.window_create(start, window=expandingbutton,
  264. padx=3, pady=5)
  265. # Insert the ExpandingButton to the list of ExpandingButtons,
  266. # while keeping the list ordered according to the position of
  267. # the buttons in the Text widget.
  268. i = len(self.expandingbuttons)
  269. while i > 0 and self.text.compare(self.expandingbuttons[i-1],
  270. ">", expandingbutton):
  271. i -= 1
  272. self.expandingbuttons.insert(i, expandingbutton)
  273. return "break"
  274. Squeezer.reload()
  275. if __name__ == "__main__":
  276. from unittest import main
  277. main('idlelib.idle_test.test_squeezer', verbosity=2, exit=False)
  278. # Add htest.