Skip to content

Commit

Permalink
fixes for editor
Browse files Browse the repository at this point in the history
  • Loading branch information
bwhitman committed Jun 10, 2024
1 parent fdb50f7 commit 649d80e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/tulip_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Tulip ships with a text editor, based on pico/nano. It supports syntax highlight
# Control-X saves the file, if no filename give will prompt for one.
# Control-O is save as -- will write to new filename given
# Control-W searches
# Control-R prompts for a filename to read into the current buffer
edit("game.py")
edit() # no filename
```
Expand Down
18 changes: 14 additions & 4 deletions tulip/shared/editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,15 @@ void editor_open_file(const char *filename) {
//dbg("opening file %s\n", filename);
int32_t fs = file_size(filename);
if(fs > 0) {
char * text = (char*) editor_malloc(fs+2);
char * text = (char*) editor_malloc(fs+3);
uint32_t bytes_read = read_file(filename, (uint8_t*)text, fs, 0);
text[fs-1] = 0;
dbg("Filesize %d bytes read %d\n", fs, bytes_read);
// if the last char is not a \n then add it.
new_lines = 1;
if(text[bytes_read-1]!='\n') { new_lines = 0; dbg("adding n\n"); text[bytes_read] = '\n'; bytes_read++; }
text[bytes_read] = 0; // end
for(uint32_t i=0;i<bytes_read;i++) if(text[i]=='\n') new_lines++;
//dbg("File %s has %d lines\n", filename, new_lines);
dbg("File %s has %d lines. %d bytes\n", filename, new_lines,bytes_read);
char ** incoming_text_lines = (char**)editor_malloc(sizeof(char*)*(new_lines));

uint32_t last = 0;
Expand All @@ -337,30 +340,37 @@ void editor_open_file(const char *filename) {
incoming_text_lines[c][x] = text[last+x];
}
incoming_text_lines[c][x] = 0;
dbg("itl %d is %s\n", c, incoming_text_lines[c]);
last = i+1;
c++;
}
}
editor_free(text);
//dbg("File %s read with %d lines. Inserting at position %d\n", filename, new_lines, y_offset+cursor_y);

dbg("File %s read with %d lines. Inserting at position %d. existing lines %d\n", filename, new_lines, y_offset+cursor_y, lines);

// Now insert the text lines at cursor_y+y_offset
char ** combined_text_lines = (char**)editor_malloc(sizeof(char*)*(new_lines+lines));
uint16_t line = 0;
for(uint16_t y=0;y<cursor_y+y_offset;y++) {
combined_text_lines[line++] = text_lines[y];
}
dbg("a\n");
for(uint16_t y=0;y<new_lines;y++) {
combined_text_lines[line++] = incoming_text_lines[y];
}
dbg("b\n");
for(uint16_t y=cursor_y+y_offset;y<lines;y++) {
combined_text_lines[line++] = text_lines[y];
}
dbg("c\n");
if(lines) {
editor_free(text_lines);
}
dbg("d\n");

editor_free(incoming_text_lines);
dbg("e\n");
text_lines = combined_text_lines;
lines = lines + new_lines;
} else {
Expand Down
18 changes: 16 additions & 2 deletions tulip/shared/py/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
# That will let us do LVGL stuff for saving/searching/etc

import tulip
editor = None


def draw(x):
global editor
editor.alttab_button.invalidate()
editor.quit_button.invalidate()

class Editor(tulip.UIScreen):
def __init__(self, filename):
Expand All @@ -20,7 +27,7 @@ def deactivate_editor_cb(self, screen):
tulip.keyboard_callback()
tulip.tfb_restore()
# Fudge the repl line as it got eaten during the TFB restore. This will never be a problem, lol
print(">>> ",end='')
#print(">>> ",end='')

def quit_editor_cb(self, screen):
tulip.keyboard_callback()
Expand All @@ -42,7 +49,14 @@ def activate_editor_cb(self,screen):
# overwriting the first line. So wait a bit and activate then
# (This also means the >>> will print on the alternate TFB, so we have to fudge on reactivate)
tulip.defer(tulip.activate_editor, None, 50)
# And because of the TFB clearing, the buttons may get destroyed, so re-draw them
tulip.defer(draw, None, 100)

# Launch the tulip editor as a UIScreen
def edit(filename=None):
editor = Editor(filename)
global editor
if 'edit' in tulip.running_apps:
print("Editor already running.")
return
else:
editor = Editor(filename)

0 comments on commit 649d80e

Please sign in to comment.