|
| 1 | +from InquirerPy import inquirer |
| 2 | + |
| 3 | +from cli.utils.get_translation import get_translation |
| 4 | +from spice.analyze import analyze_file |
| 5 | + |
| 6 | + |
| 7 | +def analyze_command(file, all, json_output, LANG_FILE): |
| 8 | + """ |
| 9 | + Analyze the given file. |
| 10 | + """ |
| 11 | + |
| 12 | + # load translations |
| 13 | + messages = get_translation(LANG_FILE) |
| 14 | + |
| 15 | + # define available stats UPDATE THIS WHEN NEEDED PLEASE !!!!!!!! |
| 16 | + available_stats = [ |
| 17 | + "line_count", |
| 18 | + "function_count", |
| 19 | + "comment_line_count" |
| 20 | + ] |
| 21 | + |
| 22 | + # dictionary for the stats UPDATE THIS WHEN NEEDED PLEASE !!!!!!!! |
| 23 | + stats_labels = { |
| 24 | + "line_count": messages.get("line_count_option", "Line Count"), |
| 25 | + "function_count": messages.get("function_count_option", "Function Count"), |
| 26 | + "comment_line_count": messages.get("comment_line_count_option", "Comment Line Count") |
| 27 | + } |
| 28 | + |
| 29 | + # If --all flag is used, skip the selection menu and use all stats |
| 30 | + if all: |
| 31 | + selected_stat_keys = available_stats |
| 32 | + else: |
| 33 | + # Don't show interactive menu in JSON mode (assumes all stats) |
| 34 | + if json_output: |
| 35 | + selected_stat_keys = available_stats |
| 36 | + else: |
| 37 | + # print checkbox menu to select which stats to show |
| 38 | + selected_stats = inquirer.checkbox( |
| 39 | + message=messages.get("select_stats", "Select stats to display:"), |
| 40 | + choices=[stats_labels[stat] for stat in available_stats], |
| 41 | + pointer="> ", |
| 42 | + default=[stats_labels[stat] for stat in available_stats], # All selected by default |
| 43 | + instruction=messages.get("checkbox_hint", "(Use space to select, enter to confirm)") |
| 44 | + ).execute() |
| 45 | + |
| 46 | + # if no stats were selected |
| 47 | + if not selected_stats: |
| 48 | + if json_output: |
| 49 | + import json |
| 50 | + print(json.dumps({"error": messages.get("no_stats_selected", "No stats selected. Analysis cancelled.")})) |
| 51 | + else: |
| 52 | + print(messages.get("no_stats_selected", "No stats selected. Analysis cancelled.")) |
| 53 | + return |
| 54 | + |
| 55 | + # create a mapping from displayed labels back to stat keys |
| 56 | + reverse_mapping = {v: k for k, v in stats_labels.items()} |
| 57 | + |
| 58 | + # convert selected labels back to stat keys |
| 59 | + selected_stat_keys = [reverse_mapping[label] for label in selected_stats] |
| 60 | + |
| 61 | + # try to analyze and if error then print the error |
| 62 | + try: |
| 63 | + # show analyzing message if not in JSON mode |
| 64 | + if not json_output: |
| 65 | + print(f"{messages['analyzing_file']}: {file}") |
| 66 | + |
| 67 | + # get analysis results from analyze_file |
| 68 | + results = analyze_file(file, selected_stats=selected_stat_keys) |
| 69 | + |
| 70 | + # output in JSON format if flag |
| 71 | + if json_output: |
| 72 | + import json |
| 73 | + print(json.dumps(results, indent=2)) |
| 74 | + else: |
| 75 | + # only print the selected stats in normal mode |
| 76 | + for stat in selected_stat_keys: |
| 77 | + if stat in results: |
| 78 | + print(messages[stat].format(count=results[stat])) |
| 79 | + |
| 80 | + except Exception as e: |
| 81 | + if json_output: |
| 82 | + import json |
| 83 | + # Replace newlines with spaces or escape them properly |
| 84 | + error_msg = str(e).replace('\n', ' ') |
| 85 | + print(json.dumps({"error": error_msg})) |
| 86 | + else: |
| 87 | + print(f"[red]{messages['error']}[/] {e}") |
| 88 | + |
0 commit comments