farfetch/main.c

363 行
8.4 KiB
C
Raw 通常表示 履歴

2024-06-17 22:41:46 +09:00
#include <stdio.h>
2024-06-20 02:36:38 +09:00
#include <stdlib.h>
#include <unistd.h>
2024-06-17 22:41:46 +09:00
#include "src/user.h"
#include "src/os.h"
2024-06-22 03:13:21 +09:00
#if defined(__linux__) || defined(__sun)
2024-06-17 22:41:46 +09:00
#include "src/distro.h"
#endif
2024-07-15 16:10:41 +09:00
#if !defined(__HAIKU__)
#include "src/host.h"
2024-07-15 16:10:41 +09:00
#endif
#include "src/uptime.h"
#if defined(__OpenBSD__)
#include "src/recording.h"
#endif
2024-06-18 19:12:24 +09:00
#include "src/packages.h"
2024-06-19 22:53:46 +09:00
#include "src/resolution.h"
2024-06-21 21:02:43 +09:00
#include "src/wm.h"
2024-06-21 21:34:05 +09:00
#include "src/shell.h"
2024-06-21 21:48:02 +09:00
#include "src/libc.h"
2024-06-18 13:39:02 +09:00
#include "src/cpu.h"
2024-06-18 19:23:34 +09:00
#include "src/gpu.h"
2024-06-18 00:16:45 +09:00
#include "src/memory.h"
2024-06-21 22:14:36 +09:00
#include "src/storage.h"
2024-06-17 22:41:46 +09:00
#include "src/config.h"
2024-06-17 22:41:46 +09:00
const char *sofname = "farfetch";
2024-07-16 18:25:27 +09:00
const char *version = "0.3.0";
#if defined(__linux__) || defined(__sun)
const char *avalopt = "ls";
#else
2024-06-21 23:46:01 +09:00
const char *avalopt = "s";
#endif
int opt;
#if defined(__linux__) || defined(__sun)
int islogo = 0;
#endif
int issmall = 0;
int iserr = 0;
2024-06-21 23:46:01 +09:00
void usage() {
#if defined(__linux__) || defined(__sun)
2024-07-09 19:31:02 +09:00
printf("%s-%s\nusage: %s [-s] [-l distro]\n", sofname, version, sofname);
#else
2024-06-21 23:46:01 +09:00
printf("%s-%s\nusage: %s [-%s]\n", sofname, version, sofname, avalopt);
#endif
2024-06-21 23:46:01 +09:00
}
2024-06-17 22:41:46 +09:00
void flags(int opt) {
switch (opt) {
#if defined(__linux__) || defined(__sunos)
case 'l':
islogo = 1;
break;
#endif
case 's':
2024-06-21 23:46:01 +09:00
issmall = 1;
break;
default:
iserr = 1;
2024-06-21 23:46:01 +09:00
usage();
break;
}
}
int main(int argc, char *argv[]) {
int lc = 0;
while ((opt = getopt(argc, argv, avalopt)) != -1) {
flags(opt);
}
if (iserr == 1) {
return 1;
}
getconf();
2024-06-18 15:25:34 +09:00
#if defined(__OpenBSD__)
#include "src/logo/openbsd.h"
2024-06-19 19:38:19 +09:00
getOS();
2024-06-18 15:59:14 +09:00
#elif defined(__NetBSD__)
#include "src/logo/netbsd.h"
2024-06-19 19:38:19 +09:00
getOS();
2024-06-18 15:59:14 +09:00
#elif defined(__FreeBSD__)
#include "src/logo/freebsd.h"
2024-06-19 19:38:19 +09:00
getOS();
2024-06-18 22:04:27 +09:00
#elif defined(__linux__)
get_distro();
2024-06-18 22:04:27 +09:00
#include "src/logo/linux.h"
2024-06-19 19:38:19 +09:00
getDistro(distroname);
2024-06-22 03:13:21 +09:00
#elif defined(__sun)
get_distro();
#include "src/logo/sunos.h"
getDistro(distroname);
2024-07-05 23:06:30 +09:00
#elif defined(__APPLE__)
#include "src/logo/macos.h"
getOS();
2024-07-15 13:31:03 +09:00
#elif defined(__HAIKU__)
#include "src/logo/haiku.h"
getOS();
2024-06-18 15:25:34 +09:00
#else
#include "src/logo/colors.h"
2024-06-20 00:47:11 +09:00
const char *color = MAGENTA;
const char *titlecolor = MAGENTA;
size_t logosize = 11;
2024-06-18 15:25:34 +09:00
char *LOGO[] = {
2024-07-17 21:29:47 +09:00
" ⢀⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⣠⠾⠛⠶⣄⢀⣠⣤⠴⢦⡀ ",
"⠀⠀⠀⢠⡿⠉⠉⠉⠛⠶⠶⠖⠒⠒⣾⠋⠀⢀⣀⣙⣯⡁⠀⠀⠀⣿⠀⠀⠀ ",
"⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⡏⠀⠀⢯⣼⠋⠉⠙⢶⠞⠛⠻⣆ ",
"⠀⠀⠀⢸⣧⠆⠀⠀⠀⠀⠀⠀⠀⠀⠻⣦⣤⡤⢿⡀⠀⢀⣼⣷⠀⠀⣽ ",
"⠀⠀⠀⣼⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠙⢏⡉⠁⣠⡾⣇⠀⠀⠀ ",
"⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠋⠉⠀⢻⡀ ",
"⣀⣠⣼⣧⣤⠀⠀⠀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡀⠀⠀⠐⠖⢻⡟⠓⠒ ",
"⠀⠀⠈⣷⣀⡀⠀⠘⠿⠇⠀⠀⠀⢀⣀⣀⠀⠀⠀⠀⠿⠟⠀⠀⠀⠲⣾⠦⢤ ",
"⠀⠀⠋⠙⣧⣀⡀⠀⠀⠀⠀⠀⠀⠘⠦⠼⠃⠀⠀⠀⠀⠀⠀⠀⢤⣼⣏ ",
"⠀⠀⢀⠴⠚⠻⢧⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⠞⠉⠉⠓⠀⠀ ",
"⠀⠀⠀⠀⠀⠀⠀⠈⠉⠛⠛⠶⠶⠶⣶⣤⣴⡶⠶⠶⠟⠛⠉⠀⠀⠀⠀⠀⠀⠀ "
};
char *LOGO_SMALL[] = {
2024-07-17 21:29:47 +09:00
"|\____/| ",
"| | ",
"| 0 0 | ",
"| ω | ",
"\_____/ "
2024-06-18 15:25:34 +09:00
};
#endif
2024-06-20 01:19:20 +09:00
int minsize = MIN_SIZE;
2024-06-20 05:43:26 +09:00
if (isresolution) {
const char *res = display_resolution();
if (!res) minsize--;
else free((void *)res);
} else minsize--;
if (iswm) {
2024-07-15 15:02:13 +09:00
const char *winman = display_wm();
if (!winman) minsize--;
#if !defined(__APPLE__) && !defined(__HAIKU__)
else free((void *)winman);
#endif
} else minsize--;
if (islibc) {
const char *clang = display_libc();
if (clang) minsize++;
}
if (isstorage) {
const char *store = display_storage();
if (!store) minsize--;
else free((void *)store);
} else minsize--;
if (isgpu) {
const char *graph = display_gpu();
if (!graph) minsize--;
else free((void *)graph);
} else minsize--;
2024-06-20 05:43:26 +09:00
2024-06-19 23:52:56 +09:00
const char *reset = RESET;
2024-06-20 01:19:20 +09:00
size_t ls = logosize <= (size_t)minsize ? (size_t)minsize : logosize;
#if defined(__linux__) || defined(__sun)
if (islogo) {
logoname = argv[2];
getDistro(logoname);
}
#endif
2024-06-18 17:48:32 +09:00
if (issmall) {
size_t ne = sizeof(LOGO_SMALL) / sizeof(LOGO_SMALL[0]);
for (size_t i = 0; i < ne; i++) {
LOGO[i] = LOGO_SMALL[i];
}
2024-06-20 01:19:20 +09:00
ls = ne >= (size_t)minsize ? (size_t)minsize : ne;
2024-06-18 17:48:32 +09:00
}
2024-06-20 05:43:26 +09:00
const char *name = display_user_name();
const char *host = display_user_host();
if (name || host) {
2024-06-20 00:47:11 +09:00
printf("%s ", LOGO[lc]);
2024-06-20 05:52:29 +09:00
if (name) printf("%s%s%s",titlecolor, name, reset);
if (name && host) printf("@");
if (host) printf("%s%s%s", titlecolor, host, reset);
printf("\n");
2024-06-20 05:43:26 +09:00
if (name) free((void *)name);
if (host) free((void *)host);
2024-06-19 23:52:56 +09:00
lc++;
}
2024-06-18 15:59:14 +09:00
printf("%s ", LOGO[lc]);
2024-06-18 00:44:43 +09:00
printf("------------------\n");
2024-06-18 15:59:14 +09:00
lc++;
2024-06-17 22:41:46 +09:00
if (isos) {
const char *os = display_os();
if (os) {
printf("%s ", LOGO[lc]);
printf("%sOS%s: %s\n", color, reset, os);
free((void *)os);
lc++;
}
} else minsize--;
2024-06-17 22:41:46 +09:00
2024-06-22 03:13:21 +09:00
#if defined(__linux__) || defined(__sun)
if (isdistro) {
const char *distroo = display_distro();
if (distroo) {
printf("%s ", LOGO[lc]);
printf("%sDistro%s: %s\n", color, reset, distroo);
lc++;
free((void *)distroo);
}
} else minsize--;
2024-06-17 22:41:46 +09:00
#endif
2024-07-15 16:10:41 +09:00
#if !defined(__HAIKU__)
if (ishost) {
2024-06-20 00:47:11 +09:00
printf("%s ", LOGO[lc]);
printf("%s%s%s%s", color, "Host", reset, ": ");
display_host_model();
2024-06-20 00:47:11 +09:00
printf("\n");
lc++;
} else minsize--;
2024-07-15 16:10:41 +09:00
#endif
2024-06-18 13:39:02 +09:00
if (isuptime) {
const char *days = display_days();
const char *time = display_time();
if (days || time) {
printf("%s ", LOGO[lc]);
printf("%s%s%s%s", color, "Uptime", reset, ": ");
if (days) {
printf("%s", days);
if (time) printf(" ");
}
if (time) {
printf("%s", time);
}
printf("\n");
if (days) free((void *)days);
if (time) free((void *)time);
lc++;
2024-06-19 23:52:56 +09:00
}
} else minsize--;
#if defined(__OpenBSD__)
if (isrecording) {
const char *audio = display_recording_audio();
const char *video = display_recording_video();
if (audio || video) {
printf("%s ", LOGO[lc]);
printf("%sRecording%s: ", color, reset);
if (audio) {
printf("audio = %s", audio);
if (video) printf(", ");
}
if (video) {
printf("video = %s", video);
}
printf("\n");
if (audio) free((void *)audio);
if (video) free((void *)video);
lc++;
2024-06-19 23:52:56 +09:00
}
} else minsize--;
#endif
if (ispackages) {
const char *packages = display_packages();
if (packages) {
printf("%s ", LOGO[lc]);
printf("%sPackages%s: %s\n", color, reset, packages);
lc++;
free((void *)packages);
}
} else minsize--;
2024-06-18 19:12:24 +09:00
if (isresolution) {
const char *resolution = display_resolution();
if (resolution) {
printf("%s ", LOGO[lc]);
printf("%sResolution%s: %s\n", color, reset, resolution);
lc++;
free((void *)resolution);
}
2024-06-19 22:53:46 +09:00
}
if (iswm) {
const char *wm = display_wm();
if (wm) {
printf("%s ", LOGO[lc]);
printf("%sWM%s: %s\n", color, reset, wm);
2024-07-15 15:02:13 +09:00
#if !defined(__APPLE__) && !defined(__HAIKU__)
free((void *)wm);
#endif
lc++;
}
2024-06-21 21:02:43 +09:00
}
if (isshell) {
const char *shell = display_shell();
if (shell) {
printf("%s ", LOGO[lc]);
printf("%sShell%s: %s\n", color, reset, shell);
free((void *)shell);
lc++;
}
} else minsize--;
2024-06-21 21:34:05 +09:00
if (islibc) {
const char *libc = display_libc();
if (libc) {
printf("%s ", LOGO[lc]);
printf("%slibc%s: %s\n", color, reset, libc);
lc++;
}
2024-06-21 21:48:02 +09:00
}
if (iscpu) {
2024-06-20 05:43:26 +09:00
const char *cpu = display_cpu();
if (cpu) {
printf("%s ", LOGO[lc]);
printf("%sCPU%s: %s\n", color, reset, cpu);
lc++;
free((void *)cpu);
}
} else minsize--;
2024-06-18 13:39:02 +09:00
if (isgpu) {
const char *gpu = display_gpu();
if (gpu) {
printf("%s ", LOGO[lc]);
printf("%sGPU%s: %s\n", color, reset, gpu);
lc++;
free((void *)gpu);
}
}
2024-06-18 19:23:34 +09:00
if (ismemory) {
2024-06-21 22:14:36 +09:00
printf("%s ", LOGO[lc]);
printf("%s%s%s%s", color, "Memory", reset, ": ");
display_memory();
printf("\n");
2024-06-21 22:14:36 +09:00
lc++;
} else minsize--;
if (isstorage) {
const char *storage = display_storage();
if (storage) {
printf("%s ", LOGO[lc]);
printf("%sStorage%s: %s\n", color, reset, storage);
lc++;
free((void *)storage);
}
2024-06-21 22:14:36 +09:00
}
2024-06-18 17:48:32 +09:00
for (size_t i = lc; i < ls; i++) {
2024-06-18 15:25:34 +09:00
printf("%s\n", LOGO[i]);
}
2024-06-17 22:41:46 +09:00
return 0;
}