Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/07_colors.c
Original file line number Diff line number Diff line change
@@ -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);

}
}
14 changes: 14 additions & 0 deletions include/ns_color.h
Original file line number Diff line number Diff line change
@@ -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