From e58e5aeaa96df42ef9b11c2af786960fff1558d3 Mon Sep 17 00:00:00 2001 From: = <=> Date: Fri, 16 Jul 2021 01:20:36 +0100 Subject: [PATCH] Some renames and general cleanup. Added spaces after `cat` `map`, etc. --- src/calc.c | 131 +++++++++++++++++++++++++++-------------------------- 1 file changed, 66 insertions(+), 65 deletions(-) diff --git a/src/calc.c b/src/calc.c index b88e91e..1d877a5 100644 --- a/src/calc.c +++ b/src/calc.c @@ -5,38 +5,32 @@ #include "pebbleobject.h" #include "calc.h" -Window *s_menu_window; -Window *s_code_window; - -// Custom layers +// Custom layers that users can build Window *s_custom_window; TextLayer *s_heading_text_layer; char header[30] = ""; -// Code layers +// Code display +Window *s_code_window; TextLayer *s_input_text_layer; TextLayer *s_result_text_layer; -// Menu layers -MenuLayer *s_menu_layer; -TextLayer *s_list_message_layer; - -int current_code; - -// Currently selected button, starts on '(' -static int8_t selected_token = 1; +// Script selection +Window *s_script_menu_window; +MenuLayer *s_script_select_layer; +int current_script_num; // PebbLisp environment static struct Environment env; // Live code text -char mytext[SMAX_LENGTH] = ""; +char current_code_text[SMAX_LENGTH] = ""; // The actual displayed code text -char temptext[SMAX_LENGTH] = ""; +char displayed_code[SMAX_LENGTH] = ""; // The result of execution -char resulttext[RESULT_LENGTH] = ""; +char result_text[RESULT_LENGTH] = ""; const char *tokens[] = { " ", "(", ")", @@ -48,12 +42,15 @@ const char *tokens[] = { "= ", "< ", "> ", "\"", "s", - "cat", "map", "fil", - "fn", "def", "if", "\n", + "cat ", "map ", "fil ", + "fn ", "def ", "if ", "\n", "...", "END" }; +// Currently selected button, starts on '(' +static int8_t selected_token = 1; + const char *func_tokens[] = { "spent ", "window ", "max ", "min ", @@ -62,11 +59,11 @@ const char *func_tokens[] = { "sec ", "mnt ", "hr ", "hrt ", "vibe ", "sub " }; -const uint32_t inbox_size = 1024; -const uint32_t outbox_size = 1024; - 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 **/ @@ -85,35 +82,39 @@ static inline const char* getToken(int8_t n) 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 static void updateText() { const char *token = getToken(selected_token); - strcpy(temptext, mytext); + strcpy(displayed_code, current_code_text); - strcat(temptext, token[0] == ' ' ? "_": // Display space as underscore - token[0] == '\n'? "\\n": // Display newline as \n - token); // Display others literally + strcat(displayed_code, + token[0] == ' ' ? "_": // Display space as underscore + 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 static void cycle_tokens(ClickRecognizerRef recognizer, void *context) { // 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++; - else + } else { selected_token--; + } // If selected token is outside of range, wrap around - if(selected_token < 0) + if(selected_token < 0) { selected_token = tokenCount() - 1; - else if(selected_token >= tokenCount()) + } else if(selected_token >= tokenCount()) { selected_token = 0; + } updateText(); } @@ -132,8 +133,8 @@ static void adjustFont() { int i = 0; int size = 0; - while(temptext[i++]) { - if(temptext[i] == '\n') + while(displayed_code[i++]) { + if(displayed_code[i] == '\n') size += 10; size++; } @@ -171,10 +172,10 @@ static void click_backspace(ClickRecognizerRef recognizer, void *context) } if(!using_func_tokens) { - if(mytext[0] == '\0') { + if(current_code_text[0] == '\0') { window_stack_remove(window_stack_get_top_window(), true); } else { - mytext[strlen(mytext) - 1] = '\0'; + current_code_text[strlen(current_code_text) - 1] = '\0'; updateText(); } } else { @@ -184,10 +185,10 @@ static void click_backspace(ClickRecognizerRef recognizer, void *context) adjustFont(); } -// Adds the current string to the main string +// Adds the current token string to the main code string static void add_token() { - strcat(mytext, getToken(selected_token)); + strcat(current_code_text, getToken(selected_token)); selected_token = 0; if(using_func_tokens) { using_func_tokens = false; @@ -202,12 +203,12 @@ static void add_token() // Calculate result, display it and reset static void calculate() { - Object obj = parseEval(mytext, &env); + Object obj = parseEval(current_code_text, &env); char temp[RESULT_LENGTH-2] = ""; stringObj(temp, &obj); - snprintf(resulttext, RESULT_LENGTH, "R:%s", temp); - text_layer_set_text(s_result_text_layer, resulttext); + snprintf(result_text, RESULT_LENGTH, "R:%s", temp); + text_layer_set_text(s_result_text_layer, result_text); } // Button press handler @@ -227,12 +228,12 @@ static void click_select(ClickRecognizerRef recognizer, void *context) // Saves text in editor to persistent storage 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++) { - 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); } @@ -286,12 +287,12 @@ static void code_window_load(Window *window) // If possible, load the previous code text #ifdef FORCE_TEXT - strncpy(mytext, FORCE_TEXT, SMAX_LENGTH); + strncpy(current_code_text, FORCE_TEXT, SMAX_LENGTH); updateText(); adjustFont(); #else - if(persist_exists(current_code)) { - persist_read_string(current_code, mytext, SMAX_LENGTH); + if(persist_exists(current_script_num)) { + persist_read_string(current_script_num, current_code_text, SMAX_LENGTH); updateText(); adjustFont(); } @@ -299,8 +300,8 @@ static void code_window_load(Window *window) } void debug_result(const char* d) { - snprintf(resulttext, RESULT_LENGTH, "%s", d); - text_layer_set_text(s_result_text_layer, resulttext); + snprintf(result_text, RESULT_LENGTH, "%s", d); + text_layer_set_text(s_result_text_layer, result_text); 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) { // 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_input_text_layer); @@ -346,7 +347,7 @@ void code_window_push() static void select_callback(struct MenuLayer *menu_layer, MenuIndex *cell_index, void *context) { - current_code = cell_index->row; + current_script_num = cell_index->row; code_window_push(); } @@ -377,26 +378,26 @@ static void menu_load(Window *window) Layer *window_layer = window_get_root_layer(window); GRect bounds = layer_get_bounds(window_layer); - s_menu_layer = menu_layer_create(bounds); - menu_layer_set_click_config_onto_window(s_menu_layer, window); + s_script_select_layer = menu_layer_create(bounds); + menu_layer_set_click_config_onto_window(s_script_select_layer, window); #if defined(PBL_COLOR) - menu_layer_set_normal_colors(s_menu_layer, GColorBlack, GColorWhite); - menu_layer_set_highlight_colors(s_menu_layer, GColorDukeBlue, GColorWhite); + menu_layer_set_normal_colors(s_script_select_layer, GColorBlack, GColorWhite); + menu_layer_set_highlight_colors(s_script_select_layer, GColorDukeBlue, GColorWhite); #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, .draw_row = draw_row_callback, .get_cell_height = get_cell_height_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) { - menu_layer_destroy(s_menu_layer); + menu_layer_destroy(s_script_select_layer); } /** Custom Window **/ @@ -417,7 +418,7 @@ static void custom_load(Window *window) layer_add_child(window_get_root_layer(s_custom_window), 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); } @@ -445,12 +446,12 @@ static void inbox_received_callback(DictionaryIterator *iter, void *context) { Tuple *script_tuple = dict_find(iter, MESSAGE_KEY_ScriptText); if(script_tuple) { if(!s_code_window) { - current_code = 0; + current_script_num = 0; code_window_push(); } char *script_text = script_tuple->value->cstring; - snprintf(temptext, sizeof(temptext), "%s", script_text); - snprintf(mytext, sizeof(mytext), "%s", script_text); + snprintf(displayed_code, sizeof(displayed_code), "%s", script_text); + snprintf(current_code_text, sizeof(current_code_text), "%s", script_text); updateText(); } } @@ -479,12 +480,12 @@ static struct Environment pebbleEnv() static void init(void) { env = pebbleEnv(); - s_menu_window = window_create(); - window_set_window_handlers(s_menu_window, (WindowHandlers) { + s_script_menu_window = window_create(); + window_set_window_handlers(s_script_menu_window, (WindowHandlers) { .load = menu_load, .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_register_inbox_received(inbox_received_callback); } @@ -493,7 +494,7 @@ static void deinit(void) { deleteEnv(&env); text_layer_destroy(s_input_text_layer); - window_destroy(s_menu_window); + window_destroy(s_script_menu_window); } int main(void)