Skip to content

Commit 84a648a

Browse files
committed
Merge branch 'dev' into master
2 parents 9391bfd + 177ecfb commit 84a648a

File tree

6 files changed

+121
-23
lines changed

6 files changed

+121
-23
lines changed

ConsolePrettify.depend

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# depslib dependency file v1.0
2-
1612746085 source:c:\users\corne\documents\c_projects\consoleprettify\prettify_functions.c
2+
1612834203 source:c:\users\corne\documents\c_projects\consoleprettify\prettify_functions.c
33
"ConsolePrettify.h"
44

5-
1612745298 c:\users\corne\documents\c_projects\consoleprettify\consoleprettify.h
5+
1612829669 c:\users\corne\documents\c_projects\consoleprettify\consoleprettify.h
66
<stdio.h>
77
<stdlib.h>
88
<string.h>
@@ -18,12 +18,12 @@
1818
<stdbool.h>
1919
"CYPHER.H"
2020

21-
1612753080 source:c:\users\corne\documents\c_projects\consoleprettify\main.c
21+
1612830218 source:c:\users\corne\documents\c_projects\consoleprettify\main.c
2222
<stdio.h>
2323
<stdlib.h>
2424
<math.h>
2525
"ConsolePrettify.h"
2626

27-
1601352266 source:c:\users\corne\documents\c_projects\consoleprettify\prettify_extras.c
27+
1612831493 source:c:\users\corne\documents\c_projects\consoleprettify\prettify_extras.c
2828
"ConsolePrettify.h"
2929

ConsolePrettify.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void cp_table(char *, ...);
7070

7171
//menu
7272
int cp_menu(char *, int, ...);
73+
int cp_menu2(char *, int, ...);
7374

7475
//ordered lists
7576
void cp_ilist(char *, int[], int);
@@ -78,4 +79,6 @@ void cp_dlist(char *, double[], int);
7879
void cp_clist(char *, char[], int);
7980
void cp_slist(char *, char[][MAX_ARRAY_LENGTH], int);
8081

82+
void repeatChar (char ch, char* ends, char* prefix, int times);
83+
8184
#endif // CONSOLEPRETTIFY_H_INCLUDED

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,25 @@ _input_
2020
`cp_password(variable)` | string input box with masking eg. *****
2121
_selection_
2222
`cp_menu(title, numOptions, ...)` | Displays a menu and returns the number of the selected option
23+
`cp_menu2(title, numOptions, ...)` | cp_menu but uses arrow keys for selection
2324
_lists_
2425
`cp_ilist(title, items, numItems)` | Displays an ordered list of integers
2526
`cp_flist(title, items, numItems)` | Displays an ordered list of floats
2627
`cp_clist(title, items, numItems)` | Displays an ordered list of characters
2728
`cp_slist(title, items, numItems)` | Displays an ordered list of strings
2829

2930
---
30-
> _**NOTE**: Please use `cp_textcolor()` instaed of system("color xx")._
31+
> _**NOTE**: Please use `cp_textcolor()` instaed of system("color xx")_
3132
3233
> _**NOTE**: `cp_textbox()` accepts ONE variable. Do not use multiple specifiers_
3334
35+
> _**NOTE**: Using newlines(\n) in `cp_menu`, `cp_menu2`, or `cp_print` will mess up the formatting_
36+
3437
<br>
3538

3639
## Available Colors 🌈
37-
use any of these constants as the color arguments of prettify functions
38-
Constant Name | Value
40+
use any of these constants as the color argument of `cp_textcolor()`
41+
Constant Name | _
3942
--- | ---
4043
RED | 12
4144
GREEN | 10
@@ -49,6 +52,8 @@ CYAN | 3
4952
GRAY | 7
5053
DARK_GRAY | 8
5154

55+
Example: `cp_textcolor (BLUE);`
56+
5257
<br>
5358

5459
# Examples
@@ -69,11 +74,19 @@ cp_password (testString);
6974
7075
## Menus
7176
```c
77+
//normal menu
7278
int choice = cp_menu("Select an option", 4,
7379
"Buy bananas",
7480
"Buy apple",
7581
"Buy mango",
7682
"Buy grapes");
83+
//arrow key selection menu
84+
choice = cp_menu2("Select an option", 5,
85+
"Buy bananas",
86+
"Buy apple",
87+
"Buy mango",
88+
"Buy grapes",
89+
"Buy Bananas");
7790
```
7891
![Output](images/output2.gif)
7992

@@ -83,7 +96,7 @@ int choice = cp_menu("Select an option", 4,
8396
float items[20] = {200, 10, 80, 900, 20, 10, 80, 900, 209, 50};
8497
cp_flist ("List of numbers", items, 10);
8598
```
86-
>![Output](images/output3.gif)
99+
![Output](images/output3.gif)
87100
88101
## TODO
89102
- Add automatic Table printing

main.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@ int main()
1414
char testString[25];
1515

1616
cp_textcolor (GREEN); //change color
17-
//cp_print("\b HELLO TO %s\n", "CONSOLE PRETTIFY");
17+
cp_print("\b HELLO TO %s", "CONSOLE PRETTIFY");
18+
19+
cp_menu2("Select an option", 5,
20+
"Buy bananas",
21+
"Buy apple",
22+
"Buy mango",
23+
"Buy grapes",
24+
"Buy Bananas");
25+
26+
cp_textcolor(BLUE);
27+
1828

1929
printf (" Enter your username");
2030
cp_textbox ("%s", testString);
2131

2232
printf (" Enter your password:");
2333
cp_password (testString);
2434

25-
system("cls");
26-
27-
/////////////////////////////////
28-
int choice = cp_menu("Select an option", 4,
29-
"Buy bananas",
30-
"Buy apple",
31-
"Buy mango",
32-
"Buy grapes");
33-
3435
////////////////////////////////
3536
/*float items[20] = {200, 10, 80, 900, 20, 10, 80, 900, 209, 50};
3637
cp_flist("List of numbers", items, 10);

prettify_extras.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
////////////////README/////////////////
1717
//////////////////////////////////////
18-
THIS FILE EXTENDS THE FUNCTIONS OF THE CONSOLE_PRETTIFY FRAMEWORK.
18+
THIS FILE EXTENDS THE FUNCTIONS OF THE CONSOLE_PRETTIFY TOOL.
1919
PRETTIFY_EXTRAS COMBINES FUNCTIONS FROM PRETTIFY AS WELL AS INTRODUCES NEW FUNCTIONS TO CREATE MORE COMPLEX USER INTERFACES
2020
2121
NOTE: prettify_functions.c and ConsolePrettify.h MUST BE PRESENT IN YOUR PROJECT

prettify_functions.c

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ void error(char function[25], char msg[100])
2828
exit(EXIT_FAILURE);
2929
}
3030

31-
void cp_setCentered(int isCentered)
32-
{
33-
34-
}
3531

3632
// CHANGES TEXT COLOR
3733
void cp_textcolor(int COLOR){
@@ -209,6 +205,90 @@ int cp_menu(char *title, int numOptions, ...)
209205
}
210206
}
211207

208+
int cp_menu2(char *title, int numOptions, ...)
209+
{
210+
int i, position;
211+
int maxLength = strlen(title);
212+
//if its the 20th option thn, then digitsInOption will be 2
213+
char key;
214+
va_list args;
215+
COORD coord;//where to put the cursor
216+
CONSOLE_SCREEN_BUFFER_INFO cursor;//the cursor
217+
218+
fflush(stdin);
219+
220+
va_start(args, numOptions);
221+
222+
if (strlen("Use arrow keys") > maxLength)
223+
maxLength = strlen("Use arrow keys");
224+
225+
//find the longest text to be displayed
226+
for (i = 0; i < numOptions; i++)
227+
{
228+
int len = strlen(va_arg(args, char *));
229+
if(len > maxLength)
230+
maxLength = len;
231+
}
232+
233+
maxLength += 5;
234+
repeatChar('_', " ", "\n ", maxLength + 2);
235+
printf ("\n | %-*s |", maxLength, title);
236+
repeatChar('-', "|", "\n ", maxLength + 2);
237+
238+
//list the options
239+
va_start(args, numOptions);
240+
for (i = 1; i <= numOptions; i++)
241+
{
242+
char* option = va_arg(args, char *);
243+
printf("\n | %-*s|", maxLength -1 , option);
244+
}
245+
246+
va_end(args);
247+
248+
//textbox part
249+
repeatChar('_', "|", "\n ", maxLength + 2);
250+
printf("\n | %-*s|", maxLength+1, "Use arrow keys");
251+
repeatChar('_', "|", "\n ", maxLength + 2);
252+
253+
//move cursor to first option
254+
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
255+
coord.X = 3;
256+
coord.Y = cursor.dwCursorPosition.Y - numOptions - 2;
257+
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
258+
259+
position = 1;
260+
while ((key = getch()) != '\r'){
261+
262+
if (key == 72) //up arrow
263+
{
264+
if (position != 1)
265+
{
266+
position--;
267+
//move the cursor to the correct option
268+
coord.Y --;
269+
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
270+
}
271+
}
272+
else if (key == 80) //down arrow
273+
{
274+
if (position != numOptions)
275+
{
276+
position++;
277+
coord.Y ++;
278+
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
279+
}
280+
}
281+
}
282+
283+
///re-place the cursor outside the box
284+
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor); //get the cursor
285+
coord.X = 0;
286+
coord.Y = cursor.dwCursorPosition.Y + (numOptions - position) + 5;
287+
288+
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
289+
290+
return position;
291+
}
212292

213293
//DISPLAYS AN ORDERED LIST
214294
/// EX. cp_ilist("Select an option", items, 3);
@@ -381,6 +461,7 @@ void cp_print(char *format, ...)
381461
centerPos.X = (screenWwidth - strlen(format)) / 2;
382462
centerPos.Y = csbi.dwCursorPosition.Y;
383463
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), centerPos);
464+
384465
/// print
385466
vprintf(format, args);
386467

0 commit comments

Comments
 (0)