Arrow keys don't work in htop on OS X in Terminal

6

2

Not sure when it happened, or what I did (if anything), but my arrow keys don't work in htop anymore to scroll around. They used to though.

You should be able to press up and down to scroll up and down the process list, but they don't work. Some keys tend to be the equivalent of 'go back' or something. If I'm on the settings screen, left, up and down all go back to the main screen.

htop seems to be the only affected program.

  • TERM is xterm-color
  • less is not affected (can scroll up and down)
  • htop came from homebrew, which uses the htop-osx repo on github, which hasn't changed since mid-2009
  • TERMINFO is empty

My cat -v output

% cat -v
^[[A^[[B^[[C^[[D
^[[A^[[B^[[C^[[D
%
  • Both less and htop use the system (and therefore the same) ncurses
  • Same problem for root user

Output from the sh script

Type <Up> <Down> <Right> <Left> <Control-D> <Control-D>
(no output until after the first <Control-D>, please type "blindly")
    ^[OA^[OB^[OC^[OD
Expected:
    ^[OA^[OB^[OC^[OD

I also tried using iTerm and it gives the same results, so it's not specifically Terminal.app

Episode 6: Return of the Jedi

It was wanting to link against libncurses, but homebrew ncurses compiles with wide char mode, so all the libraries you get are libncursesw. I removed the flags to compile with wide char in the brew, installed the homebrew ncurses, and installed htop from ncurses, and it now works.

Daniel Huckstep

Posted 2010-04-12T17:46:20.230

Reputation: 990

Answers

12

Addendum: Apparently ncurses in 10.6.3 is broken

An answer on SO links to a blog entry that describes some other investigations into the problem. One reported workaround is to copy the ncurses library from 10.6.2, but this may have unintended side effects.


ESC is usually the first character of the “control sequence” sent by a terminal (or terminal emulation program like Terminal) for special keys (like the arrow keys, F1, Home, Page Up, etc.).

ESC also serves as a ‘go back’ key in htop (usually only after a delay, so that slow connections have enough time to send a full control sequence that happens to start with ESC).

Please update your question with the answers to the following questions:

What is TERM?

Maybe your TERM environment variable has an odd value? What does echo $TERM report in a window where htop behaves as you have described? TERM should probably be xterm-color, or something similar.

Where did you get htop? MacPorts? Fink? other?

Apps compiled with the system-supplied ncurses will use entries from /usr/share/terminfo/. MacPorts usually uses /opt/local/ as its prefix, so applications that use ncurses from MacPorts will use entries from /opt/local/share/terminfo/. Fink usually uses /sw/ as its prefix, so applications that use ncurses from Fink will use entries from /sw/share/terminfo/.

Does the TERMINFO environment variable have a value? If so, ncurses (whatever the source) will look in the directory specified therein.

It could be that you have a partially corrupted terminfo definition for the terminal specified in TERM (‘partially corrupted’ because the output control sequences must be OK or you would see obvious display anomalies).

What are some examples of programs that are not affected?

If you have ncurses-based programs that handle the arrow keys OK (maybe using a different installation of ncurses), you might try using the other terminfo entries with htop and vice versa (by setting TERMINFO to the location of the entries from the other ncurses installation).

What codes does your terminal actually send?

Use cat -v to examine which codes are sent when you use the arrow keys. The following is what I see when I press Up, Down, Right, Left, Enter, Control-D after starting cat -v:

% cat -v
^[[A^[[B^[[D^[[C
^[[A^[[B^[[D^[[C
% 

The first set of codes is an echo produced by the tty device. The second set is produced by cat and may be different from the first sent, depending on the exact codes present.

These code can be configured in Terminal, but it is usually not necessary since there are special mode that change the codes (sometimes Up sends ESC [ A, sometimes it sends ESC O A.

Ultimately the problem is that htop is not recognizing whatever codes are being sent as the codes for the arrow keys, so it might be useful to know codes are actually being sent.


Addendum: htop from Homebrew; less is OK

Against which ncurses is htop linked?

It seems that the htop ‘formula’ was changed in late 2009 to not depend on the Homebrew version of ncurses. Presumably this means that versions of htop that were built from this new formula will use the system ncurses. So, which ncurses is your htop binary using?

ls -l "$(which htop)"
otool -L "$(which htop)"

If your htop is older than 2009-12-18 or it is linked against your Homebrew ncurses (probably /usr/local/lib/libncurses.5.dylib), then you might consider rebuilding the htop formula (brew remove htop; brew install htop?). Alternatively, if your existing htop binary is using a Homebrew version of ncurses (or the rebuilt version also still uses the Homebrew ncurses), then you might try rebuilding the ncurses formula.

You mention that less works OK. It seems that there is no Homebrew formula for less, so you are probably using the system-supplied less. It is worth noting which ncurses your less is using for comparison to htop.

otool -L "$(which less)"

Addendum: htop and less use system ncurses

If the problem is being caused by some bit of per-user configuration it should go away if you create a brand new user and try it while logged in as them. If the problem persists under the brand new user you know that it is a system-wide problem (or the problem is in the basic, initial configuration that is given to all new users!). Once you have finished testing, just delete the user to avoid cluttering the login chooser, etc.

Terminal and other VT100-style terminals and terminal emulators have an “Application Cursor Key” mode where the cursor keys send different control sequences to the application. In “application” mode, the terminal sends ^[OA^[OB^[OC^[OD instead of ^[[A^[[B^[[C^[[D (For Up, Down, Right, Left, both respectively). The non-“application” mode sequences that the terminal sends are identical to the codes that the terminal would expect to receive as cursor control codes.

It might be possible that your Terminal is not sending the correct application cursor key codes. This seems unlikely since you say that less works (it also uses “application” mode). Presumably you mean that the arrow keys work to navigate in less. Due to ‘muscle memory’, I almost always use the vi-style keys (kjlh) to navigate in less.

What “application cursor keys” codes is Terminal sending?

You can check that Terminal is sending the expected “application cursor key” sequences with something like this:

sh -c "$(cat <<\EOF
noecho_appmode() {
  stty -echo
  printf '\033[?1h'
}
modes="$(stty -g)"
restore_echo_and_appmode() {
  stty "$modes"
  printf '\033[?1l'
}
printf '\nType <Up> <Down> <Right> <Left> <Control-D> <Control-D>\n'
printf '(no output until after the first <Control-D>, please type "blindly")\n\t'
noecho_appmode             ; trap 'restore_echo_and_appmode' 0
cat -v
restore_echo_and_appmode   ; trap ''                         0
printf '\nExpected:\n\t'
printf 'kcu%c1\n' u d f b | /usr/bin/tput -S | cat -v
printf '\n\n'
EOF
)"

The output labeled “Expected:” is based on the system terminfo entries for your TERM, so it should be what ncurses-based programs expect to see.

If the actual codes differ from the expected codes, then you should investigate the configuration of Terminal to find where the incorrect codes have been configured.

If this does not indicate any problem (the codes from Terminal match the expected codes), I would run a non-setuid copy of htop under ktrace (or maybe a bit of DTrace?) to see the in situ I/O. The nature of the I/O might give someone a clue as the cause of the problem. The full output from kdump will be very large due to reporting the details of loading of shared libraries and supporting data files, but even with that stuff stripped out, I still get nearly 2000 lines and 100kB of output from kdump. This is probably too much to post here.

Chris Johnsen

Posted 2010-04-12T17:46:20.230

Reputation: 31 786

Added info about ncurses library. – Daniel Huckstep – 2010-04-16T15:06:45.413

I'll look into compiling ncurses through brew and try that. – Daniel Huckstep – 2010-04-30T05:17:42.907

I can't seem to make it use the homebrew ncurses no matter what I do. I'm probably missing something simple that's specific to OSX... – Daniel Huckstep – 2010-04-30T18:07:37.070

Okay so actually, ncurses is not in homebrew, it's ncursesw...compiling ncurses on my own now... – Daniel Huckstep – 2010-04-30T18:21:04.510

Oh I see the w is for wide character support... – Daniel Huckstep – 2010-04-30T18:35:56.317

Added some info about the fix. Thank you very much good sir! – Daniel Huckstep – 2010-04-30T19:19:14.247

2

This seems to have been fixed in 10.6.4 which was released on 15 June 2010. I have just updated my iMac which was running 10.6.3 and curses keys (including arrow keys) are responding properly in my apps.

David Parry-Smith

Posted 2010-04-12T17:46:20.230

Reputation: 21

0

If you're unwilling to go through the real steps to actually fix this and just want something working fast, try holding down the option key and then using the arrow keys.

eliasbagley

Posted 2010-04-12T17:46:20.230

Reputation: 101

0

Thanks for the help in trying to debug this. I'm having the same problem and got the library list on a default OSX 10.6 install also done with homebrew like Daniel. Here's the screen dump, looks like the same versions to me:

marvin:~ sheldon$ otool -L "$(which htop)"
/usr/local/bin/htop:
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.1)
marvin:~ sheldon$ otool -L "$(which less)"
/usr/bin/less:
    /usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.1)

user34384

Posted 2010-04-12T17:46:20.230

Reputation:

0

For the record, copying /usr/lib/libncurses.5.4.dylib from a 10.6.5 machine to the 10.6.3 machine solved the problem for me.

(This was done as a quick fix to get a specific ncurses program -- namely CGSecurity's TestDisk -- to work. Of course upgrading to 10.6.5 might be a good idea regardless.)

Ilya

Posted 2010-04-12T17:46:20.230

Reputation: 1 584

0

I have the small Apple bluetooth keyboard that doesn't have Page UP and Page Down keys. So to get these functions to work in htop I use these keystrokes:

Page Up: Shift Key→Function Key→Arrow Up Key
Page Down: Shift Key→Function Key→Arrow Down Key

I've seen this work in other apps that also have this same issue.

lexton

Posted 2010-04-12T17:46:20.230

Reputation: 1