更改终端大小执行Ruby

3

在命令行执行程序时,是否有一种方法可以更改终端窗口的大小?我尝试使用IO#winsize =,但似乎没有任何作用。


1
这样做完全不尊重用户设置。如果某个程序认为它更清楚 我的 终端应该有多大,我会非常恼火。 - Sergio Tulentsev
哪个操作系统? - knut
1
@SergioTulentsev 虽然我在一般情况下与您的感受相同,但有些情况下这是可以接受的。也许应用程序正在根据用户的请求更改终端设置。例如,用户按下一个键来切换应用程序的终端大小,从常规大小到“大”(例如半屏幕)大小。也许该应用程序是一个非常互动的应用程序,例如游戏,在其中移动和调整一个或多个终端是游戏的一部分。 - Jonathan Hartley
我刚想起来:Vim本身也属于这个类别:如果你使用:set lines=X命令,它会根据用户的请求调整终端大小。 - Jonathan Hartley
1个回答

1

Xterm 控制序列 用于窗口操作:

CSI Ps ; Ps ; Ps t
          Window manipulation (from dtterm, as well as extensions).
          These controls may be disabled using the allowWindowOps
          resource.  Valid values for the first (and any additional
          parameters) are:
            Ps = 1  -> De-iconify window.
            Ps = 2  -> Iconify window.
            Ps = 3  ; x ; y -> Move window to [x, y].
            Ps = 4  ; height ; width -> Resize the xterm window to
          height and width in pixels.
            Ps = 5  -> Raise the xterm window to the front of the stack-
          ing order.
            Ps = 6  -> Lower the xterm window to the bottom of the
          stacking order.
            Ps = 7  -> Refresh the xterm window.
            Ps = 8  ; height ; width -> Resize the text area to
          [height;width] in characters.
            Ps = 9  ; 0  -> Restore maximized window.
            Ps = 9  ; 1  -> Maximize window (i.e., resize to screen
          size).
            Ps = 1 1  -> Report xterm window state.  If the xterm window
          is open (non-iconified), it returns CSI 1 t .  If the xterm
          window is iconified, it returns CSI 2 t .
            Ps = 1 3  -> Report xterm window position as CSI 3 ; x; yt
            Ps = 1 4  -> Report xterm window in pixels as CSI  4  ;
          height ;  width t
            Ps = 1 8  -> Report the size of the text area in characters
          as CSI  8  ;  height ;  width t
            Ps = 1 9  -> Report the size of the screen in characters as
          CSI  9  ;  height ;  width t
            Ps = 2 0  -> Report xterm window's icon label as OSC  L
          label ST
            Ps = 2 1  -> Report xterm window's title as OSC  l  title ST
            Ps >= 2 4  -> Resize to Ps lines (DECSLPP)

要从Ruby中调用它们,您可以使用:("\e["是CSI的代码)
print "\e[8;40;80t" # resizes terminal window to 40x80 characters

这仅在您的终端支持控制序列时才有效。

我尝试使用这些退出命令,但它们似乎无效。我以前使用转义代码来更改前景和背景颜色,但它们似乎不适用于调整大小。你知道为什么吗? - Daniel
2
OSX Terminal.app支持窗口大小控制序列(通过运行resize -s 40 80进行检查,该命令会发送特定的字符串)。当然,resize是一个实用程序(不是Ruby)。 - Thomas Dickey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接