summaryrefslogtreecommitdiff
path: root/tools/u_boot_pylib/terminal.py
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2025-04-29 07:21:59 -0600
committerSimon Glass <sjg@chromium.org>2025-05-27 10:07:41 +0100
commitae3695f691c6325f1a504ee3df7f22d75c7a0c96 (patch)
treef18e01121a9e05a60835462be85662a3d85cc7d9 /tools/u_boot_pylib/terminal.py
parent6330f94a35142c135c96f5af952087ef3d9c3177 (diff)
patman: Move capture_sys_output() into terminal and rename
This function is sometimes useful outside tests. Also it can affect how terminal output is done, e.g. whether ANSI characters should be emitted or not. Move it out of the test_util package and into terminal. Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'tools/u_boot_pylib/terminal.py')
-rw-r--r--tools/u_boot_pylib/terminal.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/tools/u_boot_pylib/terminal.py b/tools/u_boot_pylib/terminal.py
index 2cd5a54ab52..84531831760 100644
--- a/tools/u_boot_pylib/terminal.py
+++ b/tools/u_boot_pylib/terminal.py
@@ -7,6 +7,8 @@
This module handles terminal interaction including ANSI color codes.
"""
+from contextlib import contextmanager
+from io import StringIO
import os
import re
import shutil
@@ -271,3 +273,17 @@ class Color(object):
base = self.BRIGHT_START if bright else self.NORMAL_START
start = base % (color + 30)
return start + text + self.RESET
+
+
+# Use this to suppress stdout/stderr output:
+# with terminal.capture() as (stdout, stderr)
+# ...do something...
+@contextmanager
+def capture():
+ capture_out, capture_err = StringIO(), StringIO()
+ old_out, old_err = sys.stdout, sys.stderr
+ try:
+ sys.stdout, sys.stderr = capture_out, capture_err
+ yield capture_out, capture_err
+ finally:
+ sys.stdout, sys.stderr = old_out, old_err