mirror of
https://github.com/perk11/runwhenidle.git
synced 2026-01-09 21:58:17 -05:00
Rework error messages printing function and make more error messages red
This commit is contained in:
12
main.c
12
main.c
@@ -116,7 +116,7 @@ char *read_remaining_arguments_as_char(int argc,
|
|||||||
// Allocate memory for the remaining_arguments_string
|
// Allocate memory for the remaining_arguments_string
|
||||||
remaining_arguments_string = malloc(memory_to_be_allocated_for_remaining_arguments_string);
|
remaining_arguments_string = malloc(memory_to_be_allocated_for_remaining_arguments_string);
|
||||||
if (remaining_arguments_string == NULL) {
|
if (remaining_arguments_string == NULL) {
|
||||||
//not using print_error here intentionally
|
//not using fprintf_error here intentionally
|
||||||
fprintf(stderr, "Failed to allocate memory while parsing command to be ran.\n");
|
fprintf(stderr, "Failed to allocate memory while parsing command to be ran.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@@ -166,8 +166,8 @@ int main(int argc, char *argv[]) {
|
|||||||
long timeout_arg_value = strtol(optarg, NULL, 10);
|
long timeout_arg_value = strtol(optarg, NULL, 10);
|
||||||
if (timeout_arg_value < TIMEOUT_MIN_SUPPORTED_VALUE ||
|
if (timeout_arg_value < TIMEOUT_MIN_SUPPORTED_VALUE ||
|
||||||
timeout_arg_value > TIMEOUT_MAX_SUPPORTED_VALUE || errno != 0) {
|
timeout_arg_value > TIMEOUT_MAX_SUPPORTED_VALUE || errno != 0) {
|
||||||
printf("Invalid timeout value: \"%s\". Range supported: %ld-%ld\n", optarg,
|
fprintf_error("Invalid timeout value: \"%s\". Range supported: %ld-%ld", optarg,
|
||||||
TIMEOUT_MIN_SUPPORTED_VALUE, TIMEOUT_MAX_SUPPORTED_VALUE);
|
TIMEOUT_MIN_SUPPORTED_VALUE, TIMEOUT_MAX_SUPPORTED_VALUE);
|
||||||
print_usage(argv[0]);
|
print_usage(argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -194,7 +194,7 @@ int main(int argc, char *argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (quiet && verbose) {
|
if (quiet && verbose) {
|
||||||
printf("Incompatible options --quiet|-q and --verbose|-v used");
|
fprintf_error("Incompatible options --quiet|-q and --verbose|-v used");
|
||||||
print_usage(argv[0]);
|
print_usage(argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -204,7 +204,7 @@ int main(int argc, char *argv[]) {
|
|||||||
x_display = XOpenDisplay(NULL);
|
x_display = XOpenDisplay(NULL);
|
||||||
if (!x_display) {
|
if (!x_display) {
|
||||||
xscreensaver_is_available = 0;
|
xscreensaver_is_available = 0;
|
||||||
print_error("Couldn't open an X11 display!\n");
|
fprintf_error("Couldn't open an X11 display!");
|
||||||
} else {
|
} else {
|
||||||
int xscreensaver_event_base, xscreensaver_error_base; //not sure why these are neeeded
|
int xscreensaver_event_base, xscreensaver_error_base; //not sure why these are neeeded
|
||||||
xscreensaver_is_available = XScreenSaverQueryExtension(x_display, &xscreensaver_event_base,
|
xscreensaver_is_available = XScreenSaverQueryExtension(x_display, &xscreensaver_event_base,
|
||||||
@@ -215,7 +215,7 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!xscreensaver_is_available) {
|
if (!xscreensaver_is_available) {
|
||||||
print_error("No available method for detecting user idle time on the system, user will be considered idle to allow the command to finish.\n");
|
fprintf_error("No available method for detecting user idle time on the system, user will be considered idle to allow the command to finish.");
|
||||||
}
|
}
|
||||||
|
|
||||||
pid = run_shell_command(shell_command_to_run, pid);
|
pid = run_shell_command(shell_command_to_run, pid);
|
||||||
|
|||||||
30
tty_utils.c
30
tty_utils.c
@@ -1,15 +1,31 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include "tty_utils.h"
|
#include "tty_utils.h"
|
||||||
|
|
||||||
void print_colored_text(FILE *stream, const char *color, const char *message) {
|
void print_colored_prefix(FILE *stream, const char *color, bool is_tty) {
|
||||||
if (isatty(fileno(stream))) {
|
if (is_tty) {
|
||||||
fprintf(stream, "\033[%sm%s\033[0m\n", color, message);
|
fprintf(stream, "\033[%sm", color);
|
||||||
} else {
|
|
||||||
fprintf(stream, "%s\n", message);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_error(const char *message) {
|
void print_colored_suffix(FILE *stream, bool is_tty) {
|
||||||
print_colored_text(stderr, "31", message);
|
if (is_tty) {
|
||||||
|
fprintf(stream, "\033[0m\n");
|
||||||
|
} else {
|
||||||
|
fprintf(stream, "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fprintf_error(const char *format, ...) {
|
||||||
|
va_list args;
|
||||||
|
va_start(args, format);
|
||||||
|
|
||||||
|
bool is_tty = isatty(fileno(stderr));
|
||||||
|
print_colored_prefix(stderr, "31", is_tty);
|
||||||
|
vfprintf(stderr, format, args);
|
||||||
|
print_colored_suffix(stderr, is_tty);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
#ifndef RUNWHENIDLE_TTY_UTILS_H
|
#ifndef RUNWHENIDLE_TTY_UTILS_H
|
||||||
#define RUNWHENIDLE_TTY_UTILS_H
|
#define RUNWHENIDLE_TTY_UTILS_H
|
||||||
#include <stdio.h>
|
|
||||||
void print_colored_text(FILE* stream, const char* color, const char* message);
|
void fprintf_error(const char *format, ...);
|
||||||
void print_error(const char* message);
|
|
||||||
|
|
||||||
#endif //RUNWHENIDLE_TTY_UTILS_H
|
#endif //RUNWHENIDLE_TTY_UTILS_H
|
||||||
|
|||||||
Reference in New Issue
Block a user