From 4e6b788ca953db129579d9f7fca95192e483efe1 Mon Sep 17 00:00:00 2001 From: nots1dd Date: Sat, 31 Aug 2024 22:37:32 +0530 Subject: [PATCH] [REFACTOR] CHANGELOG v2.7.1 --- CHANGELOG/v2_7_1.md | 13 ++ include/filepreview.h | 3 +- include/kbinput.h | 14 +- keywords/css-keywords.yaml | 162 ++++++++++++++++++++ keywords/html-keywords.yaml | 160 ++++++++++++++++++++ keywords/js-keywords.yaml | 107 +++++++++++++ keywords/py-keywords.yaml | 6 - lfm.c | 2 +- src/filepreview.c | 127 ++++++++++------ src/highlight.c | 20 ++- src/kbinput.c | 291 ++++++++++++++++++++---------------- 11 files changed, 720 insertions(+), 185 deletions(-) create mode 100644 CHANGELOG/v2_7_1.md create mode 100644 keywords/css-keywords.yaml create mode 100644 keywords/html-keywords.yaml create mode 100644 keywords/js-keywords.yaml diff --git a/CHANGELOG/v2_7_1.md b/CHANGELOG/v2_7_1.md new file mode 100644 index 0000000..d3fa67c --- /dev/null +++ b/CHANGELOG/v2_7_1.md @@ -0,0 +1,13 @@ +# CHANGE TYPE: MEDIUM + +## Summary of Changes + +-> A continuation to v2.7 in terms of refactoring + +-> Fixed synhash issue of string error (strings hashtable occurance would toggle the boolean, leading to acceptable strings like "'#hello'" to give weird output) + +-> Fixed some mime file type defs + +-> Added more keywords.yaml files + +-> Fixed the file info popup not showing up for readable files diff --git a/include/filepreview.h b/include/filepreview.h index 9dcd4c8..a53821f 100644 --- a/include/filepreview.h +++ b/include/filepreview.h @@ -62,7 +62,8 @@ #define MIME_TEXT_C "text/x-c" #define MIME_TEXT_CPP "text/x-c++" #define MIME_TEXT_PYTHON "text/x-script.python" -#define MIME_TEXT_JAVA "text/x-java-source" +#define MIME_TEXT_JAVA "text/x-java" +#define MIME_TEXT_JAVASCRIPT "application/javascript" #define MIME_APPLICATION_OCTET_STREAM "application/octet-stream" #define MIME_TEXT_MAKEFILE "text/x-makefile" #define MIME_TEXT_HTML "text/html" diff --git a/include/kbinput.h b/include/kbinput.h index 279dd12..2b7a139 100644 --- a/include/kbinput.h +++ b/include/kbinput.h @@ -16,7 +16,7 @@ * * License: * - * Notes: Some function calls that require a global scope need + * Notes: Some function calls that require a global scope need * proper implementation * * Revision History: @@ -34,15 +34,19 @@ #include #define MAX_HISTORY 256 -#define UNICODE_SEARCH "🔍" +#define UNICODE_SEARCH "🔍" void handleInputScrollUp(int* highlight, int* scroll_position); void handleInputScrollDown(int* highlight, int* scroll_position, int* item_count, int* height); void handleInputToggleHidden(int* show_hidden, int* scroll_position, int* highlight); void handleInputMovCursBtm(int* highlight, int* item_count, int* scroll_position, int* max_y); -int find_item(const char *query, FileItem items[], int *item_count, int *start_index, int direction); -void handleInputStringSearch(WINDOW* win, FileItem items[], int* item_count, int* highlight, int* scroll_position, int *height, char* last_query, const char* current_path); -void handleInputStringOccurance(int direction, const char* last_query, FileItem items[], int* item_count, int* highlight, int* scroll_position, int *height); +int find_item(const char* query, FileItem items[], int* item_count, int* start_index, + int direction); +void handleInputStringSearch(WINDOW* win, FileItem items[], int* item_count, int* highlight, + int* scroll_position, int* height, char* last_query, + const char* current_path); +void handleInputStringOccurance(int direction, const char* last_query, FileItem items[], + int* item_count, int* highlight, int* scroll_position, int* height); /* ------------------------------ * void handleInputScopeBack(int* history_count, int* highlight, int* scroll_position, const char* * current_path, DirHistory history[]); void handleInputScopeForward(WINDOW *win, WINDOW *info_win, diff --git a/keywords/css-keywords.yaml b/keywords/css-keywords.yaml new file mode 100644 index 0000000..3c3505c --- /dev/null +++ b/keywords/css-keywords.yaml @@ -0,0 +1,162 @@ +keywords: + - color + - background + - background-color + - background-image + - background-repeat + - background-position + - background-size + - font + - font-family + - font-size + - font-weight + - font-style + - font-variant + - line-height + - text-align + - text-decoration + - text-transform + - letter-spacing + - word-spacing + - white-space + - list-style + - list-style-type + - list-style-position + - list-style-image + - margin + - margin-top + - margin-right + - margin-bottom + - margin-left + - padding + - padding-top + - padding-right + - padding-bottom + - padding-left + - border + - border-width + - border-style + - border-color + - border-radius + - outline + - outline-width + - outline-style + - outline-color + - width + - height + - max-width + - max-height + - min-width + - min-height + - position + - top + - right + - bottom + - left + - z-index + - display + - float + - clear + - overflow + - overflow-x + - overflow-y + - visibility + - content + - cursor + - opacity + - transition + - transform + - animation + - keyframes + - box-shadow + - text-shadow + - border-collapse + - border-spacing + - caption-side + - empty-cells + - table-layout + - direction + - unicode-bidi + - vertical-align + - background-clip + - background-origin + - background-attachment + - grid + - grid-template + - grid-template-rows + - grid-template-columns + - grid-template-areas + - grid-row + - grid-column + - grid-area + - grid-auto-rows + - grid-auto-columns + - grid-auto-flow + - grid-gap + - grid-row-gap + - grid-column-gap + - align-content + - align-items + - align-self + - justify-content + - justify-items + - justify-self + - flex + - flex-basis + - flex-direction + - flex-flow + - flex-grow + - flex-shrink + - flex-wrap + - align-content + - justify-content + - order + +functions: + - 'rgb' + - 'rgba' + - 'hsl' + - 'hsla' + - 'calc' + - 'attr' + - 'url' + - 'min' + - 'max' + - 'clamp' + - 'linear-gradient' + - 'radial-gradient' + - 'repeating-linear-gradient' + - 'repeating-radial-gradient' + +operators: + - '+' + - '-' + - '*' + - '/' + - '%' + - '=' + - '>' + - '<' + - '!' + +symbols: + - '{' + - '}' + - ':' + - ';' + - '(' + - ')' + - '[' + - ']' + - ',' + +multicomments1: + - '/' + +multicomments2: + - '*' + +strings: + - "\"" + - "'" + - "`" diff --git a/keywords/html-keywords.yaml b/keywords/html-keywords.yaml new file mode 100644 index 0000000..7995923 --- /dev/null +++ b/keywords/html-keywords.yaml @@ -0,0 +1,160 @@ +keywords: + - html + - '' + - '' + - base + - '<link>' + - '<meta>' + - '<style>' + - '<script>' + - noscript + - body + - section + - nav + - article + - aside + - h1 + - h2 + - h3 + - h4 + - h5 + - h6 + - header + - footer + - address + - main + - p + - hr + - pre + - blockquote + - ol + - ul + - li + - dl + - dt + - dd + - figure + - figcaption + - div + - a + - em + - strong + - small + - s + - cite + - q + - dfn + - abbr + - ruby + - rt + - rp + - data + - time + - code + - var + - samp + - kbd + - sub + - sup + - i + - b + - u + - mark + - bdi + - bdo + - span + - br + - wbr + - ins + - del + - img + - iframe + - embed + - object + - param + - video + - audio + - source + - track + - map + - area + - table + - caption + - colgroup + - col + - tbody + - thead + - tfoot + - tr + - td + - th + - form + - label + - input + - button + - select + - datalist + - optgroup + - option + - textarea + - output + - progress + - meter + - fieldset + - legend + - details + - summary + - dialog + - script + - noscript + - template + - canvas + - svg + - math + +functions: + - 'document' + - 'window' + - 'alert' + - 'getElementById' + - 'querySelector' + - 'addEventListener' + - 'removeEventListener' + - 'createElement' + - 'appendChild' + - 'setAttribute' + - 'getAttribute' + - 'removeAttribute' + - 'classList' + - 'innerHTML' + - 'innerText' + - 'style' + - 'parentElement' + - 'firstChild' + - 'lastChild' + - 'nextSibling' + - 'previousSibling' + - 'insertBefore' + - 'replaceChild' + - 'removeChild' + - 'cloneNode' + - 'append' + +operators: + - '=' + - '/' + +symbols: + - '/>' + - '=' + +singlecomments: + - '<!--' + +singlecommentslen: + - '4' + +strings: + - "\"" + - "'" + - "`" diff --git a/keywords/js-keywords.yaml b/keywords/js-keywords.yaml new file mode 100644 index 0000000..6bdbb3f --- /dev/null +++ b/keywords/js-keywords.yaml @@ -0,0 +1,107 @@ +keywords: + - break + - case + - catch + - class + - const + - continue + - debugger + - default + - delete + - do + - else + - enum + - export + - extends + - finally + - for + - function + - if + - import + - in + - instanceof + - let + - new + - 'null' + - return + - super + - switch + - this + - throw + - try + - typeof + - var + - void + - while + - with + - yield + - async + - await + +functions: + - 'console' + - 'log' + - 'parseInt' + - 'parseFloat' + - 'String' + - 'Number' + - 'Boolean' + - 'Array' + - 'Object' + - 'Function' + - 'Date' + - 'Math' + - 'setTimeout' + - 'setInterval' + - 'clearTimeout' + - 'clearInterval' + - 'Promise' + - 'async' + - 'await' + +operators: + - '+' + - '-' + - '*' + - '/' + - '%' + - '=' + - '<' + - '>' + - '!' + - '&' + - '|' + - '^' + - '~' + - '?' + - ':' + +symbols: + - '(' + - ')' + - '{' + - '}' + - '[' + - ']' + - '.' + - ',' + - ';' + - '=' + - '@' + +singlecomments: + - '/' + +singlecommentslen: + - '2' + +multicomments1: + - '/' + +multicomments2: + - '*' + +strings: + - "\"" + - "'" + - "`" diff --git a/keywords/py-keywords.yaml b/keywords/py-keywords.yaml index ffe1cc5..47d9d5b 100644 --- a/keywords/py-keywords.yaml +++ b/keywords/py-keywords.yaml @@ -91,12 +91,6 @@ singlecomments: singlecommentslen: - '1' -multicomments1: - - "\"" - -multicomments2: - - "\"\"" - strings: - "\"" - "'" diff --git a/lfm.c b/lfm.c index e8946da..1f6cffc 100644 --- a/lfm.c +++ b/lfm.c @@ -826,7 +826,7 @@ int main(int argc, char* argv[]) { scroll_position = 0; break; } else { - if (strcmp(is_readable_extension(items[highlight].name, current_path), "READ") != 0) { + if (strcmp(is_readable_extension(items[highlight].name, current_path), "READ") == 0) { get_file_info_popup(win, current_path, items[highlight].name); } else { log_message(LOG_LEVEL_DEBUG, "Something went wrong..."); diff --git a/src/filepreview.c b/src/filepreview.c index 72e1a09..23a7e7e 100644 --- a/src/filepreview.c +++ b/src/filepreview.c @@ -144,7 +144,7 @@ const char* empty_message[] = {" _____ __ __ ____ _______ __ _____ ___ _ "|_____|_| |_|_| |_| |_| |_| |___|_____|_____| (_)", " "}; -const char* get_keywords_file(const char* ext) +const char* get_keywords_file(const char* mime_type) { static char keywords_file[256]; char* project_dir; @@ -156,21 +156,55 @@ const char* get_keywords_file(const char* ext) // Get the directory where this source file is located project_dir = dirname(resolved_path); - // Map multiple extensions to the same YAML file - if (strcmp(ext, "h") == 0 || strcmp(ext, "hpp") == 0 || strcmp(ext, "cpp") == 0 || - strcmp(ext, "c") == 0 || strcmp(ext, "cxx") == 0) + // Map MIME types to the corresponding YAML file + if (strcmp(mime_type, MIME_TEXT_C) == 0 || strcmp(mime_type, MIME_TEXT_CPP) == 0) { snprintf(keywords_file, sizeof(keywords_file), "%s/../keywords/c-keywords.yaml", project_dir); } - else if (strcmp(ext, "toml") == 0 || strcmp(ext, "lock") == 0) + else if (strcmp(mime_type, MIME_APPLICATION_JSON) == 0) { snprintf(keywords_file, sizeof(keywords_file), "%s/../keywords/json-keywords.yaml", project_dir); } + else if (strcmp(mime_type, MIME_TEXT_JAVASCRIPT) == 0) + { + snprintf(keywords_file, sizeof(keywords_file), "%s/../keywords/js-keywords.yaml", + project_dir); + } + else if (strcmp(mime_type, MIME_TEXT_PYTHON) == 0) + { + snprintf(keywords_file, sizeof(keywords_file), "%s/../keywords/py-keywords.yaml", + project_dir); + } + else if (strcmp(mime_type, MIME_TEXT_HTML) == 0) + { + snprintf(keywords_file, sizeof(keywords_file), "%s/../keywords/html-keywords.yaml", + project_dir); + } + else if (strcmp(mime_type, MIME_TEXT_CSS) == 0) + { + snprintf(keywords_file, sizeof(keywords_file), "%s/../keywords/css-keywords.yaml", + project_dir); + } + else if (strcmp(mime_type, MIME_TEXT_SHELLSCRIPT) == 0) + { + snprintf(keywords_file, sizeof(keywords_file), "%s/../keywords/sh-keywords.yaml", + project_dir); + } + else if (strcmp(mime_type, MIME_TEXT_JAVA) == 0) + { + snprintf(keywords_file, sizeof(keywords_file), "%s/../keywords/java-keywords.yaml", + project_dir); + } + /*else if (strcmp(mime_type, MIME_TEXT_MAKEFILE) == 0)*/ + /*{*/ + /* snprintf(keywords_file, sizeof(keywords_file), "%s/../keywords/makefile-keywords.yaml", + * project_dir);*/ + /*}*/ else { - snprintf(keywords_file, sizeof(keywords_file), "%s/../keywords/%s-keywords.yaml", project_dir, - ext); + log_message(LOG_LEVEL_ERROR, " [LIBYAML] No desired yaml file found. Going default...."); + return NULL; } log_message(LOG_LEVEL_DEBUG, " [SYNHASH] Loading in %s", keywords_file); } @@ -205,7 +239,7 @@ void display_file(WINDOW* info_win, const char* filename) return; } char keywords_file[256]; - const char* ext = get_file_extension(filename); + const char* ext = determine_file_type(filename); const char* keywords_file_path = get_keywords_file(ext); bool syntaxLoad = load_syntax(keywords_file_path, keywords, singlecomments, multicomments1, multicomments2, @@ -285,43 +319,50 @@ void display_file(WINDOW* info_win, const char* filename) wrefresh(info_win); // Refresh the window to show the content } -const char* is_readable_extension(const char* filename, const char* current_path) { - char filepath[PATH_MAX]; - snprintf(filepath, PATH_MAX, "%s/%s", current_path, filename); - const char* file_type = determine_file_type(filepath); - - if (file_type) { - if (strcmp(file_type, MIME_TEXT_PLAIN) == 0 || strcmp(file_type, MIME_TEXT_SHELLSCRIPT) == 0 || - strcmp(file_type, MIME_APPLICATION_JSON) == 0 || strcmp(file_type, MIME_TEXT_RUBY) == 0 || - strcmp(file_type, MIME_TEXT_C) == 0 || strcmp(file_type, MIME_TEXT_CPP) == 0 || - strcmp(file_type, MIME_TEXT_PYTHON) == 0 || strcmp(file_type, MIME_TEXT_JAVA) == 0 || - strcmp(file_type, MIME_APPLICATION_OCTET_STREAM) == 0 || strcmp(file_type, MIME_TEXT_MAKEFILE) == 0 || - strcmp(file_type, MIME_TEXT_HTML) == 0 || strcmp(file_type, MIME_TEXT_CSS) == 0 || strcmp(file_type, MIME_EMPTY) == 0) { - return READABLE; - } - - if (strcmp(file_type, MIME_AUDIO_MPEG) == 0 || strcmp(file_type, MIME_AUDIO_WAV) == 0 || - strcmp(file_type, MIME_AUDIO_AIFF) == 0 || strcmp(file_type, MIME_AUDIO_OGG) == 0 || - strcmp(file_type, MIME_AUDIO_FLAC) == 0 || strcmp(file_type, MIME_AUDIO_MATROSKA) == 0) { - return AUDIO; - } - - if (strcmp(file_type, MIME_VIDEO_MP4) == 0 || strcmp(file_type, MIME_VIDEO_AVI) == 0 || - strcmp(file_type, MIME_VIDEO_MATROSKA) == 0 || strcmp(file_type, MIME_VIDEO_WMV) == 0 || - strcmp(file_type, MIME_VIDEO_WEBM) == 0 || strcmp(file_type, MIME_VIDEO_FLV) == 0 || - strcmp(file_type, MIME_VIDEO_ASF) == 0) { - return VIDEO; - } - - if (strcmp(file_type, MIME_IMAGE_JPEG) == 0 || strcmp(file_type, MIME_IMAGE_PNG) == 0 || - strcmp(file_type, MIME_IMAGE_GIF) == 0 || strcmp(file_type, MIME_IMAGE_BMP) == 0 || - strcmp(file_type, MIME_IMAGE_ICON) == 0 || strcmp(file_type, MIME_IMAGE_TIFF) == 0 || - strcmp(file_type, MIME_IMAGE_WEBP) == 0) { - return IMAGE; - } +const char* is_readable_extension(const char* filename, const char* current_path) +{ + char filepath[PATH_MAX]; + snprintf(filepath, PATH_MAX, "%s/%s", current_path, filename); + const char* file_type = determine_file_type(filepath); + + if (file_type) + { + if (strcmp(file_type, MIME_TEXT_PLAIN) == 0 || strcmp(file_type, MIME_TEXT_SHELLSCRIPT) == 0 || + strcmp(file_type, MIME_APPLICATION_JSON) == 0 || strcmp(file_type, MIME_TEXT_RUBY) == 0 || + strcmp(file_type, MIME_TEXT_C) == 0 || strcmp(file_type, MIME_TEXT_CPP) == 0 || + strcmp(file_type, MIME_TEXT_PYTHON) == 0 || strcmp(file_type, MIME_TEXT_JAVA) == 0 || + strcmp(file_type, MIME_TEXT_JAVASCRIPT) == 0 || + strcmp(file_type, MIME_TEXT_MAKEFILE) == 0 || strcmp(file_type, MIME_TEXT_HTML) == 0 || + strcmp(file_type, MIME_TEXT_CSS) == 0 || strcmp(file_type, MIME_EMPTY) == 0) + { + return READABLE; + } + + if (strcmp(file_type, MIME_AUDIO_MPEG) == 0 || strcmp(file_type, MIME_AUDIO_WAV) == 0 || + strcmp(file_type, MIME_AUDIO_AIFF) == 0 || strcmp(file_type, MIME_AUDIO_OGG) == 0 || + strcmp(file_type, MIME_AUDIO_FLAC) == 0 || strcmp(file_type, MIME_AUDIO_MATROSKA) == 0) + { + return AUDIO; + } + + if (strcmp(file_type, MIME_VIDEO_MP4) == 0 || strcmp(file_type, MIME_VIDEO_AVI) == 0 || + strcmp(file_type, MIME_VIDEO_MATROSKA) == 0 || strcmp(file_type, MIME_VIDEO_WMV) == 0 || + strcmp(file_type, MIME_VIDEO_WEBM) == 0 || strcmp(file_type, MIME_VIDEO_FLV) == 0 || + strcmp(file_type, MIME_VIDEO_ASF) == 0) + { + return VIDEO; } - return "NULL"; + if (strcmp(file_type, MIME_IMAGE_JPEG) == 0 || strcmp(file_type, MIME_IMAGE_PNG) == 0 || + strcmp(file_type, MIME_IMAGE_GIF) == 0 || strcmp(file_type, MIME_IMAGE_BMP) == 0 || + strcmp(file_type, MIME_IMAGE_ICON) == 0 || strcmp(file_type, MIME_IMAGE_TIFF) == 0 || + strcmp(file_type, MIME_IMAGE_WEBP) == 0) + { + return IMAGE; + } + } + + return "NULL"; } const char* format_file_size(off_t size) diff --git a/src/highlight.c b/src/highlight.c index eda2f52..8b08cfb 100644 --- a/src/highlight.c +++ b/src/highlight.c @@ -3,8 +3,9 @@ #include "../include/cursesutils.h" #include "../include/logging.h" -int multicomments1_length = 0; -int multicomments2_length = 0; +int multicomments1_length = 0; +int multicomments2_length = 0; +char string_delimiter = '\0'; // Load syntax from YAML file bool load_syntax(const char* path, HashTable* keywords, HashTable* singlecomments, @@ -245,7 +246,18 @@ void highlight_code(WINDOW* win, int start_y, int start_x, const char* code, Has // Then check for strings else if (hash_table_contains(strings, cursor)) { - in_string = !in_string; + if (!in_string) + { + // Start of a new string + in_string = true; + string_delimiter = *cursor; // Remember whether it's " or ' + } + else if (*cursor == string_delimiter) + { + // Closing the string + in_string = false; + } + if (in_string && buffer_index > 0) { buffer[buffer_index] = '\0'; @@ -253,10 +265,12 @@ void highlight_code(WINDOW* win, int start_y, int start_x, const char* code, Has x += buffer_index; buffer_index = 0; } + highlightLine(win, 22, y, x, (char[]){*cursor, '\0'}); x++; cursor++; } + // Then check for single line comments else if (hash_table_contains(singlecomments, cursor) && !in_string) { diff --git a/src/kbinput.c b/src/kbinput.c index 361ccf4..bc130d4 100644 --- a/src/kbinput.c +++ b/src/kbinput.c @@ -2,10 +2,10 @@ #include "../include/kbinput.h" #include "../include/cursesutils.h" #include "../include/filepreview.h" +#include "../include/inodeinfo.h" #include "../include/logging.h" #include "../include/musicpreview.h" #include "../include/structs.h" -#include "../include/inodeinfo.h" void handleInputScrollUp(int* highlight, int* scroll_position) { @@ -55,139 +55,175 @@ void handleInputMovCursBtm(int* highlight, int* item_count, int* scroll_position } } -int find_item(const char *query, FileItem items[], int *item_count, int *start_index, int direction) { - char lower_query[NAME_MAX]; - for (int i = 0; query[i] && i < NAME_MAX; i++) { - lower_query[i] = tolower(query[i]); +int find_item(const char* query, FileItem items[], int* item_count, int* start_index, int direction) +{ + char lower_query[NAME_MAX]; + for (int i = 0; query[i] && i < NAME_MAX; i++) + { + lower_query[i] = tolower(query[i]); + } + lower_query[strlen(query)] = '\0'; + + if (direction == 1) + { // Forward search + for (int i = *start_index; i < *item_count; i++) + { + char truncated_name[NAME_MAX]; + strcpy(truncated_name, items[i].name); + truncate_symlink_name(truncated_name); + + char lower_name[NAME_MAX]; + for (int j = 0; truncated_name[j] && j < NAME_MAX; j++) + { + lower_name[j] = tolower(truncated_name[j]); + } + lower_name[strlen(truncated_name)] = '\0'; + + if (strstr(lower_name, lower_query) != NULL) + { + *start_index = i; + return i; + } + } + for (int i = 0; i < *start_index; i++) + { + char truncated_name[NAME_MAX]; + strcpy(truncated_name, items[i].name); + truncate_symlink_name(truncated_name); + + char lower_name[NAME_MAX]; + for (int j = 0; truncated_name[j] && j < NAME_MAX; j++) + { + lower_name[j] = tolower(truncated_name[j]); + } + lower_name[strlen(truncated_name)] = '\0'; + + if (strstr(lower_name, lower_query) != NULL) + { + *start_index = i; + return i; + } + } + } + else if (direction == -1) + { // Backward search + for (int i = *start_index; i >= 0; i--) + { + char truncated_name[NAME_MAX]; + strcpy(truncated_name, items[i].name); + truncate_symlink_name(truncated_name); + + char lower_name[NAME_MAX]; + for (int j = 0; truncated_name[j] && j < NAME_MAX; j++) + { + lower_name[j] = tolower(truncated_name[j]); + } + lower_name[strlen(truncated_name)] = '\0'; + + if (strstr(lower_name, lower_query) != NULL) + { + *start_index = i; + return i; + } } - lower_query[strlen(query)] = '\0'; - - if (direction == 1) { // Forward search - for (int i = *start_index; i < *item_count; i++) { - char truncated_name[NAME_MAX]; - strcpy(truncated_name, items[i].name); - truncate_symlink_name(truncated_name); - - char lower_name[NAME_MAX]; - for (int j = 0; truncated_name[j] && j < NAME_MAX; j++) { - lower_name[j] = tolower(truncated_name[j]); - } - lower_name[strlen(truncated_name)] = '\0'; - - if (strstr(lower_name, lower_query) != NULL) { - *start_index = i; - return i; - } - } - for (int i = 0; i < *start_index; i++) { - char truncated_name[NAME_MAX]; - strcpy(truncated_name, items[i].name); - truncate_symlink_name(truncated_name); - - char lower_name[NAME_MAX]; - for (int j = 0; truncated_name[j] && j < NAME_MAX; j++) { - lower_name[j] = tolower(truncated_name[j]); - } - lower_name[strlen(truncated_name)] = '\0'; - - if (strstr(lower_name, lower_query) != NULL) { - *start_index = i; - return i; - } - } - } else if (direction == -1) { // Backward search - for (int i = *start_index; i >= 0; i--) { - char truncated_name[NAME_MAX]; - strcpy(truncated_name, items[i].name); - truncate_symlink_name(truncated_name); - - char lower_name[NAME_MAX]; - for (int j = 0; truncated_name[j] && j < NAME_MAX; j++) { - lower_name[j] = tolower(truncated_name[j]); - } - lower_name[strlen(truncated_name)] = '\0'; - - if (strstr(lower_name, lower_query) != NULL) { - *start_index = i; - return i; - } - } - for (int i = *item_count - 1; i > *start_index; i--) { - char truncated_name[NAME_MAX]; - strcpy(truncated_name, items[i].name); - truncate_symlink_name(truncated_name); - - char lower_name[NAME_MAX]; - for (int j = 0; truncated_name[j] && j < NAME_MAX; j++) { - lower_name[j] = tolower(truncated_name[j]); - } - lower_name[strlen(truncated_name)] = '\0'; - - if (strstr(lower_name, lower_query) != NULL) { - *start_index = i; - return i; - } - } + for (int i = *item_count - 1; i > *start_index; i--) + { + char truncated_name[NAME_MAX]; + strcpy(truncated_name, items[i].name); + truncate_symlink_name(truncated_name); + + char lower_name[NAME_MAX]; + for (int j = 0; truncated_name[j] && j < NAME_MAX; j++) + { + lower_name[j] = tolower(truncated_name[j]); + } + lower_name[strlen(truncated_name)] = '\0'; + + if (strstr(lower_name, lower_query) != NULL) + { + *start_index = i; + return i; + } } + } - return -1; // Not found + return -1; // Not found } -void handleInputStringSearch(WINDOW* win, FileItem items[], int* item_count, int* highlight, int* scroll_position, int *height, char* last_query, const char* current_path) { - // Display search indicator - wattron(win, A_BOLD | COLOR_PAIR(AQUA_COLOR_PAIR)); - mvwprintw(win, LINES - 3, (COLS / 2) - 75, "%s Search ON ", UNICODE_SEARCH); - wattroff(win, A_BOLD | COLOR_PAIR(AQUA_COLOR_PAIR)); - wrefresh(win); - - // Get user input for the search query - char query[NAME_MAX]; - get_user_input_from_bottom(stdscr, query, NAME_MAX, "search", current_path); - - // Perform the search if the query is not empty - if (strlen(query) > 0) { - int start_index = *highlight + 1; - int found_index = find_item(query, items, item_count, &start_index, 1); - - if (found_index != -1) { - *highlight = found_index; - - if (*highlight >= *scroll_position + *height - 8) { - *scroll_position = *highlight - *height + 8; - } else if (*highlight < *scroll_position) { - *scroll_position = *highlight; - } - - // Store the last query - strncpy(last_query, query, NAME_MAX); - } else { - show_term_message("Item not found.", 1); - } +void handleInputStringSearch(WINDOW* win, FileItem items[], int* item_count, int* highlight, + int* scroll_position, int* height, char* last_query, + const char* current_path) +{ + // Display search indicator + wattron(win, A_BOLD | COLOR_PAIR(AQUA_COLOR_PAIR)); + mvwprintw(win, LINES - 3, (COLS / 2) - 75, "%s Search ON ", UNICODE_SEARCH); + wattroff(win, A_BOLD | COLOR_PAIR(AQUA_COLOR_PAIR)); + wrefresh(win); + + // Get user input for the search query + char query[NAME_MAX]; + get_user_input_from_bottom(stdscr, query, NAME_MAX, "search", current_path); + + // Perform the search if the query is not empty + if (strlen(query) > 0) + { + int start_index = *highlight + 1; + int found_index = find_item(query, items, item_count, &start_index, 1); + + if (found_index != -1) + { + *highlight = found_index; + + if (*highlight >= *scroll_position + *height - 8) + { + *scroll_position = *highlight - *height + 8; + } + else if (*highlight < *scroll_position) + { + *scroll_position = *highlight; + } + + // Store the last query + strncpy(last_query, query, NAME_MAX); + } + else + { + show_term_message("Item not found.", 1); } + } } -void handleInputStringOccurance(int direction, const char* last_query, FileItem items[], int* item_count, int* highlight, int* scroll_position, int *height) { - if (strlen(last_query) > 0) { - int start_index = *highlight + direction; - int found_index = find_item(last_query, items, item_count, &start_index, direction); - - if (found_index != -1 && found_index != *highlight) { - *highlight = found_index; - - if (*highlight >= *scroll_position + *height - 8) { - *scroll_position = *highlight - *height + 8; - } else if (*highlight < *scroll_position) { - *scroll_position = *highlight; - } - } else { - const char* message = (direction == 1) ? "No more NEXT occurrences found" : "No previous occurrences found"; - log_message(LOG_LEVEL_WARN, "%s for `%s` found", message, last_query); - show_term_message(message, 1); - } +void handleInputStringOccurance(int direction, const char* last_query, FileItem items[], + int* item_count, int* highlight, int* scroll_position, int* height) +{ + if (strlen(last_query) > 0) + { + int start_index = *highlight + direction; + int found_index = find_item(last_query, items, item_count, &start_index, direction); + + if (found_index != -1 && found_index != *highlight) + { + *highlight = found_index; + + if (*highlight >= *scroll_position + *height - 8) + { + *scroll_position = *highlight - *height + 8; + } + else if (*highlight < *scroll_position) + { + *scroll_position = *highlight; + } } + else + { + const char* message = + (direction == 1) ? "No more NEXT occurrences found" : "No previous occurrences found"; + log_message(LOG_LEVEL_WARN, "%s for `%s` found", message, last_query); + show_term_message(message, 1); + } + } } - /* NOTE * SCOPES ARE NOT WORKING UNLESS IT IS IN MAIN FUNC * @@ -237,7 +273,8 @@ void handleInputScopeBack(int* history_count, int* highlight, int* scroll_positi /*void handleInputScopeForward(WINDOW* win, WINDOW* info_win, int* history_count, int* highlight,*/ /* int* scroll_position, bool* firstKeyPress, FileItem items[],*/ -/* DirHistory history[], const char* cur_user, const char* current_path)*/ +/* DirHistory history[], const char* cur_user, const char* + * current_path)*/ /*{*/ /* show_term_message("", -1);*/ /* char fullPath[MAX_PATH_LENGTH];*/ @@ -248,7 +285,8 @@ void handleInputScopeBack(int* history_count, int* highlight, int* scroll_positi /* if (access(fullPath, R_OK) != 0)*/ /* {*/ /* // Log the message safely*/ -/* log_message(LOG_LEVEL_ERROR, "[%s] Access denied for inode path %s: %s\n", cur_user, fullPath,*/ +/* log_message(LOG_LEVEL_ERROR, "[%s] Access denied for inode path %s: %s\n", cur_user, + * fullPath,*/ /* strerror(errno));*/ /**/ /* // Show the message to the user*/ @@ -279,7 +317,8 @@ void handleInputScopeBack(int* history_count, int* highlight, int* scroll_positi /* {*/ /* *firstKeyPress = true;*/ /* launch_env_var(win, current_path, items[*highlight].name, "EDITOR");*/ -/* /* Since we have set firstKeyPress to true, it will not wgetch(), rather it will just refresh*/ +/* /* Since we have set firstKeyPress to true, it will not wgetch(), rather it will just + * refresh*/ /* * everything back to how it was */ /* }*/ /* else if (is_image(items[*highlight].name) && !items[*highlight].is_dir)*/