本文整理汇总了Python中curses.tparm方法的典型用法代码示例。如果您正苦于以下问题:Python curses.tparm方法的具体用法?Python curses.tparm怎么用?Python curses.tparm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类curses
的用法示例。
在下文中一共展示了curses.tparm方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Render
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def Render(self, target, foreground=None, background=None):
"""Decorate the string with the ansii escapes for the color."""
if (not self.terminal_capable or
foreground not in self.COLOR_MAP or
background not in self.COLOR_MAP):
return utils.SmartUnicode(target)
escape_seq = ""
if background:
escape_seq += utils.SmartUnicode(self.tparm(
["setab", "setb"], self.COLOR_MAP[background]))
if foreground:
escape_seq += utils.SmartUnicode(self.tparm(
["setaf", "setf"], self.COLOR_MAP[foreground]))
return (escape_seq + utils.SmartUnicode(target) +
utils.SmartUnicode(self.tparm(["sgr0"])))
示例2: __missing__
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def __missing__(self, key):
# pylint: disable=unused-argument,no-self-use
if not self.has_colors:
return ''
try:
value = curses.tigetstr(self.attrs[key])
except KeyError:
try:
value = curses.tparm(
curses.tigetstr('setaf'), self.colors.index(key))
except ValueError:
return ''
value = value.decode()
self[key] = value
return value
示例3: __call__
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def __call__(self, *args):
"""P(*args) -> FormattingString()
Return evaluated terminal capability (self), receiving arguments
``*args``, followed by the terminating sequence (self.normal) into
a FormattingString capable of being called.
"""
try:
# Re-encode the cap, because tparm() takes a bytestring in Python
# 3. However, appear to be a plain Unicode string otherwise so
# concats work.
attr = curses.tparm(self.encode('latin1'), *args).decode('latin1')
return FormattingString(attr, self._normal)
except TypeError, err:
# If the first non-int (i.e. incorrect) arg was a string, suggest
# something intelligent:
if len(args) and isinstance(args[0], basestring):
raise TypeError(
"A native or nonexistent capability template, %r received"
" invalid argument %r: %s. You probably misspelled a"
" formatting call like `bright_red'" % (
self._name, args, err))
# Somebody passed a non-string; I don't feel confident
# guessing what they were trying to do.
raise
示例4: __init__
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def __init__(self, use_colors):
logging.Handler.__init__(self)
self.use_colors = use_colors
# Initialize environment
curses.setupterm()
# Get the foreground color attribute for this environment
self.fcap = curses.tigetstr('setaf')
#Get the normal attribute
self.COLOR_NORMAL = curses.tigetstr('sgr0')
# Get + Save the color sequences
self.COLOR_INFO = curses.tparm(self.fcap, curses.COLOR_GREEN)
self.COLOR_ERROR = curses.tparm(self.fcap, curses.COLOR_RED)
self.COLOR_WARNING = curses.tparm(self.fcap, curses.COLOR_YELLOW)
self.COLOR_DEBUG = curses.tparm(self.fcap, curses.COLOR_BLUE)
示例5: __init__
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def __init__(self, color=True, fmt=DEFAULT_FORMAT,
datefmt=DEFAULT_DATE_FORMAT, colors=DEFAULT_COLORS):
r"""
:arg bool color: Enables color support.
:arg string fmt: Log message format.
It will be applied to the attributes dict of log records. The
text between ``%(color)s`` and ``%(end_color)s`` will be colored
depending on the level if color support is on.
:arg dict colors: color mappings from logging level to terminal color
code
:arg string datefmt: Datetime format.
Used for formatting ``(asctime)`` placeholder in ``prefix_fmt``.
.. versionchanged:: 3.2
Added ``fmt`` and ``datefmt`` arguments.
"""
logging.Formatter.__init__(self, datefmt=datefmt)
self._fmt = fmt
self._colors = {}
if color and _stderr_supports_color():
# The curses module has some str/bytes confusion in
# python3. Until version 3.2.3, most methods return
# bytes, but only accept strings. In addition, we want to
# output these strings with the logging module, which
# works with unicode strings. The explicit calls to
# unicode() below are harmless in python2 but will do the
# right conversion in python 3.
fg_color = (curses.tigetstr("setaf") or
curses.tigetstr("setf") or "")
if (3, 0) < sys.version_info < (3, 2, 3):
fg_color = unicode_type(fg_color, "ascii")
for levelno, code in colors.items():
self._colors[levelno] = unicode_type(curses.tparm(fg_color, code), "ascii")
self._normal = unicode_type(curses.tigetstr("sgr0"), "ascii")
else:
self._normal = ''
示例6: tparm
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def tparm(self, capabilities, *args):
"""A simplified version of tigetstr without terminal delays."""
for capability in capabilities:
term_string = curses.tigetstr(capability)
if term_string is not None:
term_string = re.sub(b"\$\<[^>]+>", "", term_string)
break
try:
return curses.tparm(term_string, *args)
except Exception as e:
self.logging.debug("Unable to set tparm: %s" % e)
return b""
示例7: _scroll
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def _scroll(self, lines):
"""
Scroll the window up or down.
:param lines: Number of lines to scroll. Negative numbers scroll
down.
"""
if lines < 0:
self._safe_write("{}{}".format(
curses.tparm(self._move_y_x, 0, 0).decode("utf-8"),
(self._up_line + self._clear_line) * -lines))
else:
self._safe_write("{}{}".format(curses.tparm(
self._move_y_x, self.height, 0).decode("utf-8"),
(self._down_line + self._clear_line) * lines))
示例8: _change_colours
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def _change_colours(self, colour, attr, bg):
"""
Change current colour if required.
:param colour: New colour to use.
:param attr: New attributes to use.
:param bg: New background colour to use.
"""
# Change attribute first as this will reset colours when swapping
# modes.
if attr != self._attr:
self._safe_write(self._a_normal)
if attr != 0:
self._safe_write(self._ATTRIBUTES[attr])
self._attr = attr
self._colour = None
self._bg = None
# Now swap colours if required.
if colour != self._colour:
self._safe_write(curses.tparm(
self._fg_color, colour).decode("utf-8"))
self._colour = colour
if bg != self._bg:
self._safe_write(curses.tparm(
self._bg_color, bg).decode("utf-8"))
self._bg = bg
示例9: _print_at
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def _print_at(self, text, x, y, width):
"""
Print string at the required location.
:param text: The text string to print.
:param x: The x coordinate
:param y: The Y coordinate
:param width: The width of the character (for dual-width glyphs in CJK languages).
"""
# Move the cursor if necessary
cursor = u""
if x != self._cur_x or y != self._cur_y:
cursor = curses.tparm(self._move_y_x, y, x).decode("utf-8")
# Print the text at the required location and update the current
# position.
try:
self._safe_write(cursor + text)
except UnicodeEncodeError:
# This is probably a sign that the user has the wrong locale.
# Try to soldier on anyway.
self._safe_write(cursor + "?" * len(text))
# Update cursor position for next time...
self._cur_x = x + width
self._cur_y = y
示例10: __init__
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def __init__(self, color=True, *args, **kwargs):
logging.Formatter.__init__(self, *args, **kwargs)
self._color = color and _stderr_supports_color()
if self._color:
# The curses module has some str/bytes confusion in
# python3. Until version 3.2.3, most methods return
# bytes, but only accept strings. In addition, we want to
# output these strings with the logging module, which
# works with unicode strings. The explicit calls to
# unicode() below are harmless in python2 but will do the
# right conversion in python 3.
fg_color = (curses.tigetstr("setaf") or
curses.tigetstr("setf") or "")
if (3, 0) < sys.version_info < (3, 2, 3):
fg_color = unicode_type(fg_color, "ascii")
self._colors = {
logging.DEBUG: unicode_type(curses.tparm(fg_color, 4), # Blue
"ascii"),
logging.INFO: unicode_type(curses.tparm(fg_color, 2), # Green
"ascii"),
logging.WARNING: unicode_type(curses.tparm(fg_color, 3), # Yellow
"ascii"),
logging.ERROR: unicode_type(curses.tparm(fg_color, 1), # Red
"ascii"),
}
self._normal = unicode_type(curses.tigetstr("sgr0"), "ascii")
示例11: __init__
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def __init__(self, *args, **kwargs):
logging.Formatter.__init__(self, *args, **kwargs)
self._coloured = COLOURED and _stderr_supports_color()
if self._coloured:
curses.setupterm()
# The curses module has some str/bytes confusion in
# python3. Until version 3.2.3, most methods return
# bytes, but only accept strings. In addition, we want to
# output these strings with the logging module, which
# works with unicode strings. The explicit calls to
# unicode() below are harmless in python2 but will do the
# right conversion in python 3.
fg_color = (curses.tigetstr("setaf") or curses.tigetstr("setf") or
"")
if (3, 0) < sys.version_info < (3, 2, 3):
fg_color = unicode(fg_color, "ascii")
self._colors = {
# blues
logging.DEBUG: unicode(curses.tparm(fg_color, 4), "ascii"),
# green
logging.INFO: unicode(curses.tparm(fg_color, 2), "ascii"),
# yellow
logging.WARNING: unicode(curses.tparm(fg_color, 3), "ascii"),
# red
logging.ERROR: unicode(curses.tparm(fg_color, 1), "ascii")
}
self._normal = unicode(curses.tigetstr("sgr0"), "ascii")
示例12: __new__
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def __new__(cls, *args):
"""
Class constructor accepting 3 positional arguments.
:arg cap: parameterized string suitable for curses.tparm()
:arg normal: terminating sequence for this capability (optional).
:arg name: name of this terminal capability (optional).
"""
assert args and len(args) < 4, args
new = six.text_type.__new__(cls, args[0])
new._normal = args[1] if len(args) > 1 else u''
new._name = args[2] if len(args) > 2 else u'<not specified>'
return new
示例13: __call__
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def __call__(self, *args):
"""
Returning :class:`FormattingString` instance for given parameters.
Return evaluated terminal capability (self), receiving arguments
``*args``, followed by the terminating sequence (self.normal) into
a :class:`FormattingString` capable of being called.
:rtype: :class:`FormattingString` or :class:`NullCallableString`
"""
try:
# Re-encode the cap, because tparm() takes a bytestring in Python
# 3. However, appear to be a plain Unicode string otherwise so
# concats work.
attr = curses.tparm(self.encode('latin1'), *args).decode('latin1')
return FormattingString(attr, self._normal)
except TypeError as err:
# If the first non-int (i.e. incorrect) arg was a string, suggest
# something intelligent:
if args and isinstance(args[0], six.string_types):
raise TypeError(
"A native or nonexistent capability template, %r received"
" invalid argument %r: %s. You probably misspelled a"
" formatting call like `bright_red'" % (
self._name, args, err))
# Somebody passed a non-string; I don't feel confident
# guessing what they were trying to do.
raise
except curses.error as err:
# ignore 'tparm() returned NULL', you won't get any styling,
# even if does_styling is True. This happens on win32 platforms
# with http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses installed
if "tparm() returned NULL" not in six.text_type(err):
raise
return NullCallableString()
示例14: unicode_parm
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def unicode_parm(cap, *parms):
"""Return the result of ``tparm(tigetstr())`` except as Unicode."""
try:
cap = curses.tigetstr(cap)
except curses.error:
cap = None
if cap:
try:
val = curses.tparm(cap, *parms)
except curses.error:
val = None
if val:
return val.decode('latin1')
return u''
示例15: _tparm
# 需要导入模块: import curses [as 别名]
# 或者: from curses import tparm [as 别名]
def _tparm(self, arg, index):
import curses
return curses.tparm(to_bytes(arg), index).decode('ascii') or ''