various input line improvements
This commit is contained in:
parent
9eae11b67e
commit
cb4f7eb808
|
@ -48,14 +48,17 @@ typedef struct inputline_s
|
||||||
{
|
{
|
||||||
char **lines; // array of lines for input history
|
char **lines; // array of lines for input history
|
||||||
int num_lines; // number of lines in arry. 1 == no history
|
int num_lines; // number of lines in arry. 1 == no history
|
||||||
int line_width; // space available in each line. includes \0
|
int line_size; // space available in each line. includes \0
|
||||||
char prompt_char; // char placed at the beginning of the line
|
char prompt_char; // char placed at the beginning of the line
|
||||||
int edit_line; // current line being edited
|
int edit_line; // current line being edited
|
||||||
int history_line; // current history line
|
int history_line; // current history line
|
||||||
int linepos; // cursor position within the current edit line
|
int linepos; // cursor position within the current edit line
|
||||||
int scroll; // beginning of displayed line
|
int scroll; // beginning of displayed line
|
||||||
|
int width; // viewable widht for horizontal scrolling
|
||||||
|
void *user_data; // eg: window pointer
|
||||||
void (*complete)(struct inputline_s *); // tab key pressed
|
void (*complete)(struct inputline_s *); // tab key pressed
|
||||||
void (*enter)(const char *line); // enter key pressed
|
void (*enter)(const char *line); // enter key pressed
|
||||||
|
void (*draw)(struct inputline_s *); // draw input line to screen
|
||||||
} inputline_t;
|
} inputline_t;
|
||||||
|
|
||||||
extern console_t con_main;
|
extern console_t con_main;
|
||||||
|
|
|
@ -44,6 +44,8 @@
|
||||||
#include "QF/console.h"
|
#include "QF/console.h"
|
||||||
#include "QF/keys.h"
|
#include "QF/keys.h"
|
||||||
|
|
||||||
|
#include "compat.h"
|
||||||
|
|
||||||
struct inputline_s *
|
struct inputline_s *
|
||||||
Con_CreateInputLine (int lines, int width, char prompt)
|
Con_CreateInputLine (int lines, int width, char prompt)
|
||||||
{
|
{
|
||||||
|
@ -62,7 +64,7 @@ Con_CreateInputLine (int lines, int width, char prompt)
|
||||||
|
|
||||||
inputline->lines = p;
|
inputline->lines = p;
|
||||||
inputline->num_lines = lines;
|
inputline->num_lines = lines;
|
||||||
inputline->line_width = width;
|
inputline->line_size = width;
|
||||||
while (lines--) {
|
while (lines--) {
|
||||||
*p++ = l;
|
*p++ = l;
|
||||||
l += width;
|
l += width;
|
||||||
|
@ -85,6 +87,7 @@ void
|
||||||
Con_ProcessInputLine (inputline_t *il, int ch)
|
Con_ProcessInputLine (inputline_t *il, int ch)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
char *text;
|
||||||
|
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case K_RETURN:
|
case K_RETURN:
|
||||||
|
@ -152,7 +155,7 @@ Con_ProcessInputLine (inputline_t *il, int ch)
|
||||||
default:
|
default:
|
||||||
if (ch >= ' ' && ch < 256 && ch != 127) {
|
if (ch >= ' ' && ch < 256 && ch != 127) {
|
||||||
i = strlen (il->lines[il->edit_line]);
|
i = strlen (il->lines[il->edit_line]);
|
||||||
if (i >= il->line_width - 1)
|
if (i >= il->line_size - 1)
|
||||||
break;
|
break;
|
||||||
// This also moves the ending \0
|
// This also moves the ending \0
|
||||||
memmove (il->lines[il->edit_line] + il->linepos + 1,
|
memmove (il->lines[il->edit_line] + il->linepos + 1,
|
||||||
|
@ -163,4 +166,17 @@ Con_ProcessInputLine (inputline_t *il, int ch)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
i = il->linepos - 1;
|
||||||
|
if (il->scroll > i)
|
||||||
|
il->scroll = i;
|
||||||
|
if (il->scroll < i - (il->width - 2) + 1)
|
||||||
|
il->scroll = i - (il->width - 2) + 1;
|
||||||
|
text = il->lines[il->edit_line] + il->scroll;
|
||||||
|
if ((int)strlen (text + 1) < il->width - 2) {
|
||||||
|
text = il->lines[il->edit_line];
|
||||||
|
il->scroll = strlen (text + 1) - (il->width - 2);
|
||||||
|
il->scroll = max (il->scroll, 0);
|
||||||
|
}
|
||||||
|
if (il->draw)
|
||||||
|
il->draw (il);
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,6 +109,38 @@ C_ExecLine (const char *line)
|
||||||
Cbuf_AddText (line);
|
Cbuf_AddText (line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
C_DrawInputLine (inputline_t *il)
|
||||||
|
{
|
||||||
|
WINDOW *win = (WINDOW *) il->user_data;
|
||||||
|
int i;
|
||||||
|
const char *text;
|
||||||
|
|
||||||
|
text = il->lines[il->edit_line] + il->scroll;
|
||||||
|
wmove (win, 0, 0);
|
||||||
|
if (il->scroll) {
|
||||||
|
waddch (win, '<' | COLOR_PAIR (5));
|
||||||
|
text++;
|
||||||
|
} else {
|
||||||
|
waddch (win, *text++);
|
||||||
|
}
|
||||||
|
for (i = 0; i < il->width - 2 && *text; i++) {
|
||||||
|
chtype ch = (byte)*text++;
|
||||||
|
ch = sys_char_map[ch] | attr_table[attr_map[ch]];
|
||||||
|
waddch (win, ch);
|
||||||
|
}
|
||||||
|
while (i++ < il->width - 2) {
|
||||||
|
waddch (win, ' ');
|
||||||
|
}
|
||||||
|
if (*text) {
|
||||||
|
waddch (win, '>' | COLOR_PAIR (5));
|
||||||
|
} else {
|
||||||
|
waddch (win, ' ');
|
||||||
|
}
|
||||||
|
wmove (win, 0, il->linepos - il->scroll);
|
||||||
|
wrefresh (win);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
sigwinch (int sig)
|
sigwinch (int sig)
|
||||||
{
|
{
|
||||||
|
@ -145,6 +177,7 @@ C_Init (void)
|
||||||
init_pair (2, COLOR_GREEN, COLOR_BLACK);
|
init_pair (2, COLOR_GREEN, COLOR_BLACK);
|
||||||
init_pair (3, COLOR_RED, COLOR_BLACK);
|
init_pair (3, COLOR_RED, COLOR_BLACK);
|
||||||
init_pair (4, COLOR_YELLOW, COLOR_BLUE);
|
init_pair (4, COLOR_YELLOW, COLOR_BLUE);
|
||||||
|
init_pair (5, COLOR_CYAN, COLOR_BLACK);
|
||||||
|
|
||||||
scrollok (output, TRUE);
|
scrollok (output, TRUE);
|
||||||
leaveok (output, TRUE);
|
leaveok (output, TRUE);
|
||||||
|
@ -169,8 +202,13 @@ C_Init (void)
|
||||||
input_line = Con_CreateInputLine (16, MAXCMDLINE, ']');
|
input_line = Con_CreateInputLine (16, MAXCMDLINE, ']');
|
||||||
input_line->complete = Con_BasicCompleteCommandLine;
|
input_line->complete = Con_BasicCompleteCommandLine;
|
||||||
input_line->enter = C_ExecLine;
|
input_line->enter = C_ExecLine;
|
||||||
|
input_line->width = screen_x;
|
||||||
|
input_line->user_data = input;
|
||||||
|
input_line->draw = C_DrawInputLine;
|
||||||
|
|
||||||
con_linewidth = screen_x;
|
con_linewidth = screen_x;
|
||||||
|
|
||||||
|
C_DrawInputLine (input_line);
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
setvbuf (stdout, 0, _IOLBF, BUFSIZ);
|
setvbuf (stdout, 0, _IOLBF, BUFSIZ);
|
||||||
|
@ -232,8 +270,6 @@ C_ProcessInput (void)
|
||||||
#ifdef HAVE_CURSES_H
|
#ifdef HAVE_CURSES_H
|
||||||
if (use_curses) {
|
if (use_curses) {
|
||||||
int ch;
|
int ch;
|
||||||
int i;
|
|
||||||
const char *text;
|
|
||||||
|
|
||||||
if (interrupted) {
|
if (interrupted) {
|
||||||
struct winsize size;
|
struct winsize size;
|
||||||
|
@ -243,9 +279,9 @@ C_ProcessInput (void)
|
||||||
resizeterm (size.ws_row, size.ws_col);
|
resizeterm (size.ws_row, size.ws_col);
|
||||||
getmaxyx (stdscr, screen_y, screen_x);
|
getmaxyx (stdscr, screen_y, screen_x);
|
||||||
con_linewidth = screen_x;
|
con_linewidth = screen_x;
|
||||||
|
input_line->width = screen_x;
|
||||||
wrefresh (curscr);
|
wrefresh (curscr);
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ch = wgetch (input);
|
ch = wgetch (input);
|
||||||
|
@ -294,36 +330,6 @@ C_ProcessInput (void)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Con_ProcessInputLine (input_line, ch);
|
Con_ProcessInputLine (input_line, ch);
|
||||||
i = input_line->linepos - 1;
|
|
||||||
if (input_line->scroll > i)
|
|
||||||
input_line->scroll = i;
|
|
||||||
if (input_line->scroll < i - (screen_x - 2) + 1)
|
|
||||||
input_line->scroll = i - (screen_x - 2) + 1;
|
|
||||||
text = input_line->lines[input_line->edit_line] + input_line->scroll;
|
|
||||||
if ((int)strlen (text + 1) < screen_x - 2) {
|
|
||||||
text = input_line->lines[input_line->edit_line];
|
|
||||||
input_line->scroll = strlen (text + 1) - (screen_x - 2);
|
|
||||||
input_line->scroll = max (input_line->scroll, 0);
|
|
||||||
text += input_line->scroll;
|
|
||||||
}
|
|
||||||
wmove (input, 0, 0);
|
|
||||||
if (input_line->scroll) {
|
|
||||||
waddch (input, '<');
|
|
||||||
} else {
|
|
||||||
waddch (input, *text++);
|
|
||||||
}
|
|
||||||
for (i = 0; i < screen_x - 2 && *text; i++)
|
|
||||||
waddch (input, *text++);
|
|
||||||
while (i++ < screen_x - 2)
|
|
||||||
waddch (input, ' ');
|
|
||||||
if (*text) {
|
|
||||||
waddch (input, '>');
|
|
||||||
} else {
|
|
||||||
waddch (input, ' ');
|
|
||||||
}
|
|
||||||
wmove (input, 0, input_line->linepos - input_line->scroll);
|
|
||||||
touchline (stdscr, screen_y - 1, 1);
|
|
||||||
wrefresh (input);
|
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|
Loading…
Reference in New Issue