From b45f3eba2a8008671eb139cb15f759e09a2c14d9 Mon Sep 17 00:00:00 2001 From: Dragorn421 Date: Wed, 26 Mar 2025 16:53:39 +0100 Subject: [PATCH] Add colors to sym_info.py full map print (#2474) --- sym_info.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/sym_info.py b/sym_info.py index 301e80110f..0b8936bb54 100755 --- a/sym_info.py +++ b/sym_info.py @@ -6,11 +6,13 @@ import argparse import bisect from dataclasses import dataclass +import os from pathlib import Path from typing import Optional import struct import sys +import colorama import elftools.elf.elffile import mapfile_parser @@ -228,9 +230,13 @@ def find_symbols_by_name( return infos -def print_map_file(map_file: mapfile_parser.mapfile.MapFile): +def print_map_file(map_file: mapfile_parser.mapfile.MapFile, *, colors: bool): for segment in map_file: - print(f"{segment.name}") + print( + f"{colorama.Fore.GREEN if colors else ""}" + f"{segment.name}" + f"{colorama.Fore.RESET if colors else ""}" + ) for file in segment: # Ignore debug sections if ( @@ -239,7 +245,11 @@ def print_map_file(map_file: mapfile_parser.mapfile.MapFile): or file.sectionType.startswith(".mdebug") ): continue - print(f" {file.asStr()}") + print( + f"{colorama.Fore.CYAN if colors else ""}" + f" {file.asStr()}" + f"{colorama.Fore.RESET if colors else ""}" + ) for sym in file: vram_str = f"{sym.vram:08X}" if sym.vrom is None: @@ -265,6 +275,12 @@ def sym_info_main(): action="store_true", help="use the map file and elf in expected/build/ instead of build/", ) + parser.add_argument( + "--color", + help="Whether to print using colors or not", + choices=("never", "always", "auto"), + default="auto", + ) parser.add_argument( "-v", "--version", @@ -275,6 +291,17 @@ def sym_info_main(): args = parser.parse_args() + if args.color == "never": + colors = False + elif args.color == "always": + colors = True + else: + # auto + if os.getenv("NO_COLOR"): + colors = False + else: + colors = sys.stdout.isatty() + BUILTMAP = Path("build") / args.oot_version / f"oot-{args.oot_version}.map" BUILTELF = Path("build") / args.oot_version / f"oot-{args.oot_version}.elf" @@ -301,7 +328,7 @@ def sym_info_main(): sym_name = args.symname if sym_name is None: - print_map_file(map_file) + print_map_file(map_file, colors=colors) sys.exit(0) infos: list[mapfile_parser.mapfile.FoundSymbolInfo] = []