Some renames and general cleanup.

Added spaces after `cat` `map`, etc.
This commit is contained in:
= 2021-07-16 01:20:36 +01:00
parent bdd8c3fae3
commit e58e5aeaa9
1 changed files with 66 additions and 65 deletions

View File

@ -5,38 +5,32 @@
#include "pebbleobject.h" #include "pebbleobject.h"
#include "calc.h" #include "calc.h"
Window *s_menu_window; // Custom layers that users can build
Window *s_code_window;
// Custom layers
Window *s_custom_window; Window *s_custom_window;
TextLayer *s_heading_text_layer; TextLayer *s_heading_text_layer;
char header[30] = ""; char header[30] = "";
// Code layers // Code display
Window *s_code_window;
TextLayer *s_input_text_layer; TextLayer *s_input_text_layer;
TextLayer *s_result_text_layer; TextLayer *s_result_text_layer;
// Menu layers // Script selection
MenuLayer *s_menu_layer; Window *s_script_menu_window;
TextLayer *s_list_message_layer; MenuLayer *s_script_select_layer;
int current_script_num;
int current_code;
// Currently selected button, starts on '('
static int8_t selected_token = 1;
// PebbLisp environment // PebbLisp environment
static struct Environment env; static struct Environment env;
// Live code text // Live code text
char mytext[SMAX_LENGTH] = ""; char current_code_text[SMAX_LENGTH] = "";
// The actual displayed code text // The actual displayed code text
char temptext[SMAX_LENGTH] = ""; char displayed_code[SMAX_LENGTH] = "";
// The result of execution // The result of execution
char resulttext[RESULT_LENGTH] = ""; char result_text[RESULT_LENGTH] = "";
const char *tokens[] = { const char *tokens[] = {
" ", "(", ")", " ", "(", ")",
@ -48,12 +42,15 @@ const char *tokens[] = {
"= ", "< ", "> ", "= ", "< ", "> ",
"\"", "\"",
"s", "s",
"cat", "map", "fil", "cat ", "map ", "fil ",
"fn", "def", "if", "\n", "fn ", "def ", "if ", "\n",
"...", "...",
"END" "END"
}; };
// Currently selected button, starts on '('
static int8_t selected_token = 1;
const char *func_tokens[] = { const char *func_tokens[] = {
"spent ", "window ", "spent ", "window ",
"max ", "min ", "max ", "min ",
@ -62,11 +59,11 @@ const char *func_tokens[] = {
"sec ", "mnt ", "hr ", "hrt ", "vibe ", "sub " "sec ", "mnt ", "hr ", "hrt ", "vibe ", "sub "
}; };
const uint32_t inbox_size = 1024;
const uint32_t outbox_size = 1024;
bool using_func_tokens = false; bool using_func_tokens = false;
// Size of messages to/from the phone app
const uint32_t inbox_size = 1024;
const uint32_t outbox_size = 1024;
/** Text Editing **/ /** Text Editing **/
@ -85,35 +82,39 @@ static inline const char* getToken(int8_t n)
return using_func_tokens? func_tokens[t] : tokens[t]; return using_func_tokens? func_tokens[t] : tokens[t];
} }
// Update the current code text with the contents of `mytext`, // Update the current code text with the contents of `current_code_text`,
// and add the current selected_token to the end // and add the current selected_token to the end
static void updateText() static void updateText()
{ {
const char *token = getToken(selected_token); const char *token = getToken(selected_token);
strcpy(temptext, mytext); strcpy(displayed_code, current_code_text);
strcat(temptext, token[0] == ' ' ? "_": // Display space as underscore strcat(displayed_code,
token[0] == '\n'? "\\n": // Display newline as \n token[0] == ' ' ? "_": // Display space as underscore
token); // Display others literally token[0] == '\n'? "\\n": // Display newline as \n
token // Display others literally
);
text_layer_set_text(s_input_text_layer, temptext); text_layer_set_text(s_input_text_layer, displayed_code);
} }
// Cycle through the current list of tokens // Cycle through the current list of tokens
static void cycle_tokens(ClickRecognizerRef recognizer, void *context) static void cycle_tokens(ClickRecognizerRef recognizer, void *context)
{ {
// Change current token // Change current token
if(click_recognizer_get_button_id(recognizer) == BUTTON_ID_DOWN) if(click_recognizer_get_button_id(recognizer) == BUTTON_ID_DOWN) {
selected_token++; selected_token++;
else } else {
selected_token--; selected_token--;
}
// If selected token is outside of range, wrap around // If selected token is outside of range, wrap around
if(selected_token < 0) if(selected_token < 0) {
selected_token = tokenCount() - 1; selected_token = tokenCount() - 1;
else if(selected_token >= tokenCount()) } else if(selected_token >= tokenCount()) {
selected_token = 0; selected_token = 0;
}
updateText(); updateText();
} }
@ -132,8 +133,8 @@ static void adjustFont()
{ {
int i = 0; int i = 0;
int size = 0; int size = 0;
while(temptext[i++]) { while(displayed_code[i++]) {
if(temptext[i] == '\n') if(displayed_code[i] == '\n')
size += 10; size += 10;
size++; size++;
} }
@ -171,10 +172,10 @@ static void click_backspace(ClickRecognizerRef recognizer, void *context)
} }
if(!using_func_tokens) { if(!using_func_tokens) {
if(mytext[0] == '\0') { if(current_code_text[0] == '\0') {
window_stack_remove(window_stack_get_top_window(), true); window_stack_remove(window_stack_get_top_window(), true);
} else { } else {
mytext[strlen(mytext) - 1] = '\0'; current_code_text[strlen(current_code_text) - 1] = '\0';
updateText(); updateText();
} }
} else { } else {
@ -184,10 +185,10 @@ static void click_backspace(ClickRecognizerRef recognizer, void *context)
adjustFont(); adjustFont();
} }
// Adds the current string to the main string // Adds the current token string to the main code string
static void add_token() static void add_token()
{ {
strcat(mytext, getToken(selected_token)); strcat(current_code_text, getToken(selected_token));
selected_token = 0; selected_token = 0;
if(using_func_tokens) { if(using_func_tokens) {
using_func_tokens = false; using_func_tokens = false;
@ -202,12 +203,12 @@ static void add_token()
// Calculate result, display it and reset // Calculate result, display it and reset
static void calculate() static void calculate()
{ {
Object obj = parseEval(mytext, &env); Object obj = parseEval(current_code_text, &env);
char temp[RESULT_LENGTH-2] = ""; char temp[RESULT_LENGTH-2] = "";
stringObj(temp, &obj); stringObj(temp, &obj);
snprintf(resulttext, RESULT_LENGTH, "R:%s", temp); snprintf(result_text, RESULT_LENGTH, "R:%s", temp);
text_layer_set_text(s_result_text_layer, resulttext); text_layer_set_text(s_result_text_layer, result_text);
} }
// Button press handler // Button press handler
@ -227,12 +228,12 @@ static void click_select(ClickRecognizerRef recognizer, void *context)
// Saves text in editor to persistent storage // Saves text in editor to persistent storage
static void click_save(ClickRecognizerRef recognizer, void *context) static void click_save(ClickRecognizerRef recognizer, void *context)
{ {
int8_t i = strlen(temptext); int8_t i = strlen(displayed_code);
for(unsigned j = 0; j < strlen(getToken(selected_token)); j++) { for(unsigned j = 0; j < strlen(getToken(selected_token)); j++) {
temptext[i-(1 + j)] = '\0'; displayed_code[i-(1 + j)] = '\0';
} }
persist_write_string(current_code, temptext); persist_write_string(current_script_num, displayed_code);
window_stack_pop(true); window_stack_pop(true);
} }
@ -286,12 +287,12 @@ static void code_window_load(Window *window)
// If possible, load the previous code text // If possible, load the previous code text
#ifdef FORCE_TEXT #ifdef FORCE_TEXT
strncpy(mytext, FORCE_TEXT, SMAX_LENGTH); strncpy(current_code_text, FORCE_TEXT, SMAX_LENGTH);
updateText(); updateText();
adjustFont(); adjustFont();
#else #else
if(persist_exists(current_code)) { if(persist_exists(current_script_num)) {
persist_read_string(current_code, mytext, SMAX_LENGTH); persist_read_string(current_script_num, current_code_text, SMAX_LENGTH);
updateText(); updateText();
adjustFont(); adjustFont();
} }
@ -299,8 +300,8 @@ static void code_window_load(Window *window)
} }
void debug_result(const char* d) { void debug_result(const char* d) {
snprintf(resulttext, RESULT_LENGTH, "%s", d); snprintf(result_text, RESULT_LENGTH, "%s", d);
text_layer_set_text(s_result_text_layer, resulttext); text_layer_set_text(s_result_text_layer, result_text);
psleep(300); psleep(300);
} }
@ -319,7 +320,7 @@ static Object run_script(Object script_num, Object obj, struct Environment *e) {
static void code_window_unload(Window *window) static void code_window_unload(Window *window)
{ {
// Save the current code text // Save the current code text
persist_write_string(current_code, temptext); persist_write_string(current_script_num, displayed_code);
text_layer_destroy(s_result_text_layer); text_layer_destroy(s_result_text_layer);
text_layer_destroy(s_input_text_layer); text_layer_destroy(s_input_text_layer);
@ -346,7 +347,7 @@ void code_window_push()
static void select_callback(struct MenuLayer *menu_layer, static void select_callback(struct MenuLayer *menu_layer,
MenuIndex *cell_index, void *context) MenuIndex *cell_index, void *context)
{ {
current_code = cell_index->row; current_script_num = cell_index->row;
code_window_push(); code_window_push();
} }
@ -377,26 +378,26 @@ static void menu_load(Window *window)
Layer *window_layer = window_get_root_layer(window); Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer); GRect bounds = layer_get_bounds(window_layer);
s_menu_layer = menu_layer_create(bounds); s_script_select_layer = menu_layer_create(bounds);
menu_layer_set_click_config_onto_window(s_menu_layer, window); menu_layer_set_click_config_onto_window(s_script_select_layer, window);
#if defined(PBL_COLOR) #if defined(PBL_COLOR)
menu_layer_set_normal_colors(s_menu_layer, GColorBlack, GColorWhite); menu_layer_set_normal_colors(s_script_select_layer, GColorBlack, GColorWhite);
menu_layer_set_highlight_colors(s_menu_layer, GColorDukeBlue, GColorWhite); menu_layer_set_highlight_colors(s_script_select_layer, GColorDukeBlue, GColorWhite);
#endif #endif
menu_layer_set_callbacks(s_menu_layer, NULL, (MenuLayerCallbacks) { menu_layer_set_callbacks(s_script_select_layer, NULL, (MenuLayerCallbacks) {
.get_num_rows = get_num_rows_callback, .get_num_rows = get_num_rows_callback,
.draw_row = draw_row_callback, .draw_row = draw_row_callback,
.get_cell_height = get_cell_height_callback, .get_cell_height = get_cell_height_callback,
.select_click = select_callback, .select_click = select_callback,
}); });
layer_add_child(window_layer, menu_layer_get_layer(s_menu_layer)); layer_add_child(window_layer, menu_layer_get_layer(s_script_select_layer));
} }
static void menu_unload(Window *window) static void menu_unload(Window *window)
{ {
menu_layer_destroy(s_menu_layer); menu_layer_destroy(s_script_select_layer);
} }
/** Custom Window **/ /** Custom Window **/
@ -417,7 +418,7 @@ static void custom_load(Window *window)
layer_add_child(window_get_root_layer(s_custom_window), layer_add_child(window_get_root_layer(s_custom_window),
text_layer_get_layer(s_heading_text_layer)); text_layer_get_layer(s_heading_text_layer));
// Push the window, setting the window animation to 'true' // Push the window with animations enabled
window_stack_push(s_custom_window, true); window_stack_push(s_custom_window, true);
} }
@ -445,12 +446,12 @@ static void inbox_received_callback(DictionaryIterator *iter, void *context) {
Tuple *script_tuple = dict_find(iter, MESSAGE_KEY_ScriptText); Tuple *script_tuple = dict_find(iter, MESSAGE_KEY_ScriptText);
if(script_tuple) { if(script_tuple) {
if(!s_code_window) { if(!s_code_window) {
current_code = 0; current_script_num = 0;
code_window_push(); code_window_push();
} }
char *script_text = script_tuple->value->cstring; char *script_text = script_tuple->value->cstring;
snprintf(temptext, sizeof(temptext), "%s", script_text); snprintf(displayed_code, sizeof(displayed_code), "%s", script_text);
snprintf(mytext, sizeof(mytext), "%s", script_text); snprintf(current_code_text, sizeof(current_code_text), "%s", script_text);
updateText(); updateText();
} }
} }
@ -479,12 +480,12 @@ static struct Environment pebbleEnv()
static void init(void) static void init(void)
{ {
env = pebbleEnv(); env = pebbleEnv();
s_menu_window = window_create(); s_script_menu_window = window_create();
window_set_window_handlers(s_menu_window, (WindowHandlers) { window_set_window_handlers(s_script_menu_window, (WindowHandlers) {
.load = menu_load, .load = menu_load,
.unload = menu_unload .unload = menu_unload
}); });
window_stack_push(s_menu_window, true); window_stack_push(s_script_menu_window, true);
app_message_open(inbox_size, outbox_size); app_message_open(inbox_size, outbox_size);
app_message_register_inbox_received(inbox_received_callback); app_message_register_inbox_received(inbox_received_callback);
} }
@ -493,7 +494,7 @@ static void deinit(void)
{ {
deleteEnv(&env); deleteEnv(&env);
text_layer_destroy(s_input_text_layer); text_layer_destroy(s_input_text_layer);
window_destroy(s_menu_window); window_destroy(s_script_menu_window);
} }
int main(void) int main(void)