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.
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.
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.
$ 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
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.
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
#!/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:
-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!
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).
发布评论
评论(8)
一些较新版本的
watch
现在支持颜色。例如
watch --color ls -ahl --color
。相关。
如果您在期望彩色时看到单色输出,则可能是您在
watch
中运行的命令检测到它未连接到终端,您需要强制彩色输出。一些示例:对于
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:For
ls
and other programs you may need to do other configuration, but that will show up even if you're not usingwatch
.不要使用
watch
...当您使用 watch 程序时,可以检测到它们没有写入终端,然后剥离颜色。您必须使用特定的程序标志来将控制代码保留在那里。如果您不知道标志或没有标志,您可以通过以下方式让穷人观看:
它会有一点闪烁(手表工作“双缓冲”),但对于某些东西来说它足够有用。
您可能会想使用以下命令制作双缓冲穷人手表,
但随后您会再次遇到“我没有写入终端”功能。
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:
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
But then you'll hit again the "I am not writing to a terminal" feature.
虽然其他答案可以解决此问题,但实现此问题的最简单方法是使用
unbuffer
工具。要使用它,只需执行以下操作:这样您就不必寻找程序的控制序列启用标志。但需要注意的是,您的手表版本应该支持
--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: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
.您可以在几行 shell 脚本中复制
watch
的基本、简单的操作。最终,一些非常聪明的人会在这个脚本中加入一个
tr
命令,该命令会删除控制字符,然后强制用户使用--color
来禁用该逻辑。就目前而言,这种实现方式纯粹是为了让食色怪物远离。如果您遇到
watch
没有--color
选项的情况,并且您出于某种原因无法升级软件包,也许您可以将其放入。You can duplicate the fundamental, no-frills operation of
watch
in a couple lines of shell script.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.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
如果命令没有强制颜色输出的选项,其他答案可能不起作用。或者您可能像我一样很懒,不想浏览每个命令的手册来找到正确的设置。我尝试了几种不同的技术:
script
命令脚本命令捕获交互式终端会话运行时的输出。结合
watch
的--color
参数,保留颜色:-q
表示安静,-c
> 用于命令,/dev/null
是日志文件,这不是必需的,因为 stdout 也显示输出。编辑:这是迄今为止最好的选择,我为感兴趣的人留下了下面早期的解决方案。
早期尝试:重写终端窗口
正如一些人所建议的,带有
clear
和sleep
的简单 while 循环可用于在终端中运行命令,而不捕获其输出。这通常会导致闪烁,因为clear
会删除所有字符,然后该命令需要一些时间才能逐行打印新的输出。幸运的是,您可以使用
tput
通过一些巧妙的终端技巧来解决此问题。只需让旧的输出可见,同时在上面写入新的输出即可。这是脚本:
该脚本修复了颜色问题,但仍然存在另一个错误:如果命令输出的行变短,则该行的其余部分不一定会被删除,因此输出可能与现实不匹配!< /强>
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
commandScript command captures output as run by an interactive terminal session. With the combination of
--color
parameter ofwatch
, it retains colors:-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
andsleep
can be used to run the command in terminal without capturing its output. This usually causes flicker, asclear
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:
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!
来自手表手册:
不过,我不确定如何使用它。
From watch manual:
Though, I am not sure how to use it.
unbuffer
是避免让进程知道它是否正在写入 TTY 的好方法,但值得注意的是watch
不支持 8 位颜色及以上尚未。如果您使用更现代的东西比如
bat
< /a> 或exa
,您应该附加--theme=ansi
(甚至--theme=base16
也不起作用)。git log
开箱即用,因为它始终使用 3 位颜色 (来源)。示例:
也可以使用
-f
代替--color=always
。另一种选择是 Pygments。
unbuffer
is a great way to avoid letting process know if it is writing to TTY, but worth noting thatwatch
does not support 8-bit colors and above yet.If instead of
ls
you use something more modern likebat
orexa
, 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:
Could also use
-f
instead of--color=always
.Another alternative could be Pygments.