EOL 之前的流程

发布于 2024-12-17 20:38:51 字数 684 浏览 0 评论 0原文

我有一个小脚本,可以对编译器(maven)输出进行着色,这样错误就不会再被忽视。

#!/bin/sh
export TEXT_YELLOW=`tput setaf 3`
export TEXT_RED=`tput setaf 1`
export RESET_FORMATTING=`tput sgr0`

mvn $* | sed \
-e "s/\(\[WARNING\].*\)/${TEXT_YELLOW}\1${RESET_FORMATTING}/g" \
-e "s/\(\[ERROR\].*\)/${TEXT_RED}\1${RESET_FORMATTING}/g"

问题是我的编译器具有 shell 中的 shell 功能(mvn cli:execute-phase)。屏幕上应该会出现像 maven2> 这样的命令提示符,然后用户就可以输入命令。不幸的是,由于我的脚本,我只看到一个闪烁的光标。仅当我按 Enter 键时,才会弹出 maven2>提示前缀文本。

我的猜测是 sed 正在等待 EOL,然后再在屏幕上打印内容。要修复它,我必须查看流是否以字符串 ma​​ven2> 开头。如果是,则直接打印到终端,否则将其转发到sed。在bash中可以吗?

I have a small script that colorizes compiler(maven) output so errors won't be overlooked anymore.

#!/bin/sh
export TEXT_YELLOW=`tput setaf 3`
export TEXT_RED=`tput setaf 1`
export RESET_FORMATTING=`tput sgr0`

mvn $* | sed \
-e "s/\(\[WARNING\].*\)/${TEXT_YELLOW}\1${RESET_FORMATTING}/g" \
-e "s/\(\[ERROR\].*\)/${TEXT_RED}\1${RESET_FORMATTING}/g"

Problem is the shell within a shell feature my compiler has (mvn cli:execute-phase). There a command prompt like maven2> should be visible on screen and the user is then able to type in commands. Unfortunately due to my script I see nothing but a blinking cursor. Only when I press the enter key the maven2> prompt-prefix-text pops up.

My guess is that sed is waiting for an EOL before it prints something on screen. To fix it I would have to look if the stream starts with the string maven2>. If it does then print out directly to terminal, otherwise forward it to sed. Is it possible in bash?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘东忘西忘不掉你 2024-12-24 20:38:51

好吧,我找到了解决方案。我尝试了很多方法,但最终无法纯粹用 bash 解决问题。所以我写了一个 python 脚本,它可以工作。只需将 Maven 输出通过管道传输到脚本,如 mvn cli:execute-phase | colorize.py。也可以在 bashrc 中编写别名,以便所有 Maven 调用都有彩色输出。

#!/usr/bin/python
# Same as regular mvn command but with colored output.

import sys, subprocess, os, time

def colorize( line ):
    red = '\033[1;31m'
    yellow = '\033[1;33m'
    endcolor = '\033[1;m'

    if ("[ERROR]" in line) or ("ERROR" in line ) or ( "Failures" in line ) or ( "Errors" in line ):        
        print red + line + endcolor,    
    elif ("[WARNING]" in line) or ( "WARN" in line ):
        print yellow + line + endcolor,
    else:
        print line,


line=""
printPrompt = True
while True:

    c = sys.stdin.read(1)

    if not c:
        # eof
        break

    line = line + c

    if printPrompt:
        if line.startswith("maven2>"):        
            print "maven2>",
            sys.stdout.flush()
            printPrompt = False
    else:
        # rewrite the current line to console
        sys.stdout.write('\r')
        print line,
        sys.stdout.flush()

    if c.endswith("\n"):
        colorize(line)
        line = ""
        printPrompt = True

Okay, I found a solution. I tried a lot of things but was unable to solve the problem purely with bash in the end. So I wrote a python script instead and it works. Just pipe the maven output to the script like mvn cli:execute-phase | colorize.py. It is also possible to write an alias in your bashrc so all maven calls have colorized output.

#!/usr/bin/python
# Same as regular mvn command but with colored output.

import sys, subprocess, os, time

def colorize( line ):
    red = '\033[1;31m'
    yellow = '\033[1;33m'
    endcolor = '\033[1;m'

    if ("[ERROR]" in line) or ("ERROR" in line ) or ( "Failures" in line ) or ( "Errors" in line ):        
        print red + line + endcolor,    
    elif ("[WARNING]" in line) or ( "WARN" in line ):
        print yellow + line + endcolor,
    else:
        print line,


line=""
printPrompt = True
while True:

    c = sys.stdin.read(1)

    if not c:
        # eof
        break

    line = line + c

    if printPrompt:
        if line.startswith("maven2>"):        
            print "maven2>",
            sys.stdout.flush()
            printPrompt = False
    else:
        # rewrite the current line to console
        sys.stdout.write('\r')
        print line,
        sys.stdout.flush()

    if c.endswith("\n"):
        colorize(line)
        line = ""
        printPrompt = True
旧梦荧光笔 2024-12-24 20:38:51

@mainbrain当显示提示“定义属性‘groupId’的值::”时,你的python脚本工作正常,除了“mvn archetype:generate”。

示例

mvn archetype:generate \
   -DarchetypeRepository=repo1.maven.org \
   -DarchetypeGroupId=org.codehaus.mojo \
   -DarchetypeArtifactId=gwt-maven-plugin \
   -DarchetypeVersion=2.4.0

@mainbrain Your pythons's script works fine except for "mvn archetype:generate" when the prompt "Define value for property 'groupId': : " is showing.

example :

mvn archetype:generate \
   -DarchetypeRepository=repo1.maven.org \
   -DarchetypeGroupId=org.codehaus.mojo \
   -DarchetypeArtifactId=gwt-maven-plugin \
   -DarchetypeVersion=2.4.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文