2010-02-28 23 views
2

我想打印來自ruby程序的列輸出,但爲了充分利用可用的屏幕空間,我需要確定程序運行的終端的字符寬度。如何做到這一點?如何確定從ruby程序控制臺字符寬度?

+0

字符的寬度或字符的終端的寬度。第一個是不可能的,第二個是可行的。 – johannes 2010-03-01 02:22:39

回答

0

HighLine寶石使用該功能查找終端的尺寸:

# 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 

此方法還應該工作在Windows上,如果你使用Linux(或類似的東西),只有這樣,你可以使用stty size找到尺寸更容易使用類似的東西:

def terminal_size 
    `stty size`.split.map { |x| x.to_i }.reverse 
end 
相關問題