HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

*Code for fake UI

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
codeforfake

Problem

While I was coding it, I got a 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

Solution

(char nodes[], int FOREGROUND, int BACKGROUND)


Your function does not modify nodes so it should be declared const (const char nodes[]).

Normally, all-uppercase identifiers are used for macros, not function parameters. Consider calling these foreground and background.

do { spaces += (chrtodigit(buff[(i+len+1)-l]) * multiplier); l--; multiplier /= 10; } while( l != -1 );


Code like the above is trying way too hard to write loops in one line. This harms readability because it's not clear what the loop body is and what the terminating condition is. Suggest writing loops like this on multiple lines. There are other instances of this such as the loop in intlen.

In your handling of ~, you can combine the two loops that measure and then convert the integer, into one. Or, call a function such as strtol() that does the job for you.

You don't need the splice_away() function at all. You can replace the call to it with i += len to skip over the integer in the input. This also means you don't need buff, and can use nodes directly instead of copying it to buff.

intlen() does not appear to be used anywhere in your code. Also, it doesn't properly handle negative numbers.

while(ui1-->0) putchar(205);


The above appears to be a gratuitous use of the --> operator and should probably be written in a more conventional style. For example, for (ui1 = 0; ui1 < 80; ui1++).

Code Snippets

(char nodes[], int FOREGROUND, int BACKGROUND)
do { spaces += (chrtodigit(buff[(i+len+1)-l]) * multiplier); l--; multiplier /= 10; } while( l != -1 );
while(ui1-->0) putchar(205);

Context

StackExchange Code Review Q#67247, answer score: 5

Revisions (0)

No revisions yet.