Created
March 23, 2024 11:41
-
-
Save secemp9/7a9b5f87851326b055cb13bfcd1357cf to your computer and use it in GitHub Desktop.
get size of terminal in pixels (curses)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import curses | |
| def get_terminal_size(): | |
| try: | |
| # Initialize curses | |
| stdscr = curses.initscr() | |
| # Turn off echoing of keys, and enter cbreak mode, | |
| # where no buffering is performed on keyboard input | |
| curses.noecho() | |
| curses.cbreak() | |
| # Get the size of the window | |
| height, width = stdscr.getmaxyx() | |
| return f"Width (Columns): {width}, Height (Rows): {height}" | |
| finally: | |
| # Revert the settings | |
| curses.echo() | |
| curses.nocbreak() | |
| # Restore the terminal to its original operating mode | |
| curses.endwin() | |
| # Example usage | |
| print(get_terminal_size()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment