Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix(onfileopen): handle encoding errors in file opening with fallback #54

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 24 additions & 32 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,21 @@ def OnFileOpen(self, event):
self.SetTitle("XediX - Text Editor")
self.SetTitle(f"XediX - Text Editor - editing {file_name}")
file_path = os.path.join(os.getcwd(), file_name)
with open(file_path, 'r') as file:
content = file.read()
try:
with open(file_path, 'r') as file:
content = file.read()
except UnicodeDecodeError:
try:
# If UTF-8 fails, try with a more permissive encoding
with open(file_path, 'r', encoding='latin-1') as file:
content = file.read()
except Exception as e:
wx.MessageBox(f"Error reading file: {str(e)}", "Error", wx.OK | wx.ICON_ERROR)
return
except Exception as e:
wx.MessageBox(f"Error reading file: {str(e)}", "Error", wx.OK | wx.ICON_ERROR)
return


if not self.notebook.IsShown():
# Hide, message and default screen
Expand Down Expand Up @@ -732,6 +745,7 @@ def on_text_change(event):

# Set dark background and light text for the entire control
for text_area in (text_area, minimap):

with open("theme.xcfg", 'r') as file:
theme = file.read()

Expand Down Expand Up @@ -913,7 +927,7 @@ def on_text_change(event):

def OnNewFile(self, event):
filename = wx.TextEntryDialog(self, "File name:")
fileext = wx.TextEntryDialog(self, "File extension(without the dot):")
fileext = wx.TextEntryDialog(self, "File extension (without the dot):")
if filename.ShowModal() == wx.ID_OK:
filename_value = filename.GetValue()
if fileext.ShowModal() == wx.ID_OK:
Expand Down Expand Up @@ -1017,9 +1031,12 @@ def HandleExecution(self, start_time):
self.output_window.SetSizer(main_sizer)

# Prepare output message
output_message = f"Errors:\n{stderr}\n"
output_message += f"Execution Time: {execution_time:.4f} milliseconds\n"
output_message += f"Memory Usage: {self.memory_usage:.2f} MB\n"
try:
output_message = f"Errors:\n{stderr}\n"
output_message += f"Execution Time: {execution_time:.4f} milliseconds\n"
output_message += f"Memory Usage: {self.memory_usage:.2f} MB\n"
except Exception as e:
output_message = f"An error occurred: {str(e)}"

# Update the output text area
self.output_text.SetValue(output_message)
Expand All @@ -1044,32 +1061,7 @@ def export_to_html(self, log_filename, output_message, return_visualization):
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Execution Log</title>
<style>
body {{
background-color: #1f2937;
color: #ffffff; /* White text color */
font-family: 'Arial', sans-serif;
margin: 20px;
}}
h1 {{
color: #f472b6; /* Pink color for headings */
font-size: 2rem;
margin-bottom: 1rem;
}}
h2 {{
color: #e5e7eb; /* Gray color for subheadings */
font-size: 1.5rem;
margin-top: 2rem;
}}
pre {{
background-color: #374151; /* Dark gray background for preformatted text */
padding: 10px;
border-radius: 5px;
overflow-x: auto; /* Allow horizontal scrolling */
white-space: pre-wrap; /* Wrap long lines */
}}
</style>
<title>Log</title>
</head>
<body>
<h1>Execution Log</h1>
Expand Down
Loading