Skip to main content
Code Review

Return to Revisions

2 of 6
edited body
Edenia
  • 1.6k
  • 8
  • 24

Code for fake UI

While i was coding it, i got better idea.. but i continued because i can not compare both of those ideas, because they are different. The idea of that function is to make your fake ui-building easier AND more advanced.

void
write_ui
(char nodes[], int FOREGROUND, int BACKGROUND)
{
 int i;
 char buff[strlen(nodes)];
 strcpy(buff, nodes);
 HANDLE Handle = GetConsoleWindow();
 SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND | BACKGROUND);
 for(i=0; buff[i] != '0円'; i++)
 {
 if(buff[i] == '-') { printf("%c", 196); } // Horizontal line
 else if(buff[i] != '|' && buff[i] != '_' &&
 buff[i] != '^' && buff[i] != '~') { printf("%c", buff[i]); } // Any other character
 if(buff[i] == '|' && buff[i-1] != '-' && buff[i+1] != '-' &&
 buff[i-1] != '_' && buff[i+1] != '^' && buff[i-1] != '^') { printf("%c", 179); } // vertical line && !isdigit(buff[i])
 if(buff[i] == '|' && buff[i+1] == '_') { printf("\n%c", 192); } // bottom right corner
 if(buff[i] == '_' && buff[i+1] == '|') { printf("%c", 217); } // bottom left corner
 if(buff[i] == '|' && buff[i+1] == '^') { printf("%c", 218); } // top left corner
 if(buff[i] == '^' && buff[i+1] == '|') { printf("%c", 191); } // top right corner
 if(buff[i] == '-' && buff[i+1] == '|') { printf("%c", 180); } // ┤
 if(buff[i] == '|' && buff[i+1] == '-') { printf("%c", 195); } // ├
 if(buff[i] == '~')
 {
 int spaces = 0, len = 0, l = 0, multiplier = 1;
 do { len++; multiplier *= 10; } while(isdigit(buff[i+(len+1)])); multiplier /= 10;
 l = len;
 do { spaces += (chrtodigit(buff[(i+len+1)-l]) * multiplier); l--; multiplier /= 10; } while( l != -1 );
 splice_away(buff, i, i+len); // To cut the number passed as number of spaces from screen
 l = 0;
 do { l++; printf("%c", ' '); } while(l != spaces);
 }
 }
}
int chrtodigit (char chr)
{
 if(chr >= '0' && chr <= '9') { return chr-48; }
 else return 0;
}
int intlen (int integer)
{
 int number, len;
 for(number = integer; number != 0; len++, number/=10);
 return len;
}
void splice_away(char str[], int from, int to)
{
 memmove(str + from, str + to, 1 + strlen(str + to));
}

Example:

int main()
{
 int ui1=80;
 HANDLE Handle = GetConsoleWindow();
 SetWindowPos(Handle, HWND_TOP, 50, 50, 1000, 500, SWP_DRAWFRAME);
 SetConsoleTitle("E X A M P L E"); system("COLOR 1B");
 SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 9 | BACKGROUND_BLUE);
 while(ui1-->0) putchar(205);
 SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED);
 write_ui("|^----------------------^|\n|Test box - fake UI~4|\n|Written manually~6|\n|~22|_--------| Test |------_|", 1, 1);
 Sleep(10000);
 return 1;
}

Result: enter image description here

The function is cappable of a lot.. I would like to see some UIs created with this function by you guys. Here is how you can use the function:

String escape sequences: | For vertical line - For horizontal line -| For left pause (break for titles etc) |- For right pause |_ Bottom left corner _| Bottom right corner |^ Top left corner ^| Top left corner ~x Any number to represent count of spaces

Edenia
  • 1.6k
  • 8
  • 24
lang-c

AltStyle によって変換されたページ (->オリジナル) /