-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender.c
More file actions
128 lines (106 loc) · 3.51 KB
/
render.c
File metadata and controls
128 lines (106 loc) · 3.51 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tmd.h"
char* read_stdin();
char* file_contents(FILE* file);
void print_help_message();
int main(int argc, char** argv)
{
argv++; // advance to first actual argument
bool help_flag = *argv && (!strcmp(*argv, "-h") || !strcmp(*argv, "--help"));
bool unformat_flag = *argv && (!strcmp(*argv, "-u") || !strcmp(*argv, "--unformatted"));
if (unformat_flag) argv++;
if (argc == 2 && help_flag)
print_help_message();
else if (argc == 1 || (argc == 2 && unformat_flag)) // read from stdin if no files provided
{
char* original_text = read_stdin();
format_and_print(original_text, !unformat_flag);
free(original_text);
}
else for (; *argv; argv++)
{
FILE* file = fopen(*argv, "r");
if (file == NULL)
{
char* ERROR = "\e[1m\e[31mError:\e[39m\e[22m"; // bold and red 'Error:' text
fprintf(stderr, "%s couldn't read file '%s'.\n", ERROR, *argv);
continue;
}
char* original_text = file_contents(file);
format_and_print(original_text, !unformat_flag);
free(original_text);
fclose(file);
}
return 0;
}
char* read_stdin()
{
int text_capacity = 8;
int text_index = 0;
char* text = calloc(text_capacity, sizeof(char));
for (int c; (c = getchar()) != EOF; text_index++)
{
if (text_index == text_capacity - 1) // -1 to guarantee space for NULL
{
text_capacity *= 2;
text = realloc(text, text_capacity);
}
text[text_index] = c;
}
text[text_index] = '\0';
return text;
}
char* file_contents(FILE* file)
{
fseek(file, 0L, SEEK_END);
long numbytes = ftell(file);
fseek(file, 0L, SEEK_SET);
char* buffer = calloc(numbytes + 1, sizeof(char));
fread(buffer, sizeof(char), numbytes, file);
return buffer;
}
void print_help_message()
{
char* help_lines[] = {
"_#Options:# _",
"",
" |b #-u# |B #_--unformatted_# |",
" |b |B *Prints text without any styling.* |",
"",
" |b #-h# |B #_--help_# |",
" |b |B *Prints help information.* |",
"",
"_#Special Characters:# _",
"",
" ^G\\#^ : #BOLD#",
" ^G\\~^ : ~DIM~",
" ^G\\*^ : *ITALIC*",
" ^G\\_^ : _UNDERLINED_",
" ^G\\@^ : @BLINKING@",
" ^G\\$^ : $INVERTED$",
" ^G\\`^ : `HIDDEN` ~(hidden)~",
" ^G\\%^ : %STRIKETHROUGH%",
" ^G\\^^MX^ : ^rFO^gRE^yGR^bOU^mND^cS^",
" ^G\\|^MX^ : |rBA|gCK|yGR|bOU|mND|cS|",
"",
"_#Available Colors:# _",
"",
" ^MX^ = l : ^lBLACK ^ L : ^LWHITE ^ ~('l' for 'lightness')~",
" ^MX^ = r : ^rRED ^ R : ^RBRIGHT RED ^",
" ^MX^ = g : ^gGREEN ^ G : ^GBRIGHT GREEN ^",
" ^MX^ = y : ^yYELLOW ^ Y : ^YBRIGHT YELLOW ^",
" ^MX^ = b : ^bBLUE ^ B : ^BBRIGHT BLUE ^",
" ^MX^ = c : ^cCYAN ^ C : ^CBRIGHT CYAN ^",
" ^MX^ = m : ^mMAGENTA ^ M : ^MBRIGHT MAGENTA ^",
"",
"*Use a backslash to escape these characters.*",
NULL,
};
for (char** line = help_lines; *line; line++)
{
format_and_print(*line, true);
printf("\n");
}
}