Last active
May 1, 2023 16:22
-
-
Save s-hiiragi/09499284172ca1fa7f3f to your computer and use it in GitHub Desktop.
端末のサイズ(列数,行数)を取得
This file contains 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
# @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