How to get the width of terminal window in Ruby

前端 未结 9 1662
你的背包
你的背包 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:15

    Ruby actually comes with a built-in class called "Curses", which lets you do all kinds of things with the terminal window.

    For example, you can do this:

    require 'curses'
    
    Curses.init_screen()
    
    puts Curses.lines # Gives you the height of terminal window
    puts Curses.cols # Gives you the width of terminal window
    

    For more info: http://ruby-doc.org/stdlib-1.9.3/libdoc/curses/rdoc/Curses/Window.html

提交回复
热议问题