diff --git a/examples/07_colors.c b/examples/07_colors.c new file mode 100644 index 0000000..bb372a1 --- /dev/null +++ b/examples/07_colors.c @@ -0,0 +1,39 @@ +#include "../include/ns.h" +#include "../include/ns_color.h" + +int main() +{ + ns_println("Printing in colors"); + ns_println(NS_COLOR_GREEN "Green" NS_COLOR_RESET); + + ns_error_t err; + + int age; + + ns_print(NS_COLOR_YELLOW "Enter age: " ); + + NS_TRY(err, ns_read(&age)) { + ns_print(NS_COLOR_RESET); + ns_print(NS_COLOR_GREEN "Success, you entered : "); + ns_println(age); + ns_print(NS_COLOR_RESET); + } + NS_EXCEPT(err, NS_ERROR_INVALID_INPUT) { + + // Yellow for a user input warning + ns_println(NS_COLOR_RED "That is not a valid number! Please enter digits only." NS_COLOR_RESET); + + } + NS_EXCEPT(err, NS_ERROR_IO_READ) { + + ns_println(NS_COLOR_RED "Failed to read from standard input." NS_COLOR_RESET); + + } + NS_EXCEPT(err, NS_ERROR_ANY) { + + ns_print(NS_COLOR_RED "An error occurred: "); + ns_println(ns_error_message(err)); + ns_print(NS_COLOR_RESET); + + } +} diff --git a/include/ns_color.h b/include/ns_color.h new file mode 100644 index 0000000..4ca54d6 --- /dev/null +++ b/include/ns_color.h @@ -0,0 +1,14 @@ +#ifndef NS_COLOR_H +#define NS_COLOR_H + +// Standard ANSI Escape Codes for Terminal Colors +#define NS_COLOR_RESET "\x1b[0m" +#define NS_COLOR_RED "\x1b[31m" +#define NS_COLOR_GREEN "\x1b[32m" +#define NS_COLOR_YELLOW "\x1b[33m" +#define NS_COLOR_BLUE "\x1b[34m" +#define NS_COLOR_MAGENTA "\x1b[35m" +#define NS_COLOR_CYAN "\x1b[36m" +#define NS_COLOR_BOLD "\x1b[1m" + +#endif // !NS_COLOR_H