-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnc-list.c
More file actions
151 lines (142 loc) · 3.54 KB
/
Copy pathnc-list.c
File metadata and controls
151 lines (142 loc) · 3.54 KB
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
/*
* ncurses listbox
*
* Copyright (C) 2017-2021 Nicholas Christopoulos.
*
* This is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* It is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with it. If not, see <http://www.gnu.org/licenses/>.
*
* Written by Nicholas Christopoulos <nereus@freemail.gr>
*/
#include <stdbool.h>
#include <ctype.h>
#include <wctype.h>
#include "nc-plus.h"
/*
* returns the selected item index or -1
*/
int nc_listbox(const char *title, const char **items, int defidx) {
int x, y, dx, dy, c, exf = 0;
int i, lines, wlines, offset;
int maxlen = 0, maxcols = 0, pos = 0;
bool found;
for ( lines = 0; items[lines]; lines ++ ) {
maxlen = MAX(maxlen, strlen(items[lines]));
maxcols = MAX(maxcols, u8width(items[lines]));
}
if ( title ) {
maxlen = MAX(maxlen, strlen(title));
maxcols = MAX(maxcols, u8width(title));
}
if ( defidx < 0 || defidx >= lines )
defidx = 0;
// outer window
dx = maxcols + 6;
dy = MIN(LINES - 4, lines + 2);
x = COLS / 2 - dx / 2;
y = LINES / 2 - dy / 2;
WINDOW *wout = newwin(dy, dx, y, x);
keypad(wout, TRUE);
WINDOW *w = subwin(wout, dy - 2, dx - 4, y + 1, x + 2);
keypad(w, TRUE);
wlines = dy - 4;
offset = 0;
pos = defidx;
do {
if ( offset > pos ) offset = pos;
if ( offset + lines < pos ) offset = pos - lines;
if ( offset < 0 ) offset = 0;
werase(wout);
box(wout, 0, 0);
if ( title ) nc_wtitle(wout, title, 2);
wrefresh(wout);
werase(w);
for ( i = offset, y = 0; i < lines; i ++, y ++ ) {
if ( i == pos ) wattron(w, A_REVERSE);
mvwhline(w, y, 0, ' ', getmaxx(w));
mvwprintw(w, y, 1, "%s", items[i]);
if ( i == pos ) wattroff(w, A_REVERSE);
}
wrefresh(w);
c = wgetch(w);
switch ( c ) {
case 'q': pos = -1; exf ++; break;
case '': case '':
case '\033': pos = -1; exf ++; break;
case '\r': case '\n': exf ++; break;
case KEY_UP:
if ( pos )
pos --;
break;
case KEY_DOWN:
if ( pos < (lines - 1) )
pos ++;
break;
case KEY_PPAGE:
pos -= wlines;
if ( pos < 0 )
pos = 0;
break;
case KEY_NPAGE:
pos += wlines;
if ( pos > (lines - 1) )
pos = lines - 1;
break;
case '\001':
case KEY_HOME: offset = pos = 0; break;
case '\005':
case KEY_END: pos = lines - 1; break;
default: {
found = false;
wchar_t wi, wc;
int clen = u8csize(c);
if ( clen > 1 ) {
char mbs[7];
mbs[0] = c;
for ( i = 1; i < clen; i ++ )
mbs[i] = wgetch(w);
mbs[clen] = '\0';
wc = u8towc(mbs);
}
else
wc = (wchar_t) c;
wi = u8towc(items[pos]);
if ( towupper(wi) >= towupper(wc) ) { // find next
for ( i = pos + 1; i < lines; i ++ ) {
wi = u8towc(items[i]);
if ( towupper(wi) == towupper(wc) ) {
pos = i;
found = true;
break;
}
}
}
if ( !found ) { // search from the beginning
for ( i = 0; i < lines; i ++ ) {
wi = u8towc(items[i]);
if ( towupper(wi) == towupper(wc) ) {
pos = i;
found = true;
break;
}
}
}
}
}
} while ( !exf );
// cleanup
delwin(w);
delwin(wout);
refresh();
return pos;
}