added several flags for customization
This commit is contained in:
parent
e38bf84a6c
commit
90ce4d2338
3 changed files with 118 additions and 14 deletions
BIN
cclock
Executable file
BIN
cclock
Executable file
Binary file not shown.
132
cclock.c
132
cclock.c
|
|
@ -124,18 +124,117 @@ bool fetch_temp_owm(const config_t *cfg, char *out, size_t out_size) {
|
||||||
|
|
||||||
/* ---------------- Main ---------------- */
|
/* ---------------- Main ---------------- */
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
config_t cfg = {0};
|
bool twelve_hour = false;
|
||||||
char config_path[512];
|
bool human_date = false;
|
||||||
snprintf(config_path, sizeof(config_path), "%s/.config/cclock/config", getenv("HOME"));
|
bool quiet = false;
|
||||||
|
bool hide_temp = false;
|
||||||
|
bool force_c = false;
|
||||||
|
bool force_f = false;
|
||||||
|
|
||||||
if (!load_config_toml(&cfg, config_path)) {
|
/* Parse CLI flags FIRST */
|
||||||
printf("Failed to load config from %s\n", config_path);
|
|
||||||
return 1;
|
for (int i = 1; i < argc; i++) {
|
||||||
|
if (strcmp(argv[i], "--help") == 0) {
|
||||||
|
goto show_help;
|
||||||
}
|
}
|
||||||
|
if (argv[i][0] != '-') continue;
|
||||||
|
|
||||||
bool twelve_hour = false;
|
for (int j = 1; argv[i][j]; j++) {
|
||||||
for (int i = 1; i < argc; i++)
|
switch (argv[i][j]) {
|
||||||
if (strcmp(argv[i], "-t") == 0) twelve_hour = true;
|
case 't': twelve_hour = true; break;
|
||||||
|
case 'H': human_date = true; break;
|
||||||
|
case 'q': quiet = true; break;
|
||||||
|
case 'T': hide_temp = true; break;
|
||||||
|
case 'C': force_c = true; break;
|
||||||
|
case 'F': force_f = true; break;
|
||||||
|
|
||||||
|
case 'h':
|
||||||
|
goto show_help;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printf("Unknown flag: -%c\n", argv[i][j]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Help/Usage output */
|
||||||
|
show_help:
|
||||||
|
|
||||||
|
printf("Usage: %s [-12HqtCF]\n\n", argv[0]);
|
||||||
|
|
||||||
|
printf("Options:\n");
|
||||||
|
printf(" -12 Use 12-hour time format\n");
|
||||||
|
printf(" -H Human-readable date (\"Tuesday 16, Feb\")\n");
|
||||||
|
printf(" -q Hide \"Press q to quit\" footer\n");
|
||||||
|
printf(" -t Hide temperature display\n");
|
||||||
|
printf(" -C Force Celsius\n");
|
||||||
|
printf(" -F Force Fahrenheit\n");
|
||||||
|
printf(" -h,--help Show this help\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* NOW load config */
|
||||||
|
|
||||||
|
config_t cfg = {0};
|
||||||
|
char config_path[512];
|
||||||
|
snprintf(config_path, sizeof(config_path), "%s/.config/cclock/config", getenv("HOME"));
|
||||||
|
|
||||||
|
if (!load_config_toml(&cfg, config_path)) {
|
||||||
|
printf("Failed to load config from %s\n", config_path);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* NOW apply overrides */
|
||||||
|
|
||||||
|
if (force_c) {
|
||||||
|
strncpy(cfg.units, "metric", sizeof(cfg.units) - 1);
|
||||||
|
cfg.units[sizeof(cfg.units) - 1] = '\0';
|
||||||
|
}
|
||||||
|
else if (force_f) {
|
||||||
|
strncpy(cfg.units, "imperial", sizeof(cfg.units) - 1);
|
||||||
|
cfg.units[sizeof(cfg.units) - 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < argc; i++) {
|
||||||
|
if (argv[i][0] != '-') continue;
|
||||||
|
|
||||||
|
for (int j = 1; argv[i][j]; j++) {
|
||||||
|
switch (argv[i][j]) {
|
||||||
|
case 't':
|
||||||
|
twelve_hour = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'T':
|
||||||
|
hide_temp = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'C':
|
||||||
|
force_c = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'F':
|
||||||
|
force_f = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'H':
|
||||||
|
human_date = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'q':
|
||||||
|
quiet = true;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'h':
|
||||||
|
printf("Usage: %s [-tH]\n", argv[0]);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printf("Unknown flag: -%c\n", argv[i][j]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
signal(SIGINT, handle_sigint);
|
signal(SIGINT, handle_sigint);
|
||||||
signal(SIGWINCH, handle_winch);
|
signal(SIGWINCH, handle_winch);
|
||||||
|
|
@ -149,7 +248,7 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||||
|
|
||||||
char time_buf[16], date_buf[11], temp_buf[64];
|
char time_buf[16], date_buf[15], temp_buf[64];
|
||||||
time_t last_fetch = 0;
|
time_t last_fetch = 0;
|
||||||
const int refresh_interval = 600; /* 10 minutes */
|
const int refresh_interval = 600; /* 10 minutes */
|
||||||
|
|
||||||
|
|
@ -172,7 +271,9 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
const char *time_fmt = twelve_hour ? "%I:%M %p" : "%H:%M";
|
const char *time_fmt = twelve_hour ? "%I:%M %p" : "%H:%M";
|
||||||
strftime(time_buf, sizeof(time_buf), time_fmt, tm_now);
|
strftime(time_buf, sizeof(time_buf), time_fmt, tm_now);
|
||||||
strftime(date_buf, sizeof(date_buf), "%m/%d/%Y", tm_now);
|
|
||||||
|
const char *date_fmt = human_date ? "%A %d, %b" : "%m/%d/%Y";
|
||||||
|
strftime(date_buf, sizeof(date_buf), date_fmt, tm_now);
|
||||||
|
|
||||||
if (difftime(now, last_fetch) >= refresh_interval) {
|
if (difftime(now, last_fetch) >= refresh_interval) {
|
||||||
if (!fetch_temp_owm(&cfg, temp_buf, sizeof(temp_buf)))
|
if (!fetch_temp_owm(&cfg, temp_buf, sizeof(temp_buf)))
|
||||||
|
|
@ -192,10 +293,13 @@ int main(int argc, char **argv) {
|
||||||
int date_x = (cols - strlen(date_buf)) / 2;
|
int date_x = (cols - strlen(date_buf)) / 2;
|
||||||
mvprintw(y_mid + 1, date_x, "%s", date_buf);
|
mvprintw(y_mid + 1, date_x, "%s", date_buf);
|
||||||
|
|
||||||
int temp_x = (cols - strlen(temp_buf)) / 2;
|
if (!hide_temp) {
|
||||||
mvprintw(y_mid + 2, temp_x, "%s", temp_buf);
|
int temp_x = (cols - strlen(temp_buf)) / 2;
|
||||||
|
mvprintw(y_mid + 2, temp_x, "%s", temp_buf);
|
||||||
|
}
|
||||||
|
|
||||||
mvprintw(rows - 1, 0, "Press 'q' to quit");
|
if (!quiet)
|
||||||
|
mvprintw(rows - 1, 0, "Press 'q' to quit");
|
||||||
|
|
||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
|
|
|
||||||
BIN
clock
BIN
clock
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue