2021-03-11 17:21:18 +01:00
#include "fastfetch.h"
2021-04-15 18:59:26 +02:00
#define FF_TERMFONT_MODULE_NAME "Terminal Font"
#define FF_TERMFONT_NUM_FORMAT_ARGS 4
2021-04-05 19:25:34 +02:00
static void printTerminalFont ( FFinstance * instance , const char * font )
2021-03-19 20:57:07 +01:00
{
2021-04-05 19:25:34 +02:00
FF_STRBUF_CREATE ( name );
double size ;
2021-04-09 14:07:05 +02:00
ffGetFont ( font , & name , & size );
2021-04-05 19:25:34 +02:00
FF_STRBUF_CREATE ( pretty );
2021-04-09 14:07:05 +02:00
ffGetFontPretty ( & pretty , & name , size );
2021-04-05 19:25:34 +02:00
2021-03-23 19:48:52 +01:00
if ( instance -> config . termFontFormat . length == 0 )
2021-03-19 20:57:07 +01:00
{
2021-04-15 18:59:26 +02:00
ffPrintLogoAndKey ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . termFontKey );
2021-04-05 19:25:34 +02:00
ffStrbufPutTo ( & pretty , stdout );
2021-03-19 20:57:07 +01:00
}
else
{
2021-04-15 18:59:26 +02:00
ffPrintFormatString ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . termFontKey , & instance -> config . termFontFormat , NULL , FF_TERMFONT_NUM_FORMAT_ARGS , ( FFformatarg []){
{ FF_FORMAT_ARG_TYPE_STRING , font },
{ FF_FORMAT_ARG_TYPE_STRBUF , & name },
{ FF_FORMAT_ARG_TYPE_DOUBLE , & size },
{ FF_FORMAT_ARG_TYPE_STRBUF , & pretty }
});
2021-03-19 20:57:07 +01:00
}
2021-04-05 19:25:34 +02:00
ffStrbufDestroy ( & name );
ffStrbufDestroy ( & pretty );
2021-03-19 20:57:07 +01:00
}
2021-05-09 12:30:10 +02:00
static void printTerminalFontFromConfigFile ( FFinstance * instance , const char * configFile , const char * regex )
{
char font [ 128 ];
ffParsePropFileConfig ( instance , configFile , regex , font );
if ( font [ 0 ] == '\0' )
{
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . termFontKey , & instance -> config . termFontFormat , FF_TERMFONT_NUM_FORMAT_ARGS , "Couldn't find \" %s \" in \" $XDG_CONFIG_HOME/%s \" " , regex , configFile );
return ;
}
printTerminalFont ( instance , font );
}
2021-05-09 13:40:17 +02:00
static void printXCFETerminal ( FFinstance * instance )
{
FFstrbuf fontName ;
ffStrbufInitA ( & fontName , 256 );
ffParsePropFileConfig ( instance , "xfce4/terminal/terminalrc" , "FontUseSystem=%[^ \n ]" , fontName . chars );
2021-05-09 20:39:00 +02:00
if (( fontName . chars [ 0 ] == '\0' ) || ! ffStrbufCompS ( & fontName , "FALSE" ))
2021-05-09 13:40:17 +02:00
{
printTerminalFontFromConfigFile ( instance , "xfce4/terminal/terminalrc" , "FontName=%[^ \n ]" );
ffStrbufDestroy ( & fontName );
return ;
}
FFstrbuf absolutePath ;
ffStrbufInitA ( & absolutePath , 64 );
ffStrbufAppendS ( & absolutePath , instance -> state . passwd -> pw_dir );
ffStrbufAppendC ( & absolutePath , '/' );
ffStrbufAppendS ( & absolutePath , ".config/xfce4/xfconf/xfce-perchannel-xml/xsettings.xml" );
FILE * file = fopen ( absolutePath . chars , "r" );
if ( file == NULL )
{
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . termFontKey , & instance -> config . termFontFormat , FF_TERMFONT_NUM_FORMAT_ARGS , "Couldn't open \" %s \" " , absolutePath . chars );
ffStrbufDestroy ( & absolutePath );
ffStrbufDestroy ( & fontName );
return ;
}
FFstrbuf matchText ;
ffStrbufInitAS ( & matchText , 64 , "<property name= \" MonospaceFontName \" type= \" string \" value= \" " );
2021-05-09 20:39:00 +02:00
char * line = NULL ;
size_t len = 0 ;
2021-05-09 13:40:17 +02:00
while ( getline ( & line , & len , file ) != - 1 )
{
2021-05-09 20:39:00 +02:00
ffStrbufAppendS ( & fontName , line );
2021-05-09 13:40:17 +02:00
ffStrbufTrimLeft ( & fontName , ' ' );
if ( ffStrbufStartsWithS ( & fontName , matchText . chars ))
{
ffStrbufSubstrAfter ( & fontName , matchText . length - 1 );
ffStrbufSubstrBefore ( & fontName , fontName . length - 4 ); // ["/>\n]
break ;
}
2021-05-09 20:39:00 +02:00
ffStrbufClear ( & fontName );
2021-05-09 13:40:17 +02:00
}
2021-05-09 20:39:00 +02:00
ffStrbufDestroy ( & matchText );
ffStrbufDestroy ( & absolutePath );
2021-05-09 13:40:17 +02:00
if ( line != NULL )
free ( line );
fclose ( file );
if ( fontName . chars [ 0 ] != '\0' )
printTerminalFont ( instance , fontName . chars );
else
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . termFontKey , & instance -> config . termFontFormat , FF_TERMFONT_NUM_FORMAT_ARGS , "Couldn't find \" MonospaceFontName=%%[^ \\ n] \" in \" xsettings.xml \" " );
ffStrbufDestroy ( & fontName );
}
2021-03-11 17:21:18 +01:00
static void printKonsole ( FFinstance * instance )
{
char profile [ 128 ];
2021-05-07 14:55:43 +02:00
ffParsePropFileConfig ( instance , "konsolerc" , "DefaultProfile=%[^ \n ]" , profile );
2021-03-11 17:21:18 +01:00
if ( profile [ 0 ] == '\0' )
{
2021-04-15 18:59:26 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . termFontKey , & instance -> config . termFontFormat , FF_TERMFONT_NUM_FORMAT_ARGS , "Couldn't find \" DefaultProfile=%[^ \n ] \" in \" .config/konsolerc \" " );
2021-03-11 17:21:18 +01:00
return ;
}
char profilePath [ 256 ];
sprintf ( profilePath , ".local/share/konsole/%s" , profile );
char font [ 128 ];
ffParsePropFileHome ( instance , profilePath , "Font=%[^ \n ]" , font );
if ( font [ 0 ] == '\0' )
{
2021-04-15 18:59:26 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . termFontKey , & instance -> config . termFontFormat , FF_TERMFONT_NUM_FORMAT_ARGS , "Couldn't find \" Font=%%[^ \\ n] \" in \" %s \" " , profilePath );
2021-03-19 20:57:07 +01:00
return ;
2021-03-11 17:21:18 +01:00
}
2021-04-05 19:25:34 +02:00
printTerminalFont ( instance , font );
2021-03-11 17:21:18 +01:00
}
2021-05-08 20:31:18 +02:00
static void printTilixTerminal ( FFinstance * instance )
2021-05-09 08:03:09 +02:00
{ // Note - Subject to change when DConf/GSettings fix their implementations of defaults/relocatable schemas respectively
2021-05-08 21:17:04 +02:00
const char * fontName = NULL ;
2021-05-08 20:31:18 +02:00
2021-05-08 21:26:01 +02:00
const char * defaultProfile = ffSettingsGetGsettings ( instance , "com.gexperts.Tilix.ProfilesList" , NULL , "default" , FF_VARIANT_TYPE_STRING ). strValue ;
2021-05-08 20:31:18 +02:00
2021-05-09 08:03:09 +02:00
if ( ! defaultProfile )
2021-05-08 20:31:18 +02:00
{
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . termFontKey , & instance -> config . termFontFormat , FF_TERMFONT_NUM_FORMAT_ARGS , "Couldn't find \" Default \" profile in Tilix settings" );
return ;
}
2021-05-08 21:17:04 +02:00
FFstrbuf key ;
ffStrbufInitAS ( & key , 64 , "/com/gexperts/Tilix/profiles/" );
ffStrbufAppendS ( & key , defaultProfile );
2021-05-08 21:26:01 +02:00
uint32_t keyLen = key . length ;
2021-05-08 21:17:04 +02:00
ffStrbufAppendS ( & key , "/use-system-font" );
FFvariant res = ffSettingsGetDConf ( instance , key . chars , FF_VARIANT_TYPE_BOOL );
if ( res . boolValueSet && ! res . boolValue ) // custom font
2021-05-08 20:31:18 +02:00
{
2021-05-08 21:17:04 +02:00
ffStrbufSubstrBefore ( & key , keyLen );
ffStrbufAppendS ( & key , "/font" );
fontName = ffSettingsGetDConf ( instance , key . chars , FF_VARIANT_TYPE_STRING ). strValue ;
2021-05-08 20:31:18 +02:00
}
2021-05-08 21:17:04 +02:00
else if ( ! res . boolValueSet || res . boolValue ) // system font
fontName = ffSettingsGetDConf ( instance , "/org/gnome/desktop/interface/monospace-font-name" , FF_VARIANT_TYPE_STRING ). strValue ;
2021-05-08 20:31:18 +02:00
2021-05-09 08:03:09 +02:00
if ( ! fontName )
2021-05-08 20:31:18 +02:00
{
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . termFontKey , & instance -> config . termFontFormat , FF_TERMFONT_NUM_FORMAT_ARGS , "Couldn't find \" Terminal font \" in Tilix settings" );
ffStrbufDestroy ( & key );
return ;
}
printTerminalFont ( instance , fontName );
ffStrbufDestroy ( & key );
}
2021-03-11 17:21:18 +01:00
static void printTTY ( FFinstance * instance )
{
2021-05-01 16:54:59 +02:00
FFstrbuf font ;
ffStrbufInit ( & font );
2021-03-27 18:14:19 +01:00
2021-05-01 16:54:59 +02:00
ffParsePropFile ( "/etc/vconsole.conf" , "Font=%[^ \n ]" , font . chars );
ffStrbufRecalculateLength ( & font );
if ( font . length == 0 )
{
ffStrbufAppendS ( & font , "VGA default kernel font " );
ffProcessAppendStdOut ( & font , ( char * const []){
"showconsolefont" ,
"--info" ,
NULL
});
}
printTerminalFont ( instance , font . chars );
ffStrbufDestroy ( & font );
2021-03-11 17:21:18 +01:00
}
void ffPrintTerminalFont ( FFinstance * instance )
{
2021-04-28 19:53:30 +02:00
const FFTerminalResult * result = ffDetectTerminal ( instance );
2021-03-27 18:14:19 +01:00
2021-04-22 21:41:42 +02:00
if ( result -> exeName . length == 0 )
2021-03-11 17:21:18 +01:00
{
2021-04-15 18:59:26 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . termFontKey , & instance -> config . termFontFormat , FF_TERMFONT_NUM_FORMAT_ARGS , "Terminal font needs successfull terminal detection" );
2021-03-19 20:57:07 +01:00
return ;
2021-03-11 17:21:18 +01:00
}
2021-04-22 21:41:42 +02:00
if ( ffStrbufIgnCaseCompS ( & result -> exeName , "konsole" ) == 0 )
2021-03-11 17:21:18 +01:00
printKonsole ( instance );
2021-04-24 13:12:40 +02:00
else if ( ffStrbufIgnCaseCompS ( & result -> exeName , "xfce4-terminal" ) == 0 )
2021-05-09 13:40:17 +02:00
printXCFETerminal ( instance );
2021-05-09 12:30:10 +02:00
else if ( ffStrbufIgnCaseCompS ( & result -> exeName , "lxterminal" ) == 0 )
printTerminalFontFromConfigFile ( instance , "lxterminal/lxterminal.conf" , "fontname=%[^ \n ]" );
2021-05-08 20:31:18 +02:00
else if ( ffStrbufIgnCaseCompS ( & result -> exeName , "tilix" ) == 0 )
printTilixTerminal ( instance );
2021-05-01 16:54:59 +02:00
else if ( ffStrbufStartsWithIgnCaseS ( & result -> exeName , "/dev/tty" ))
2021-03-11 17:21:18 +01:00
printTTY ( instance );
else
2021-05-01 16:54:59 +02:00
ffPrintError ( instance , FF_TERMFONT_MODULE_NAME , 0 , & instance -> config . termFontKey , & instance -> config . termFontFormat , FF_TERMFONT_NUM_FORMAT_ARGS , "Unknown terminal: %s" , result -> exeName . chars );
2021-03-27 18:14:19 +01:00
}