2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
2021-05-01 21:22:07 +02:00
#include <string.h>
2021-02-18 22:25:36 +01:00
#include <dirent.h>
2021-04-15 18:59:26 +02:00
#define FF_PACKAGES_MODULE_NAME "Packages"
2021-05-15 18:17:34 +02:00
#define FF_PACKAGES_NUM_FORMAT_ARGS 7
2021-04-15 18:59:26 +02:00
2021-05-01 21:22:07 +02:00
static uint32_t getNumElements ( const char * dirname , unsigned char type ) {
2021-04-27 21:52:55 +02:00
uint32_t num_elements = 0 ;
2021-02-18 22:25:36 +01:00
DIR * dirp ;
struct dirent * entry ;
dirp = opendir ( dirname );
2021-02-19 16:23:41 +01:00
if ( dirp == NULL )
return 0 ;
2021-02-18 22:25:36 +01:00
2021-04-28 16:05:06 +02:00
while (( entry = readdir ( dirp )) != NULL ) {
2021-04-28 07:10:55 +02:00
if ( entry -> d_type == type )
2021-04-27 21:52:55 +02:00
++ num_elements ;
2021-02-18 22:25:36 +01:00
}
2021-03-27 18:14:19 +01:00
2021-04-28 06:59:44 +02:00
if ( type == DT_DIR )
2021-04-27 21:52:55 +02:00
num_elements -= 2 ; // accounting for . and ..
2021-02-18 22:25:36 +01:00
closedir ( dirp );
2021-04-27 21:52:55 +02:00
return num_elements ;
2021-02-18 22:25:36 +01:00
}
2021-05-01 21:22:07 +02:00
static uint32_t getNumStrings ( const char * filename , const char * needle )
{
FILE * file = fopen ( filename , "r" );
if ( file == NULL )
return 0 ;
uint32_t count = 0 ;
char * line = NULL ;
size_t len = 0 ;
while ( getline ( & line , & len , file ) != EOF )
{
if ( strstr ( line , needle ) != NULL )
++ count ;
}
if ( line != NULL )
free ( line );
fclose ( file );
return count ;
}
2021-02-27 18:30:05 +01:00
void ffPrintPackages ( FFinstance * instance )
2021-02-18 22:25:36 +01:00
{
2021-05-01 21:22:07 +02:00
uint32_t pacman = getNumElements ( "/var/lib/pacman/local" , DT_DIR );
uint32_t xbps = getNumElements ( "/var/db/xbps" , DT_REG );
uint32_t dpkg = getNumStrings ( "/var/lib/dpkg/status" , "Status: " );
uint32_t flatpak = getNumElements ( "/var/lib/flatpak/app" , DT_DIR );
uint32_t snap = getNumElements ( "/snap" , DT_DIR );
2021-03-11 19:40:46 +01:00
2021-05-01 21:22:07 +02:00
uint32_t all = pacman + xbps + dpkg + flatpak + snap ;
2021-03-11 19:40:46 +01:00
2021-04-03 21:51:50 +02:00
if ( all == 0 )
2021-03-11 19:54:34 +01:00
{
2021-04-15 18:59:26 +02:00
ffPrintError ( instance , FF_PACKAGES_MODULE_NAME , 0 , & instance -> config . packagesKey , & instance -> config . packagesFormat , FF_PACKAGES_NUM_FORMAT_ARGS , "No packages from known package managers found" );
2021-04-03 21:51:50 +02:00
return ;
}
2021-05-15 18:17:34 +02:00
2021-05-15 17:21:02 +02:00
FFstrbuf manjaroBranch ;
ffStrbufInit ( & manjaroBranch );
2021-05-15 18:12:10 +02:00
if ( ffParsePropFile ( "/etc/pacman-mirrors.conf" , "Branch =" , & manjaroBranch ) && manjaroBranch . length == 0 )
ffStrbufSetS ( & manjaroBranch , "stable" );
2021-03-27 15:39:56 +01:00
2021-04-03 21:51:50 +02:00
if ( instance -> config . packagesFormat . length == 0 )
{
2021-04-15 18:59:26 +02:00
ffPrintLogoAndKey ( instance , FF_PACKAGES_MODULE_NAME , 0 , & instance -> config . batteryKey );
2021-03-11 19:40:46 +01:00
#define FF_PRINT_PACKAGE(name) \
2021-03-19 20:57:07 +01:00
if(name > 0) \
{ \
printf("%u ("#name")", name); \
if((all = all - name) > 0) \
printf(", "); \
};
2021-03-11 19:40:46 +01:00
2021-05-15 17:21:02 +02:00
if ( pacman > 0 )
{
printf ( "%u (pacman)" , pacman );
if ( manjaroBranch . length > 0 )
printf ( "[%s]" , manjaroBranch . chars );
if (( all = all - pacman ) > 0 )
printf ( ", " );
};
2021-05-15 18:12:10 +02:00
2021-04-27 21:52:55 +02:00
FF_PRINT_PACKAGE ( xbps )
2021-05-01 21:22:07 +02:00
FF_PRINT_PACKAGE ( dpkg )
FF_PRINT_PACKAGE ( flatpak )
FF_PRINT_PACKAGE ( snap )
2021-03-11 19:40:46 +01:00
2021-05-13 12:27:44 +02:00
//Fix linter warning of unused value of all
( void ) all ;
2021-03-11 19:40:46 +01:00
#undef FF_PRINT_PACKAGE
putchar ( '\n' );
}
2021-03-27 15:39:56 +01:00
else
{
2021-04-15 18:59:26 +02:00
ffPrintFormatString ( instance , FF_PACKAGES_MODULE_NAME , 0 , & instance -> config . packagesKey , & instance -> config . packagesFormat , NULL , FF_PACKAGES_NUM_FORMAT_ARGS , ( FFformatarg []){
{ FF_FORMAT_ARG_TYPE_UINT , & all },
{ FF_FORMAT_ARG_TYPE_UINT , & pacman },
2021-05-15 18:17:34 +02:00
{ FF_FORMAT_ARG_TYPE_STRBUF , & manjaroBranch },
2021-05-01 21:22:07 +02:00
{ FF_FORMAT_ARG_TYPE_UINT , & xbps },
{ FF_FORMAT_ARG_TYPE_UINT , & dpkg },
2021-04-27 21:52:55 +02:00
{ FF_FORMAT_ARG_TYPE_UINT , & flatpak },
2021-05-01 21:22:07 +02:00
{ FF_FORMAT_ARG_TYPE_UINT , & snap }
2021-04-15 18:59:26 +02:00
});
2021-03-27 15:39:56 +01:00
}
2021-05-15 18:17:34 +02:00
ffStrbufDestroy ( & manjaroBranch );
2021-03-27 18:14:19 +01:00
}