代码之家  ›  专栏  ›  技术社区  ›  too much php

如何找到终端窗口的宽度和高度?

  •  238
  • too much php  · 技术社区  · 17 年前

    =

    #!/usr/bin/env php
    <?php
    echo str_repeat('=', ???);
    

    或

    #!/usr/bin/env python
    print '=' * ???
    

    或

    #!/usr/bin/env bash
    x=0
    while [ $x -lt ??? ]; do echo -n '='; let x=$x+1 done; echo
    
    9 回复  |  直到 7 年前
        1
  •  651
  •   Flow Matt McDonald    13 年前
    • tput cols 告诉您列数。
    • tput lines 告诉您行数。
        2
  •  120
  •   David Dean    17 年前

    在bash中 $LINES 和 $COLUMNS 环境变量应该能够做到这一点。当终端尺寸发生任何变化时,将自动设置。(即 SIGWINCH (信号)

        3
  •  86
  •   ryenus    4 年前

    还有 stty stty: Print or change terminal characteristics ,更具体地说 Special settings

    $ stty size
    60 120 # <= sample output
    

    它将分别打印行数和列数,或高度和宽度。

    cut 或 awk 提取所需的零件。

    那是 stty size | cut -d" " -f1 stty size | cut -d" " -f2 对于宽度/列

        4
  •  19
  •   pixelbeat    17 年前
    yes = | head -n$(($(tput lines) * $COLUMNS)) | tr -d '\n'
    
        5
  •  12
  •   lyceus    15 年前

    function getTerminalSizeOnWindows() {
      $output = array();
      $size = array('width'=>0,'height'=>0);
      exec('mode',$output);
      foreach($output as $line) {
        $matches = array();
        $w = preg_match('/^\s*columns\:?\s*(\d+)\s*$/i',$line,$matches);
        if($w) {
          $size['width'] = intval($matches[1]);
        } else {
          $h = preg_match('/^\s*lines\:?\s*(\d+)\s*$/i',$line,$matches);
          if($h) {
            $size['height'] = intval($matches[1]);
          }
        }
        if($size['width'] AND $size['height']) {
          break;
        }
      }
      return $size;
    }
    

    我希望它有用!

    :返回的高度是缓冲区中的行数,而不是窗口中可见的行数。还有更好的选择吗?

        6
  •  11
  •   LeoNerd    14 年前

    TIOCGWINSZ (获取窗口大小) ioctl() 呼叫大多数语言都应该有这样的包装器。例如,在Perl中,您可以使用 Term::Size :

    use Term::Size qw( chars );
    
    my ( $columns, $rows ) = chars \*STDOUT;
    
        7
  •  4
  •   Camilo Martin    12 年前

    mode

    mode command output

    您可以在不查找文本的情况下找到正确的子字符串:

     preg_match('/---+(\n[^|]+?){2}(?<cols>\d+)/', `mode`, $matches);
     $cols = $matches['cols'];
    

    编辑:

     preg_match('/CON.*:(\n[^|]+?){3}(?<cols>\d+)/', `mode`, $matches);
     $cols = $matches['cols'];
    

    但一定要测试一下,因为我没有测试。

        8
  •  2
  •   huoneusto    8 年前

    tput ,轻微误用 printf 填充/填充和 tr

    printf "%0$(tput cols)d" 0|tr '0' '='
    
        9
  •  1
  •   Odin Kroeger    5 年前

    获取窗口宽度

    $TERM_SIZE 跟踪终端窗口的大小:

    set_term_size() {
        TERM_SIZE="$(stty size 2>/dev/null)" && [ "$TERM_SIZE" ] ||
            TERM_SIZE='25 80'
    }
    trap set_term_size WINCH
    set_term_size
    

    它尝试 stty size 在回到假设终端为25行高和80个字符宽之前。POSIX有 授权 size stty

    echo "${TERM_SIZE% *}" # Prints the terminal's height.
    echo "${TERM_SIZE#* }" # Prints the terminal's width.
    

    当然,您使用的脚本语言可能会提供一个库来为您解决这个问题,您应该使用它。

    打印一行

    一旦知道了终端的宽度,打印水平线就很容易了,例如,通过滥用 printf 的字符串填充:

    printf '%*s\n' "${TERM_SIZE#* }" '' | 
    tr ' ' -
    

    第一行告诉我们 printf 在管道中打印尽可能多的空格(通过滥用字符串paddin)。注意,POSIX是这样做的 提及 * 语法,因此这可能不像上面的代码那样可移植。

    第二行告诉我们 tr 从那根管子里读,用催眠代替每一个空间。

        10
  •  0
  •   pourhaus    7 年前

    在某些情况下,行/行和列与正在使用的“终端”的实际大小不匹配。可能您没有可用的“tput”或“stty”。

    下面是一个bash函数,您可以使用它直观地检查大小。这将最多使用140列x 80行。您可以根据需要调整最大值。

    function term_size
    {
        local i=0 digits='' tens_fmt='' tens_args=()
        for i in {80..8}
        do
            echo $i $(( i - 2 ))
        done
        echo "If columns below wrap, LINES is first number in highest line above,"
        echo "If truncated, LINES is second number."
        for i in {1..14}
        do
            digits="${digits}1234567890"
            tens_fmt="${tens_fmt}%10d"
            tens_args=("${tens_args[@]}" $i)
        done
        printf "$tens_fmt\n" "${tens_args[@]}"
        echo "$digits"
    }