Skip to content

Commit f4811d9

Browse files
committed
Improve string highighting in various fields
1 parent 65b77e4 commit f4811d9

File tree

4 files changed

+42
-22
lines changed

4 files changed

+42
-22
lines changed

test/unit/processor/command/test_cmd_info_files.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ def test_info_files():
4444

4545
assert len(cmdhelper.msgs) == 2
4646
assert cmdhelper.msgs[0].index("is not cached in debugger") > 0
47-
assert cmdhelper.msgs[1].startswith("Canonic name:")
47+
assert cmdhelper.msgs[1].startswith("Canonic name is the same as")
4848

4949
# Run again. File should now be cached.
5050

5151
reset_output()
5252
sub.run([])
5353
assert len(cmdhelper.msgs) == 2
5454
assert cmdhelper.msgs[0].index("is cached in debugger") > 0
55-
assert cmdhelper.msgs[1].startswith("Canonic name:")
55+
assert cmdhelper.msgs[1].startswith("Canonic name is the same as")
5656
assert "width" in sub.settings
5757

5858
# Test all sub options with different line-widths settings

trepan/processor/command/info_subcmd/files.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2008-2009, 2012-2013, 2015, 2023-2024 Rocky Bernstein
3+
# Copyright (C) 2008-2009, 2012-2013, 2015, 2023-2024, 2026 Rocky Bernstein
44
55
#
66
# This program is free software: you can redistribute it and/or modify
@@ -21,11 +21,10 @@
2121
import columnize
2222
import pyficache
2323

24-
from trepan import misc as Mmisc
2524
from trepan.lib.complete import complete_token
2625
from trepan.lib.file import file_list
27-
28-
# Our local modules
26+
from trepan.lib.format import Filename, format_token
27+
from trepan.misc import wrapped_lines
2928
from trepan.processor.command.base_subcmd import DebuggerSubcommand
3029

3130

@@ -34,12 +33,10 @@ class InfoFiles(DebuggerSubcommand):
3433
need_stack = False
3534
short_help = "Show information about an imported or loaded Python file"
3635

37-
3836
def complete(self, prefix):
3937
completions = sorted(["."] + file_list())
4038
return complete_token(completions, prefix)
4139

42-
4340
def run(self, args):
4441
"""**info files** [*filename* [**all** | **brkpts** | **sha1** | **size**]]
4542
@@ -67,13 +64,14 @@ def run(self, args):
6764
filename = args[0]
6865
pass
6966

70-
m = filename + " is"
67+
style = self.settings["style"]
68+
m = f"{format_token(Filename, filename, style=style)} is"
7169
filename_cache = self.core.filename_cache
7270
if filename in filename_cache:
7371
m += " cached in debugger"
7472
if filename_cache[filename] != filename:
7573
m += " as:"
76-
m = Mmisc.wrapped_lines(
74+
m = wrapped_lines(
7775
m, filename_cache[filename] + ".", self.settings["width"]
7876
)
7977
else:
@@ -95,9 +93,16 @@ def run(self, args):
9593
self.msg(m + " not cached in debugger.")
9694
pass
9795
canonic_name = self.core.canonic(filename)
98-
self.msg(
99-
Mmisc.wrapped_lines("Canonic name:", canonic_name, self.settings["width"])
100-
)
96+
if canonic_name == filename:
97+
self.msg("Canonic name is the same as the filename.")
98+
else:
99+
self.msg(
100+
wrapped_lines(
101+
"Canonic name:",
102+
format_token(Filename, canonic_name, style=style),
103+
self.settings["width"],
104+
)
105+
)
101106
for name in (canonic_name, filename):
102107
if name in sys.modules:
103108
for key in [k for k, v in list(sys.modules.items()) if name == v]:

trepan/processor/command/info_subcmd/frame.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from trepan.lib.complete import complete_token
2222
from trepan.lib.disassemble import PYTHON_OPCODES as python_opcodes
23-
from trepan.lib.format import Function, Keyword, format_token
23+
from trepan.lib.format import Function, Keyword, LineNumber, format_token
2424
from trepan.lib.stack import (
2525
format_function_name,
2626
get_column_start_from_frame,
@@ -152,11 +152,12 @@ def run(self, args):
152152
file_path = code.co_filename
153153

154154
line_text = getline(file_path, line_number, {"style": style})
155+
formatted_line_number = format_token(LineNumber, str(frame.f_lineno), style=style)
155156
if line_text is None:
156-
self.msg(f" current line number: {frame.f_lineno}")
157+
self.msg(f" current line number: {formatted_line_number}")
157158
else:
158159
formatted_text = highlight_string(line_text.strip(), style=style)
159-
self.msg_nocr(f" current line number: {frame.f_lineno}: {formatted_text}")
160+
self.msg_nocr(f" current line number: {formatted_line_number}: {formatted_text}")
160161

161162
start_column = get_column_start_from_frame(frame)
162163
if start_column >= 0:

trepan/processor/command/info_subcmd/line.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
#
1616
# You should have received a copy of the GNU General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18-
import columnize
1918
import inspect
2019
import os.path as osp
2120
import re
2221

22+
import columnize
2323
from pyficache import get_linecache_info
2424

2525
from trepan.clifns import search_file
26+
from trepan.lib.format import Filename, LineNumber, format_token
2627
from trepan.misc import wrapped_lines
2728
from trepan.processor.cmdbreak import parse_break_cmd
2829

@@ -119,22 +120,35 @@ def run(self, args):
119120
linecache_info = get_linecache_info(filename)
120121
if line_number not in linecache_info.line_numbers:
121122
self.errmsg(
122-
"No line information for line %d of %s"
123-
% (line_number, filename)
123+
"No line information for line %d of %s" % (line_number, filename)
124124
)
125125
return
126-
msg1 = 'Line %d of "%s"' % (line_number, self.core.filename(filename),)
126+
127+
style = self.settings["style"]
128+
formatted_filename = format_token(
129+
Filename,
130+
format_token(Filename, self.core.filename(filename), style=style),
131+
style=style,
132+
)
133+
formatted_line_number = format_token(LineNumber, str(line_number), style=style)
134+
msg1 = "Line %s of %s" % (formatted_line_number, formatted_filename)
127135
line_info = linecache_info.line_info
128136
line_number_offsets = line_info.get(line_number)
129137
if line_number_offsets:
130-
offset_data = [f"{code.co_name}:*{offset}" for code, offset in line_number_offsets]
138+
offset_data = [
139+
f"{code.co_name}:*{offset}" for code, offset in line_number_offsets
140+
]
131141
if len(offset_data) == 1:
132142
msg2 = f"is at bytecode offset {offset_data[0]}"
133143
self.msg(wrapped_lines(msg1, msg2, self.settings["width"]))
134144
else:
135145
msg2 = "is at bytecode offsets:"
136146
self.msg(wrapped_lines(msg1, msg2, self.settings["width"]))
137-
self.msg(columnize.columnize(offset_data, colsep=", ", ljust=False, lineprefix=" "))
147+
self.msg(
148+
columnize.columnize(
149+
offset_data, colsep=", ", ljust=False, lineprefix=" "
150+
)
151+
)
138152
else:
139153
self.errmsg(
140154
"No line information for line %d of %s"

0 commit comments

Comments
 (0)