gnu watch:在终端左下角对齐

发布于 2024-12-27 22:39:32 字数 608 浏览 2 评论 0原文

我想每 N 秒在 mysql 查询上应用一次 watch 命令,但希望将结果放在终端的左下角而不是左上角:

watch -n 120 "mysql_query" | column -t"

显示我的结果,如下所示:

--------------------------
|xxxxxxxxxxx             |
|xxxxxxxxxxx             |
|xxxxxxxxxxx             |
|                        |
|                        |
--------------------------

而我希望它们像这样:

--------------------------
|                        |
|                        |
|xxxxxxxxxxx             |
|xxxxxxxxxxx             |
|xxxxxxxxxxx             |
--------------------------

建议?

I want to apply a watch command on a mysql query every N seconds, but would like to have the results on the bottom left of the terminal instead of the top left:

watch -n 120 "mysql_query" | column -t"

Shows my results like so:

--------------------------
|xxxxxxxxxxx             |
|xxxxxxxxxxx             |
|xxxxxxxxxxx             |
|                        |
|                        |
--------------------------

Whereas I would like them to have like so:

--------------------------
|                        |
|                        |
|xxxxxxxxxxx             |
|xxxxxxxxxxx             |
|xxxxxxxxxxx             |
--------------------------

Suggestion?

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

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

发布评论

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

评论(2

伪心 2025-01-03 22:39:32

我没有看到直接的方法来做到这一点,但我设法使用以下方法强制它工作。我还没有对此进行充分测试,因此我不能保证这在所有情况下都有效。

使用此脚本:

#!/bin/bash
TERM_HEIGHT=`tput lines`  # determine terminal height
WATCH_BANNER_HEIGHT=2  # account for the lines taken up by the header of "watch"
let VIS_LINES="TERM_HEIGHT - WATCH_BANNER_HEIGHT"  # height of visible area
(yes " " | head -n $VIS_LINES; cat | head -n $VIS_LINES) | tail -n $VIS_LINES

watch 调用时对命令的输出进行后处理(假设脚本保存为 align_bottom,使其可执行,并存储在 watch 中的某个位置) code>$PATH):

watch -n 120 "mysql_query | column -t | align_bottom"

脚本的作用:

  1. 确定终端的高度(行数)
  2. 计算 watch 输出的可见区域
  3. 打印空行以填充输出(推送输出向下)
  4. 从标准输入读取输出,并修剪它,这样我们就可以如果输出超出屏幕,则显示输出的顶部。如果您想查看输出的底部,只需删除 cat 之后的 head 命令即可。
  5. tail 步骤 (3) 和 (4) 的输出,因此多余的填充被删除,最终输出紧贴 watch

我不得不承认这看起来有点黑客,但是希望它能让您更接近您想要实现的目标。


更新:

也应该可以将其实现为一个函数,这样它就可以轻松地放在 .bashrc 中。

function align_bottom() {
  (( VIS = $(tput lines) - 2 ))  # height of visible area
  (yes " " | head -n $VIS; cat | head -n $VIS) | tail -n $VIS
}
typeset -fx align_bottom  # !! make it callable from subshell

用法是相同的:

watch -n 120 "mysql_query | column -t | align_bottom"

请注意,watch 使用 sh -c 运行给定的命令,因此,正如丹尼斯在评论中指出的那样,在不链接 的系统上>/bin/sh/bin/bash 上面显示的函数方法将不起作用。

使用sign 可以使其工作:

watch -n 120 "mysql_query | column -t | bash -c align_bottom"

但为了可移植性和可用性,简单地使用shell 脚本方法会更干净。

I don't see a straight-forward way to do this, but I managed to force it to work using the following approach. I haven't fully tested this so I cannot guarantee that this will work in all situations.

Using this script:

#!/bin/bash
TERM_HEIGHT=`tput lines`  # determine terminal height
WATCH_BANNER_HEIGHT=2  # account for the lines taken up by the header of "watch"
let VIS_LINES="TERM_HEIGHT - WATCH_BANNER_HEIGHT"  # height of visible area
(yes " " | head -n $VIS_LINES; cat | head -n $VIS_LINES) | tail -n $VIS_LINES

Post process the output of your command as it is called by watch e.g. (assuming the script was saved as align_bottom, made executable, and store somewhere within your $PATH):

watch -n 120 "mysql_query | column -t | align_bottom"

What the script does:

  1. Determine the height (number of lines) of the terminal
  2. Calculate the visible area of the watch output
  3. Print blank lines to pad the output (pushing the output down)
  4. Read in output from stdin, and trim it so we only show the top of the output if it extends beyond the screen. If you want to see the bottom of the output instead, simple remove the head command after cat.
  5. tail the output of steps (3) and (4) so excess padding is removed and the final output fits snugly within watch

I have to admit this seems a little hackish, but hopefully it gets you closer to what you're trying to achieve.


Update:

It should also be possible to implement that as a function instead just so it can sit comfortably in .bashrc.

function align_bottom() {
  (( VIS = $(tput lines) - 2 ))  # height of visible area
  (yes " " | head -n $VIS; cat | head -n $VIS) | tail -n $VIS
}
typeset -fx align_bottom  # !! make it callable from subshell

Usage would be the same:

watch -n 120 "mysql_query | column -t | align_bottom"

Note that watch runs the given command using sh -c, therefore, as Dennis pointed out in the comments, on systems that does not link /bin/sh to /bin/bash the function approach shown above will not work.

It is possible to make it work usign:

watch -n 120 "mysql_query | column -t | bash -c align_bottom"

but for portability and usability, it's cleaner to simply use the shell script approach.

抹茶夏天i‖ 2025-01-03 22:39:32

我不知道 watch 是否可以做到这一点,但我要做的是使用另一种工具来拥有多个终端,并根据 watch 正在运行的终端调整大小我的需要。

其中一些有用的工具是:

我希望这有帮助。

I don't know if watch can do that, but what I'd do is use another tool to have multiple terminals and resize the one in which watch is running according to my needs.

A couple of these tools that can be useful are:

I hope this helps.

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