Last active
October 18, 2017 11:50
-
-
Save nixpulvis/6025433 to your computer and use it in GitHub Desktop.
Get the terminal window size. Not in a bullshit ENV dependent manor.
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
# 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") | |
# The int in this call is === TIOCGWINSZ. No idea where it is defined. | |
# I found it using ruby-termios with `Termios::TIOCGWINSZ` | |
terminal.ioctl(1074295912, window) | |
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