Skip to content

Commit

Permalink
Add small history buffer to aline.c.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirWumpus committed Oct 22, 2024
1 parent 76fd6f5 commit 75a2640
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ To leave Post4 either type the `EOF` terminal character, `BYE`, or `code BYE-COD

Post4 implements simple TTY style line editing for the last interactive input line. GNU `readline` or BSD `libedit` are not supported. Essentially the `stty(1)` keys are supported, in addition to being able to edit the last input line.

up ^K Edit the previous input line.
up ^K Cycle to the previous input line to edit.
down ^L Cycle to the next input line to edit.
left right Cursor left or right within line.
ERASE ^H ^? Erase character before the cursor.
WERASE Erase the previous white space delimited word.
Expand Down
23 changes: 14 additions & 9 deletions src/aline.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ int is_tty;
static int tty_fd = -1;
static int tty_mode = 0;
static struct termios tty_modes[3];
static char lastline[256];
static char history[ALINE_HISTORY][MAX_INPUT];
static const char *ps2;
static int lastline;

#define tty_saved tty_modes[ALINE_CANONICAL]
#define tty_raw tty_modes[ALINE_RAW]
Expand Down Expand Up @@ -136,7 +137,7 @@ int
alineInput(FILE *fp, const char *prompt, char *buf, size_t size)
{
unsigned i;
int ch, pcol, pos[2];
int ch, pcol, pos[2], prevline = lastline;

if (buf == NULL || size < 1) {
return EOF;
Expand All @@ -153,8 +154,8 @@ alineInput(FILE *fp, const char *prompt, char *buf, size_t size)
prompt = ps2;
}
pcol = strlen(prompt);
if (sizeof (lastline) < size) {
size = sizeof (lastline);
if (sizeof (*history) < size) {
size = sizeof (*history);
}
alineGetRowCol(pos);
(void) printf(ANSI_SAVE_CURSOR);
Expand All @@ -171,8 +172,10 @@ alineInput(FILE *fp, const char *prompt, char *buf, size_t size)
if (ch == '\e') {
if ((ch = fgetc(stdin)) == '[') {
ch = fgetc(stdin);
if (ch == 'A' || ch == 'B') {
if (ch == 'A') {
ch = '\v';
} else if (ch == 'B') {
ch = '\f';
} else if (ch == 'C') {
i += i < size && buf[i] != '\0';
continue;
Expand All @@ -182,10 +185,11 @@ alineInput(FILE *fp, const char *prompt, char *buf, size_t size)
}
}
}
if (ch == '\v') {
if (ch == '\v' || ch == '\f') {
/* Restore intput to last input line. */
(void) strncpy(buf, lastline, size-1);
i = strlen(lastline);
prevline = (prevline+(ch == '\v' ? -1 : 1)) & (ALINE_HISTORY-1);
(void) strncpy(buf, history[prevline], size-1);
i = strlen(history[prevline]);
} else if (ch == tty_saved.c_cc[VERASE] || ch == '\b' || ch == 127) {
if (0 < i) {
i--;
Expand All @@ -210,7 +214,8 @@ alineInput(FILE *fp, const char *prompt, char *buf, size_t size)
}
}
if (0 < i) {
(void) strncpy(lastline, buf, size-1);
(void) strncpy(history[lastline], buf, size-1);
lastline = (lastline+1) & (ALINE_HISTORY-1);
}
return (int) strlen(buf);
}
Expand Down
3 changes: 3 additions & 0 deletions src/aline.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ extern "C" {
#include "ansiterm.h"

#include <stdio.h>
#include <limits.h>

#ifdef HAVE_TERMIOS_H
# include <termios.h>
Expand All @@ -50,6 +51,8 @@ extern struct winsize window;
#define ALINE_RAW 1
#define ALINE_RAW_NB 2

#define ALINE_HISTORY 4

extern void alineInit(void);
extern void alineFini(void);
extern int alineReadByte(void);
Expand Down

0 comments on commit 75a2640

Please sign in to comment.