summaryrefslogtreecommitdiffstats
path: root/python/mozterm/mozterm
diff options
context:
space:
mode:
Diffstat (limited to 'python/mozterm/mozterm')
-rw-r--r--python/mozterm/mozterm/__init__.py4
-rw-r--r--python/mozterm/mozterm/terminal.py50
-rw-r--r--python/mozterm/mozterm/widgets.py67
3 files changed, 121 insertions, 0 deletions
diff --git a/python/mozterm/mozterm/__init__.py b/python/mozterm/mozterm/__init__.py
new file mode 100644
index 0000000000..ff15e588ff
--- /dev/null
+++ b/python/mozterm/mozterm/__init__.py
@@ -0,0 +1,4 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+from .terminal import NullTerminal, Terminal # noqa
diff --git a/python/mozterm/mozterm/terminal.py b/python/mozterm/mozterm/terminal.py
new file mode 100644
index 0000000000..f82daa67fd
--- /dev/null
+++ b/python/mozterm/mozterm/terminal.py
@@ -0,0 +1,50 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+import os
+import sys
+
+import six
+
+
+class NullTerminal(object):
+ """Replacement for `blessed.Terminal()` that does no formatting."""
+
+ number_of_colors = 0
+ width = 0
+ height = 0
+
+ def __init__(self, stream=None, **kwargs):
+ self.stream = stream or sys.__stdout__
+ try:
+ self.is_a_tty = os.isatty(self.stream.fileno())
+ except Exception:
+ self.is_a_tty = False
+
+ class NullCallableString(six.text_type):
+ """A dummy callable Unicode stolen from blessings"""
+
+ def __new__(cls):
+ new = six.text_type.__new__(cls, "")
+ return new
+
+ def __call__(self, *args):
+ if len(args) != 1 or isinstance(args[0], int):
+ return ""
+ return args[0]
+
+ def __getattr__(self, attr):
+ return self.NullCallableString()
+
+
+def Terminal(raises=False, disable_styling=False, **kwargs):
+ if disable_styling:
+ return NullTerminal(**kwargs)
+ try:
+ import blessed
+ except Exception:
+ if raises:
+ raise
+ return NullTerminal(**kwargs)
+ return blessed.Terminal(**kwargs)
diff --git a/python/mozterm/mozterm/widgets.py b/python/mozterm/mozterm/widgets.py
new file mode 100644
index 0000000000..2cf5bf250c
--- /dev/null
+++ b/python/mozterm/mozterm/widgets.py
@@ -0,0 +1,67 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from .terminal import Terminal
+
+DEFAULT = "\x1b(B\x1b[m"
+
+
+class BaseWidget(object):
+ def __init__(self, terminal=None):
+ self.term = terminal or Terminal()
+ self.stream = self.term.stream
+
+
+class Footer(BaseWidget):
+ """Handles display of a footer in a terminal."""
+
+ def clear(self):
+ """Removes the footer from the current terminal."""
+ self.stream.write(self.term.move_x(0))
+ self.stream.write(self.term.clear_eol())
+
+ def write(self, parts):
+ """Write some output in the footer, accounting for terminal width.
+
+ parts is a list of 2-tuples of (encoding_function, input).
+ None means no encoding."""
+
+ # We don't want to write more characters than the current width of the
+ # terminal otherwise wrapping may result in weird behavior. We can't
+ # simply truncate the line at terminal width characters because a)
+ # non-viewable escape characters count towards the limit and b) we
+ # don't want to truncate in the middle of an escape sequence because
+ # subsequent output would inherit the escape sequence.
+ max_width = self.term.width
+ written = 0
+ write_pieces = []
+ for part in parts:
+ try:
+ func, part = part
+ attribute = getattr(self.term, func)
+ # In Blessed, these attributes aren't always callable
+ if callable(attribute):
+ encoded = attribute(part)
+ else:
+ # If it's not callable, assume it's just the raw
+ # ANSI Escape Sequence and prepend it ourselves.
+ # Append DEFAULT to stop text that comes afterwards
+ # from inheriting the formatting we prepended.
+ encoded = attribute + part + DEFAULT
+ except ValueError:
+ encoded = part
+
+ len_part = len(part)
+ len_spaces = len(write_pieces)
+ if written + len_part + len_spaces > max_width:
+ write_pieces.append(part[0 : max_width - written - len_spaces])
+ written += len_part
+ break
+
+ write_pieces.append(encoded)
+ written += len_part
+
+ with self.term.location():
+ self.term.move(self.term.height - 1, 0)
+ self.stream.write(" ".join(write_pieces))