2023-07-26 10:27:40 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "detection/terminalsize/terminalsize.h"
#include "modules/terminalsize/terminalsize.h"
#include "util/stringUtils.h"
#define FF_TERMINALSIZE_DISPLAY_NAME "Terminal Size"
#define FF_TERMINALSIZE_NUM_FORMAT_ARGS 4
void ffPrintTerminalSize ( FFTerminalSizeOptions * options )
{
FFTerminalSizeResult result = {};
if ( ! ffDetectTerminalSize ( & result ))
{
2024-03-07 11:10:18 +08:00
ffPrintError ( FF_TERMINALSIZE_DISPLAY_NAME , 0 , & options -> moduleArgs , FF_PRINT_TYPE_DEFAULT , "Failed to detect terminal size" );
2023-07-26 10:27:40 +08:00
}
else
{
if ( options -> moduleArgs . outputFormat . length == 0 )
{
2023-08-15 21:40:22 +08:00
ffPrintLogoAndKey ( FF_TERMINALSIZE_DISPLAY_NAME , 0 , & options -> moduleArgs , FF_PRINT_TYPE_DEFAULT );
2023-07-26 10:27:40 +08:00
printf ( "%u columns x %u rows" , result . columns , result . rows );
if ( result . width != 0 && result . height != 0 )
printf ( " (%upx x %upx)" , result . width , result . height );
putchar ( '\n' );
}
else
{
2024-03-07 10:47:28 +08:00
FF_PRINT_FORMAT_CHECKED ( FF_TERMINALSIZE_DISPLAY_NAME , 0 , & options -> moduleArgs , FF_PRINT_TYPE_DEFAULT , FF_TERMINALSIZE_NUM_FORMAT_ARGS , (( FFformatarg []){
2024-09-03 14:52:40 +08:00
FF_FORMAT_ARG ( result . rows , "rows" ),
FF_FORMAT_ARG ( result . columns , "columns" ),
FF_FORMAT_ARG ( result . width , "width" ),
FF_FORMAT_ARG ( result . height , "height" ),
2024-03-07 10:47:28 +08:00
}));
2023-07-26 10:27:40 +08:00
}
}
}
bool ffParseTerminalSizeCommandOptions ( FFTerminalSizeOptions * options , const char * key , const char * value )
{
const char * subKey = ffOptionTestPrefix ( key , FF_TERMINALSIZE_MODULE_NAME );
if ( ! subKey ) return false ;
if ( ffOptionParseModuleArgs ( key , subKey , value , & options -> moduleArgs ))
return true ;
return false ;
}
2023-08-17 10:56:54 +08:00
void ffParseTerminalSizeJsonObject ( FFTerminalSizeOptions * options , yyjson_val * module )
2023-07-26 10:27:40 +08:00
{
2023-08-17 16:09:41 +08:00
yyjson_val * key_ , * val ;
size_t idx , max ;
yyjson_obj_foreach ( module , idx , max , key_ , val )
2023-07-26 10:27:40 +08:00
{
2023-08-17 16:09:41 +08:00
const char * key = yyjson_get_str ( key_ );
if ( ffStrEqualsIgnCase ( key , "type" ))
continue ;
2023-07-26 10:27:40 +08:00
2023-08-17 16:09:41 +08:00
if ( ffJsonConfigParseModuleArgs ( key , val , & options -> moduleArgs ))
continue ;
2023-07-26 10:27:40 +08:00
2024-03-07 11:10:18 +08:00
ffPrintError ( FF_TERMINALSIZE_MODULE_NAME , 0 , & options -> moduleArgs , FF_PRINT_TYPE_DEFAULT , "Unknown JSON key %s" , key );
2023-07-26 10:27:40 +08:00
}
}
2023-09-12 16:17:17 +08:00
2023-10-24 14:41:29 +08:00
void ffGenerateTerminalSizeJsonConfig ( FFTerminalSizeOptions * options , yyjson_mut_doc * doc , yyjson_mut_val * module )
{
__attribute__ (( __cleanup__ ( ffDestroyTerminalSizeOptions ))) FFTerminalSizeOptions defaultOptions ;
ffInitTerminalSizeOptions ( & defaultOptions );
ffJsonConfigGenerateModuleArgsConfig ( doc , module , & defaultOptions . moduleArgs , & options -> moduleArgs );
}
2023-10-23 10:14:22 +08:00
void ffGenerateTerminalSizeJsonResult ( FF_MAYBE_UNUSED FFTerminalOptions * options , yyjson_mut_doc * doc , yyjson_mut_val * module )
2023-09-12 16:17:17 +08:00
{
FFTerminalSizeResult result ;
if ( ! ffDetectTerminalSize ( & result ))
{
yyjson_mut_obj_add_str ( doc , module , "error" , "Failed to detect terminal size" );
return ;
}
yyjson_mut_val * obj = yyjson_mut_obj_add_obj ( doc , module , "result" );
yyjson_mut_obj_add_uint ( doc , obj , "columns" , result . columns );
yyjson_mut_obj_add_uint ( doc , obj , "rows" , result . rows );
yyjson_mut_obj_add_uint ( doc , obj , "width" , result . width );
yyjson_mut_obj_add_uint ( doc , obj , "height" , result . height );
}
2023-10-07 13:40:40 +08:00
void ffPrintTerminalSizeHelpFormat ( void )
{
2024-03-07 10:47:28 +08:00
FF_PRINT_MODULE_FORMAT_HELP_CHECKED ( FF_TERMINALSIZE_MODULE_NAME , "{1} columns x {2} rows ({3}px x {4}px)" , FF_TERMINALSIZE_NUM_FORMAT_ARGS , (( const char * []) {
2024-05-27 16:41:37 +08:00
"Terminal rows - rows" ,
"Terminal columns - columns" ,
"Terminal width (in pixels) - width" ,
"Terminal height (in pixels) - height" ,
2024-03-07 10:47:28 +08:00
}));
2023-10-07 13:40:40 +08:00
}
2023-10-23 13:32:18 +08:00
void ffInitTerminalSizeOptions ( FFTerminalSizeOptions * options )
{
2023-10-23 13:39:07 +08:00
ffOptionInitModuleBaseInfo (
& options -> moduleInfo ,
FF_TERMINALSIZE_MODULE_NAME ,
2023-11-17 14:12:17 +08:00
"Print current terminal size" ,
2023-10-23 13:39:07 +08:00
ffParseTerminalSizeCommandOptions ,
ffParseTerminalSizeJsonObject ,
ffPrintTerminalSize ,
ffGenerateTerminalSizeJsonResult ,
ffPrintTerminalSizeHelpFormat ,
2023-10-24 14:41:29 +08:00
ffGenerateTerminalSizeJsonConfig
2023-10-23 13:39:07 +08:00
);
2024-07-23 23:02:35 +08:00
ffOptionInitModuleArg ( & options -> moduleArgs , "" );
2023-10-23 13:32:18 +08:00
}
void ffDestroyTerminalSizeOptions ( FFTerminalSizeOptions * options )
{
ffOptionDestroyModuleArg ( & options -> moduleArgs );
}