ncneofetch: user/hostname on unix, put 'em in title #550

This commit is contained in:
nick black 2020-06-17 20:45:58 -04:00
parent 356f5e66fa
commit 85cef91ad9
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

View File

@ -1,4 +1,8 @@
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <locale.h> #include <locale.h>
#include <sys/types.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <notcurses/notcurses.h> #include <notcurses/notcurses.h>
@ -7,6 +11,11 @@ typedef struct distro_info {
const char* logofile; // kept at original aspect ratio, lain atop bg const char* logofile; // kept at original aspect ratio, lain atop bg
} distro_info; } distro_info;
typedef struct fetched_info {
char* username; // we borrow a reference
char hostname[HOST_NAME_MAX];
} fetched_info;
static distro_info distros[] = { static distro_info distros[] = {
{ {
.name = "Debian", .name = "Debian",
@ -67,12 +76,42 @@ done:
return dinfo; return dinfo;
} }
static int
unix_getusername(fetched_info* fi){
if( (fi->username = getenv("LOGNAME")) ){
if( (fi->username = strdup(fi->username)) ){
return 0;
}
}
uid_t uid = getuid();
struct passwd* p = getpwuid(uid);
if(p == NULL){
return -1;
}
fi->username = strdup(p->pw_name);
return 0;
}
static int
unix_gethostname(fetched_info* fi){
if(gethostname(fi->hostname, sizeof(fi->hostname)) == 0){
char* fqdn = strchr(fi->hostname, '.');
if(fqdn){
*fqdn = '\0';
}
return 0;
}
return -1;
}
static const distro_info* static const distro_info*
linux_ncneofetch(void){ linux_ncneofetch(fetched_info* fi){
const distro_info* dinfo = getdistro(); const distro_info* dinfo = getdistro();
if(dinfo == NULL){ if(dinfo == NULL){
return NULL; return NULL;
} }
unix_gethostname(fi);
unix_getusername(fi);
return dinfo; return dinfo;
} }
@ -122,16 +161,18 @@ display(struct notcurses* nc, const distro_info* dinfo){
} }
static const distro_info* static const distro_info*
freebsd_ncneofetch(void){ freebsd_ncneofetch(fetched_info* fi){
static const distro_info fbsd = { static const distro_info fbsd = {
.name = "FreeBSD", .name = "FreeBSD",
.logofile = NULL, // FIXME .logofile = NULL, // FIXME
}; };
unix_gethostname(fi);
unix_getusername(fi);
return &fbsd; return &fbsd;
} }
static int static int
infoplane(struct notcurses* nc){ infoplane(struct notcurses* nc, const fetched_info* fi){
const int dimy = ncplane_dim_y(notcurses_stdplane(nc)); const int dimy = ncplane_dim_y(notcurses_stdplane(nc));
const int planeheight = 8; const int planeheight = 8;
struct ncplane* infop = ncplane_aligned(notcurses_stdplane(nc), struct ncplane* infop = ncplane_aligned(notcurses_stdplane(nc),
@ -144,19 +185,24 @@ infoplane(struct notcurses* nc){
if(ncplane_perimeter_rounded(infop, 0, 0, 0)){ if(ncplane_perimeter_rounded(infop, 0, 0, 0)){
return -1; return -1;
} }
if(ncplane_printf_aligned(infop, 0, NCALIGN_CENTER, "[ %s@%s ]",
fi->username, fi->hostname) < 0){
return -1;
}
return 0; return 0;
} }
static int static int
ncneofetch(struct notcurses* nc){ ncneofetch(struct notcurses* nc){
fetched_info fi = {};
const distro_info* dinfo = NULL; const distro_info* dinfo = NULL;
ncneo_kernel_e kern = get_kernel(); ncneo_kernel_e kern = get_kernel();
switch(kern){ switch(kern){
case NCNEO_LINUX: case NCNEO_LINUX:
dinfo = linux_ncneofetch(); dinfo = linux_ncneofetch(&fi);
break; break;
case NCNEO_FREEBSD: case NCNEO_FREEBSD:
dinfo = freebsd_ncneofetch(); dinfo = freebsd_ncneofetch(&fi);
break; break;
case NCNEO_UNKNOWN: case NCNEO_UNKNOWN:
break; break;
@ -167,7 +213,7 @@ ncneofetch(struct notcurses* nc){
if(display(nc, dinfo)){ if(display(nc, dinfo)){
return -1; // FIXME soldier on, perhaps? return -1; // FIXME soldier on, perhaps?
} }
if(infoplane(nc)){ if(infoplane(nc, &fi)){
return -1; return -1;
} }
if(notcurses_render(nc)){ if(notcurses_render(nc)){