nev*_*ame 101 linux bash ubuntu watch
我使用的一些命令显示颜色,但当我使用它们时,颜色会消失:
watch -n 1 node file.js
Run Code Online (Sandbox Code Playgroud)
有可能以某种方式重新获得颜色吗?
Pau*_*ce. 129
一些较新版本watch
现在支持颜色.
例如watch --color ls -ahl --color
.
相关.
the*_*ist 24
不要使用watch
......当您使用手表程序时,可以检测到它们没有写入终端然后剥离颜色.您必须使用特定的程序标志来保存控制代码.
如果你不知道旗帜或没有旗帜,你可以让穷人的手表:
while sleep <time>; do clear; <command>; done
Run Code Online (Sandbox Code Playgroud)
它会有一点闪烁(手表工作"双缓冲"),但对于一些东西,它是足够有用的.
你可能想要使用双缓冲穷人的手表
while sleep <time>; do <command> > /tmp/file; clear; cat /tmp/file; done
Run Code Online (Sandbox Code Playgroud)
但是你会再次点击"我不是写到终端"功能.
Kaz*_*Kaz 16
您可以watch
在几行shell脚本中复制基本的,简单的操作.
$ 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
Run Code Online (Sandbox Code Playgroud)
最终,非常聪明的人会破解一个tr
命令进入这个删除控制字符的脚本,然后强制用户使用它--color
来禁用该逻辑.目前,这种实施的纯粹天真就是让吃颜色的怪物远离.
如果您处于watch
无法--color
选择的情况下,无论出于何种原因您无法升级软件包,也许您可以将其投入使用.
lol*_*lol 13
虽然其他答案解决了这个问题,但最简单的方法是使用该unbuffer
工具。要使用它,只需执行以下操作:
$ watch --color 'unbuffer <your-program>'
Run Code Online (Sandbox Code Playgroud)
这样您就不必寻找控制序列启用程序的标志。但是需要注意的是,您的手表版本应该支持该--color
标志。
您可以在 Debian 或 Ubuntu 上使用sudo apt-get install expect
.
是
手表适用于彩色输出.它是procps包的一部分(至少在debian中)这里是你的问题的bugreport http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=129334 他们回答你应该更新procps包
例如,使用ubuntu 11.04,这个软件包可以运行http://packages.debian.org/wheezy/procps
TL;博士
更新procps
如果命令没有强制颜色输出的选项,其他答案可能不起作用。或者您可能像我一样很懒,不想浏览每个命令的手册来找到正确的设置。我尝试了几种不同的技术:
script
脚本命令捕获交互式终端会话运行时的输出。结合 的--color
参数watch
,保留颜色:
watch --color "script -q -c '<command>' /dev/null"
Run Code Online (Sandbox Code Playgroud)
-q
是安静的,-c
是命令的,/dev/null
是日志文件,这不是必需的,因为 stdout 也显示输出。
编辑:这是迄今为止最好的选择,我为感兴趣的人留下了下面早期的解决方案。
正如一些人所建议的,可以使用带有 和 的简单 while 循环clear
在sleep
终端中运行命令而不捕获其输出。这通常会导致闪烁,因为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
Run Code Online (Sandbox Code Playgroud)
该脚本修复了颜色问题,但仍然存在一个不同的错误:如果命令输出的行变短,则该行的其余部分不一定会被删除,因此输出可能与现实不匹配!
归档时间: |
|
查看次数: |
35285 次 |
最近记录: |