作为一个简单的例子,我想写一个CLI脚本,它可以在终端窗口的整个宽度上打印=。

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

or

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

or

#!/usr/bin/env bash
x=0
while [ $x -lt ??? ]; do echo -n '='; let x=$x+1 done; echo

当前回答

Tput cols表示列数。 Tput行数告诉你行数。

其他回答

yes = | head -n$(($(tput lines) * $COLUMNS)) | tr -d '\n'

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

在某些情况下,您的行/行和列与所使用的“终端”的实际大小不匹配。也许你没有“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"
}

获取窗口宽度

这个shell代码让一个全局变量$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大小,然后再返回到假设终端有25行高和80个字符宽。POSIX并不强制要求stty '的大小操作数,因此需要回退。

然后你可以使用shell有限的字符串替换功能来访问columsn参数:

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从该管道读取数据,并将每个空格替换为连字符。

受到@pixelbeat的回答的启发,这里有一个由tput带来的水平条,稍微滥用printf填充/填充和tr

printf "%0$(tput cols)d" 0|tr '0' '='