How to get the width of terminal window in Ruby

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

    def winsize
     #Ruby 1.9.3 added 'io/console' to the standard library.
     require 'io/console'
     IO.console.winsize
     rescue LoadError
     # This works with older Ruby, but only with systems
     # that have a tput(1) command, such as Unix clones.
    [Integer(`tput li`), Integer(`tput co`)]
    end
    
    rows, cols = winsize
    printf "%d rows by %d columns\n", rows, cols
    

    Link

提交回复
热议问题