Tput cup in python on command line

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

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

It has already nibbled several times in my head :)

thanks

+4
source share
3 answers

All terminfo features are available through curses . Initialize it and use curses.tiget*() to get the features you need.

+3
source

Thanks to Ignacio Vasquez-Abrams for your contribution, 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 instead of using print is really a better solution.

+4
source

Given that

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

all of the proposed script leads to 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 
+3
source

All Articles