Returns console width >>> any((getConsoleWidth(), True)) True
(default=80)
| 2344 | |
| 2345 | @cachedmethod |
| 2346 | def getConsoleWidth(default=80): |
| 2347 | """ |
| 2348 | Returns console width |
| 2349 | |
| 2350 | >>> any((getConsoleWidth(), True)) |
| 2351 | True |
| 2352 | """ |
| 2353 | |
| 2354 | width = None |
| 2355 | |
| 2356 | if os.getenv("COLUMNS", "").isdigit(): |
| 2357 | width = int(os.getenv("COLUMNS")) |
| 2358 | else: |
| 2359 | try: |
| 2360 | output = shellExec("stty size") |
| 2361 | match = re.search(r"\A\d+ (\d+)", output) |
| 2362 | |
| 2363 | if match: |
| 2364 | width = int(match.group(1)) |
| 2365 | except (OSError, MemoryError): |
| 2366 | pass |
| 2367 | |
| 2368 | if width is None: |
| 2369 | try: |
| 2370 | import curses |
| 2371 | |
| 2372 | stdscr = curses.initscr() |
| 2373 | _, width = stdscr.getmaxyx() |
| 2374 | curses.endwin() |
| 2375 | except: |
| 2376 | pass |
| 2377 | |
| 2378 | return width or default |
| 2379 | |
| 2380 | def shellExec(cmd): |
| 2381 | """ |
no test coverage detected
searching dependent graphs…