Initialize settings for Squeezer. editwin is the shell's Editor window. self.text is the editor window text widget. self.base_test is the actual editor window Tk text widget, rather than EditorWindow's wrapper. self.expandingbuttons is the list of all but
(self, editwin)
| 212 | ) |
| 213 | |
| 214 | def __init__(self, editwin): |
| 215 | """Initialize settings for Squeezer. |
| 216 | |
| 217 | editwin is the shell's Editor window. |
| 218 | self.text is the editor window text widget. |
| 219 | self.base_test is the actual editor window Tk text widget, rather than |
| 220 | EditorWindow's wrapper. |
| 221 | self.expandingbuttons is the list of all buttons representing |
| 222 | "squeezed" output. |
| 223 | """ |
| 224 | self.editwin = editwin |
| 225 | self.text = text = editwin.text |
| 226 | |
| 227 | # Get the base Text widget of the PyShell object, used to change |
| 228 | # text before the iomark. PyShell deliberately disables changing |
| 229 | # text before the iomark via its 'text' attribute, which is |
| 230 | # actually a wrapper for the actual Text widget. Squeezer, |
| 231 | # however, needs to make such changes. |
| 232 | self.base_text = editwin.per.bottom |
| 233 | |
| 234 | # Twice the text widget's border width and internal padding; |
| 235 | # pre-calculated here for the get_line_width() method. |
| 236 | self.window_width_delta = 2 * ( |
| 237 | int(text.cget('border')) + |
| 238 | int(text.cget('padx')) |
| 239 | ) |
| 240 | |
| 241 | self.expandingbuttons = [] |
| 242 | |
| 243 | # Replace the PyShell instance's write method with a wrapper, |
| 244 | # which inserts an ExpandingButton instead of a long text. |
| 245 | def mywrite(s, tags=(), write=editwin.write): |
| 246 | # Only auto-squeeze text which has just the "stdout" tag. |
| 247 | if tags != "stdout": |
| 248 | return write(s, tags) |
| 249 | |
| 250 | # Only auto-squeeze text with at least the minimum |
| 251 | # configured number of lines. |
| 252 | auto_squeeze_min_lines = self.auto_squeeze_min_lines |
| 253 | # First, a very quick check to skip very short texts. |
| 254 | if len(s) < auto_squeeze_min_lines: |
| 255 | return write(s, tags) |
| 256 | # Now the full line-count check. |
| 257 | numoflines = self.count_lines(s) |
| 258 | if numoflines < auto_squeeze_min_lines: |
| 259 | return write(s, tags) |
| 260 | |
| 261 | # Create an ExpandingButton instance. |
| 262 | expandingbutton = ExpandingButton(s, tags, numoflines, self) |
| 263 | |
| 264 | # Insert the ExpandingButton into the Text widget. |
| 265 | text.mark_gravity("iomark", tk.RIGHT) |
| 266 | text.window_create("iomark", window=expandingbutton, |
| 267 | padx=3, pady=5) |
| 268 | text.see("iomark") |
| 269 | text.update() |
| 270 | text.mark_gravity("iomark", tk.LEFT) |
| 271 |