root/Search.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#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;
}