Skip to content

Instantly share code, notes, and snippets.

@s-hiiragi
Last active May 1, 2023 16:22
Show Gist options
  • Save s-hiiragi/09499284172ca1fa7f3f to your computer and use it in GitHub Desktop.
Save s-hiiragi/09499284172ca1fa7f3f to your computer and use it in GitHub Desktop.
端末のサイズ(列数,行数)を取得
# @name get_termsize.rb
# @desc 端末のサイズ(列数,行数)を取得
=begin
man
http://linuxjm.sourceforge.jp/html/LDP_man-pages/man4/tty_ioctl.4.html
/usr/include/sys/termios.h
/* Extra stuff to make porting stuff easier. */
struct winsize
{
unsigned short ws_row, ws_col;
unsigned short ws_xpixel, ws_ypixel;
};
#define TIOCGWINSZ (('T' << 8) | 1)
[Linux][開発]ioctlについて調べてみた
http://d.hatena.ne.jp/mikenekoDX/20100216/1266474540
=end
TIOCGWINSZ = (0x54 << 8) | 1
def get_termsize()
fd = open('/dev/tty', 'r')
res = [0,0,0,0,0].pack('S!4C') # S!4だけだとオーバーフローが発生する
fd.ioctl(TIOCGWINSZ, res) # return value overflowed string
winsize = res.unpack('S!4C')
return { :w => winsize[1], :h => winsize[0] }
end
puts get_termsize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment