terminal.py revision 7816
112027Sjungma@eit.uni-kl.de# Copyright (c) 2011 Advanced Micro Devices, Inc.
212027Sjungma@eit.uni-kl.de# All rights reserved.
312027Sjungma@eit.uni-kl.de#
412027Sjungma@eit.uni-kl.de# Redistribution and use in source and binary forms, with or without
512027Sjungma@eit.uni-kl.de# modification, are permitted provided that the following conditions are
612027Sjungma@eit.uni-kl.de# met: redistributions of source code must retain the above copyright
712027Sjungma@eit.uni-kl.de# notice, this list of conditions and the following disclaimer;
812027Sjungma@eit.uni-kl.de# redistributions in binary form must reproduce the above copyright
912027Sjungma@eit.uni-kl.de# notice, this list of conditions and the following disclaimer in the
1012027Sjungma@eit.uni-kl.de# documentation and/or other materials provided with the distribution;
1112027Sjungma@eit.uni-kl.de# neither the name of the copyright holders nor the names of its
1212027Sjungma@eit.uni-kl.de# contributors may be used to endorse or promote products derived from
1312027Sjungma@eit.uni-kl.de# this software without specific prior written permission.
1412027Sjungma@eit.uni-kl.de#
1512027Sjungma@eit.uni-kl.de# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612027Sjungma@eit.uni-kl.de# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712027Sjungma@eit.uni-kl.de# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812027Sjungma@eit.uni-kl.de# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912027Sjungma@eit.uni-kl.de# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012027Sjungma@eit.uni-kl.de# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112027Sjungma@eit.uni-kl.de# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212027Sjungma@eit.uni-kl.de# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312027Sjungma@eit.uni-kl.de# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412027Sjungma@eit.uni-kl.de# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512027Sjungma@eit.uni-kl.de# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612027Sjungma@eit.uni-kl.de#
2712027Sjungma@eit.uni-kl.de# Author: Steve Reinhardt
2812027Sjungma@eit.uni-kl.de
2912027Sjungma@eit.uni-kl.deimport sys
3012027Sjungma@eit.uni-kl.de
3112027Sjungma@eit.uni-kl.de# Intended usage example:
3212027Sjungma@eit.uni-kl.de#
3312027Sjungma@eit.uni-kl.de# if force_colors:
3412027Sjungma@eit.uni-kl.de#    from m5.util.terminal import termcap
3512027Sjungma@eit.uni-kl.de# elif no_colors:
3612027Sjungma@eit.uni-kl.de#    from m5.util.terminal import no_termcap as termcap
3712027Sjungma@eit.uni-kl.de# else:
3812027Sjungma@eit.uni-kl.de#    from m5.util.terminal import tty_termcap as termcap
3912027Sjungma@eit.uni-kl.de# print termcap.Blue + "This could be blue!" + termcap.Normal
4012027Sjungma@eit.uni-kl.de
4112027Sjungma@eit.uni-kl.de# ANSI color names in index order
4212027Sjungma@eit.uni-kl.decolor_names = "Black Red Green Yellow Blue Magenta Cyan".split()
4312027Sjungma@eit.uni-kl.de
4412027Sjungma@eit.uni-kl.de# Character attribute capabilities.  Note that not all terminals
4512027Sjungma@eit.uni-kl.de# support all of these capabilities, or support them
4612027Sjungma@eit.uni-kl.de# differently/meaningfully.  For example:
4712027Sjungma@eit.uni-kl.de#
4812027Sjungma@eit.uni-kl.de# - In PuTTY (with the default settings), Dim has no effect, Standout
4912027Sjungma@eit.uni-kl.de#   is the same as Reverse, and Blink does not blink but switches to a
5012027Sjungma@eit.uni-kl.de#   gray background.
5112027Sjungma@eit.uni-kl.de#
5212027Sjungma@eit.uni-kl.de# Please feel free to add information about other terminals here.
5312027Sjungma@eit.uni-kl.de#
5412027Sjungma@eit.uni-kl.decapability_map = {
5512027Sjungma@eit.uni-kl.de         'Bold': 'bold',
5612027Sjungma@eit.uni-kl.de          'Dim': 'dim',
5712027Sjungma@eit.uni-kl.de        'Blink': 'blink',
5812027Sjungma@eit.uni-kl.de    'Underline': 'smul',
5912027Sjungma@eit.uni-kl.de      'Reverse': 'rev',
6012027Sjungma@eit.uni-kl.de     'Standout': 'smso',
6112027Sjungma@eit.uni-kl.de       'Normal': 'sgr0'
6212027Sjungma@eit.uni-kl.de}
6312027Sjungma@eit.uni-kl.de
6412027Sjungma@eit.uni-kl.decapability_names = capability_map.keys()
6512027Sjungma@eit.uni-kl.de
6612027Sjungma@eit.uni-kl.dedef null_cap_string(s, *args):
6712027Sjungma@eit.uni-kl.de    return ''
6812027Sjungma@eit.uni-kl.de
6912027Sjungma@eit.uni-kl.detry:
7012027Sjungma@eit.uni-kl.de    import curses
7112027Sjungma@eit.uni-kl.de    curses.setupterm()
7212027Sjungma@eit.uni-kl.de    def cap_string(s, *args):
7312027Sjungma@eit.uni-kl.de        cap = curses.tigetstr(s)
7412027Sjungma@eit.uni-kl.de        if cap:
7512027Sjungma@eit.uni-kl.de            return curses.tparm(cap, *args)
7612027Sjungma@eit.uni-kl.de        else:
7712027Sjungma@eit.uni-kl.de            return ''
7812027Sjungma@eit.uni-kl.deexcept:
7912027Sjungma@eit.uni-kl.de    cap_string = null_cap_string
8012027Sjungma@eit.uni-kl.de
8112027Sjungma@eit.uni-kl.declass ColorStrings(object):
8212027Sjungma@eit.uni-kl.de    def __init__(self, cap_string):
8312027Sjungma@eit.uni-kl.de        for i, c in enumerate(color_names):
8412027Sjungma@eit.uni-kl.de            setattr(self, c, cap_string('setaf', i))
8512027Sjungma@eit.uni-kl.de        for name, cap in capability_map.iteritems():
8612027Sjungma@eit.uni-kl.de            setattr(self, name, cap_string(cap))
8712027Sjungma@eit.uni-kl.de
8812027Sjungma@eit.uni-kl.determcap = ColorStrings(cap_string)
8912027Sjungma@eit.uni-kl.deno_termcap = ColorStrings(null_cap_string)
9012027Sjungma@eit.uni-kl.de
9112027Sjungma@eit.uni-kl.deif sys.stdout.isatty():
9212027Sjungma@eit.uni-kl.de    tty_termcap = termcap
9312027Sjungma@eit.uni-kl.deelse:
9412027Sjungma@eit.uni-kl.de    tty_termcap = no_termcap
9512027Sjungma@eit.uni-kl.de
9612027Sjungma@eit.uni-kl.dedef test_termcap(obj):
9712027Sjungma@eit.uni-kl.de    for c_name in color_names:
9812027Sjungma@eit.uni-kl.de        c_str = getattr(obj, c_name)
9912027Sjungma@eit.uni-kl.de        print c_str + c_name + obj.Normal
10012027Sjungma@eit.uni-kl.de        for attr_name in capability_names:
10112027Sjungma@eit.uni-kl.de            if attr_name == 'Normal':
10212027Sjungma@eit.uni-kl.de                continue
10312027Sjungma@eit.uni-kl.de            attr_str = getattr(obj, attr_name)
10412027Sjungma@eit.uni-kl.de            print attr_str + c_str + attr_name + " " + c_name + obj.Normal
10512027Sjungma@eit.uni-kl.de        print obj.Bold + obj.Underline + \
10612027Sjungma@eit.uni-kl.de              c_name + "Bold Underline " + c + obj.Normal
10712027Sjungma@eit.uni-kl.de
10812027Sjungma@eit.uni-kl.deif __name__ == '__main__':
10912027Sjungma@eit.uni-kl.de    print "=== termcap enabled ==="
11012027Sjungma@eit.uni-kl.de    test_termcap(termcap)
11112027Sjungma@eit.uni-kl.de    print termcap.Normal
11212027Sjungma@eit.uni-kl.de    print "=== termcap disabled ==="
11312027Sjungma@eit.uni-kl.de    test_termcap(no_termcap)
11412027Sjungma@eit.uni-kl.de