How to get the width of terminal window in Ruby

前端 未结 9 1648
你的背包
你的背包 2021-01-30 16:30

Have you ever noticed that if you run rake -T in rails the list of rake descriptions are truncated by the width of the terminal window. So there should be a way to get it in Rub

9条回答
  •  无人共我
    2021-01-30 17:18

    I wrote the tty-screen gem to detect terminal size across different OS systems and Ruby interpreters. It covers many ways of checking the size including Win API on Windows, Java libs on JRuby and Unix utilities.

    This is a module which you can include in your class or call directly:

    require 'tty-screen'
    
    TTY::Screen.size     # => [51, 280]
    TTY::Screen.width    # => 280
    TTY::Screen.height   # => 51
    

    For more info see: https://github.com/piotrmurach/tty-screen

提交回复
热议问题