本文整理汇总了Python中blessed.Terminal.get_location方法的典型用法代码示例。如果您正苦于以下问题:Python Terminal.get_location方法的具体用法?Python Terminal.get_location怎么用?Python Terminal.get_location使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类blessed.Terminal
的用法示例。
在下文中一共展示了Terminal.get_location方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from blessed import Terminal [as 别名]
# 或者: from blessed.Terminal import get_location [as 别名]
def main():
"""Program entry point."""
# pylint: disable=invalid-name
# Invalid variable name "Position"
Position = collections.namedtuple('Position', ('row', 'column'))
# particularly strange, we use sys.stderr as our output stream device,
# this 'stream' file descriptor is only used for side effects, of which
# this application uses two: the term.location() has an implied write,
# as does get_position().
#
# the reason we chose stderr is to ensure that the terminal emulator
# receives our bytes even when this program is wrapped by shell eval
# `resize.py`; backticks gather stdout but not stderr in this case.
term = Terminal(stream=sys.stderr)
# Move the cursor to the farthest lower-right hand corner that is
# reasonable. Due to word size limitations in older protocols, 999,999
# is our most reasonable and portable edge boundary. Telnet NAWS is just
# two unsigned shorts: ('!HH' in python struct module format).
with term.location(999, 999):
# We're not likely at (999, 999), but a well behaved terminal emulator
# will do its best to accommodate our request, positioning the cursor
# to the farthest lower-right corner. By requesting the current
# position, we may negotiate about the window size directly with the
# terminal emulator connected at the distant end.
pos = Position(*term.get_location(timeout=5.0))
if -1 not in pos:
# true size was determined
lines, columns = pos.row, pos.column
else:
# size could not be determined. Oh well, the built-in blessed
# properties will use termios if available, falling back to
# existing environment values if it has to.
lines, columns = term.height, term.width
print("COLUMNS={columns};\nLINES={lines};\nexport COLUMNS LINES;"
.format(columns=columns, lines=lines))
示例2: RokuCLI
# 需要导入模块: from blessed import Terminal [as 别名]
# 或者: from blessed.Terminal import get_location [as 别名]
class RokuCLI():
""" Command-line interpreter for processing user input and relaying
commands to Roku """
def __init__(self):
self.term = Terminal()
self.roku = None
def parseargs(self):
parser = argparse.ArgumentParser(
description='Interactive command-line control of Roku devices')
parser.add_argument(
'ipaddr',
nargs='?',
help=('IP address of Roku to connect to. By default, will ' +
'automatically detect Roku within LAN.'))
return parser.parse_args()
def text_entry(self):
""" Relay literal text entry from user to Roku until
<Enter> or <Esc> pressed. """
allowed_sequences = set(['KEY_ENTER', 'KEY_ESCAPE', 'KEY_DELETE'])
sys.stdout.write('Enter text (<Esc> to abort) : ')
sys.stdout.flush()
# Track start column to ensure user doesn't backspace too far
start_column = self.term.get_location()[1]
cur_column = start_column
with self.term.cbreak():
val = ''
while val != 'KEY_ENTER' and val != 'KEY_ESCAPE':
val = self.term.inkey()
if not val:
continue
elif val.is_sequence:
val = val.name
if val not in allowed_sequences:
continue
if val == 'KEY_ENTER':
self.roku.enter()
elif val == 'KEY_ESCAPE':
pass
elif val == 'KEY_DELETE':
self.roku.backspace()
if cur_column > start_column:
sys.stdout.write(u'\b \b')
cur_column -= 1
else:
self.roku.literal(val)
sys.stdout.write(val)
cur_column += 1
sys.stdout.flush()
# Clear to beginning of line
sys.stdout.write(self.term.clear_bol)
sys.stdout.write(self.term.move(self.term.height, 0))
sys.stdout.flush()
def run(self):
ipaddr = self.parseargs().ipaddr
# If IP not specified, use Roku discovery and let user choose
if ipaddr:
self.roku = Roku(ipaddr)
else:
self.roku = discover_roku()
if not self.roku:
return
print(usage_menu)
cmd_func_map = {
'B': self.roku.back,
'KEY_DELETE': self.roku.back,
'H': self.roku.home,
'h': self.roku.left,
'KEY_LEFT': self.roku.left,
'j': self.roku.down,
'KEY_DOWN': self.roku.down,
'k': self.roku.up,
'KEY_UP': self.roku.up,
'l': self.roku.right,
'KEY_RIGHT': self.roku.right,
'KEY_ENTER': self.roku.select,
'R': self.roku.replay,
'i': self.roku.info,
'r': self.roku.reverse,
'f': self.roku.forward,
' ': self.roku.play,
'/': self.text_entry}
# Main interactive loop
with self.term.cbreak():
val = ''
while val.lower() != 'q':
val = self.term.inkey()
#.........这里部分代码省略.........