summaryrefslogtreecommitdiff
path: root/tools/exofetch.c
blob: 826464dd0e412e461cab0f5a690015926fe08e2c (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <locale.h>
#include "../accent.h"

static ColorShm *accentshm;
static char accentcol[8] = "#005577";

static void
updateaccent(void)
{
    if (!readaccent(&accentshm, accentcol))
        memcpy(accentcol, "#005577", 8);
}

static void
hex2rgb(const char *hex, int *r, int *g, int *b)
{
    unsigned int ri, gi, bi;
    if (sscanf(hex + 1, "%02x%02x%02x", &ri, &gi, &bi) != 3) {
        *r = 0x00; *g = 0x55; *b = 0x77;
        return;
    }
    *r = ri; *g = gi; *b = bi;
}

static const char *icon[] = {
"                   ██████████████                                   ██████████████                   ",
"                   ██████████████                                   ██████████████                   ",
"                   ██████▓▓▓█████                                   █████▓▓▓██████                   ",
"                   █████▓▓▓▓█████                                   █████▓▓▓▓█████                   ",
"                   █████▓▓▓▓█████                                   █████▓▓▓▓█████                   ",
"                   █████▓▓▓▓▓████                                   ████▓▓▓▓▓█████                   ",
"                   █████▓▓▓▓▓████████   █████████████████████   ████████▓▓▓▓▓█████                   ",
"                   ██████▓▓▓▓▓▓▓█████   █████████████████████   █████▓▓▓▓▓▓▓██████                   ",
"▓▓▓▓▓▓▓▓           ██████▓▓▓▓▓▓▓█████   █████████████████████   █████▓▓▓▓▓▓▓██████           ▓▓▓▓▓▓▓▓",
"▓▓▓▓▓▓▓▓▓              █████▓▓▓▓▓████   ████▓▒▒▒▒▓▓▓▒▒▒▒▓████   ████▓▓▓▓▓█████              ▓▓▓▓▓▓▓▓▓",
"▓▓▓▓▓▓▓▓▓              █████▓▓▓▓▓████   ████▓▒▒▒▒▓▓▓▒▒▒▒▓████   ████▓▓▓▓▓█████              ▓▓▓▓▓▓▓▓▓",
"▓▓▓▓▒░░░▒▓▓▓▓          █████▓▓▓▓▓▓▓▓█████▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓█████▓▓▓▓▓▓▓▓█████          ▓▓▓▓▒░░░▒▓▓▓▓",
"▓▓▓▓▒░░░▒▓▓▓▓          █████▓▓▓▓▓▓▓▓█████▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓█████▓▓▓▓▓▓▓▓█████          ▓▓▓▓▒░░░░▓▓▓▓",
"▓▓▓▓▒▒▒▒░░░░▒▓▓▓▓  █████████▓▓▓▓▓▒▒▒▓███▓▓▓▓▓▓▓▓█████▓▓▓▓▓▓▓▓███▓▒▒▒▒▓▓▓▓█████████  ▓▓▓▓▒░░░░▒▒▒▒▓▓▓▓",
"   ▒▒▒▒▒░░░░▒▓▓▓▓  ██████████▓▓▓▒▒▒▒▓███▓▓▓▓▓███████████▓▓▓▓▓███▓▒▒▒▒▓▓▓▓█████████  ▓▓▓▓▒░░░░▒▒▒▒▒   ",
"   ▓▓▓▓▓░░░░▒▒▒▒▓▓▓▓████████████▓▒▒▒▓███████▓▓▓▓█████▓▓▓▓███████▓▒▒▒▒▓████████████▓▓▓▒▒▒▒░░░░▒▓▓▓▓   ",
"   ████▓▒░░░░░░▒▒▓▓▓████████████▓▒▒▒▓████████▓▓▓▓▓▓▓▓▓▓▓████████▓▒▒▒▓████████████▓▓▓▓▒░░░░░░░▓████   ",
"   ████▓▒▒▒▒░░░░▒▓▓▓▓███████████▓▒▒▒▓▓▓▓████▓▒▒▒▒▒▒▒▒▒▒▒▓████▓▓▓▓▒▒▒▓████████████▓▓▓▒▒░░░▒▒▒▒▓████   ",
"       ▓▓▓▓▓▒░░░▒▒▒▒▓▓▓▓▓███████▓▒▒▒▒▒▒▒▓███▒░░░░░░░░░░░▒▓██▓▒▒▒▒▒▒▒▒▓██████▓▓▓▓▓▒▒▒▒░░░▒▓▓▓▓▓       ",
"       ▓▓▓▓▓▒░░░▒▒▒▒▒▓▓▓▓███████▓▒▒▒▒▒▒▒▓███▒░░░░░░░░░░░▒▓██▓▒▒▒▒▒▒▒▓▓██████▓▓▓▓▓▒▒▒▒░░░▒▓▓▓▓▓       ",
"       ▓▓▓▓▓▒░░░▒▒▒▒▒▒▒▒▓▓▓▓████▓▓▓▒▒░░░▒███▒░░░░░░░░░░░▒▓██▓░░░▒▒▓▓▓████▓▓▓▓▒▒▒▒▒▒▒▒░░░▒▓▓▓▓▓       ",
"       ▓▓▓▓▓▒░░░▒▒▒▒▒▒▒▒▓▓▓▓████▓▓▓▒▒░░░▒███▒░░░░░░░░░░░▒▓██▓░░░▒▒▓▓▓████▓▓▓▓▒▒▒▒▒▒▒▒░░░▒▓▓▓▓▓       ",
"           ▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▓██████▓▒░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░▒▓██████▓▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓           ",
"           ▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▓██████▓▒░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░▒▓██████▓▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓           ",
"           ████▓▓▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▓▓████           ",
"           ████▓▓▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓███▓▓▓▓▓▓▓▓▓▓▓▓▓███▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▓▓████           ",
"           █████▓▓▓▓▓███▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓███▓▓▓▓█████           ",
"           █████████████▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█████████████           ",
"           █████▓▓▓▓████▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓████▓▓▓▓█████           ",
"           ████▓▓▒▒▒▓███████████▓▓▓▓▓▓▓▓▒░░░░░░░░░░░░░░░░░░░▒▒▓▓▓▓▓▓▓███████████▓▒▒▒▒▓████           ",
"        ████▓▓▓▓▒▒▒▒▓▓▓▓████████▓▓▓▓▓▓▓▓▒▒▒▒▒░░░░░░░░░░░░▒▒▒▒▓▓▓▓▓▓▓▓█████████▓▓▓▒▒▒▒▓▓▓▓████        ",
"       █████▓▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓████████████▓▓▓▓▒░░░░░░░░░░░▒▓▓▓▓████████████▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▓█████       ",
"       █████▓▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓████████████▓▓▓▓▒░░░░░░░░░░░▒▓▓▓▓████████████▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▓█████       ",
"       █████▓▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓███████████████████████████████████████████▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▓█████       ",
"       █████▓▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓███████████████████████████████████████████▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▓█████       ",
"       █████▓▓▓▓▓▓▓▓▓▓▓▓█████████████████████▓▓▓██████▓▓█████████████████████▓▓▓▓▓▓▓▓▓▓▓▓█████       ",
"       ██████▓▓▓▓▓▓▓▓████████▓▓▓█████████████▓▓▓█████▓▓▓█████████████▓▓▓████████▓▓▓▓▓▓▓▓▓█████       ",
"       █████████▓▓▓▓▓████████▓▓▓▓████████████▓▓▓█████▓▓▓▓████████████▓▓▓████████▓▓▓▓▓█████████       ",
"           █████▓▓▓▓▓████████▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█████▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓████████▓▓▓▓▓█████           ",
"           ██████▓▓▓▓▓▓▓████████▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█████▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓████████▓▓▓▓▓▓▓▓█████           ",
"           ██████▓▓▓▓▓▓▓▓████████▓▓▓█████████▓▓▓▓▓▓▓▓▓▓▓█████████▓▓▓████████▓▓▓▓▓▓▓▓██████           ",
"           ██████▓▓▓▓▓▓▓▓████████████████████▓▓▓▓▓▓▓▓▓▓▓████████████████████▓▓▓▓▓▓▓▓██████           ",
"           ██████████████████████████████████▓▓▓▓▓▓▓▓▓▓▓██████████████████████████████████           ",
"           ██████████████████████████████████▓▓▓▓▓▓▓▓▓▓▓██████████████████████████████████           ",
"                       ██████▓▓▓▓▓▓██████▓▓▓████▓▓▓▓▓████▓▓▓██████▓▓▓▓▓▓▓█████                       ",
"                       ██████▓▓▓▓▓▓▓█████▓▓▓████▓▓▓▓▓████▓▓▓██████▓▓▓▓▓▓▓█████                       ",
"                       █████▓▓▓▓▓▓▓▓█████████████████████████████▓▓▓▓▓▓▓▓█████                       ",
"                       █████▓▓▓▓▓▓▓▓█████████▓▓▓█████▓▓▓█████████▓▓▓▓▓▓▓▓█████                       ",
"                       ██████▓▓▓▓▓▓▓█████████▓▓▓▓███▓▓▓▓██████████▓▓▓▓▓▓██████                       ",
"                       ██████████████████▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓██████████████████                       ",
"                       █████████████████████▓▓▓▓▓▓▓▓▓▓▓▓▓█████████████████████                       ",
"                            █████████████████▓▓▓▓▓▓▓▓▓▓▓██████████████████                           ",
"                            ██████████████████████████████████████████████                           ",
"                                            █████████████                                            ",
"                                            █████████████                                            ",
NULL};

int
main(void)
{
    setlocale(LC_ALL, "");
    updateaccent();
    int r, g, b;
    hex2rgb(accentcol, &r, &g, &b);

    for (int i = 0; icon[i]; i++)
        printf("\x1b[38;2;%d;%d;%dm%s\x1b[0m\n", r, g, b, icon[i]);

    printf("\n");

    char host[256] = "";
    gethostname(host, sizeof host);
    struct utsname uts;
    uname(&uts);
    struct sysinfo info;
    sysinfo(&info);

    char os[128] = "Unknown";
    FILE *fp = fopen("/etc/os-release", "r");
    if (fp) {
        char line[256];
        while (fgets(line, sizeof line, fp)) {
            if (strncmp(line, "PRETTY_NAME=", 12) == 0) {
                char *p = line + 12;
                if (*p == '"') p++;
                char *e = strchr(p, '"');
                if (e) *e = '\0';
                snprintf(os, sizeof os, "%s", p);
                break;
            }
        }
        fclose(fp);
    }

    long updays = info.uptime / 86400;
    long uphours = (info.uptime / 3600) % 24;
    long upmins = (info.uptime / 60) % 60;

    char cpu[128] = "Unknown";
    fp = fopen("/proc/cpuinfo", "r");
    if (fp) {
        char line[256];
        while (fgets(line, sizeof line, fp)) {
            if (strncmp(line, "model name", 10) == 0) {
                char *p = strchr(line, ':');
                if (p) {
                    p += 2;
                    p[strcspn(p, "\n")] = '\0';
                    snprintf(cpu, sizeof cpu, "%s", p);
                }
                break;
            }
        }
        fclose(fp);
    }

    long totalmem = info.totalram / 1024 / 1024;
    long freemem = info.freeram / 1024 / 1024;
    long usedmem = totalmem - freemem;

    char shell[64];
    snprintf(shell, sizeof shell, "%s", getenv("SHELL") ? getenv("SHELL") : "Unknown");
    char term[64];
    snprintf(term, sizeof term, "%s", getenv("TERM") ? getenv("TERM") : "Unknown");

    char packages[32] = "Unknown";
    if ((fp = popen("dpkg-query -f '${binary:Package}\\n' -W 2>/dev/null | wc -l", "r"))) {
        if (fgets(packages, sizeof packages, fp))
            packages[strcspn(packages, "\n")] = '\0';
        pclose(fp);
    }

    printf("\x1b[38;2;%d;%d;%dmHost:\x1b[0m %s\n", r, g, b, host);
    printf("\x1b[38;2;%d;%d;%dmOS:\x1b[0m %s\n", r, g, b, os);
    printf("\x1b[38;2;%d;%d;%dmKernel:\x1b[0m %s\n", r, g, b, uts.release);
    printf("\x1b[38;2;%d;%d;%dmUptime:\x1b[0m %ldd %ldh %ldm\n", r, g, b, updays, uphours, upmins);
    printf("\x1b[38;2;%d;%d;%dmPackages:\x1b[0m %s\n", r, g, b, packages);
    printf("\x1b[38;2;%d;%d;%dmShell:\x1b[0m %s\n", r, g, b, shell);
    printf("\x1b[38;2;%d;%d;%dmTerminal:\x1b[0m %s\n", r, g, b, term);
    printf("\x1b[38;2;%d;%d;%dmCPU:\x1b[0m %s\n", r, g, b, cpu);
    printf("\x1b[38;2;%d;%d;%dmMemory:\x1b[0m %ldMiB / %ldMiB\n", r, g, b, usedmem, totalmem);

    return 0;
}