Skip to content

Commit 57bffe0

Browse files
authored
Merge branch 'main' into identation
2 parents 67798eb + 23466e9 commit 57bffe0

File tree

9 files changed

+236
-176
lines changed

9 files changed

+236
-176
lines changed

cli/commands/__init__.py

Whitespace-only changes.

cli/commands/analyze.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+

cli/commands/hello.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from rich import print # this add colors to the printed text
2+
3+
from cli.utils.get_translation import get_translation
4+
5+
6+
def hello_command(LANG_FILE):
7+
8+
# load translations
9+
messages = get_translation(LANG_FILE)
10+
11+
# print the hello message
12+
print(messages["welcome"])
13+
print(messages["description"])

cli/commands/translate.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from InquirerPy import inquirer
2+
3+
4+
def translate_command(LANG_FILE):
5+
"""
6+
Set the language for CLI messages.
7+
"""
8+
9+
# LIST OF ALL AVAILIBLE LANGAUGES ADD NEW TRANSLATIONS HERE PLEASE !!!!!!!!!!!!!!!!!!!!!!!!
10+
LANGUAGES = {
11+
"en": {"name": "English"},
12+
"pt-br": {"name": "Portuguese"},
13+
"fremen": {"name": "Fremen"},
14+
# Add more languages as needed
15+
}
16+
17+
# this just makes the list above actually work (i wanted to add emojis but flag emojies dont work on pc 😭)
18+
choices = [
19+
f"{info['name']} ({lang})" for lang, info in LANGUAGES.items()
20+
]
21+
22+
# intereacitive menu
23+
selected_choice = inquirer.select(
24+
message="Choose a language:",
25+
choices=choices,
26+
pointer="> ",
27+
default="English"
28+
).execute()
29+
30+
# will read the dicionary to see what langauggue is which does that make sense? its like the reverse of before
31+
selected_lang = next(
32+
lang for lang, info in LANGUAGES.items() if f"{info['name']} ({lang})" == selected_choice
33+
)
34+
35+
# will write the new language to the langague file (to save it to HD instead of memory) (so its persistant between commands)
36+
with open(LANG_FILE, "w") as file:
37+
file.write(selected_lang)
38+
39+
print(f"[green]Language set to:[/] {selected_lang}")
40+

cli/commands/version.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
from rich import print # this add colors to the printed text
3+
4+
from cli.utils.get_translation import get_translation
5+
6+
7+
def version_command(LANG_FILE, CURRENT_DIR):
8+
"""
9+
Display the current version of the application.
10+
"""
11+
12+
# load translations
13+
messages = get_translation(LANG_FILE)
14+
15+
try:
16+
# Get the path to setup.py in the parent directory
17+
setup_path = os.path.join(os.path.dirname(CURRENT_DIR), "setup.py")
18+
19+
# Check if setup.py exists
20+
if not os.path.exists(setup_path):
21+
print(f"[red]{messages.get('setup_not_found', 'Error: setup.py not found.')}")
22+
return
23+
24+
# Read setup.py to extract version
25+
version_info = None
26+
with open(setup_path, "r") as file:
27+
for line in file:
28+
if line.strip().startswith("version="):
29+
# Extract version using string manipulation
30+
version_line = line.strip()
31+
version_info = version_line.split("=")[1].strip().strip('"').strip("'").strip(",")
32+
break
33+
34+
# Display version information
35+
if version_info:
36+
print(f"[green]{messages.get('version_info', 'SpiceCode Version:')}[/] {version_info}")
37+
else:
38+
print(f"[yellow]{messages.get('version_not_found', 'Version information not found in setup.py')}")
39+
40+
except Exception as e:
41+
print(f"[red]{messages.get('error', 'Error:')}[/] {e}")

0 commit comments

Comments
 (0)