terminal.py revision 8947:217fbc57df05
1# Copyright (c) 2011 Advanced Micro Devices, Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Author: Steve Reinhardt
28
29import sys
30
31# Intended usage example:
32#
33# if force_colors:
34#    from m5.util.terminal import termcap
35# elif no_colors:
36#    from m5.util.terminal import no_termcap as termcap
37# else:
38#    from m5.util.terminal import tty_termcap as termcap
39# print termcap.Blue + "This could be blue!" + termcap.Normal
40
41# ANSI color names in index order
42color_names = "Black Red Green Yellow Blue Magenta Cyan".split()
43
44# Character attribute capabilities.  Note that not all terminals
45# support all of these capabilities, or support them
46# differently/meaningfully.  For example:
47#
48# - In PuTTY (with the default settings), Dim has no effect, Standout
49#   is the same as Reverse, and Blink does not blink but switches to a
50#   gray background.
51#
52# Please feel free to add information about other terminals here.
53#
54capability_map = {
55         'Bold': 'bold',
56          'Dim': 'dim',
57        'Blink': 'blink',
58    'Underline': 'smul',
59      'Reverse': 'rev',
60     'Standout': 'smso',
61       'Normal': 'sgr0'
62}
63
64capability_names = capability_map.keys()
65
66def null_cap_string(s, *args):
67    return ''
68
69try:
70    import curses
71    curses.setupterm()
72    def cap_string(s, *args):
73        cap = curses.tigetstr(s)
74        if cap:
75            return curses.tparm(cap, *args)
76        else:
77            return ''
78except:
79    cap_string = null_cap_string
80
81class ColorStrings(object):
82    def __init__(self, cap_string):
83        for i, c in enumerate(color_names):
84            setattr(self, c, cap_string('setaf', i))
85        for name, cap in capability_map.iteritems():
86            setattr(self, name, cap_string(cap))
87
88termcap = ColorStrings(cap_string)
89no_termcap = ColorStrings(null_cap_string)
90
91if sys.stdout.isatty():
92    tty_termcap = termcap
93else:
94    tty_termcap = no_termcap
95
96def get_termcap(use_colors = None):
97    if use_colors:
98        return termcap
99    elif use_colors is None:
100        # option unspecified; default behavior is to use colors iff isatty
101        return tty_termcap
102    else:
103        return no_termcap
104
105def test_termcap(obj):
106    for c_name in color_names:
107        c_str = getattr(obj, c_name)
108        print c_str + c_name + obj.Normal
109        for attr_name in capability_names:
110            if attr_name == 'Normal':
111                continue
112            attr_str = getattr(obj, attr_name)
113            print attr_str + c_str + attr_name + " " + c_name + obj.Normal
114        print obj.Bold + obj.Underline + \
115              c_name + "Bold Underline " + c + obj.Normal
116
117if __name__ == '__main__':
118    print "=== termcap enabled ==="
119    test_termcap(termcap)
120    print termcap.Normal
121    print "=== termcap disabled ==="
122    test_termcap(no_termcap)
123