I want to print columnar output from a ruby program, but in order to take full advantage of the available screen space, I need to determine the character width of the terminal the program is running in. How does one do this?
A:
The HighLine gem uses this function to find the dimensions of a terminal:
# A Windows savvy method to fetch the console columns, and rows.
def terminal_size
stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE)
bufx, bufy, curx, cury, wattr, left, top, right, bottom, maxx, maxy =
GetConsoleScreenBufferInfo(stdout_handle)
return right - left + 1, bottom - top + 1
end
This method should work also on Windows, if you use Linux (or something similar) only then you can use stty size
to find the dimensions more easily using something like:
def terminal_size
`stty size`.split.map { |x| x.to_i }.reverse
end
Veger
2010-02-28 20:01:24