66import argparse
77import bisect
88from dataclasses import dataclass
9+ import os
910from pathlib import Path
1011from typing import Optional
1112import struct
1213import sys
1314
15+ import colorama
1416import elftools .elf .elffile
1517import mapfile_parser
1618
@@ -228,9 +230,13 @@ def find_symbols_by_name(
228230 return infos
229231
230232
231- def print_map_file (map_file : mapfile_parser .mapfile .MapFile ):
233+ def print_map_file (map_file : mapfile_parser .mapfile .MapFile , * , colors : bool ):
232234 for segment in map_file :
233- print (f"{ segment .name } " )
235+ print (
236+ f"{ colorama .Fore .GREEN if colors else "" } "
237+ f"{ segment .name } "
238+ f"{ colorama .Fore .RESET if colors else "" } "
239+ )
234240 for file in segment :
235241 # Ignore debug sections
236242 if (
@@ -239,7 +245,11 @@ def print_map_file(map_file: mapfile_parser.mapfile.MapFile):
239245 or file .sectionType .startswith (".mdebug" )
240246 ):
241247 continue
242- print (f" { file .asStr ()} " )
248+ print (
249+ f"{ colorama .Fore .CYAN if colors else "" } "
250+ f" { file .asStr ()} "
251+ f"{ colorama .Fore .RESET if colors else "" } "
252+ )
243253 for sym in file :
244254 vram_str = f"{ sym .vram :08X} "
245255 if sym .vrom is None :
@@ -265,6 +275,12 @@ def sym_info_main():
265275 action = "store_true" ,
266276 help = "use the map file and elf in expected/build/ instead of build/" ,
267277 )
278+ parser .add_argument (
279+ "--color" ,
280+ help = "Whether to print using colors or not" ,
281+ choices = ("never" , "always" , "auto" ),
282+ default = "auto" ,
283+ )
268284 parser .add_argument (
269285 "-v" ,
270286 "--version" ,
@@ -275,6 +291,17 @@ def sym_info_main():
275291
276292 args = parser .parse_args ()
277293
294+ if args .color == "never" :
295+ colors = False
296+ elif args .color == "always" :
297+ colors = True
298+ else :
299+ # auto
300+ if os .getenv ("NO_COLOR" ):
301+ colors = False
302+ else :
303+ colors = sys .stdout .isatty ()
304+
278305 BUILTMAP = Path ("build" ) / args .oot_version / f"oot-{ args .oot_version } .map"
279306 BUILTELF = Path ("build" ) / args .oot_version / f"oot-{ args .oot_version } .elf"
280307
@@ -301,7 +328,7 @@ def sym_info_main():
301328
302329 sym_name = args .symname
303330 if sym_name is None :
304- print_map_file (map_file )
331+ print_map_file (map_file , colors = colors )
305332 sys .exit (0 )
306333
307334 infos : list [mapfile_parser .mapfile .FoundSymbolInfo ] = []
0 commit comments