# -*- coding: utf-8 -*- line endings: unix -*-
#------------------------------------------------------------------------------
# miniboa/terminal.py
# Copyright 2009 Jim Storch
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain a
# copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#------------------------------------------------------------------------------
# Changes made by pR0Ps.CM[at]gmail[dot]com on 18/07/2012
# -Updated for use with Python 3.x
# -Repackaged into a single file to simplify distribution
# -Other misc fixes and changes
#
# Report any bugs in this implementation to me (email above)
#------------------------------------------------------------------------------
# Additional changes by Quixadhal on 2014.06.16
# -Re-split code into multiple files, for ease of maintenance
# -Rewrote terminal system
#
# Pinkfish style color codes are now available, as used in various
# LPMUD systems, as well as the I3 intermud network.
#
# Briefly, a color token is surrounded by the special symbol %^, which
# acts as a seperator for multiple tokens in a row. So, an example like
#
# A %^RED%^red apple%^RESET%^ and a %^BOLD%^BLUE%^blue ball%^RESET%^.
#
# Would result in "red apple" and "blue ball" being colored, and
# "blue ball" would also be in bold.
#
# Some terminals will amke that actual bold, others will make it a
# brighter blue color.
#
# The replacement is dependant on the terminal type passed in, which
# defaults to ANSI, but can be "unknown" to strip colors, or
# "i3" or "imc2" for the intermud networks, or "mxp" for that.
#------------------------------------------------------------------------------
"""
Support for color and formatting for various terminals or
terminal-like systems
"""
import re
from miniboa.colors import _TERMINAL_TYPES, _COLOR_TOKENS, _COLOR_MAP
_PARA_BREAK = re.compile(r"(\n\s*\n)", re.MULTILINE)
def word_wrap(text, columns = 80, indent = 4, padding = 2):
"""
Given a block of text, breaks into a list of lines wrapped to
length.
"""
paragraphs = _PARA_BREAK.split(text)
lines = []
columns -= padding
for para in paragraphs:
if para.isspace():
continue
line = ' ' * indent
linelen = len(line)
for word in para.split():
bareword = colorize(word, None)
if (linelen + 1 + len(bareword)) > columns:
lines.append(line)
line = ' ' * padding
linelen = len(line)
line += word
linelen += len(bareword)
else:
line += ' ' + word
linelen += len(bareword) + 1
if not line.isspace():
lines.append(line)
return lines
def colorize(text, terminal = 'ansi'):
"""
Given a chunk of text, replace color tokesn with the appropriate
color codes for the given terminal type
"""
if text == None or text == '':
return text
if terminal == None or terminal not in _TERMINAL_TYPES:
terminal = 'unknown'
words = text.split('%^')
for word in words:
if word == '':
continue
if word not in _COLOR_TOKENS:
continue
if word not in _COLOR_MAP[terminal]:
continue
i = words.index(word)
words[i] = _COLOR_MAP[terminal][word]
return ''.join(words)
def escape(text):
"""
Escape all the color tokens in the given text chunk, so they
can be safely printed through the color parser
"""
if text == None or text == '':
return text
text = text.replace('%^', '%%^^')
return text