Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hstr 3.0] pressing Esc on system w/o TIOCSTI outputs page full of garbage #485

Open
jakedane opened this issue Apr 17, 2023 · 10 comments
Open

Comments

@jakedane
Copy link

jakedane commented Apr 17, 2023

I just installed hstr 3.0 on my Arch Linux system w/o TIOCSTI and replaced the hstr config in my .bashrc with the output of hstr --show-configuration. Then I opened a new console.

When I invoke hstr with Ctrl+R and then abort it by pressing Esc, instead of getting my shell prompt back immediately, it waits for another key press and then outputs a page full of garbage. It looks to have grabbed some lines from my .bash_history and shortened long lines by putting … at the end, and line endings are replaced with a ^M^[[<nr>d sequence where <nr> is a number starting from 1 and counting up for each next line.

Example of what happens after pressing Esc, to abort hstr;
image

I think there's a problem with this function from hstr --show-configuration output:

function hstrnotiocsti {
    { HSTR_OUT="$( { </dev/tty hstr ${READLINE_LINE}; } 2>&1 1>&3 3>&- )"; } 3>&1;
    READLINE_LINE="$(hstr ${READLINE_LINE})"
    READLINE_POINT=${#READLINE_LINE}
}

If I replace that with the previous code, from hstrcygwin function in #481 (comment), then hstr works as expected and pressing Esc cleanly aborts it:

function hstrnotiocsti {
    offset=${READLINE_POINT}
    READLINE_POINT=0
    { READLINE_LINE=$(</dev/tty hstr ${READLINE_LINE:0:offset} 2>&1 1>&$hstrout); } {hstrout}>&1
    READLINE_POINT=${#READLINE_LINE}
}
@dvorka
Copy link
Owner

dvorka commented Apr 18, 2023

@jakedane great catch! It is reproducible. I wanted to make Bash and Zsh functions as similar as possible (simplification and maintenance)... however, it clearly does not work. I will strive to release the fixed version as soon as possible. Thank you for the bug report and Cygwin function test!

@dvorka
Copy link
Owner

dvorka commented Apr 18, 2023

Fix hint: I unfortunately didn't test Bash version properly and left Zsh version/variable in the Bash function (it didn't work even w/o ESC ... I used a development/debug version of the function) 🤦

v buggy version
{ HSTR_OUT="$( { </dev/tty hstr ${READLINE_LINE}; } 2>&1 1>&3 3>&- )"; } 3>&1;
{ READLINE_LINE="$( { </dev/tty hstr ${READLINE_LINE}; } 2>&1 1>&3 3>&- )"; } 3>&1;
^ fixed version

I can keep simplified function version for both shells, however, the target variable must be fixed.

@jakedane
Copy link
Author

I replaced hstrnotiocsti with this and that seems to work okay!

function hstrnotiocsti {
    { READLINE_LINE="$( { </dev/tty hstr ${READLINE_LINE}; } 2>&1 1>&3 3>&- )"; } 3>&1;
    READLINE_POINT=${#READLINE_LINE}
}

@dvorka
Copy link
Owner

dvorka commented Apr 18, 2023

@jakedane ... actually I think that it might need an initialization in certain cases ... still testing the new version...

EDIT: yes, your function is OK and should be working as expected 👍

@dvorka
Copy link
Owner

dvorka commented Apr 18, 2023

There were actually two 🐞 in the original function 🤦

function hstrnotiocsti {
    { HSTR_OUT="$( { </dev/tty hstr ${READLINE_LINE}; } 2>&1 1>&3 3>&- )"; } 3>&1;    <--- BUG: HSTR_OUT var is wrong
    READLINE_LINE="$(hstr ${READLINE_LINE})"     <--- BUG: this row is not needed at all
    READLINE_POINT=${#READLINE_LINE}
}

@jakedane thank you for your help!

@jakedane
Copy link
Author

I was wondering about that READLINE_LINE="$(hstr ${READLINE_LINE})" line but yes I had to remove it to make hstrnotiocsti function work correctly. Happy to help!

dvorka added a commit that referenced this issue Apr 18, 2023
@dvorka dvorka closed this as completed in 2f5f2dc Apr 18, 2023
@leapfog
Copy link

leapfog commented Apr 20, 2023

3.1 manpage still shows the incorrect bash config

@leapfog
Copy link

leapfog commented Apr 20, 2023

there is also still an issue in hstrnotiocsti, there should be a -- added after hstr:

{ READLINE_LINE="$( { </dev/tty hstr -- ${READLINE_LINE}; } 2>&1 1>&3 3>&- )"; } 3>&1;

otherwise the following wouldn't work:

$ --help [ctrl-r]

@mooreye
Copy link

mooreye commented Apr 25, 2023

I replaced hstrnotiocsti with this and that seems to work okay!

function hstrnotiocsti {
    { READLINE_LINE="$( { </dev/tty hstr ${READLINE_LINE}; } 2>&1 1>&3 3>&- )"; } 3>&1;
    READLINE_POINT=${#READLINE_LINE}
}

This works for me too, however after selecting a command from the finder, it just gets written to command line without being sent, which means I have to press Enter twice.

bind -x '"\C-r": "hstrnotiocsti"' <-- this likely needs to be replaced with something else to fix this

Can you help?

@jakedane
Copy link
Author

jakedane commented Apr 25, 2023

I replaced hstrnotiocsti with this and that seems to work okay!

This works for me too, however after selecting a command from the finder, it just gets written to command line without being sent, which means I have to press Enter twice.

Sorry if my statement put anybody on the wrong footing. I meant the changed hstrnotiocsti worked the same as my workaround from before the 3.1 patch, that Enter doesn't paste & run the command put only pastes it. So Enter needs to be pressed twice (which is actually the way I prefer it, but wasn't intentional in my workaround).

I don't know how/if this can be done differently in bash but if it's important you could (for the time being) enable TIOCSTI with the kernel parameter dev.tty.legacy_tiocsti=1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants