wez / wezterm

A GPU-accelerated cross-platform terminal emulator and multiplexer written by @wez and implemented in Rust
https://wezfurlong.org/wezterm/
Other
17.99k stars 807 forks source link

Configurable triggers based on terminal output #1759

Open hktonylee opened 2 years ago

hktonylee commented 2 years ago

Is your feature request related to a problem? Please describe. I'm always frustrated when I want to make my customized highlight for the terminal output. In iTerm, I can define regular expression to highlight date or Java namespace. And I can even send notification when the command outputs some specific text.

Describe the solution you'd like Basically, this feature - https://iterm2.com/documentation-triggers.html

Describe alternatives you've considered I think at least we need colorization, and run command.

Additional context Here is some example as well - https://github.com/scottdware/iterm2-triggers

kaddkaka commented 2 years ago

Alternatives could be:

Having a regexp check all text that is printed seems like heavy load? 🤔

hktonylee commented 2 years ago

Not everything can be pipe to other commands :( Let's say, I can't do that when checking log in production servers. Those servers do not allow installing color tool. And the log files can't leave the server too because of compliance / size problem.

If I had this feature, I could read the remote log with color -- This makes a huge difference.

Yes I agree running regexp is super heavy. But this is very convenient. I think users can decide whether to sacrifice the performance for this.

digitallyserviced commented 2 years ago

@hktonylee Just a heads up! You sure you cannot pipe to other commands, you should be able to pipe all the things! Just need to know the syntax. Have you tried to do the following?

# pipe a remote servers log file to logstalgia visualizer
ssh user@server.com 'tail -f /var/log/remoteserverlogfile.log' | /my/local/bin/logstalgia --opts 
# And the other way!
tail -f /var/log/mylocallogfile.log | ssh user@server.com '/usr/bin/remoteserverslogeater'
# even more fun the - (dash) when used after an option to specify a file usually makes it look at stdin
# unzip a tarball to a remote server (-C specifies destination)
tar zxf - somelocaltarball.tar.gz | ssh user@server.com 'tar zxf - -C /remoteserver/destination/'

Piping the data over the remote conn back to your local machine that has the colorizer or log viz or whatever tool that will consume a stdin pipe. That may be more customizable and flexible then having to hack it into the Term. You could write scripts in any language and have it output to (stdout) or consume (stdin) to do whatever you want with output from cmds.

hktonylee commented 2 years ago

@digitallyserviced Ok I lied :) I know I can pipe everything. But the problem is, I need to pipe them before I execute the command.

What if I run a length query like for 30 minutes. And suddenly realize I haven't piped to the colorize command?

Again, because of size and compliance, I can't download the log to my local machine. (Imagine 10000 of 1G logs files). And the production network cannot connect to development network. So I think only your ssh user@server.com 'tail -f /var/log/remoteserverlogfile.log' | /my/local/bin/logstalgia --opts works (of course need grep in the server side. Otherwise the local machine will run out of memory).

Yes I know it is possible to pipe to some colorizer. But I think it is more convenient to let the terminal app to handle. Just like imgcat, I can always pipe the output to some temporary file, and open with local browser. But this is still not comparable to imgcat.


Or, do you think if possible for wezterm to open some callback function / extension? So that people can write custom transformer to outputs :)

wez commented 2 years ago

FWIW, if you're using wezterm ssh to connect to the remote network appliances mentioned in one of the links in the OP, then you can't simply pipe stuff.

I've been thinking about this request and looking at the possible actions in iterm2 and having a hard time seeing how to make those all fit nicely into wezterm, as there are some significant architectural differences and other cases that wezterm needs to support.

What I'm thinking here is to use the existing hyperlink rule stuff as a base to provide a way to manipulate the cell attributes for text that matches regular expressions. Hyperlink rules allow setting an implied link destination, but we could also allow for setting any of the attributes (fg/bg color, bold, italic and so on).

The key difference from iTerm's trigger function is that this treatment would be done at render time, and does not rewrite text at tricky places in the terminal emulator. Doing it at render time has the advantage of allowing you to live-reload your config to adjust the regex's to match the content.

I don't really want to support functions that transform the output beyond manipulating existing cell attributes at render time.

The reason is: while the terminal processes a stream of rendering instructions, those instructions are not guaranteed to be ordered in the same order as the text appears on the screen. The application can eg: output hello starting with the o and generating a series of left cursor movements intermingled with the other letters until it has produced hello in the form that you see on the screen. TUI toolkits may produce crazily ordered output in order to optimize their display repainting operations, so any given run of text may be incomplete and a given line may be updated at any time. Successfully transforming that kind of input stream is rather difficult.

A second reason is: with remote multiplexer connections, the output will already have been accumulated by the remote host and there isn't a place to insert stream processing/transformation.

If you want to perform actual transformation of the content, then you may wish to look at this custom event example which shows how to capture the output from a pane and open it in an editor in a new window: https://wezfurlong.org/wezterm/config/lua/wezterm/on.html#custom-events That technique could be used to send the output through some custom processing/transformation instead of launching an editor.

digitallyserviced commented 2 years ago

@wez I agree, the only thing I think we would be missing after using custom events is the ability to create/render custom overlays using lua.

The overlays functionality you have for debug, search, copy, etc... would be a great way to tap into customizing interfaces or interaction with the terminal.

wez commented 2 years ago

using the right-status but that is quite limited in size, as well as functionality. (doesn't seem to support hyperlinks or standard ansi color codes).

Yes it does! The example shows how to compose escapes using the wezterm.format function, but you can directly use ANSI escape sequences if you wish.

I haven't tried using hyperlinks in the right status area: it should know what they are, but the UI may not let you click them.

digitallyserviced commented 2 years ago

@wez I am quite aware of wezterm.format and "composing escapes" for coloring, but it is quite unwieldy and not DRY. The biggest thing that seems to be missing is the ability to pass a string literally and have it be parsed for osc7, csi, and other escapes.

I should probably share my config since I have added quite a few things that I've tried to make wezterm a more flexible tool

Mainly because the styling is a bit of a PITA having to define each attribute with the text, especially when a long string that may contain multiple groups of style attributes.

config.lua_modules.styles = require('plugins.styles')
wezterm.styles.add_attr_style("name", {{Attribute = {Foreground = {Color = "#fff"}})
table.insert(elements, {Style=wezterm.styles.name, Text = "ballz"}}
table.insert(elements, {Style=wezterm.styles.default, Text = " > "}}
wezterm.format(elements)

I will be playing around and see what may be the most practical and maybe open enhancement issues if it would be something of interest...