How to get the width of terminal window in Ruby

南笙酒味 提交于 2019-12-20 08:27:39

问题


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 Ruby and Use it.

I'm printing some Ascii-art on the screen and I don't want it to be broken. therefore I need to find out the width of terminal at run time some how.

Any Idea how to do that?


回答1:


I've found that on Ubuntu, none of the other methods specified here (ENV['COLUMNS'], tput columns or hirb) give the correct result if the terminal is resized while the Ruby application is running. This is not an issue for scripts, but it is an issue for interactive consoles, such as irb.

The ruby-terminfo gem is the best solution I've find so far to give the correct dimensions after a resize, but it requires that you install an additional gem, and is unix-specific.

The gem's usage is simple:

require 'terminfo'
p TermInfo.screen_size        # [lines, columns]

TermInfo internally uses TIOCGWINSZ ioctl for the screen size.

Alternatively, as mentioned by user83510, highline's system_extensions also works:

require 'highline'
HighLine::SystemExtensions.terminal_size # [columns, lines]

Interally, highline uses stty size on Unix, and other implementations for ncurses and Windows.

To listen for changes to the terminal size (instead of polling), we can trap the SIGWINCH signal:

require 'terminfo'
Signal.trap('SIGWINCH', proc { puts TermInfo.screen_size.inspect })

This is specifically useful for applications using Readline, such as irb:

Signal.trap('SIGWINCH', proc { Readline.set_screen_size(TermInfo.screen_size[0], TermInfo.screen_size[1]) })



回答2:


There is a common unix command:

tput cols

This returns the width of the terminal.




回答3:


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




回答4:


If you want your code to work across platforms, here's what I use: http://github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb#L61-71

Also check out the system_extensions file in highline




回答5:


I'm a bit late but in rake tasks I do the following: Rake.application.terminal_width




回答6:


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




回答7:


ENV['COLUMNS'] will give you the number of columns in the terminal.




回答8:


    require 'io/console'
    puts "Rows by columns: #{IO.console.winsize}"
    puts "Ruby 2.6.4"



回答9:


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



来源:https://stackoverflow.com/questions/2068859/how-to-get-the-width-of-terminal-window-in-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!