#include "Search.h" #include "utils.h" #include "utf.h" #define MAX_QUERY_LENGTH 200 void Search_init(struct Search* this) { this->status = searchInactive; this->queryBuffer = malloc(sizeof(uint32_t)*MAX_QUERY_LENGTH); /* shitty arbitary limitation */ this->queryBuffer[0] = 0; this->queryUtf8 = NULL; } void Search_destroyMembers(struct Search* this) { if (this) { safe_free(this->queryBuffer); safe_free(this->queryUtf8); } } void Search_clear(struct Search* this) { this->queryBuffer[0] = 0; } size_t queryLength(uint32_t* queryBuffer) { size_t length = 0; for (size_t i=0; i < MAX_QUERY_LENGTH; ++i) { int c = queryBuffer[i]; if (c == 0) { break; } length+=1; } return length; } bool Search_handleInput(struct Search* this, int event) { /* bool isPrintableAscii = event >= 32 && !(KEY_CODE_YES & event); */ bool isBackSpace = event == KEY_BACKSPACE; bool isConfirm = event == '/' || event == '\n'; if (!isBackSpace && (this->status != searchRead /* || !isPrintableAscii */)) { return false; } if (isBackSpace) { if (this->queryBuffer[0] == 0) { this->status = searchInactive; return true; } for (size_t i=0; i < MAX_QUERY_LENGTH; ++i) { int c = this->queryBuffer[i]; if (c == 0) { this->queryBuffer[i-1] = 0; return true; } } return true; } if (isConfirm) { safe_free(this->queryUtf8); this->queryUtf8 = utf32_to_utf8(this->queryBuffer, queryLength(this->queryBuffer)); this->status = searchBrowse; return true; } /* TODO figure out how to get unicode values from ncurses */ if (event > 127) { event = event + 64; } /* printf("code1:%d\n", event); */ for (size_t i=0; i < MAX_QUERY_LENGTH-2; ++i) { int c = this->queryBuffer[i]; if (c == 0) { this->queryBuffer[i] = event; this->queryBuffer[i+1] = 0; return true; } } return false; } void Search_draw(struct Search* this, WINDOW* w) { wmove(w, 0, 0); char* utf8 = utf32_to_utf8(this->queryBuffer, queryLength(this->queryBuffer)); /* printf("code:%d\n", (int)this->queryBuffer[0]); */ waddstr(w, "Search: /"); waddstr(w, utf8); wclrtoeol(w); free(utf8); wrefresh(w); /* for (size_t i=0; i < MAX_QUERY_LENGTH; ++i) { */ /* int c = this->queryBuffer[i]; */ /* if (c == 0) { */ /* return; */ /* } */ /* waddch(w, c); */ /* } */ } void Search_deactivate(struct Search* this) { this->status = searchInactive; this->queryBuffer[0] = 0; }