Skip to content

Instantly share code, notes, and snippets.

@KINGSABRI
Last active March 17, 2023 05:14
Show Gist options
  • Save KINGSABRI/4687864 to your computer and use it in GitHub Desktop.
Save KINGSABRI/4687864 to your computer and use it in GitHub Desktop.
Getting Terminal size by ruby
# Here are many ways to get terminal size from ruby
# By Readline
require 'readline'
Readline.get_screen_size #=> [31, 268]
# Get terminal size in Environemtn like IRB
[ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
# or
# Get terminal size by line command line "stty"
`stty size`.scan(/\d+/).map { |s| s.to_i }.reverse
# or
# Get the current width of the terminal
#
# @return [Integer] number of columns
def terminal_width
guess = `tput cols`.to_i
guess == 0 ? 80 : guess
end
# Get the current height of the terminal
#
# @return [Integer] number of lines
def terminal_height
guess = `tput lines`.to_i
guess == 0 ? 25 : guess
end
@tonytonyjan
Copy link

ruby -rreline -e 'p Reline.get_screen_size'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment