Better message reading using arrow keys.

This commit is contained in:
Andrew Pamment 2016-11-30 21:45:23 +10:00
parent e9b84728f8
commit 51e130db00
2 changed files with 124 additions and 88 deletions

View File

@ -184,3 +184,4 @@ Login Name:
\r\n-------------------------------------\r\n \r\n-------------------------------------\r\n
Is this Correct? (Y/N) Is this Correct? (Y/N)
\e[1;37mPress any key to continue...\e[0m \e[1;37mPress any key to continue...\e[0m
\e[24;1H\e[1;37;44mUp / Down to Scroll, Left / Right Change Message, R reply, Q quit\e[K\e[0m

View File

@ -765,6 +765,11 @@ void read_message(struct user_record *user, struct msg_headers *msghs, int mailn
int chars = 0; int chars = 0;
int ansi; int ansi;
int sem_fd; int sem_fd;
char **msg_lines;
int msg_line_count;
int start_line;
int should_break;
int position;
jb = open_jam_base(conf.mail_conferences[user->cur_mail_conf]->mail_areas[user->cur_mail_area]->path); jb = open_jam_base(conf.mail_conferences[user->cur_mail_conf]->mail_areas[user->cur_mail_area]->path);
if (!jb) { if (!jb) {
@ -845,92 +850,104 @@ void read_message(struct user_record *user, struct msg_headers *msghs, int mailn
lines = 0; lines = 0;
chars = 0; chars = 0;
msg_line_count = 0;
start_line = 0;
// count the number of lines...
for (z=0;z<z2;z++) { for (z=0;z<z2;z++) {
if (body[z] == '\r' || chars == 79) { if (body[z] == '\r' || chars == 79) {
chars = 0; if (msg_line_count == 0) {
if (body[z] == '\r') { msg_lines = (char **)malloc(sizeof(char *));
s_printf("\r\n");
} else { } else {
s_putchar(body[z]); msg_lines = (char **)realloc(msg_lines, sizeof(char *) * (msg_line_count + 1));
} }
lines++;
if (lines >= 17) { msg_lines[msg_line_count] = (char *)malloc(sizeof(char) * (z - start_line + 1));
s_printf(get_string(185));
s_getc(); if (z == start_line) {
lines = 0; msg_lines[msg_line_count][0] = '\0';
s_printf("\e[7;1H\e[0J"); } else {
strncpy(msg_lines[msg_line_count], &body[start_line], z - start_line);
msg_lines[msg_line_count][z-start_line] = '\0';
} }
} else if (body[z] == '\e' && body[z + 1] == '[') { msg_line_count++;
if (body[z] == '\r') {
start_line = z + 1;
} else {
start_line = z;
}
chars = 0;
} else {
if (body[z] == '\e') {
ansi = z; ansi = z;
while (strchr("ABCDEFGHIGJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", body[z]) == NULL) while (strchr("ABCDEFGHIGJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", body[z]) == NULL)
z++; z++;
if (body[z] == 'm') { if (body[z] == 'm') {
strncpy(buffer, &body[ansi], (z - ansi) + 1); // do nothing
buffer[z - ansi + 1] = '\0';
s_printf("%s", buffer);
} else if (body[z] == 'A') {
j = atoi(&body[ansi + 2]);
if (j == 0 && ansi + 2 == z) {
j = 1;
}
for (i=0;i<j;i++) {
if (lines - 1 >= 0) {
s_printf("\e[A");
lines--;
} else {
break;
}
}
} else if (body[z] == 'C') { } else if (body[z] == 'C') {
j = atoi(&body[ansi + 2]); chars += atoi(&body[ansi + 2]);
if (j == 0 && ansi + 2 == z) {
j = 1;
}
for (i=0;i<j;i++) {
if (chars + 1 <= 79) {
s_printf("\e[C");
chars++;
} else { } else {
break; i = strlen(body);
} for (j=ansi;j<i;j++) {
} body[ansi++] = body[j];
} else if (body[z] == 'B') {
j = atoi(&body[ansi + 2]);
if (j == 0 && ansi + 2 == z) {
j = 1;
}
for (i=0;i<j;i++) {
if (lines + 1 < 17) {
s_printf("\e[B");
lines++;
} else {
break;
}
}
} else if (body[z] == 'D') {
j = atoi(&body[ansi + 2]);
if (j == 0 && ansi + 2 == z) {
j = 1;
}
for (i=0;i<j;i++) {
if (chars - 1 >= 0) {
s_printf("\e[D");
chars--;
} else {
break;
}
}
}
} else {
chars++;
s_putchar(body[z]);
} }
} }
s_printf(get_string(112)); } else {
chars ++;
}
}
}
lines = 0;
position = 0;
should_break = 0;
while (!should_break) {
s_printf("\e[7;1H\e[0J");
for (z=position;z<msg_line_count;z++) {
s_printf("%s\e[K\r\n", msg_lines[z]);
if (z - position >= 16) {
break;
}
}
s_printf(get_string(186));
c = s_getc(); c = s_getc();
if (c == 'r') {
should_break = 1;
} else if (c == 'q') {
should_break = 1;
} else if (c == '\e') {
c = s_getc();
if (c == 91) {
c = s_getc();
if (c == 65) {
position--;
if (position < 0) {
position = 0;
}
} else if (c == 66) {
position++;
if (position + 16 > msg_line_count) {
position--;
}
} else if (c == 67) {
c = ' ';
should_break = 1;
} else if (c == 68) {
c = 'b';
should_break = 1;
}
}
}
}
if (tolower(c) == 'r') { if (tolower(c) == 'r') {
JAM_CloseMB(jb); JAM_CloseMB(jb);
if (user->sec_level < conf.mail_conferences[user->cur_mail_conf]->mail_areas[user->cur_mail_area]->write_sec_level) { if (user->sec_level < conf.mail_conferences[user->cur_mail_conf]->mail_areas[user->cur_mail_area]->write_sec_level) {
@ -1003,6 +1020,10 @@ void read_message(struct user_record *user, struct msg_headers *msghs, int mailn
free(subject); free(subject);
free(to); free(to);
free(from); free(from);
for (i=0;i<msg_line_count;i++) {
free(msg_lines[i]);
}
free(msg_lines);
return; return;
} }
@ -1172,6 +1193,10 @@ void read_message(struct user_record *user, struct msg_headers *msghs, int mailn
free(to); free(to);
free(from); free(from);
dolog("Failed to lock msg base!"); dolog("Failed to lock msg base!");
for (i=0;i<msg_line_count;i++) {
free(msg_lines[i]);
}
free(msg_lines);
return; return;
} }
} }
@ -1216,6 +1241,7 @@ void read_message(struct user_record *user, struct msg_headers *msghs, int mailn
} }
} else if (tolower(c) == 'q') { } else if (tolower(c) == 'q') {
free(body);
doquit = 1; doquit = 1;
} else if (c == ' ') { } else if (c == ' ') {
mailno++; mailno++;
@ -1223,11 +1249,20 @@ void read_message(struct user_record *user, struct msg_headers *msghs, int mailn
s_printf(get_string(118)); s_printf(get_string(118));
doquit = 1; doquit = 1;
} }
free(body);
} else if (tolower(c) == 'b') { } else if (tolower(c) == 'b') {
if (mailno > 0) { if (mailno > 0) {
mailno--; mailno--;
} }
free(body);
} else {
free(body);
} }
for (i=0;i<msg_line_count;i++) {
free(msg_lines[i]);
}
free(msg_lines);
msg_line_count = 0;
} }
} }