terminal.py (13709:dd6b7ac5801f) terminal.py (13714:35636064b7a1)
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
29from __future__ import print_function
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
29from __future__ import print_function
30from __future__ import absolute_import
30
31import sys
32
33# Intended usage example:
34#
35# if force_colors:
36# from m5.util.terminal import termcap
37# elif no_colors:
38# from m5.util.terminal import no_termcap as termcap
39# else:
40# from m5.util.terminal import tty_termcap as termcap
41# print(termcap.Blue + "This could be blue!" + termcap.Normal)
42
43# ANSI color names in index order
44color_names = "Black Red Green Yellow Blue Magenta Cyan".split()
45
46# Character attribute capabilities. Note that not all terminals
47# support all of these capabilities, or support them
48# differently/meaningfully. For example:
49#
50# - In PuTTY (with the default settings), Dim has no effect, Standout
51# is the same as Reverse, and Blink does not blink but switches to a
52# gray background.
53#
54# Please feel free to add information about other terminals here.
55#
56capability_map = {
57 'Bold': 'bold',
58 'Dim': 'dim',
59 'Blink': 'blink',
60 'Underline': 'smul',
61 'Reverse': 'rev',
62 'Standout': 'smso',
63 'Normal': 'sgr0'
64}
65
66capability_names = list(capability_map.keys())
67
68def null_cap_string(s, *args):
69 return ''
70
71try:
72 import curses
73 curses.setupterm()
74 def cap_string(s, *args):
75 cap = curses.tigetstr(s)
76 if cap:
77 return curses.tparm(cap, *args)
78 else:
79 return ''
80except:
81 cap_string = null_cap_string
82
83class ColorStrings(object):
84 def __init__(self, cap_string):
85 for i, c in enumerate(color_names):
86 setattr(self, c, cap_string('setaf', i))
87 for name, cap in capability_map.items():
88 setattr(self, name, cap_string(cap))
89
90termcap = ColorStrings(cap_string)
91no_termcap = ColorStrings(null_cap_string)
92
93if sys.stdout.isatty():
94 tty_termcap = termcap
95else:
96 tty_termcap = no_termcap
97
98def get_termcap(use_colors = None):
99 if use_colors:
100 return termcap
101 elif use_colors is None:
102 # option unspecified; default behavior is to use colors iff isatty
103 return tty_termcap
104 else:
105 return no_termcap
106
107def test_termcap(obj):
108 for c_name in color_names:
109 c_str = getattr(obj, c_name)
110 print(c_str + c_name + obj.Normal)
111 for attr_name in capability_names:
112 if attr_name == 'Normal':
113 continue
114 attr_str = getattr(obj, attr_name)
115 print(attr_str + c_str + attr_name + " " + c_name + obj.Normal)
116 print(obj.Bold + obj.Underline +
117 c_name + "Bold Underline " + c + obj.Normal)
118
119if __name__ == '__main__':
120 print("=== termcap enabled ===")
121 test_termcap(termcap)
122 print(termcap.Normal)
123 print("=== termcap disabled ===")
124 test_termcap(no_termcap)
31
32import sys
33
34# Intended usage example:
35#
36# if force_colors:
37# from m5.util.terminal import termcap
38# elif no_colors:
39# from m5.util.terminal import no_termcap as termcap
40# else:
41# from m5.util.terminal import tty_termcap as termcap
42# print(termcap.Blue + "This could be blue!" + termcap.Normal)
43
44# ANSI color names in index order
45color_names = "Black Red Green Yellow Blue Magenta Cyan".split()
46
47# Character attribute capabilities. Note that not all terminals
48# support all of these capabilities, or support them
49# differently/meaningfully. For example:
50#
51# - In PuTTY (with the default settings), Dim has no effect, Standout
52# is the same as Reverse, and Blink does not blink but switches to a
53# gray background.
54#
55# Please feel free to add information about other terminals here.
56#
57capability_map = {
58 'Bold': 'bold',
59 'Dim': 'dim',
60 'Blink': 'blink',
61 'Underline': 'smul',
62 'Reverse': 'rev',
63 'Standout': 'smso',
64 'Normal': 'sgr0'
65}
66
67capability_names = list(capability_map.keys())
68
69def null_cap_string(s, *args):
70 return ''
71
72try:
73 import curses
74 curses.setupterm()
75 def cap_string(s, *args):
76 cap = curses.tigetstr(s)
77 if cap:
78 return curses.tparm(cap, *args)
79 else:
80 return ''
81except:
82 cap_string = null_cap_string
83
84class ColorStrings(object):
85 def __init__(self, cap_string):
86 for i, c in enumerate(color_names):
87 setattr(self, c, cap_string('setaf', i))
88 for name, cap in capability_map.items():
89 setattr(self, name, cap_string(cap))
90
91termcap = ColorStrings(cap_string)
92no_termcap = ColorStrings(null_cap_string)
93
94if sys.stdout.isatty():
95 tty_termcap = termcap
96else:
97 tty_termcap = no_termcap
98
99def get_termcap(use_colors = None):
100 if use_colors:
101 return termcap
102 elif use_colors is None:
103 # option unspecified; default behavior is to use colors iff isatty
104 return tty_termcap
105 else:
106 return no_termcap
107
108def test_termcap(obj):
109 for c_name in color_names:
110 c_str = getattr(obj, c_name)
111 print(c_str + c_name + obj.Normal)
112 for attr_name in capability_names:
113 if attr_name == 'Normal':
114 continue
115 attr_str = getattr(obj, attr_name)
116 print(attr_str + c_str + attr_name + " " + c_name + obj.Normal)
117 print(obj.Bold + obj.Underline +
118 c_name + "Bold Underline " + c + obj.Normal)
119
120if __name__ == '__main__':
121 print("=== termcap enabled ===")
122 test_termcap(termcap)
123 print(termcap.Normal)
124 print("=== termcap disabled ===")
125 test_termcap(no_termcap)