aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2022-12-14 15:03:20 -0800
committerEli Schwartz <eschwartz93@gmail.com>2023-01-03 14:49:02 -0500
commit7460ab20cd42d79a3394a81f103228b1337aff34 (patch)
treef7766f176a148174c62e1a4af55a4831267627d1
parentb9ef66292602349c7a48d8779355bdc87cfc06fa (diff)
downloadmeson-7460ab20cd42d79a3394a81f103228b1337aff34.zip
meson-7460ab20cd42d79a3394a81f103228b1337aff34.tar.gz
meson-7460ab20cd42d79a3394a81f103228b1337aff34.tar.bz2
mlog: move code for printing code with a caret to the mlog module
We need this outside the constructor for the ParseException class, so let's pull it out. mlog seemed like a good place since it's a text formatting function, and has no dependencies.
-rw-r--r--mesonbuild/mlog.py11
-rw-r--r--mesonbuild/mparser.py2
2 files changed, 12 insertions, 1 deletions
diff --git a/mesonbuild/mlog.py b/mesonbuild/mlog.py
index e23091e..ae8c7a3 100644
--- a/mesonbuild/mlog.py
+++ b/mesonbuild/mlog.py
@@ -495,3 +495,14 @@ def stop_pager() -> None:
pass
log_pager.wait()
log_pager = None
+
+
+def code_line(text: str, line: str, colno: int) -> str:
+ """Print a line with a caret pointing to the colno
+
+ :param text: A message to display before the line
+ :param line: The line of code to be pointed to
+ :param colno: The column number to point at
+ :return: A formatted string of the text, line, and a caret
+ """
+ return f'{text}\n{line}\n{" " * colno}^'
diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py
index 6b49365..98f530f 100644
--- a/mesonbuild/mparser.py
+++ b/mesonbuild/mparser.py
@@ -47,7 +47,7 @@ def decode_match(match: T.Match[str]) -> str:
class ParseException(MesonException):
def __init__(self, text: str, line: str, lineno: int, colno: int) -> None:
# Format as error message, followed by the line with the error, followed by a caret to show the error column.
- super().__init__("{}\n{}\n{}".format(text, line, '{}^'.format(' ' * colno)))
+ super().__init__(mlog.code_line(text, line, colno))
self.lineno = lineno
self.colno = colno