-
-
Save Mark24Code/5691c032fc0bc2b3494c427a749dd0b9 to your computer and use it in GitHub Desktop.
Getting Terminal size by ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment