如何使用行缓冲提示包装命令?

时间:2016-03-01 23:00:08

标签: bash prompt readline read-eval-print-loop

我正在寻找一个首先通过给定命令生成进程的命令,然后提示用户使用给定的提示字符串输入一行(使用readline功能),将输入的行传递给该流程,然后重复。该过程的任何输出都打印在提示行上方的行上,以防止出现混乱,因此提示始终是屏幕上的最后一行,但该过程可以随时输出内容。

例如,一个提示命令,使prompt -p "> " cat在每行输入之前运行带有提示的cat。它看起来像这样:

$ prompt -p "> " cat
> hello
hello
> every time it's time for me to type, there's a prompt!
every time it's time for me to type, there's a prompt!
> for sure
for sure

也许您也可以指定输出命令的提示,如下所示:

$ prompt -p "[IN] " -o "[OUT] " grep hi
[IN] hello
[IN] this is another example
[OUT] this is another example
[IN] it sure is, i'm glad you know

我找到了rlwrap(https://github.com/hanslub42/rlwrap),它似乎使用readline功能进行行缓冲,但没有输入提示。

基本上,我想要一个命令,可以将对输入流进行操作的任何命令转换为友好的repl。

这个几乎有效,但每当进程输出一些内容时,光标就会以错误的位置结束:

CMD="grep hi" # as an example

prompt () {
    while true
    do
        printf "> \033\067"
        read -e line || break
        echo $line > $1
    done
}

prompt >(stdbuf -oL $CMD |
    stdbuf -oL sed 's/^/< /' |
    stdbuf -oL sed 's/^/'`echo -ne "\033[0;$(expr $(tput lines) - 1)r\033[$(expr $(tput lines) - 1);0H\033E"`'/;s/$/'`echo -ne "\033[0;$(tput lines)r\033\070\033M"`'/')

这是另一个清晰的例子。想象一下一个简单的irc客户端命令,它从stdin读取命令并将简单消息输出到stdout。它没有任何接口甚至是提示,只是简单地读取,并直接从stdin打印到stdout:

$ irc someserver
NOTICE (*): *** Looking up your hostname...
NOTICE (*): *** Found your hostname
001 (madeline): Welcome to someserver IRC!! madeline!madeline@somewhere
(...)
/join #box
JOIN (): #box
353 (madeline = #box): madeline @framboos
366 (madeline #box): End of /NAMES list.
hello!
<madeline> hello!
(5 seconds later)
<framboos> hii

使用prompt命令看起来更像是这样:

$ prompt -p "[IN] " -o "[OUT] " irc someserver
[OUT] NOTICE (*): *** Looking up your hostname...
[OUT] NOTICE (*): *** Found your hostname
[OUT] 001 (madeline): Welcome to someserver IRC!! madeline!madeline@somewhere
(...)
[IN] /join #box
[OUT] JOIN (): #box
[OUT] 353 (madeline = #box): madeline @framboos
[OUT] 366 (madeline #box): End of /NAMES list.
[IN] hello!
[OUT] <madeline> hello!
(5 seconds later)
[OUT] <framboos> hii
[IN] 

关键在于生成单个流程并且您输入的每一行都通过管道传输到同一个流程,它不会为每一行生成新流程。另请注意[IN]提示如何不被来自framboos的消息破坏,而是消息打印在上面行的提示符上。上面提到的rlwrap程序正确地做到了这一点。我所知道的唯一不足的是提示字符串。

2 个答案:

答案 0 :(得分:0)

在您的脚本中,您可以定义以下prompt例程:

#!/bin/bash

prompt() {
    if [[ "$3" = "-o" ]]; then
        symbol1="$2"
        symbol2="$4"
        shift 4
        process="$*"
        while true; do
            printf "%s" "$symbol1"
            read -e line
            output=$( echo "$line" | $process )
            if [[ "$output" != "" ]]; then
                printf "%s" "$symbol2"
                echo "$output"
            fi
        done
    else
        symbol="$2"
        shift 2
        process="$*"
        while true; do
            printf "%s" "$symbol"
            read -e line
            echo "$line" | $process
        done
     fi
}

然后,您可以获取脚本文件以使例程可用:source ./file

用法示例:

测试1

$ prompt -p "> " cat
> hello
hello
> every time it's time for me to type, there's a prompt!
every time it's time for me to type, there's a prompt!
> for sure
for sure 

测试2

$ prompt -p "[IN] " -o "[OUT] " grep hi
[IN] hello
[IN] this is another example
[OUT] this is another example
[IN] it sure is, i'm glad you know

答案 1 :(得分:0)

首先,我对rlwrap错了,你可以用它来提示:

rlwrap -S "> " grep hi

它运作良好。但是如果你在进程打印某些内容时输入了某些东西,它会留下提示的文物。

然后我找到了 socat ,它可以做与上面相同的事情(除此之外),但它不会留下那些文物(通过在你键入时阻止stdout直到你按下输入,行再次清除):

socat READLINE,prompt="[IN] " EXEC:"stdbuf -oL grep hi"
[IN] hello
[IN] this is another example
this is another example
[IN] it sure is, i'm glad you know

然后我可以使用sed向输出添加提示符:

socat READLINE,prompt="[IN] " SYSTEM:"stdbuf -oL grep hi | stdbuf -oL sed \'s/^/[OUT] /\'"
[IN] hello
[IN] this is another example
[OUT] this is another example
[IN] it sure is, i'm glad you know