Skip to content

Commit 83121d4

Browse files
committed
Change the odg list and diff printing
* Default print output to compact view * Remove --compact option * Add --verbose option that can be used many times * Add --verbose on diff with the same effect * Fix printing when using --minus
1 parent a968e18 commit 83121d4

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

src/objdictgen/__main__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
149149
""", aliases=['compare'])
150150
subp.add_argument('od1', help="Object dictionary")
151151
subp.add_argument('od2', help="Object dictionary")
152+
subp.add_argument('-v', '--verbose', action="count", default=0, help="Verbose output (can be used multiple times)")
152153
subp.add_argument('--show', action="store_true", help="Show difference data")
153154
subp.add_argument('--internal', action="store_true", help="Diff internal object")
154155
subp.add_argument('--data', action="store_true", help="Show difference as data")
@@ -166,9 +167,7 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
166167
""", aliases=['cat'])
167168
subp.add_argument('od', nargs="+", help="Object dictionary")
168169
subp.add_argument('-i', '--index', action="append", help="Specify parameter index to show")
169-
subp.add_argument('--all', action="store_true",
170-
help="Show all subindexes, including subindex 0")
171-
subp.add_argument('--compact', action="store_true", help="Compact listing")
170+
subp.add_argument('-v', '--verbose', action="count", default=0, help="Verbose output (can be used multiple times)")
172171
subp.add_argument('--raw', action="store_true", help="Show raw parameter values")
173172
subp.add_argument('--short', action="store_true", help="Do not list sub-index")
174173
subp.add_argument('--unused', action="store_true", help="Include unused profile parameters")
@@ -275,7 +274,7 @@ def main(debugopts: DebugOpts, args: Sequence[str]|None = None):
275274
od2 = open_od(opts.od2, validate=not opts.novalidate)
276275

277276
lines = list(format_diff_nodes(od1, od2, data=opts.data, raw=opts.raw,
278-
internal=opts.internal, show=opts.show))
277+
internal=opts.internal, show=opts.show, verbose=opts.verbose))
279278

280279
for line in lines:
281280
print(line)

src/objdictgen/printing.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
@dataclass
1919
class FormatNodeOpts:
2020
""" Options for formatting the node """
21-
compact: bool = False
21+
verbose: int = 0
2222
short: bool = False
2323
unused: bool = False
24-
all: bool = False
2524
raw: bool = False
2625
internal: bool = False
2726

@@ -78,25 +77,26 @@ def format_node(
7877
extra = f"{pname} (not loaded)"
7978
profiles.append(extra)
8079

81-
if not kwargs.get("compact"):
82-
yield f"{Fore.CYAN}File:{Style.RESET_ALL} {name}"
83-
yield f"{Fore.CYAN}Name:{Style.RESET_ALL} {node.Name} [{node.Type.upper()}] {node.Description}"
80+
yield f"{Fore.CYAN}File:{Style.RESET_ALL} {name}"
81+
yield f"{Fore.CYAN}Name:{Style.RESET_ALL} {node.Name} [{node.Type.upper()}] {node.Description}"
82+
if opts.verbose:
8483
tp = ", ".join(profiles) or None
8584
yield f"{Fore.CYAN}Profiles:{Style.RESET_ALL} {tp}"
86-
if node.ID:
87-
yield f"{Fore.CYAN}ID:{Style.RESET_ALL} {node.ID}"
88-
yield ""
85+
if node.ID:
86+
yield f"{Fore.CYAN}ID:{Style.RESET_ALL} {node.ID}"
87+
yield ""
8988

9089
index_range = None
9190
header = ''
91+
last_is_equal = False
9292

9393
for k in keys:
9494

9595
# Get the index range title
9696
ir = maps.INDEX_RANGES.get_index_range(k)
9797
if index_range != ir:
9898
index_range = ir
99-
if not opts.compact:
99+
if opts.verbose:
100100
header = Fore.YELLOW + ir.description + Style.RESET_ALL
101101

102102
obj = node.GetIndexEntry(k)
@@ -107,14 +107,19 @@ def format_node(
107107
if obj == minusobj:
108108
linegen = format_od_object(node, k, short=True)
109109
lines = [remove_color(line) for line in linegen]
110-
lines[0] = Fore.LIGHTBLACK_EX + lines[0] + f" {Fore.LIGHTRED_EX}<EQUAL>{Style.RESET_ALL}"
110+
pre = " " if opts.verbose else ''
111+
lines[0] = f"{pre}{Fore.LIGHTBLACK_EX}{lines[0]} {Fore.GREEN}<EQUAL>{Style.RESET_ALL}"
111112
yield from lines
113+
last_is_equal = True
112114
continue
113115

116+
if last_is_equal and opts.verbose:
117+
yield ""
118+
114119
# Yield the text for the index
115120
lines = list(format_od_object(
116-
node, k, short=opts.short, compact=opts.compact, unused=opts.unused,
117-
verbose=opts.all, raw=opts.raw
121+
node, k, short=opts.short, verbose=opts.verbose, unused=opts.unused,
122+
raw=opts.raw
118123
))
119124

120125
if opts.internal and lines[-1] == "":
@@ -133,12 +138,12 @@ def format_node(
133138
obj = node.GetIndexEntry(k)
134139
lines = pformat(obj, width=TERM_COLS).splitlines()
135140
yield from lines
136-
if not opts.compact:
141+
if opts.verbose:
137142
yield ""
138143

139144

140145
def format_od_header(
141-
node: Node, index: int, *, unused=False, compact=False, raw=False,
146+
node: Node, index: int, *, unused=False, verbose=False, raw=False,
142147
entry: TIndexEntry|None = None
143148
) -> tuple[str, dict[str, str]]:
144149
"""Get the print output for a dictionary entry header"""
@@ -194,13 +199,13 @@ def format_od_header(
194199
'name': f"{Fore.LIGHTWHITE_EX}{t_name}{Style.RESET_ALL}",
195200
'struct': f"{Fore.LIGHTYELLOW_EX}[{t_string.upper()}]{Style.RESET_ALL}",
196201
'flags': f" {Fore.MAGENTA}{t_flags}{Style.RESET_ALL}" if flags else '',
197-
'pre': ' ' if not compact else '',
202+
'pre': ' ' if verbose else '',
198203
}
199204

200205

201206
def format_od_object(
202-
node: Node, index: int, *, short=False, compact=False,
203-
unused=False, verbose=False, raw=False,
207+
node: Node, index: int, *, short=False,
208+
unused=False, verbose: int=0, raw=False,
204209
) -> Generator[str, None, None]:
205210
"""Return the print formatting for an object dictionary entry."""
206211

@@ -210,7 +215,7 @@ def format_od_object(
210215

211216
# Get the header for the entry and output it unless it is empty
212217
line, fmt = format_od_header(
213-
node, index, unused=unused, compact=compact, entry=param, raw=raw
218+
node, index, unused=unused, verbose=verbose, entry=param, raw=raw
214219
)
215220
if not line:
216221
return
@@ -278,7 +283,7 @@ def format_od_object(
278283
t_comment = f"{Fore.LIGHTBLACK_EX}/* {t_comment} */{Style.RESET_ALL}"
279284

280285
# Omit printing the first element unless specifically requested
281-
if (not verbose and i == 0
286+
if (verbose < 2 and i == 0
282287
and obj['struct'] & OD.MultipleSubindexes
283288
and not t_comment
284289
):
@@ -317,20 +322,20 @@ def format_od_object(
317322
for infoentry in infos:
318323
yield fmt.format(**infoentry)
319324

320-
if not compact and infos:
325+
if verbose and infos:
321326
yield ""
322327

323328

324329
def format_diff_nodes(
325-
od1: Node, od2: Node, *, data=False, raw=False,
330+
od1: Node, od2: Node, *, data=False, raw=False, verbose: int=0,
326331
internal=False, show=False
327332
) -> Generator[str, None, None]:
328333
""" Compare two object dictionaries and return the formatted differences. """
329334

330335
if internal or data:
331336
diffs = jsonod.diff(od1, od2, internal=internal)
332337
else:
333-
diffs = text_diff(od1, od2, data_mode=raw)
338+
diffs = text_diff(od1, od2, data_mode=raw, verbose=verbose)
334339

335340
rst = Style.RESET_ALL
336341

@@ -395,7 +400,7 @@ def _pprint(text: str, prefix: str = ' '):
395400
yield f"{Fore.RED}{chtype} {ppath} {change}{rst}"
396401

397402

398-
def text_diff(od1: Node, od2: Node, data_mode: bool=False) -> TDiffNodes:
403+
def text_diff(od1: Node, od2: Node, data_mode: bool=False, verbose: int=0) -> TDiffNodes:
399404
""" Compare two object dictionaries as text and return the differences. """
400405

401406
# Get all indices for the nodes
@@ -412,10 +417,10 @@ def text_diff(od1: Node, od2: Node, data_mode: bool=False) -> TDiffNodes:
412417
entry1: TIndexEntry = {}
413418
entry2: TIndexEntry = {}
414419
if index in keys1:
415-
text1 = list(format_od_object(od1, index, unused=True))
420+
text1 = list(format_od_object(od1, index, unused=True, verbose=verbose))
416421
entry1 = od1.GetIndexEntry(index)
417422
if index in keys2:
418-
text2 = list(format_od_object(od2, index, unused=True))
423+
text2 = list(format_od_object(od2, index, unused=True, verbose=verbose))
419424
entry2 = od2.GetIndexEntry(index)
420425

421426
if data_mode:

tests/test_printing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_printing_format_node(odpath, file):
1818
od.ID = 1
1919

2020
opts = FormatNodeOpts(
21-
compact=False, short=False, unused=True, all=True, raw=False
21+
verbose=True, short=False, unused=True, raw=False
2222
)
2323

2424
lines = format_node(od, file, opts=opts)
@@ -27,7 +27,7 @@ def test_printing_format_node(odpath, file):
2727
assert isinstance(line, str)
2828

2929
opts = FormatNodeOpts(
30-
compact=False, short=False, unused=True, all=True, raw=False
30+
verbose=True, short=False, unused=True, raw=False
3131
)
3232

3333
lines = format_node(od, file, index=[0x1000], opts=opts)
@@ -36,7 +36,7 @@ def test_printing_format_node(odpath, file):
3636
assert isinstance(line, str)
3737

3838
opts = FormatNodeOpts(
39-
compact=False, short=False, unused=True, all=True, raw=False
39+
verbose=True, short=False, unused=True, raw=False
4040
)
4141

4242
with pytest.raises(ValueError) as exc:

0 commit comments

Comments
 (0)