python获取文本光标_使用python readline时如何获取(并设置)当前bash光标位置?

我可以建议

Python curses吗?

The curses module provides an interface to the curses library, the de-facto standard for portable advanced terminal handling.

While curses is most widely used in the Unix environment, versions are available for DOS, OS/2, and possibly other systems as well. This extension module is designed to match the API of ncurses, an open-source curses library hosted on Linux and the BSD variants of Unix.

另外

这是一个用法示例,显示进度条:

class ProgressBar:

"""

A 3-line progress bar, which looks like::

Header

20% [===========----------------------------------]

progress message

The progress bar is colored, if the terminal supports color

output; and adjusts to the width of the terminal.

"""

BAR = '%3d%% ${GREEN}[${BOLD}%s%s${NORMAL}${GREEN}]${NORMAL}\n'

HEADER = '${BOLD}${CYAN}%s${NORMAL}\n\n'

def __init__(self, term, header):

self.term = term

if not (self.term.CLEAR_EOL and self.term.UP and self.term.BOL):

raise ValueError("Terminal isn't capable enough -- you "

"should use a simpler progress dispaly.")

self.width = self.term.COLS or 75

self.bar = term.render(self.BAR)

self.header = self.term.render(self.HEADER % header.center(self.width))

self.cleared = 1 #: true if we haven't drawn the bar yet.

self.update(0, '')

def update(self, percent, message):

if self.cleared:

sys.stdout.write(self.header)

self.cleared = 0

n = int((self.width-10)*percent)

sys.stdout.write(

self.term.BOL + self.term.UP + self.term.CLEAR_EOL +

(self.bar % (100*percent, '='*n, '-'*(self.width-10-n))) +

self.term.CLEAR_EOL + message.center(self.width))

def clear(self):

if not self.cleared:

sys.stdout.write(self.term.BOL + self.term.CLEAR_EOL +

self.term.UP + self.term.CLEAR_EOL +

self.term.UP + self.term.CLEAR_EOL)

self.cleared = 1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值