命令行上的python中的tput cup

Ric*_*rdo 4 python printing terminal

是否有一个优雅的解决方案可以在Python中执行此shell脚本而不导入os?

    tput cup 14 15; echo -ne "\033[1;32mtest\033[0m" ; tput cup 50 0
Run Code Online (Sandbox Code Playgroud)

这只是在我心中了一段时间:)

谢谢

Ign*_*ams 6

所有 terminfo 功能都可以通过curses. 初始化它并用于curses.tiget*()获取您关心的功能。


tzo*_*zot 5

鉴于

  • 你假设 ANSI 转义序列
  • tput cup 14 15 | cat -v显示^[[15;16H

整个建议的脚本将生成以下 Python 脚本:

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
Run Code Online (Sandbox Code Playgroud)


Ric*_*rdo 5

感谢Ignacio Vazquez-Abrams的意见,这是朝正确方向的巨大推动。最后,我想到了这个小代码,它将帮助我征服世界:)

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
Run Code Online (Sandbox Code Playgroud)

@ TZ.TZIOY,谢谢,我认为使用stdout而不是使用print确实是更好的解决方案。