Skip to content

Instantly share code, notes, and snippets.

@Kwpolska
Created September 12, 2012 18:51
Show Gist options
  • Save Kwpolska/3709048 to your computer and use it in GitHub Desktop.
Save Kwpolska/3709048 to your computer and use it in GitHub Desktop.
\b and \r magic
#!/usr/bin/python3
print('Hello world #0', end='')
for i in range(1, 50):
print(len(str(i-1)) * '\b' + str(i))
print()
###
print('Hello world #0', end='')
for i in range(49, 100):
print('\rHello world #' + str(i))
print()
#!/usr/bin/python3
print('Hello world #0', end='')
for i in range(1, 50):
print(len(str(i-1)) * '\b' + str(i), end='')
print()
###
print('Hello world #0', end='')
for i in range(49, 100):
print('\rHello world #' + str(i), end='')
print()

Hello. Prompted by fosskers/aura#10 <fosskers/aura#10>, I did a quick analysis of behavior of \b and \r in various terminals.

Long story short, @fosskers <https://github.com/fosskers>, author of aura, an AUR helper (and don’t forget I am a developer of one, too!) is using \b and \r in his code for doing magic like displaying progress messages in a row. Aura is written in Haskell, but I do not speak Haskell, so I used Python.

Other info

Expected output

Hello world #49
Hello world #99

Files

(Python 3, py2k users should replace , end='' by ``, `` — note the trailing space)

  • DESCRIPTION.rst — this very file.
  • btest-nonl.py — works, no new lines printed while writing “Hello world #..?”
  • btest-nl.py — fails, new lines printed while writing “Hello world #..?”
  • testresults.rst — a list of all results.
  • urxvt-weird-nl.png — an image of urxvt’s weird behavior.

Testing by Kwpolska, 2012-09-12.

Test results

urxvt means just that. Rest of the world is:

  • mate-terminal (I guess GNOME 3 terminal would work the same)
  • konsole
  • Xfce terminal
  • xterm
  • roxterm
  • guake
  • sakura

(That’s all I tested, there are more emulators, but the top 3 are the most important.)

Expected output

Hello world #49
Hello world #99

Exactly that.

Contestant nonl nl
urxvt PASS [FAIL2]_
Rest of the world PASS [FAIL]_

[FAIL]_

Hello world #0
1
[...]
49

Hello world #0
Hello world #50
[...]
Hello world #99

(an extra newline at the end!)

[FAIL2]_ :: see the urxvt-weird-nl.png file.

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