pindexis / qfc

Quick Command-line File Completion
MIT License
580 stars 23 forks source link

Bash: read Input/output error in get_cursor_position #30

Closed michaelcontento closed 1 year ago

michaelcontento commented 8 years ago

I've followed the instructions but somehow read in get_cursor_position does fail on my mac. If I hit <ctrl>-f on a fresh console my termial is stuck with the following message:

-bash: read: read error: 0: Input/output error

But if I call get_current_position directly everything seems to work:

$ get_cursor_position
1 0

I've added some set -x marks to the code in bin/qfc.sh and here is some output that might help you:

$ get_cursor_position
+ exec
++ stty -g
+ oldstty=gfmt1:cflag=4b00:iflag=6b02:lflag=200005cf:oflag=3:discard=f:dsusp=19:eof=4:eol=ff:eol2=ff:erase=7f:intr=3:kill=15:lnext=16:min=1:quit=1c:reprint=12:start=11:status=14:stop=13:susp=1a:time=0:werase=ff:ispeed=38400:ospeed=38400
+ stty raw -echo min 0
+ echo -en '\033[6n'
                    + IFS=';'
                             + read -r -d R row col
                                                   + stty gfmt1:cflag=4b00:iflag=6b02:lflag=200005cf:oflag=3:discard=f:dsusp=19:eof=4:eol=ff:eol2=ff:erase=7f:intr=3:kill=15:lnext=16:min=1:quit=1c:reprint=12:start=11:status=14:stop=13:susp=1a:time=0:werase=ff:ispeed=38400:ospeed=38400
                                                                                                               + row=7
+ col=20
+ echo '7 20'
7 20

get_current_position survived, but qfc_complete will get stuck:

$ qfc_complete
++ offset=0
++ READLINE_POINT=0
+++ cut -f 2 -d ' '
++++ get_cursor_position
++++ exec
+++++ stty -g
++++ oldstty=gfmt1:cflag=4b00:iflag=6a02:lflag=4c7:oflag=3:discard=f:dsusp=ff:eof=4:eol=ff:eol2=ff:erase=7f:intr=3:kill=15:lnext=ff:min=1:quit=1c:reprint=12:start=11:status=14:stop=13:susp=1a:time=0:werase=ff:ispeed=38400:ospeed=38400
++++ stty raw -echo min 0
++++ echo -en '\033[6n'
                       ++++ IFS=';'
                                   ++++ read -r -d R row col
                                                            -bash: read: read error: 0: Input/output error
++++ stty gfmt1:cflag=4b00:iflag=6a02:lflag=4c7:oflag=3:discard=f:dsusp=ff:eof=4:eol=ff:eol2=ff:erase=7f:intr=3:kill=15:lnext=ff:min=1:quit=1c:reprint=12:start=11:status=14:stop=13:susp=1a:time=0:werase=ff:ispeed=38400:ospeed=38400

And here some facts about my system:

$ bash --version
GNU bash, version 4.4.0(1)-release (x86_64-apple-darwin16.0.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ python --version
Python 2.7.12
$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.12.1
BuildVersion:   16B2555
michaelcontento commented 8 years ago

Same problem on a different Mac:

$ bash --version
GNU bash, version 4.4.0(1)-release (x86_64-apple-darwin15.6.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ python --version
Python 2.7.12
$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.11.6
BuildVersion:   15G1004

Used terminals:

Integralist commented 7 years ago

Just discovered this myself as well this morning :-(

ProductName:    Mac OS X
ProductVersion: 10.11.6
BuildVersion:   15G1108

Terminal.app 2.6.1 (361.1)

Bash version 4.4.0(1)-release (x86_64-apple-darwin15.6.0)

Python version: 2.7.6

My .bashrc: https://github.com/Integralist/dotfiles/blob/master/.bashrc

Integralist commented 7 years ago

One thing that's interesting is if you try it from inside tmux, it kinda works?

In that you don't get the above error, so you can filter and navigate by arrow, but nothing else actually takes the match and puts it back on the command line for it to be used (so ultimately equally not useful lol) :-)

Don't know if that actually helps identify the source of the problem?

Integralist commented 7 years ago

OK, so I've discovered that tmux was kinda working because it was loading the default system bash, where as once I configured it to use the homebrew install of bash it would error as per the above description @michaelcontento

nougad commented 7 years ago

Its related to bash > 4.4

If you remove the unnecessary subshell in:

col=$(echo $(get_cursor_position) | cut -f 2 -d " ") and row=$(echo $(get_cursor_position) | cut -f 1 -d " ")

with:

--- bin/qfc.sh
+++ bin/qfc.sh
@@ -85,7 +85,7 @@ elif [[ -n "$BASH" ]]; then
         # pretty similar to zsh flow
         offset=${READLINE_POINT}
         READLINE_POINT=0
-        col=$(echo $(get_cursor_position) | cut -f 2 -d " ")
+        col=$(get_cursor_position | cut -f 2 -d " ")

         word=${READLINE_LINE:0:offset}
         word=${word##* }
@@ -100,7 +100,7 @@ elif [[ -n "$BASH" ]]; then
         READLINE_LINE=${READLINE_LINE:0:$((offset-word_length))}${result}${READLINE_LINE:$((offset))}
         offset=$(($offset - $word_length + $result_length))

-        row=$(echo $(get_cursor_position) | cut -f 1 -d " ")
+        row=$(get_cursor_position | cut -f 1 -d " ")
         tput cup $row $col
         READLINE_POINT=${offset}
     }

it works again

Integralist commented 7 years ago

Nice @nougad - fancy making a quick PR? :-)

Naereen commented 7 years ago

@Integralist if @nougad hasn't send a PR yet, maybe you can do it quickly yourself?

Integralist commented 7 years ago

https://github.com/pindexis/qfc/pull/33

Naereen commented 7 years ago

LGTM

christopher-john-czettel commented 6 years ago

Had the same problem #30 on Debian testing/buster:

bash --version
GNU bash, version 4.4.18(1)-release (x86_64-pc-linux-gnu)

Applied fd5d6e9, now everything is working fine. 1up for merge of the PR. Thanks!

michaelcontento commented 1 year ago

I'll close this issue as it's fixed in #33