Skip to content
Snippets Groups Projects
Commit eb72e4bb authored by deuce's avatar deuce
Browse files

Overhaul of ansi_gotoxy() including better optimizations.

Fix usage of ansi_sendch() for chars which do not move the cursor a single
char to the right.
parent 7beb8524
No related branches found
No related tags found
No related merge requests found
...@@ -182,24 +182,27 @@ cfmakeraw(struct termios *t) ...@@ -182,24 +182,27 @@ cfmakeraw(struct termios *t)
} }
#endif #endif
/* Do NOT call this to output to the last column of the last row. */
/* ONLY call this for chars which will move the cursor */
void ansi_sendch(char ch) void ansi_sendch(char ch)
{ {
if(!ch) if(!ch)
ch=' '; ch=' ';
if(ansi_row<ansi_rows-1 || (ansi_row==ansi_rows-1 && ansi_col<ansi_cols-1)) { ansi_col++;
ansi_col++; if(ansi_col>=ansi_cols) {
if(ansi_col>=ansi_cols) { /* Column 80 sux0rz */
ansi_col=0; force_move=1;
ansi_row++; ansi_col=0;
if(ansi_row>=ansi_rows) { ansi_row++;
ansi_col=ansi_cols-1; if(ansi_row>=ansi_rows) {
ansi_row=ansi_rows-1; ansi_col=ansi_cols-1;
} ansi_row=ansi_rows-1;
} }
fwrite(&ch,1,1,stdout);
if(ch<' ')
force_move=1;
} }
fwrite(&ch,1,1,stdout);
/* We sent a control char... better make the next movement explicit */
if(ch<' ' && ch > 0)
force_move=1;
} }
void ansi_sendstr(char *str,int len) void ansi_sendstr(char *str,int len)
...@@ -249,6 +252,8 @@ int ansi_puttext(int sx, int sy, int ex, int ey, void* buf) ...@@ -249,6 +252,8 @@ int ansi_puttext(int sx, int sy, int ex, int ey, void* buf)
continue; continue;
ansivmem[y*ansi_cols+x]=sch; ansivmem[y*ansi_cols+x]=sch;
ansi_gotoxy(x+1,y+1); ansi_gotoxy(x+1,y+1);
if(y>=ansi_rows-1 && x>=ansi_cols-1)
continue;
if(attrib!=sch>>8) { if(attrib!=sch>>8) {
textattr(sch>>8); textattr(sch>>8);
attrib=sch>>8; attrib=sch>>8;
...@@ -555,7 +560,7 @@ int ansi_putch(int ch) ...@@ -555,7 +560,7 @@ int ansi_putch(int ch)
} }
break; break;
case 7: /* Bell */ case 7: /* Bell */
ansi_sendch(7); ansi_sendstr("\007",1);
break; break;
case '\t': case '\t':
for(i=0;i<10;i++) { for(i=0;i<10;i++) {
...@@ -599,79 +604,87 @@ void ansi_gotoxy(int x, int y) ...@@ -599,79 +604,87 @@ void ansi_gotoxy(int x, int y)
{ {
char str[16]; char str[16];
str[0]=0;
if(x < 1 if(x < 1
|| x > ansi_cols || x > ansi_cols
|| y < 1 || y < 1
|| y > ansi_rows) || y > ansi_rows)
return; return;
/* Movement forced... always send position code */
if(force_move) { if(force_move) {
force_move=0;
sprintf(str,"\033[%d;%dH",y,x); sprintf(str,"\033[%d;%dH",y,x);
ansi_sendstr(str,-1);
force_move=0;
ansi_row=y-1;
ansi_col=x-1;
return;
} }
else {
/* /* Moving to col 1 (and not already there)... use \r */
* If we're moving to column one, we can use \r if(x==1 && ansi_col) {
* Only use it if we're not already there though :-) ansi_sendstr("\r",1);
*/ ansi_col=0;
if(x==1 && ansi_col != 0) { }
ansi_sendch('\r');
force_move=0; /* Do we even NEED to move? */
ansi_col=0; if(x==ansi_col+1 && y==ansi_row+1)
} return;
/* If we're already on the correct column */
if(x==ansi_col+1) { /* If we're already on the correct column */
/* We're already in the right place! */ if(x==ansi_col+1) {
if(y==ansi_row+1) { /* Do we need to move up? */
str[0]=0; if(y<ansi_row+1) {
} if(y==ansi_row)
else { /* Only up one */
/* We need to move up */ strcpy(str,"\033[A");
if(y<ansi_row+1) { else
/* Only one, no number required */ sprintf(str,"\033[%dA",ansi_row+1-y);
if(y==ansi_row) ansi_sendstr(str,-1);
strcpy(str,"\033[A"); ansi_row=y-1;
else return;
sprintf(str,"\033[%dA",ansi_row+1-y);
}
/* Need to move down */
else {
/* Only one row */
if(y==ansi_row+2)
strcpy(str,"\033[B");
else
sprintf(str,"\033[%dB",y-ansi_row-1);
}
}
} }
/* We need to change the column at least */
else { /* We must have to move down then. */
/* We're on the right row, just move left or right */ /* Only one, use a newline */
if(y==ansi_row+1) { if(y==ansi_row+2)
/* Move left */ strcpy(str,"\n");
if(x<ansi_col+1) { else
if(x==ansi_col) sprintf(str,"\033[%dB",y-ansi_row-1);
strcpy(str,"\033[D"); ansi_sendstr(str,-1);
else ansi_row=y-1;
sprintf(str,"\033[%dD",ansi_col+1-x); return;
} }
/* Move right */
else { /* Ok, we need to change the column then... is the row right though? */
if(x==ansi_col+2) if(y==ansi_row+1) {
strcpy(str,"\033[C"); /* Do we need to move left then? */
else if(x<ansi_col+1) {
sprintf(str,"\033[%dC",x-ansi_col-1); if(x==ansi_col)
} strcpy(str,"\033[D");
} else
/* Do a full move then... row and col need changing. */ sprintf(str,"\033[%dD",ansi_col+1-x);
else { ansi_sendstr(str,-1);
sprintf(str,"\033[%d;%dH",y,x); ansi_col=x-1;
} return;
} }
/* Must need to move right then */
if(x==ansi_col+2)
strcpy(str,"\033[C");
else
sprintf(str,"\033[%dC",x-ansi_col-1);
ansi_sendstr(str,-1);
ansi_col=x-1;
return;
} }
/* Changing the row and the column... better use a fill movement then. */
sprintf(str,"\033[%d;%dH",y,x);
ansi_sendstr(str,-1); ansi_sendstr(str,-1);
ansi_row=y-1; ansi_row=y-1;
ansi_col=x-1; ansi_col=x-1;
return;
} }
void ansi_gettextinfo(struct text_info *info) void ansi_gettextinfo(struct text_info *info)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment