Tennyson H
Tennyson H

Reputation: 1716

How to monitor bash/zsh prompt input?

I need to view the user's prompt input in a background script. (in both zsh and bash).

For example if the user has typed

[3:30pm@home]> ssh [email protected] ls -alh

My background process needs the string "ssh [email protected] ls -alh" before they press enter.

$PS1 has the prompt string, but not the user input.

Is this possible?

For background, the overall goal is to take user's partial input and translate shell to english. For example, if I were to type ls -al, my $PS1 or $PS2 would change to say "list all files in a long format in the current directory". So, if someone didn't exactly remember if -X or -x enabled X11 forwarding with ssh, they can type it and see if the plain language english translation matches their expectations.

I want it to be a zsh plugin, or a script that people toss into the rc files, so there can't be anything too major done.

Upvotes: 1

Views: 1231

Answers (1)

bew
bew

Reputation: 503

Do you need the input in real time ? or at least just before the line is accepted ?

For the last option, in zsh there is a widget accept-line that is triggered when the input line is accepted. You can override the default widget with you own, like this :

function accept-line
{
    #here, send the input to your script
    yourscript "$BUFFER"

    # ... do something here if you want

    # trigger the real widget to accept the line and run the input
    zle .accept-line
}
# register your new accept-line widget to replace the builtin one
zle -N accept-line

Here I used the name accept-line but if you want to name your override function my_superOverride you can override the default widget at the end with :

zle -N accept-line my_superOverride

Upvotes: 1

Related Questions