Bug 1845017 - Disable the TestPHCExhaustion test r=glandium
[gecko.git] / python / mozterm / mozterm / terminal.py
blobf82daa67fd3353c0486f15357cf2c9ee5bfa56a4
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 import os
6 import sys
8 import six
11 class NullTerminal(object):
12 """Replacement for `blessed.Terminal()` that does no formatting."""
14 number_of_colors = 0
15 width = 0
16 height = 0
18 def __init__(self, stream=None, **kwargs):
19 self.stream = stream or sys.__stdout__
20 try:
21 self.is_a_tty = os.isatty(self.stream.fileno())
22 except Exception:
23 self.is_a_tty = False
25 class NullCallableString(six.text_type):
26 """A dummy callable Unicode stolen from blessings"""
28 def __new__(cls):
29 new = six.text_type.__new__(cls, "")
30 return new
32 def __call__(self, *args):
33 if len(args) != 1 or isinstance(args[0], int):
34 return ""
35 return args[0]
37 def __getattr__(self, attr):
38 return self.NullCallableString()
41 def Terminal(raises=False, disable_styling=False, **kwargs):
42 if disable_styling:
43 return NullTerminal(**kwargs)
44 try:
45 import blessed
46 except Exception:
47 if raises:
48 raise
49 return NullTerminal(**kwargs)
50 return blessed.Terminal(**kwargs)