Added insert mode, for faster typing onto input.
This commit is contained in:
parent
c0f45c8869
commit
edeffff139
@ -11,7 +11,9 @@ passed as arguments.
|
||||
|
||||
## Controls
|
||||
In the ASCII table viewer, you can navigate with arrow keys or h/j/k/l.
|
||||
Pressing `enter` will append to the output buffer and `backspace` will delete
|
||||
from the output buffer. Pressing `q` or `escape` will exit and print the output
|
||||
Pressing `enter` will append the selected character to the output buffer and
|
||||
`backspace` will delete from the output buffer. Pressing `i` will enter insert
|
||||
mode, where you can type freely into the output buffer, pressing `escape` will
|
||||
leave insert mode. Pressing `q` or `escape` will exit and print the output
|
||||
buffer to stdout. Hitting control-c will quit without printing the output
|
||||
buffer.
|
||||
|
56
ascii.c
56
ascii.c
@ -81,7 +81,7 @@ static int new_colorpair(int fg, int bg)
|
||||
}
|
||||
|
||||
int DECIMAL_COLORS, HEX_COLORS, CHAR_COLORS, DESCRIPTION_COLORS, OUTPUT_CHAR_COLORS,
|
||||
OUTPUT_ESCAPE_COLORS, OUTPUT_LABEL_COLORS;
|
||||
OUTPUT_ESCAPE_COLORS, OUTPUT_LABEL_COLORS, BROWSE_MODE_COLORS, INSERT_MODE_COLORS;
|
||||
|
||||
const int COL_WIDTH = 42;
|
||||
int W = 0;
|
||||
@ -103,8 +103,8 @@ static inline void shift(int dy, int dx) {
|
||||
}
|
||||
|
||||
#define printwattrs(_attrs, ...) \
|
||||
{ char _buf[COL_WIDTH+1];\
|
||||
chtype _bufch[COL_WIDTH+1];\
|
||||
{ char _buf[W+1];\
|
||||
chtype _bufch[W+1];\
|
||||
sprintf(_buf, __VA_ARGS__);\
|
||||
for (int i = 0; i == 0 || _buf[i-1]; i++) _bufch[i] = _buf[i] | _attrs;\
|
||||
addchstr(_bufch);\
|
||||
@ -114,9 +114,9 @@ static inline void shift(int dy, int dx) {
|
||||
// Draw the information for one character
|
||||
static inline void draw_entry(int i, int scrollx, int attrs)
|
||||
{
|
||||
int x = (i / (H-1)) * COL_WIDTH - scrollx;
|
||||
int x = (i / (H-2)) * COL_WIDTH - scrollx;
|
||||
if (x > W || x < 0) return;
|
||||
int y = i % (H-1);
|
||||
int y = (i % (H-2)) + 1;
|
||||
move(y, x);
|
||||
printwattrs(DECIMAL_COLORS | attrs, "%3d ", i);
|
||||
printwattrs(HEX_COLORS | attrs, " 0x%02X ", i);
|
||||
@ -140,7 +140,7 @@ static inline void draw_entry(int i, int scrollx, int attrs)
|
||||
static void redraw(int selected)
|
||||
{
|
||||
static int last_selected = -1, last_scrollx = -1, last_W = -1, last_H = -1;
|
||||
int selectedx = (selected / (H-1)) * COL_WIDTH;
|
||||
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
|
||||
@ -211,6 +211,8 @@ int main(int argc, char *argv[])
|
||||
OUTPUT_CHAR_COLORS = new_colorpair(COLOR_BLACK, COLOR_WHITE) | A_BOLD;
|
||||
OUTPUT_ESCAPE_COLORS = new_colorpair(COLOR_RED, COLOR_WHITE) | A_BOLD;
|
||||
OUTPUT_LABEL_COLORS = new_colorpair(COLOR_BLACK, COLOR_YELLOW);
|
||||
BROWSE_MODE_COLORS = new_colorpair(COLOR_BLACK, COLOR_BLUE);
|
||||
INSERT_MODE_COLORS = new_colorpair(COLOR_WHITE, COLOR_RED) | A_BOLD;
|
||||
|
||||
size_t chunk = 256;
|
||||
size_t buf_size = chunk, buf_i = 0;
|
||||
@ -244,9 +246,21 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
int insert_mode = 0;
|
||||
int selected = 0;
|
||||
while (1) {
|
||||
redraw(selected);
|
||||
// Title bar
|
||||
move(0,0);
|
||||
if (insert_mode) {
|
||||
const char *str = "ASCII TABLE (typing, press 'esc' to browse)";
|
||||
int spacer = W/2-strlen(str)/2;
|
||||
printwattrs(INSERT_MODE_COLORS, "%*s%s%*s", spacer, "", str, spacer+1, "");
|
||||
} else {
|
||||
const char *str = "ASCII TABLE (browsing, press 'esc' to exit)";
|
||||
int spacer = W/2-strlen(str)/2;
|
||||
printwattrs(BROWSE_MODE_COLORS, "%*s%s%*s", spacer, "", str, spacer+1, "");
|
||||
}
|
||||
// Bar at the bottom of the screen showing the output text:
|
||||
// left-truncate output so the end of it fits on screen:
|
||||
char *visible = &outbuf[buf_i];
|
||||
@ -259,6 +273,23 @@ int main(int argc, char *argv[])
|
||||
refresh();
|
||||
next_input:;
|
||||
int key = getch();
|
||||
if (insert_mode) {
|
||||
if (key == 27) {
|
||||
insert_mode = 0;
|
||||
continue;
|
||||
} else if (key == 127) {
|
||||
// Backspace
|
||||
if (buf_i > 0) buf_i--;
|
||||
continue;
|
||||
} else if (0 <= key && key < 127) {
|
||||
outbuf[buf_i++] = (char)key;
|
||||
if (buf_i >= buf_size) {
|
||||
buf_size *= 2;
|
||||
outbuf = realloc(outbuf, buf_size);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
switch (key) {
|
||||
case 'j': case KEY_DOWN:
|
||||
selected++;
|
||||
@ -277,11 +308,15 @@ int main(int argc, char *argv[])
|
||||
break;
|
||||
|
||||
case 'l': case 'L': case KEY_RIGHT: case KEY_SRIGHT:
|
||||
selected += H-1;
|
||||
selected += H-2;
|
||||
break;
|
||||
|
||||
case 'h': case 'H': case KEY_LEFT: case KEY_SLEFT:
|
||||
selected -= H-1;
|
||||
selected -= H-2;
|
||||
break;
|
||||
|
||||
case 'i': case 'a':
|
||||
insert_mode = 1;
|
||||
break;
|
||||
|
||||
case KEY_RESIZE:
|
||||
@ -311,10 +346,7 @@ int main(int argc, char *argv[])
|
||||
break;
|
||||
|
||||
case KEY_BACKSPACE: case KEY_DC: case 127:
|
||||
if (buf_i > 0) {
|
||||
outbuf[buf_i] = 'X';
|
||||
buf_i--;
|
||||
}
|
||||
if (buf_i > 0) buf_i--;
|
||||
break;
|
||||
|
||||
case 'q': case 'Q': case KEY_CANCEL: case KEY_CLOSE: case KEY_EXIT: case 27:
|
||||
|
@ -28,7 +28,10 @@ ASCII values will be appended to the output buffer after any piped input.
|
||||
In the ASCII table, arrow keys or h/j/k/l can be used to navigate.
|
||||
|
||||
.B Editing:
|
||||
You can use 'enter' to append to the output buffer and 'backspace' to delete from the output buffer.
|
||||
You can use 'enter' to append the selected character to the output buffer and
|
||||
\'backspace' to delete from the output buffer. Or, for more rapid input, press
|
||||
\'i' to enter insert mode, where you can type freely, and 'escape' when you're
|
||||
done.
|
||||
|
||||
.B Exiting:
|
||||
\'q' or 'escape' will quit and print the output buffer. Control-c can be used
|
||||
|
Loading…
Reference in New Issue
Block a user