Skip to content

Commit

Permalink
Hydefix (#137)
Browse files Browse the repository at this point in the history
* Fix textbox colors

* Fix cursor position in HyDE

Determining cursor position by slicing the text and then measuring its width did not work because the width of off-screen characters affected the text position, but not the cursor position. The solution made here has to measure all text before the cursor, which is probably not ideal for performance, but there is lots of room for other performance improvements in HyDE.

This commit also removes line breaks before calculating cursor position to prevent other odd issues.

Resolves #128

* Fix non-py syntax hilighting
  • Loading branch information
echo-lalia authored Sep 3, 2024
1 parent 553ab3b commit 9af7e8f
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/launcher/HyDE.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def exit_options(target_file, overlay, editor):

def boot_into_file(target_file, overlay):
"""Restart and load into target file."""
overlay.draw_textbox("Restarting...", _MH_DISPLAY_WIDTH//2, _MH_DISPLAY_HEIGHT//2)
overlay.draw_textbox("Restarting...")
DISPLAY.show()

RTC.memory(target_file)
Expand All @@ -248,7 +248,7 @@ def boot_into_file(target_file, overlay):
def run_file_here(filepath, overlay, editor):
"""Try running the target file here"""
editor.save_file(filepath)
overlay.draw_textbox("Running...", _MH_DISPLAY_WIDTH//2, _MH_DISPLAY_HEIGHT//2)
overlay.draw_textbox("Running...")
DISPLAY.show()
try:
# you have to slice off the ".py" to avoid importerror
Expand Down Expand Up @@ -912,7 +912,13 @@ def get_total_width(self,line):
return width

def draw_cursor(self):
cursor_x = self.get_total_width(self.lines[self.cursor_index[1]][self.display_index[0]:self.cursor_index[0]+1]) + _LEFT_PADDING
line = format_display_line(
self.lines[self.cursor_index[1]][:self.cursor_index[0]]
)
cursor_x = self.get_total_width(line) \
- (self.display_index[0] * 8) \
+ _LEFT_PADDING

cursor_y = (_SMALL_FONT_HEIGHT * 3) + (self.cursor_index[1] - self.display_index[1] - 3) * _TEXT_HEIGHT
if time.ticks_ms() % _CURSOR_BLINK_MS < _CURSOR_BLINK_HALF:
DISPLAY.vline(cursor_x, cursor_y, 16, CONFIG.palette[9])
Expand All @@ -929,7 +935,7 @@ def draw_bg(self):

def save_file(self, filepath):
"""Reverse temporary formatting and Save the file."""
self.overlay.draw_textbox("Saving...",_MH_DISPLAY_WIDTH//2,_MH_DISPLAY_HEIGHT//2)
self.overlay.draw_textbox("Saving...")
DISPLAY.show()
with open(filepath,"w") as file:
for line in self.lines:
Expand Down Expand Up @@ -973,7 +979,7 @@ def del_line(self):
def main_loop():
"""Main loop of the program."""

global STR_COLOR, DARK_STR_COLOR, KEYWORD_COLOR, COMMENT_COLOR, DARK_COMMENT_COLOR, USE_TABS
global STR_COLOR, DARK_STR_COLOR, KEYWORD_COLOR, NUM_COLOR, OP_COLOR, COMMENT_COLOR, DARK_COMMENT_COLOR, USE_TABS

DISPLAY.fill(CONFIG.palette[2])
overlay = popup.UIOverlay()
Expand All @@ -985,11 +991,17 @@ def main_loop():
if target_file == "":
target_file = "/log.txt"

# remove syntax hilighting for plain txt files.
if target_file.endswith('.txt'):
STR_COLOR = CONFIG.palette[8]; DARK_STR_COLOR = CONFIG.palette[8]
# subtle syntax hilighting for non-py files.
if not target_file.endswith('.py'):
print('text file')
STR_COLOR = CONFIG.palette[7]
DARK_STR_COLOR = CONFIG.palette[6]
NUM_COLOR = CONFIG.palette[7]
OP_COLOR = CONFIG.palette[7]

KEYWORD_COLOR = CONFIG.palette[8]
COMMENT_COLOR = CONFIG.palette[8]; DARK_COMMENT_COLOR = CONFIG.palette[8]
COMMENT_COLOR = CONFIG.palette[8]
DARK_COMMENT_COLOR = CONFIG.palette[6]


try:
Expand Down

0 comments on commit 9af7e8f

Please sign in to comment.