From 49b26d28220999258fdd4bfac2c12d3651010cb2 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Fri, 13 Feb 2026 13:23:48 +0300 Subject: [PATCH] Replace func/struct/enum with unified show command - Add 'show' command to display code for functions/structs/enums by name - Remove separate func/struct/enum commands - Update usage message and docstring - Command format: c_util show name1 name2 ... --- c_util | 117 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 58 insertions(+), 59 deletions(-) diff --git a/c_util b/c_util index baf20df3..1482ea1d 100755 --- a/c_util +++ b/c_util @@ -4,9 +4,7 @@ c_util - C code navigation utility Commands: toc - Show table of contents for all files description - Show selected items with comments - func - Show function code - struct - Show struct definition - enum - Show enum definition + show [name2] - Show code for functions/structs/enums """ import sys @@ -659,7 +657,7 @@ def get_function_comments(pre_lines): def main(): if len(sys.argv) < 2: - print("Usage: c_util toc | description [name2] ... | func | struct | enum ") + print("Usage: c_util toc | description [name2] ... | show [name2] ...") sys.exit(1) cmd = sys.argv[1] @@ -784,65 +782,66 @@ def main(): # struct or enum - print with preserved formatting on multiple lines print(f"[{start + 1}-{end + 1}] {extra}") - elif cmd == 'func': - if len(sys.argv) < 4: - print("Usage: c_util func ") - sys.exit(1) - - file = sys.argv[2] - funcname = sys.argv[3] - - if file in project_functions: - for func in project_functions[file]: - if func['name'] == funcname: - lines = contents[file] - pre = func['pre'] - body = lines[func['start']:func['end'] + 1] - print('\n'.join(pre + body)) - sys.exit(0) - - print("Function not found") - sys.exit(1) - - elif cmd == 'struct': - if len(sys.argv) < 4: - print("Usage: c_util struct ") + elif cmd == 'show': + if len(sys.argv) < 3: + print("Usage: c_util show [name2] ...") sys.exit(1) - file = sys.argv[2] - structname = sys.argv[3] - - if file in project_structs: - for s in project_structs[file]: - if s['name'] == structname: - lines = contents[file] - pre = s['pre'] - body = lines[s['start']:s['end'] + 1] - print('\n'.join(pre + body)) - sys.exit(0) + target_names = set(sys.argv[2:]) + first_file = True + found_any = False - print("Struct not found") - sys.exit(1) - - elif cmd == 'enum': - if len(sys.argv) < 4: - print("Usage: c_util enum ") + for f in files: + if f not in contents: + continue + + # Collect all matching items + items = [] + + # Check functions + for func in project_functions.get(f, []): + if func['name'] in target_names: + items.append(('function', func)) + found_any = True + + # Check structs + for s in project_structs.get(f, []): + if s['name'] in target_names: + items.append(('struct', s)) + found_any = True + + # Check enums + for e in project_enums.get(f, []): + if e['name'] in target_names: + items.append(('enum', e)) + found_any = True + + if not items: + continue + + # Sort by line number + items.sort(key=lambda x: x[1]['start']) + + # Print empty line between files (except before first) + if not first_file: + print() + first_file = False + + # Print filename header + print(f"{f}:") + + # Print items + for item_type, item in items: + lines = contents[f] + pre = item['pre'] + body = lines[item['start']:item['end'] + 1] + if pre: + print('\n'.join(pre)) + print('\n'.join(body)) + + if not found_any: + print("Not found") sys.exit(1) - - file = sys.argv[2] - enumname = sys.argv[3] - - if file in project_enums: - for e in project_enums[file]: - if e['name'] == enumname: - lines = contents[file] - pre = e['pre'] - body = lines[e['start']:e['end'] + 1] - print('\n'.join(pre + body)) - sys.exit(0) - - print("Enum not found") - sys.exit(1) else: print("Unknown command")