06 - determining terminal size.rb

来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 57 行

RB
57
字号
Curses.program do |scr|  max_y, max_x = scr.maxy, scr.maxx  scr.setpos(0, 0)  scr.addstr("Your terminal size is #{max_x}x#{max_y}. Press any key to exit.")  scr.getchend#---Ncurses.program do |scr|  max_y, max_x = [], []  scr.getmaxyx(max_y, max_x)  max_y, max_x = max_y[0], max_x[0]  str = "Your terminal size is #{max_x}x#{max_y}. Press any key to exit."  scr.mvaddstr(0, 0, str)  scr.getchend#---TIOCGWINSZ = 0x5413                 # For an Intel processor# TIOCGWINSZ = 0x40087468           # For a PowerPC processordef terminal_size   rows, cols = 25, 80  buf = [ 0, 0, 0, 0 ].pack("SSSS")  if STDOUT.ioctl(TIOCGWINSZ, buf) >= 0 then    rows, cols, row_pixels, col_pixels = buf.unpack("SSSS")[0..1]  end  return rows, colsendterminal_size                            # => [21, 80]#---STDOUT_HANDLE = 0xFFFFFFF5def terminal_size  m_GetStdHandle = Win32API.new('kernel32', 'GetStdHandle', ['L'], 'L')  m_GetConsoleScreenBufferInfo = Win32API.new ('kernel32',                                                'GetConsoleScreenBufferInfo',                                                ['L', 'P'], 'L' )  format = 'SSSSSssssSS'  buf = ([0] * format.size).pack(format)  stdout_handle = m_GetStdHandle.call(STDOUT_HANDLE)  m_GetConsoleScreenBufferInfo.call(stdout_handle, buf)  (bufx, bufy, curx, cury, wattr,    left, top, right, bottom, maxx, maxy) = buf.unpack(format)  return bottom - top + 1, right - left + 1endterminal_size                          # => [25, 80]#---def terminal_size  %x{stty size}.split.collect { |x| x.to_i }endterminal_size                          # => [21, 80]#---

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?