0

I have been using the SoX play command line tool to playback audio files from the console, and have noticed that there is a nice little display of the time info and left/right levels that update in real time with the audio source . . .

Screenshot from SoX homepage Screenshot from SoX homepage

However, after cloning the source, I could not find any mention of Ncurses. Does anyone know how SoX achieves this neat little console trick? I just don't know what to look for, or which file to look in to see how this is implemented.

learnvst
  • 15,455
  • 16
  • 74
  • 121
  • It probably just prints the status without a newline, and then sends a bunch of backspace characters (`'\b'`) to move the cursor back to the start of line to overwrite it. – pat Apr 02 '14 at 23:43
  • @pat doh. I guess a carriage return (`\r`) would be equally effective – learnvst Apr 03 '14 at 00:03

1 Answers1

1

I doubt it's merely using the \b character. My guess would be that it's using the terminal cursor movement commands, which are a set of escape sequences supported by many (but not all) Linux terminals.

You can even use these straight from the command line using echo. For instance:

$ s=($(stty size)); echo -en "\e[2J\e[$((s[0]/2));$((s[1]/2-6))HHello world.\e[${s[0]};0H"

This should give you a blank screen, except for the the message "Hello world." printed right in the middle of the screen, and a prompt at the very bottom.

Mike Holt
  • 4,452
  • 1
  • 17
  • 24
  • Interesting. Is there any way to programatically query the console to see if it is capable of cursor movement commands. This so that a fallback method can be called if the console doesn't understand. – learnvst Apr 03 '14 at 01:14
  • Not that I know of. But I haven't found any modern terminal thus far that hasn't supported this. Then again, I do tend to use mainly whatever the default terminal is on a given distro, and I mostly use mainstream distros like RedHat, Debian, Ubuntu, etc. So I can't really say for sure, but I doubt you'll find a lot of terminals that don't support at least the basic cursor positioning escape sequences, unless you're running a really old OS, like maybe some 20 year old version of SunOS or HPuX, etc. that would be my *guess*, anyway. – Mike Holt Apr 03 '14 at 04:53