summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 01bc02acd6e29d97bd76d4ca29a14c174cbc2883 (plain)
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
159
160
161
162
163
#undef _POSIX_C_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>

#define LINE_LENGTH 256
#define NUM_LENGTH 32
#define MAX_LINES 64
#define INIT_BUFFER_LEN 8

static char* get_line(char* buf, int len, FILE* file) {
    char* ptr = fgets(buf, len, file);
    if (ptr == NULL) return NULL;
    buf[strcspn(buf, "\n")] = 0;
    return ptr;
}

#define LINE(buf, line) buf + line * LINE_LENGTH
#define MAX(a, b) a > b ? a : b
static char* read_logo(FILE* file, uint16_t* height) {
    uint16_t capacity = INIT_BUFFER_LEN;
    uint16_t h = 0;

    char* buf = malloc(LINE_LENGTH * capacity); 
    while (h < MAX_LINES && get_line(LINE(buf, h), LINE_LENGTH - 1, file) != NULL) {
        h++;
        if (h == capacity) {
            capacity *= 2;
            buf = realloc(buf, LINE_LENGTH * capacity);
        }
    }

    *height = h;
    return buf;
}

static char* read_data(FILE* file, uint16_t* height, uint16_t* offset) {
    uint16_t capacity = INIT_BUFFER_LEN;
    uint16_t h = 2, o = 0;
    char accent_color[NUM_LENGTH], normal_color[NUM_LENGTH], offset_buffer[NUM_LENGTH], title_buffer[LINE_LENGTH];

    char* end;
    if (get_line(offset_buffer, NUM_LENGTH - 1, file) == NULL) {
        printf("error: data file missing offset\n");
        exit(EXIT_FAILURE);
    }
    o = strtol(offset_buffer, &end, 10);
    if (offset_buffer == end) {
        printf("error: data offset is not a valid numver\n");
        exit(EXIT_FAILURE);
    }
    *offset = o;

    if (get_line(accent_color, NUM_LENGTH - 1, file) == NULL) {
        printf("error: data file missing accent color\n");
        exit(EXIT_FAILURE);
    }

    if (get_line(normal_color, NUM_LENGTH - 1, file) == NULL) {
        printf("error: data file missing normal color\n");
        exit(EXIT_FAILURE);
    }
    
    if (get_line(title_buffer, LINE_LENGTH - 1, file) == NULL) {
        printf("error: data file missing title\n");
        exit(EXIT_FAILURE);
    }

    char* buf = malloc(LINE_LENGTH * capacity);
   
    // funny stuff be goofy ahh'ing here
    size_t title_len = strlen(title_buffer);
    snprintf(LINE(buf, 1), LINE_LENGTH, "\x1b[%sm", normal_color);
    size_t ansi_len = strlen(LINE(buf, 1));
    memset(LINE(buf, 1) + ansi_len, '~', title_len);
    (LINE(buf, 1))[title_len + ansi_len] = 0;
    snprintf(LINE(buf, 0), LINE_LENGTH, "\x1b[%sm%s", accent_color, title_buffer);

    char key[LINE_LENGTH];
    char value[LINE_LENGTH];

    while(h < MAX_LINES) {
        if (get_line(key, LINE_LENGTH - 1, file) == NULL) break;
        if (get_line(value, LINE_LENGTH - 1, file) == NULL) break;
      
        #pragma GCC diagnostic ignored "-Wformat-truncation"
        snprintf(LINE(buf, h), LINE_LENGTH, "\x1b[%sm%s\x1b[%sm: %s", accent_color, key, normal_color, value);

        h++;
        if (h == capacity) {
            capacity *= 2;
            buf = realloc(buf, LINE_LENGTH * capacity);
        }
    }

    *height = h;
    return buf;
}

__attribute__ ((__format__(printf, 1, 2)))
static FILE* run_command(const char* format, ...) {
    va_list valist;
    va_start(valist, format);
    
    char buf[LINE_LENGTH];
    vsnprintf(buf, LINE_LENGTH, format, valist);

    FILE* out = popen(buf, "r");
    if (out == NULL) {
        printf("error: failed to run command %s: %s\n", buf, strerror(errno));
        exit(EXIT_FAILURE);
    }
    return out;
}

static void close_command(FILE* out) {
    int code = pclose(out);
    if (code != 0) {
        printf("error: command errored with exit code %d\n", code);
        exit(EXIT_FAILURE);
    }
}

int main(int argc, char** argv) {

    if (argc != 2) {
        printf("usage: ritfetch domain\n");
        return EXIT_FAILURE;
    }

    FILE* out;

    out = run_command("curl -Lsf %s/.well-known/ritfetch/logo", argv[1]);
    uint16_t logo_height;
    char* logo = read_logo(out, &logo_height);
    close_command(out);

    out = run_command("curl -Lsf %s/.well-known/ritfetch/data", argv[1]);
    uint16_t data_height, offset;
    char* data = read_data(out, &data_height, &offset);
    close_command(out);

    uint16_t max_height = MAX(logo_height, data_height);
    for (uint16_t i = 0; i < max_height; i++) {
        if (i < logo_height) {
            printf("%s", LINE(logo, i));
        }
        if (i < data_height) {
            printf("\x1b[%uG\x1b[m", offset);
            printf("%s", LINE(data, i));
        }
        printf("\n");
    }

    free(data);
    free(logo);

    return EXIT_SUCCESS;
}