-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.cpp
More file actions
158 lines (145 loc) · 4.48 KB
/
printer.cpp
File metadata and controls
158 lines (145 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "printer.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <chrono>
#include <thread>
#include <assert.h>
/// <summary>
/// Simply copies to destination buffer
/// </summary>
/// <param name="dest">Target Buffer</param>
/// <param name="text">Source C-String</param>
void printerFormatText(char* dest, const char* text)
{
printerFormatText(dest, text, _text_foreground_default);
}
/// <summary>
/// Adds Foregound text colors to a given text and copy to destination buffer
/// </summary>
/// <param name="dest">Target Buffer</param>
/// <param name="text">Source C-String</param>
/// <param name="foreground">Text Color</param>
void printerFormatText(char* dest, const char* text,
const e_text_foreground_colors foreground)
{
printerFormatText(dest, text, foreground, _text_background_default);
}
/// <summary>
/// Adds Foregound and Background text colors to a given text and copy to destination buffer
/// </summary>
/// <param name="dest">Target Buffer</param>
/// <param name="text">Source C-String</param>
/// <param name="foreground">Text Color</param>
/// <param name="background">Text Highlight color</param>
void printerFormatText(char* dest, const char* text,
const e_text_foreground_colors foreground,
const e_text_background_colors background)
{
printerFormatText(dest, text, foreground, background, _text_style_reset);
}
/// <summary>
/// Style a C-String Text as you like
/// </summary>
/// <param name="dest">Target Buffer</param>
/// <param name="text">Source C-String</param>
/// <param name="foreground">Text Color</param>
/// <param name="background">Text Highlight color</param>
/// <param name="style">Additional Options</param>
void printerFormatText(char* dest, const char* text,
const e_text_foreground_colors foreground,
const e_text_background_colors background,
const e_text_styles style)
{
int result = 0;
assert(IN_RANGE(foreground, _text_foreground_start, _text_foreground_end));
assert(IN_RANGE(background, _text_background_start, _text_background_end));
result = snprintf(dest,
MAX_BUFFER_LENGTH,
"\033[%d;%d;%dm%s\033[0m",
style,
foreground,
background,
text);
assert(result >= 0 && result < MAX_BUFFER_LENGTH);//if this happens then verify text length or arguments
}
/// <summary>
/// Prints text with default settings
/// </summary>
/// <param name="text">Source C-String</param>
void printerPrintText(const char* text)
{
printerPrintText(text, _text_foreground_default);
}
/// <summary>
/// Prints text with added Foreground color
/// </summary>
/// <param name="text">Source C-String</param>
/// <param name="foreground">Text Color</param>
void printerPrintText(const char* text,
const e_text_foreground_colors foreground)
{
printerPrintText(text, foreground, _text_background_default);
}
/// <summary>
/// Prints text with added Foreground and Background colors
/// </summary>
/// <param name="text">Source C-String</param>
/// <param name="foreground">Text Color</param>
/// <param name="background">Text Highlight color</param>
void printerPrintText(const char* text,
const e_text_foreground_colors foreground,
const e_text_background_colors background)
{
printerPrintText(text, foreground, background, _text_style_reset);
}
/// <summary>
/// Print a C-String Text as you like
/// </summary>
/// <param name="text">Source C-String</param>
/// <param name="foreground">Text Color</param>
/// <param name="background">Text Highlight color</param>
/// <param name="style">Additional Options</param>
void printerPrintText(const char* text,
const e_text_foreground_colors foreground,
const e_text_background_colors background,
const e_text_styles style)
{
char buffer[MAX_BUFFER_LENGTH];
printerFormatText(buffer, text, foreground, background, style);
puts(buffer);
}
/// <summary>
/// Print a C-String slowly with DEFAULT_DELAY_MS interval
/// </summary>
/// <param name="text"></param>
void printerSlowPrint(const char* text)
{
printerSlowPrint(text, DEFAULT_DELAY_MS);
}
/// <summary>
/// Print a C-String slowly with delay between each characters
/// </summary>
/// <param name="text"></param>
/// <param name="delay"></param>
void printerSlowPrint(const char* text, int delay)
{
int len = strnlen(text, MAX_BUFFER_LENGTH);
for (int i = 0; i < len; i++)
{
putchar(text[i]);
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
}
}
/// <summary>
/// Clears the Console window
/// </summary>
void printerClearScreen()
{
#if PLATFORM_WINDOWS
system("cls");
#endif // PLATFORM_WINDOWS
#if PLATFORM_LINUX
system("clear");
#endif // PLATFORM_LINUX
}