tput cup in python on the commandline

点点圈 提交于 2019-12-20 01:49:30

问题


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


回答1:


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




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/6199285/tput-cup-in-python-on-the-commandline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!