UNIX 命令“watch”的颜色?

发布于 2024-09-24 20:46:19 字数 1817 浏览 5 评论 0 原文

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

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

发布评论

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

评论(8

灵芸 2024-10-01 20:46:19

一些较新版本的 watch 现在支持颜色。

例如watch --color ls -ahl --color

相关

如果您在期望彩色时看到单色输出,则可能是您在 watch 中运行的命令检测到它未连接到终端,您需要强制彩色输出。一些示例:

CLICOLOR_FORCE=1 watch --color ls -ahl    # MacOS
watch --color ls -ahl --color=always      # Ubuntu

对于 ls 和其他程序,您可能需要进行其他配置,但即使您不使用 watch,这些配置也会显示出来。

Some newer versions of watch now support color.

For example watch --color ls -ahl --color.

Related.

If you're seeing monochrome output when you expect color, it's likely that the command you're running within watch is detecting that it's not attached to the terminal and you'll need to force color output. Some examples:

CLICOLOR_FORCE=1 watch --color ls -ahl    # MacOS
watch --color ls -ahl --color=always      # Ubuntu

For ls and other programs you may need to do other configuration, but that will show up even if you're not using watch.

我爱人 2024-10-01 20:46:19

不要使用 watch ...当您使用 watch 程序时,可以检测到它们没有写入终端,然后剥离颜色。您必须使用特定的程序标志来将控制代码保留在那里。

如果您不知道标志或没有标志,您可以通过以下方式让穷人观看:

while sleep <time>; do clear; <command>; done

它会有一点闪烁(手表工作“双缓冲”),但对于某些东西来说它足够有用。

您可能会想使用以下命令制作双缓冲穷人手表,

while sleep <time>; do <command> > /tmp/file; clear; cat /tmp/file; done

但随后您会再次遇到“我没有写入终端”功能。

Do not use watch ... When you use watch programs can detect they're not writing to a terminal and then strip the color. You must use specific program flags to keep the control codes there.

If you don't know the flags or there isn't you can make a poor's man watch by:

while sleep <time>; do clear; <command>; done

It will have a bit of flicker (watch works "double buffered") but for some stuff it is useful enough.

You may be tempted to make a double buffered poor man's watch using

while sleep <time>; do <command> > /tmp/file; clear; cat /tmp/file; done

But then you'll hit again the "I am not writing to a terminal" feature.

少女净妖师 2024-10-01 20:46:19

虽然其他答案可以解决此问题,但实现此问题的最简单方法是使用 unbuffer 工具。要使用它,只需执行以下操作:

$ watch --color 'unbuffer <your-program>'

这样您就不必寻找程序的控制序列启用标志。但需要注意的是,您的手表版本应该支持 --color 标志。

您可以使用 sudo apt-get install Expect 在 Debian 或 Ubuntu 上安装 unbuffer。

While other answers solve this problem, the easiest way to accomplish this is using the unbuffer tool. To use it simply do:

$ watch --color 'unbuffer <your-program>'

This way you don't have to hunt for control sequence enabling flags of your program. The caveat however is that your version of watch should support the --color flag.

You can install unbuffer on Debian or Ubuntu using sudo apt-get install expect.

毁梦 2024-10-01 20:46:19

您可以在几行 shell 脚本中复制 watch 的基本、简单的操作。

$ cat cheapwatch 
#!/bin/sh

# Not quite your Rolex

while true ; do
  clear
  printf "[%s] Output of %s:\n" "$(date)" "$*"
  # "$@" <- we don't want to do it this way, just this:
  ${SHELL-/bin/sh} -c "$*"
  sleep 1  # genuine Quartz movement
done

$ ./cheapwatch ls --color  # no problem

最终,一些非常聪明的人会在这个脚本中加入一个 tr 命令,该命令会删除控制字符,然后强制用户使用 --color 来禁用该逻辑。就目前而言,这种实现方式纯粹是为了让食色怪物远离。

如果您遇到 watch 没有 --color 选项的情况,并且您出于某种原因无法升级软件包,也许您可​​以将其放入。

You can duplicate the fundamental, no-frills operation of watch in a couple lines of shell script.

$ cat cheapwatch 
#!/bin/sh

# Not quite your Rolex

while true ; do
  clear
  printf "[%s] Output of %s:\n" "$(date)" "$*"
  # "$@" <- we don't want to do it this way, just this:
  ${SHELL-/bin/sh} -c "$*"
  sleep 1  # genuine Quartz movement
done

$ ./cheapwatch ls --color  # no problem

Eventually, someone very clever will hack a tr command into this script which strips control characters, and then force the user to use --color to disable that logic. For the time being, the sheer naivete of this implementation is keeping the color-eating monster away.

If you're in a situation where watch doesn't have the --color option and you can't upgrade the package for whatever reason, maybe you can throw this in.

萤火眠眠 2024-10-01 20:46:19

YES

手表支持彩色输出。
它是 procps 包的一部分(至少在 debian 中)
这是针对您的问题的错误报告 http://bugs.debian.org/cgi-bin/bugreport。 cgi?bug=129334
他们回答说,您应该更新 procps 包,

例如在 ubuntu 11.04 中,此软件包可以工作 http://packages.debian.org/wheezy/procps

tl;dr

update procps

YES

watch works with color output.
it is part of the procps package (at least in debian)
here is bugreport for your question http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=129334
where they answer, that you should update the procps package

e.g. with ubuntu 11.04 this package works http://packages.debian.org/wheezy/procps

tl;dr

update procps

债姬 2024-10-01 20:46:19

如果命令没有强制颜色输出的选项,其他答案可能不起作用。或者您可能像我一样很懒,不想浏览每个命令的手册来找到正确的设置。我尝试了几种不同的技术:

script 命令

脚本命令捕获交互式终端会话运行时的输出。结合watch--color参数,保留颜色:

watch --color "script -q -c '<command>' /dev/null"

-q表示安静,-c > 用于命令,/dev/null 是日志文件,这不是必需的,因为 stdout 也显示输出。

编辑:这是迄今为止最好的选择,我为感兴趣的人留下了下面早期的解决方案。

早期尝试:重写终端窗口

正如一些人所建议的,带有 clearsleep 的简单 while 循环可用于在终端中运行命令,而不捕获其输出。这通常会导致闪烁,因为 clear 会删除所有字符,然后该命令需要一些时间才能逐行打印新的输出。

幸运的是,您可以使用 tput 通过一些巧妙的终端技巧来解决此问题。只需让旧的输出可见,同时在上面写入新的输出即可。

这是脚本:

#!/bin/sh
trap "tput cnorm" EXIT  # unhide the cursor when the script exits or is interrupted

# simple interval parameter parsing, can be improved
INTERVAL=2s
case $1 in
  -n|--interval)
    INTERVAL="$2"
    shift; shift
  ;;
esac

clear           # clear the terminal
tput civis      # invisible cursor, prevents cursor flicker

while true; do
  tput cup 0 0  # move cursor to topleft, without clearing the previous output
  sh -c "$*"    # pass all arguments to sh, like the original watch
  tput ed       # clear all to the end of window, if the new output is shorter
  sleep "$INTERVAL"
done

该脚本修复了颜色问题,但仍然存在另一个错误:如果命令输出的行变短,则该行的其余部分不一定会被删除,因此输出可能与现实不匹配!< /强>

Other answers might not work if the command does not have option to force color output. Or you might be lazy like me and do not want to browse manual for each command to find the correct setting. I tried a few different techniques:

The script command

Script command captures output as run by an interactive terminal session. With the combination of --color parameter of watch, it retains colors:

watch --color "script -q -c '<command>' /dev/null"

-q is for quiet, -c is for command and /dev/null is the log file, which is not needed as stdout also shows the output.

Edit: This is the best option so far, I left the earlier solution below for the interested.

Earlier try: Rewrite the terminal window

As suggested by some, a simple while loop with clear and sleep can be used to run the command in terminal without capturing its output. This usually causes flicker, as clear removes all characters and then the command take some time to print the new output line by line.

Fortunately, you can fix this with some clever terminal tricks using tput. Just leave the old output visible while writing the new on top.

Here is the script:

#!/bin/sh
trap "tput cnorm" EXIT  # unhide the cursor when the script exits or is interrupted

# simple interval parameter parsing, can be improved
INTERVAL=2s
case $1 in
  -n|--interval)
    INTERVAL="$2"
    shift; shift
  ;;
esac

clear           # clear the terminal
tput civis      # invisible cursor, prevents cursor flicker

while true; do
  tput cup 0 0  # move cursor to topleft, without clearing the previous output
  sh -c "$*"    # pass all arguments to sh, like the original watch
  tput ed       # clear all to the end of window, if the new output is shorter
  sleep "$INTERVAL"
done

The script fixes color problems, but a different bug remains: If the lines of the command output gets shorter, the rest of the line is not necessarily erased, so the output might not match with reality!

江挽川 2024-10-01 20:46:19

来自手表手册:

非打印字符将从程序输出中去除。如果您想查看它们,请使用“cat -v”作为命令管道的一部分。

不过,我不确定如何使用它。

From watch manual:

Non-printing characters are stripped from program output. Use "cat -v" as part of the command pipeline if you want to see them.

Though, I am not sure how to use it.

享受孤独 2024-10-01 20:46:19

unbuffer is a great way to avoid letting process know if it is writing to TTY, but worth noting that watch does not support 8-bit colors and above yet.

If instead of ls you use something more modern like bat or exa, you should append --theme=ansi (not even --theme=base16 works). git log works out of the box because it always uses 3-bit colors (source).

Example:

watch --color -d -n 0.5 'mycommand file | bat --color=always --theme=ansi'

Could also use -f instead of --color=always.

Another alternative could be Pygments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文