Last active
December 28, 2024 06:08
-
-
Save KINGSABRI/4687864 to your computer and use it in GitHub Desktop.
Getting Terminal size by ruby
This file contains 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 |
tonytonyjan
commented
Mar 17, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment