Skip to content

Instantly share code, notes, and snippets.

@alexmchale
Forked from nixpulvis/term_size.rb
Last active November 3, 2015 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexmchale/352f1e3fa965e06c88e0 to your computer and use it in GitHub Desktop.
Save alexmchale/352f1e3fa965e06c88e0 to your computer and use it in GitHub Desktop.
Get the terminal window size. Not in a bullshit ENV dependent manor.
# From the tty_ioctl man page in Linux.
#
# TIOCGWINSZ struct winsize *argp
# Get window size.
#
# TIOCSWINSZ const struct winsize *argp
# Set window size.
#
# The struct used by these ioctls is defined as
#
# struct winsize {
# unsigned short ws_row;
# unsigned short ws_col;
# unsigned short ws_xpixel; /* unused */
# unsigned short ws_ypixel; /* unused */
# };
# rows cols width height
window = [ 0, 0, 0, 0 ].pack('SSSS')
fd = IO.sysopen("/dev/tty", "w")
terminal = IO.new(fd, "w")
terminal.ioctl(0x5413, window) # defined in asm-generic/ioctls.h
rows, cols, width, height = window.unpack('SSSS')
puts "Rows: #{rows}, Cols: #{cols}, Width: #{width}, Height: #{height}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment