Adding comments
This commit is contained in:
parent
a9062812aa
commit
b3264e954c
53
ascii.c
53
ascii.c
@ -91,6 +91,7 @@ static inline void shift(int dy, int dx) {
|
||||
move(y+dy, x+dx);
|
||||
}
|
||||
|
||||
// Same as printw, except it doesn't do text wrapping, and it sets attributes
|
||||
#define printwattrs(_attrs, ...) \
|
||||
{ char _buf[W+1];\
|
||||
chtype _bufch[W+1];\
|
||||
@ -132,7 +133,7 @@ static void redraw(int selected)
|
||||
int selectedx = (selected / (H-2)) * COL_WIDTH;
|
||||
int scrollx = (selectedx + COL_WIDTH < W) ? 0 : (((selectedx + COL_WIDTH) - W)/COL_WIDTH+1)*COL_WIDTH;
|
||||
if (last_selected == -1 || last_scrollx != scrollx || last_W != W || last_H != H) {
|
||||
// Redraw everything
|
||||
// Redraw all the entries
|
||||
erase();
|
||||
for (int i = 0; i < 128; i++) {
|
||||
draw_entry(i, scrollx, i == selected ? A_REVERSE : 0);
|
||||
@ -141,12 +142,15 @@ static void redraw(int selected)
|
||||
last_H = H;
|
||||
last_W = W;
|
||||
} else if (selected != last_selected) {
|
||||
// Otherwise, we can lazily just redraw 2 entries
|
||||
draw_entry(selected, scrollx, A_REVERSE);
|
||||
draw_entry(last_selected, scrollx, 0);
|
||||
}
|
||||
last_selected = selected;
|
||||
}
|
||||
|
||||
// Find the ascii character starting at `start`, going in `direction`, matching
|
||||
// `searchbuff`, and return `fallback` if nothing matches.
|
||||
static int find_ascii_match(int start, int fallback, int direction, char *searchbuf)
|
||||
{
|
||||
if (searchbuf[0] == '\0') return fallback;
|
||||
@ -173,10 +177,10 @@ static int find_ascii_match(int start, int fallback, int direction, char *search
|
||||
return fallback;
|
||||
}
|
||||
|
||||
// To prevent wasted work, wait until 200ms has elapsed with no additional
|
||||
// resizes before doing anything, then set the correct W/H values.
|
||||
static void handle_resize()
|
||||
{
|
||||
// To prevent wasted work, wait until 200ms has elapsed
|
||||
// with no additional resizes before doing anything
|
||||
timeout(0);
|
||||
do {
|
||||
// Wait 200ms
|
||||
@ -199,24 +203,30 @@ int main(int argc, char *argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* term_type = getenv("TERM");
|
||||
if (term_type == NULL || *term_type == '\0') {
|
||||
term_type = "unknown";
|
||||
// Instead of initscr(), set up the terminal this way to play nicely with
|
||||
// the unix pipeline:
|
||||
{
|
||||
char* term_type = getenv("TERM");
|
||||
if (term_type == NULL || *term_type == '\0') {
|
||||
term_type = "unknown";
|
||||
}
|
||||
FILE* term_in = fopen("/dev/tty", "r");
|
||||
if (term_in == NULL) {
|
||||
perror("fopen(/dev/tty)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
SCREEN *screen = newterm(term_type, stderr, term_in);
|
||||
set_term(screen);
|
||||
}
|
||||
FILE* term_in = fopen("/dev/tty", "r");
|
||||
if (term_in == NULL) {
|
||||
perror("fopen(/dev/tty)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
SCREEN *screen = newterm(term_type, stderr, term_in);
|
||||
set_term(screen);
|
||||
set_escdelay(5);
|
||||
|
||||
W = getmaxx(stdscr);
|
||||
H = getmaxy(stdscr);
|
||||
noecho();
|
||||
set_escdelay(5);
|
||||
keypad(stdscr, 1);
|
||||
curs_set(2);
|
||||
start_color();
|
||||
|
||||
DECIMAL_COLORS = new_colorpair(COLOR_YELLOW, COLOR_BLACK) | A_BOLD;
|
||||
HEX_COLORS = new_colorpair(COLOR_GREEN, COLOR_BLACK) | A_BOLD;
|
||||
CHAR_COLORS = new_colorpair(COLOR_MAGENTA, COLOR_BLACK) | A_BOLD;
|
||||
@ -231,8 +241,8 @@ int main(int argc, char *argv[])
|
||||
INSERT_MODE_COLORS = new_colorpair(COLOR_WHITE, COLOR_RED) | A_BOLD;
|
||||
SEARCH_MODE_COLORS = new_colorpair(COLOR_BLACK, COLOR_BLUE);
|
||||
|
||||
size_t chunk = 256;
|
||||
size_t buf_size = chunk, buf_i = 0;
|
||||
size_t buf_chunk = 1024;
|
||||
size_t buf_size = buf_chunk, buf_i = 0;
|
||||
char *outbuf = calloc(buf_size, sizeof(char));
|
||||
|
||||
{ // Read stdin if anything was piped in
|
||||
@ -241,11 +251,12 @@ int main(int argc, char *argv[])
|
||||
desc.events = POLLIN;
|
||||
int ret = poll(&desc, 1, 50);
|
||||
if (ret > 0) {
|
||||
for (size_t consumed; (consumed = fread(&outbuf[buf_i], 1, chunk, stdin));
|
||||
buf_i += consumed) {
|
||||
if (consumed == chunk) {
|
||||
chunk *= 2;
|
||||
buf_size += chunk;
|
||||
size_t consumed;
|
||||
while ((consumed = fread(&outbuf[buf_i], 1, buf_chunk, stdin))) {
|
||||
buf_i += consumed;
|
||||
if (consumed == buf_chunk) {
|
||||
buf_chunk *= 2;
|
||||
buf_size += buf_chunk;
|
||||
outbuf = realloc(outbuf, buf_size);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user