7

Is there an elegant solution to do this shell script in Python without importing os ?

    tput cup 14 15; echo -ne "\033[1;32mtest\033[0m" ; tput cup 50 0

This just has been gnawing in my mind for some time now :)

Thanks

animuson
  • 53,861
  • 28
  • 137
  • 147
Riccardo B.
  • 354
  • 2
  • 10

3 Answers3

7

Thanks Ignacio Vazquez-Abrams for your input, it was a great push in the right direction. In the end i came up with this little code that will help me conquer the world :)

from curses import *
setupterm()

#cols = tigetnum("cols")
#lines = tigetnum("lines")
#print str(cols) + "x" + str(lines)

place_begin = tparm(tigetstr("cup"), 15, 14)
place_end = tparm(tigetstr("cup"), 50, 0)

print place_begin + "-- some text --" + place_end

@TZ.TZIOY, thanks, i think using stdout rather than using print indeed is a better solution.

Riccardo B.
  • 354
  • 2
  • 10
6

All the terminfo capabilities are accessible via curses. Initialize it and use curses.tiget*() to get the capabilities you care about.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
5

Given that

  • you assume ANSI escape sequences
  • tput cup 14 15 | cat -v displays ^[[15;16H

the whole suggested script results in the following Python script:

import sys
sys.stdout.write("\033[15;16H\033[1;32mtest\033[m\033[51;1H")
# and a possible sys.stdout.flush() here, depending on your needs
tzot
  • 92,761
  • 29
  • 141
  • 204