Skip to content

Instantly share code, notes, and snippets.

@secemp9
Created March 23, 2024 11:41
Show Gist options
  • Save secemp9/7a9b5f87851326b055cb13bfcd1357cf to your computer and use it in GitHub Desktop.
Save secemp9/7a9b5f87851326b055cb13bfcd1357cf to your computer and use it in GitHub Desktop.
get size of terminal in pixels (curses)
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