summaryrefslogtreecommitdiff
path: root/src/fruits/cmc/ui/panes/search.c
blob: e46c27d865554a3aebedc8f0a6db4389a745740e (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "../../cmc.h"

#include "../ui.h"

AL_ASSERT_TYPE_SIZE(wchar_t, 4);

//void cmc_sp_init(struct cmc_ui *ui)
//{
//    al_array_init(ui->sp.tabs);
//    ui->sp.current = 0;
//}
//
//void cmc_sp_layout(struct cmc_ui *ui, struct ncplane *parent)
//{
//    if (ui->sp.n) ncplane_destroy(ui->sp.n);
//    struct ncplane_options nopts = { 0 };
//    nopts.rows = ncplane_dim_y(parent) - 1;
//    nopts.cols = ncplane_dim_x(parent) - 1;
//    ui->sp.n = ncplane_create(parent, &nopts);
//    struct cmc_search_tab tab = {
//        .search = NULL,
//    };
//    nopts.x = 3;
//    nopts.y = 2;
//    nopts.rows = 10;
//    nopts.cols = ncplane_dim_x(ui->sp.n) - 3;
//    tab.input = ncplane_create(ui->sp.n, &nopts);
//    tab.cursor = 0;
//    tab.input_active = false;
//    tab.input_text = al_wstr_null();
//    tab.input_changed = false;
//    al_array_push(ui->sp.tabs, tab);
//}
//
//void cmc_sp_add_search(struct cmc_ui *ui, struct cmc_search *search)
//{
//    struct cmc_search_tab tab = {
//        .search = search,
//    };
//    struct ncplane_options nopts = { 0 };
//    nopts.x = 3;
//    nopts.y = 2;
//    nopts.rows = 10;
//    nopts.cols = ncplane_dim_x(ui->sp.n) - 3;
//    tab.input = ncplane_create(ui->sp.n, &nopts);
//    tab.cursor = 0;
//    tab.input_active = false;
//    tab.input_text = al_wstr_null();
//    tab.input_changed = false;
//    al_array_push(ui->sp.tabs, tab);
//}
//
//static bool handle_text_input(struct cmc_search_tab *tab, struct ncinput *input)
//{
//    bool entered = false;
//
//    switch (input->id) {
//    case NCKEY_BACKSPACE:
//        if (tab->cursor > 0) {
//            al_wstr_remove_at(&tab->input_text, tab->cursor - 1);
//            tab->cursor--;
//            tab->input_changed = true;
//        }
//        break;
//    case NCKEY_LEFT:
//        if (tab->cursor > 0) {
//            tab->cursor--;
//        }
//        break;
//    case NCKEY_RIGHT:
//        if (tab->cursor < tab->input_text.length) {
//            tab->cursor++;
//        }
//        break;
//    case NCKEY_RETURN:
//        entered = true;
//        // fallthrough
//    case NCKEY_ESC:
//        tab->input_active = false;
//        tab->input_changed = true; // Erase text on next render.
//        break;
//    default:
//        if (ncinput_ctrl_p(input)) {
//            switch (input->id) {
//            case 'A':
//                tab->cursor = 0;
//                break;
//            case 'E':
//                tab->cursor = tab->input_text.length;
//                break;
//            case 'B':
//                if (tab->cursor > 0) {
//                    tab->cursor--;
//                }
//                break;
//            case 'F':
//                if (tab->cursor < tab->input_text.length) {
//                    tab->cursor++;
//                }
//                break;
//            case 'U':
//                for (u32 i = 1; i <= tab->cursor; i++) {
//                    al_wstr_remove_at(&tab->input_text, tab->cursor - i);
//                }
//                tab->cursor = 0;
//                tab->input_changed = true;
//                break;
//            }
//            break;
//        }
//
//        if (ncinput_alt_p(input) || nckey_synthesized_p(input->id)) {
//            break;
//        }
//
//        al_wstr_insert(&tab->input_text, tab->cursor, &al_wstr_w((wchar_t *)input->eff_text, 0, 1));
//        tab->cursor++;
//        tab->input_changed = true;
//
//        /*
//        // https://github.com/dankamongmen/notcurses/blob/c11efe877f2245901f5c9ce110de7a2c83cfa2ed/src/lib/reader.c#L404
//        for (s32 c = 0; input->eff_text[c] != 0; c++){
//            uchar egc[5] = { 0 };
//            if (notcurses_ucs32_to_utf8(&input->eff_text[c], 1, egc, 4) >= 0) {
//                // This probably doesn't work on Windows.
//                al_wstr_insert(&tab->input_text, tab->cursor, &al_wstr_w((wchar_t *)egc, 0, 1));
//            }
//        }
//        */
//
//        break;
//    }
//
//    tab->visual_cursor = 0;
//    if (tab->cursor > 0) {
//        for (u32 i = 0; i < tab->cursor; i++) {
//            tab->visual_cursor += al_wchar_width(al_wstr_at(&tab->input_text, i));
//        }
//    }
//
//    return entered;
//}
//
//bool cmc_sp_handle_input(struct cmc_ui *ui, struct ncinput *input)
//{
//    struct cmc_search_tab *tab = NULL;
//    if (ui->sp.tabs.count > 0) {
//        tab = &al_array_at(ui->sp.tabs, ui->sp.current);
//    }
//
//    if (tab) {
//        if (tab->input_active) {
//            if (cmc_ui_consider_input(input, true)) {
//                if (handle_text_input(tab, input)) {
//                    str query;
//                    al_str_from_wstr(&query, &tab->input_text);
//                    camu_client_create_search(&ui->c->client, &al_str_c("youtube"), &query);
//                    al_str_free(&query);
//                }
//                if (!tab->input_active) notcurses_cursor_disable(ui->nc);
//            }
//        } else {
//            switch (input->id) {
//            case 'i':
//                CONSIDER_INPUT();
//                tab->input_active = true;
//                tab->input_changed = true;
//                s32 absx = ncplane_abs_x(tab->input);
//                s32 absy = ncplane_abs_y(tab->input);
//                notcurses_cursor_enable(ui->nc, absy, absx + tab->visual_cursor);
//                break;
//            }
//        }
//    }
//
//    return true;
//}
//
//void cmc_sp_erase(struct cmc_ui *ui)
//{
//    ncplane_erase(ui->sp.n);
//}
//
//void cmc_sp_render(struct cmc_ui *ui)
//{
//    struct cmc_search_tab *tab = NULL;
//    if (ui->sp.tabs.count > 0) {
//        tab = &al_array_at(ui->sp.tabs, ui->sp.current);
//    }
//
//    if (tab) {
//        if (tab->input_changed) {
//            ncplane_erase(tab->input);
//            if (tab->input_active) {
//                cmc_ui_putnwstr_yx(tab->input, 0, 0, tab->input_text.length, &tab->input_text);
//            }
//            tab->input_changed = false;
//        }
//        if (tab->input_active) {
//            s32 absx = ncplane_abs_x(tab->input);
//            s32 absy = ncplane_abs_y(tab->input);
//            notcurses_cursor_enable(ui->nc, absy, absx + tab->visual_cursor);
//        }
//    }
//
//    /*
//    if (ui->overlay_shown) {
//        ncplane_erase(ui->oln);
//        u32 max_height = ncplane_dim_y(ui->oln) - 2;
//        u32 count = ui->c->searches.count;
//        u32 index = ui->sp.overlay_index - (ui->sp.overlay_index % max_height);
//        u32 end = MIN(index + max_height, count);
//        for (u32 i = index; i < end; i++) {
//            struct cmc_search *search = al_array_at(ui->c->searches, i);
//            if (i == ui->sp.overlay_index) {
//                ncplane_set_fg_palindex(ui->oln, 6);
//                ncplane_putchar_yx(ui->oln, i - index + 1, 2, '>');
//                cmc_ui_putstr_maxwidth_yx(ui->oln, i - index + 1, 4, search->query.length, &search->query);
//            } else {
//                ncplane_set_fg_default(ui->oln);
//                cmc_ui_putstr_maxwidth_yx(ui->oln, i - index + 1, 2, search->query.length, &search->query);
//            }
//        }
//    }
//    */
//}