ncneofetch: parse lsb_release -i #550

This commit is contained in:
nick black 2020-06-16 13:29:21 -04:00 committed by Nick Black
parent ffab8b5294
commit 6538fb9c08

View File

@ -1,9 +1,115 @@
#include <locale.h>
#include <notcurses/notcurses.h>
typedef struct distro_info {
const char* name;
const char* logofile;
} distro_info;
static distro_info distros[] = {
{
.name = "Debian",
// from desktop-base package
.logofile = "/usr/share/desktop-base/debian-logos/logo-text-256.png",
}, {
.name = NULL,
.logofile = NULL,
},
};
static const distro_info*
getdistro(void){
FILE* p = popen("lsb_release -i", "re");
if(p == NULL){
fprintf(stderr, "Error running lsb_release -i (%s)\n", strerror(errno));
return NULL;
}
const distro_info* dinfo = NULL;
char* buf = malloc(BUFSIZ); // gatesv("BUFSIZ bytes is enough for anyone")
if(fgets(buf, BUFSIZ, p) == NULL){
fprintf(stderr, "Error reading from lsb_release -i (%s)\n", strerror(errno));
fclose(p);
goto done;
}
if(fclose(p)){
fprintf(stderr, "Error closing pipe (%s)\n", strerror(errno));
goto done;
}
const char* colon = strchr(buf, ':');
if(colon == NULL){
goto done;
}
const char* distro = ++colon;
while(*distro && isspace(*distro)){
++distro;
}
const char* nl = strchr(distro, '\n');
if(nl == NULL){
goto done;
}
size_t len = nl - distro;
if(len){
for(dinfo = distros ; dinfo->name ; ++dinfo){
if(strncmp(dinfo->name, distro, nl - distro) == 0){
if(strlen(dinfo->name) == len){
break;
}
}
}
}
if(dinfo->name == NULL){
dinfo = NULL;
}
done:
free(buf);
return dinfo;
}
static int
ncneofetch(struct notcurses* nc){
const distro_info* dinfo = getdistro();
if(dinfo == NULL){
return -1;
}
nc_err_e err;
struct ncvisual* ncv = ncvisual_from_file(dinfo->logofile, &err);
if(ncv == NULL){
fprintf(stderr, "Error opening logo file at %s\n", dinfo->logofile);
return -1;
}
struct ncvisual_options vopts = {
.scaling = NCSCALE_SCALE,
.blitter = NCBLIT_2x2,
};
struct ncplane* n = ncvisual_render(nc, ncv, &vopts);
if(n == NULL){
ncvisual_destroy(ncv);
return -1;
}
if(notcurses_render(nc)){
ncvisual_destroy(ncv);
return -1;
}
ncplane_destroy(n);
ncvisual_destroy(ncv);
return 0;
}
int main(int argc, const char** argv){
struct notcurses* nc = notcurses_init(NULL, NULL);
if(setlocale(LC_ALL, "") == NULL){
fprintf(stderr, "Warning: couldn't set locale based off LANG\n");
}
struct notcurses_options nopts = {
.flags = NCOPTION_INHIBIT_SETLOCALE | NCOPTION_NO_ALTERNATE_SCREEN,
};
struct notcurses* nc = notcurses_init(&nopts, NULL);
if(nc == NULL){
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
int r = ncneofetch(nc);
if(notcurses_stop(nc)){
return EXIT_FAILURE;
}
return r ? EXIT_FAILURE : EXIT_SUCCESS;
}