tput cup in python on the commandline
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开发者_Go百科
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.
All the terminfo capabilities are accessible via curses
. Initialize it and use curses.tiget*()
to get the capabilities you care about.
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
精彩评论