Skip to content

Commit 85e5faf

Browse files
committed
Add file reading support for the message
It basically just checks if the given message is a readable file, read the content and uses this as msg. I also added multiline support for the message. Closes: #134
1 parent 0365a63 commit 85e5faf

File tree

1 file changed

+81
-16
lines changed

1 file changed

+81
-16
lines changed

cmatrix.c

+81-16
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ int *updates = NULL; /* What does this do again? */
8787
volatile sig_atomic_t signal_status = 0; /* Indicates a caught signal */
8888
#endif
8989

90+
#ifdef _WIN32
91+
char *DELIMITER = "\r\n";
92+
#else
93+
char *DELIMITER = "\n";
94+
#endif
95+
9096
int va_system(char *str, ...) {
9197

9298
va_list ap;
@@ -115,6 +121,7 @@ void finish(void) {
115121
exit(0);
116122
}
117123

124+
118125
/* What we do when we're all set to exit */
119126
void c_die(char *msg, ...) {
120127

@@ -140,6 +147,32 @@ void c_die(char *msg, ...) {
140147
exit(0);
141148
}
142149

150+
/* Reads the content of a give path into a char pointer */
151+
char *read_file(char *path) {
152+
FILE *file;
153+
char *buf;
154+
long fs;
155+
156+
file = fopen(path, "r");
157+
if (!file){
158+
c_die("Could not read message file.\n");
159+
}
160+
161+
fseek(file , 0L , SEEK_END);
162+
fs = ftell(file);
163+
fseek(file, 0L, SEEK_SET);
164+
165+
buf = (char*)calloc(fs, sizeof(char));
166+
if (!buf) {
167+
c_die("Could not allocate memory.\n");
168+
}
169+
170+
fread(buf, sizeof(char), fs, file);
171+
fclose(file);
172+
173+
return buf;
174+
}
175+
143176
void usage(void) {
144177
printf(" Usage: cmatrix -[abBcfhlsmVxk] [-u delay] [-C color] [-t tty] [-M message]\n");
145178
printf(" -a: Asynchronous scroll\n");
@@ -155,7 +188,7 @@ void usage(void) {
155188
printf(" -s: \"Screensaver\" mode, exits on first keystroke\n");
156189
printf(" -x: X window mode, use if your xterm is using mtx.pcf\n");
157190
printf(" -V: Print version information and exit\n");
158-
printf(" -M [message]: Prints your message in the center of the screen. Overrides -L's default message.\n");
191+
printf(" -M [message]: Prints your message in the center of the screen. Overrides -L's default message. Accepts readable files.\n");
159192
printf(" -u delay (0 - 10, default 4): Screen update delay\n");
160193
printf(" -C [color]: Use this color for matrix (default green)\n");
161194
printf(" -r: rainbow mode\n");
@@ -326,6 +359,7 @@ int main(int argc, char *argv[]) {
326359
int classic = 0;
327360
int changes = 0;
328361
char *msg = "";
362+
char *mfile = "";
329363
char *tty = NULL;
330364

331365
srand((unsigned) time(NULL));
@@ -390,6 +424,11 @@ int main(int argc, char *argv[]) {
390424
break;
391425
case 'M':
392426
msg = strdup(optarg);
427+
428+
// check if msg is a readable file
429+
if(access(msg, F_OK) == 0) {
430+
mfile = msg;
431+
}
393432
break;
394433
case 'n':
395434
bold = -1;
@@ -845,30 +884,56 @@ if (console) {
845884
}
846885
}
847886

887+
// check if -M was used with a file
888+
// if yes, read the msg from the file
889+
if (mfile[0] != '\0') {
890+
msg = read_file(mfile);
891+
}
892+
848893
//check if -M and/or -L was used
849894
if (msg[0] != '\0') {
850-
//Add our message to the screen
851-
int msg_x = LINES/2;
852-
int msg_y = COLS/2 - strlen(msg)/2;
895+
// Add our message to the screen
896+
int multiline = 0;
853897
int i = 0;
854898

899+
// count the newlines
900+
for (i = 0; i < strlen(msg); i++)
901+
if(msg[i] == '\n')
902+
multiline++;
903+
904+
char *line = strtok(msg, DELIMITER);
905+
if (line == NULL)
906+
line = msg;
907+
908+
int x_offset = -1 * multiline/2;
909+
int msg_x = LINES/2 + x_offset;
910+
int msg_y = COLS/2 - strlen(line)/2;
911+
855912
//Add space before message
856913
move(msg_x-1, msg_y-2);
857-
for (i = 0; i < strlen(msg)+4; i++)
914+
for (i = 0; i < strlen(line)+4; i++)
858915
addch(' ');
859916

860-
//Write message
861-
move(msg_x, msg_y-2);
862-
addch(' ');
863-
addch(' ');
864-
addstr(msg);
865-
addch(' ');
866-
addch(' ');
867-
868-
//Add space after message
869-
move(msg_x+1, msg_y-2);
870-
for (i = 0; i < strlen(msg)+4; i++)
917+
while (line != NULL){
918+
msg_x = LINES/2 + x_offset;
919+
msg_y = COLS/2 - strlen(line)/2;
920+
921+
//Write message
922+
move(msg_x, msg_y-2);
923+
addch(' ');
924+
addch(' ');
925+
addstr(line);
926+
addch(' ');
871927
addch(' ');
928+
929+
//Add space after message
930+
move(msg_x+1, msg_y-2);
931+
for (i = 0; i < strlen(line)+4; i++)
932+
addch(' ');
933+
934+
line = strtok(NULL, DELIMITER);
935+
x_offset++;
936+
}
872937
}
873938

874939
napms(update * 10);

0 commit comments

Comments
 (0)