light refactoring

This commit is contained in:
root committed 2026-10-08 21:48:35 +02:00
1 parent 4a8132c0b1
commit 5b1b974ac7
6 files changed
+97 -137

No files matched your search

+15 -15
View File
@@ -5,21 +5,21 @@
/* -- colour and style configuration */ /* -- colour and style configuration */
// additional information // additional information
// #define PERM_STYLE_NUM ( ( int )1 ) #define PERM_STYLE_NUM ( ( int )1 )
// const style_t permission_style[ PERM_STYLE_NUM ] = { style_none }; const style_t permission_style[ PERM_STYLE_NUM ] = { style_none };
// const uint32_t permission_colour = 0xEEEEEE; const uint32_t permission_colour = 0xD0D0D0;
//
// #define DATE_STYLE_NUM ( ( int )1 ) #define DATE_STYLE_NUM ( ( int )1 )
// const style_t date_style[ DATE_STYLE_NUM ] = { style_none }; const style_t date_style[ DATE_STYLE_NUM ] = { style_none };
// const uint32_t date_colour = 0xEEEEEE; const uint32_t date_colour = 0xEEEEEE;
//
// #define TYPE_STYLE_NUM ( ( int )1 ) #define TYPE_STYLE_NUM ( ( int )1 )
// const style_t type_style[ TYPE_STYLE_NUM ] = { style_none }; const style_t type_style[ TYPE_STYLE_NUM ] = { style_none };
// const uint32_t type_colour = 0xEEEEEE; const uint32_t type_colour = 0x886699;
//
// #define SIZE_STYLE_NUM ( ( int )1 ) #define SIZE_STYLE_NUM ( ( int )1 )
// const style_t size_style[ SIZE_STYLE_NUM ] = { style_none }; const style_t size_style[ SIZE_STYLE_NUM ] = { style_none };
// const uint32_t size_colour = 0xEEEEEE; const uint32_t size_colour = 0xA3BCF5;
// files by type // files by type
#define BLK_STYLE_NUM ( ( int )1 ) #define BLK_STYLE_NUM ( ( int )1 )
+5 -5
View File
@@ -3,10 +3,10 @@
#include "type_definitions.h" #include "type_definitions.h"
void display_link_target(const directory_entry_t* const entry); void display_type( char type );
void set_type_style( const directory_entry_t* const entry ); void display_permissions( const char* const permissions );
void reset_type_style( const directory_entry_t* const entry ); void display_size( const char* const size );
void set_file_style( const directory_entry_t* const entry ); void display_entry( const directory_entry_t* const entry );
void reset_file_style( const directory_entry_t* const entry ); void display_link_target( const directory_entry_t* const entry );
#endif #endif
+2 -4
View File
@@ -38,9 +38,7 @@ extern const uint8_t STYLE_RESET[ NUMBER_STYLES ];
void format_clear( void ); void format_clear( void );
void format_set_style( const style_t* const styles, uint8_t number_styles ); void format_set_style( const style_t* const styles, uint8_t number_styles );
void format_reset_style( const style_t* const styles, uint8_t number_styles ); void format_reset_style( const style_t* const styles, uint8_t number_styles );
void format_set_fg_colour( uint32_t colour ); void format_set_colour( uint32_t colour );
void format_set_bg_colour( uint32_t colour ); void format_clear_colour( void );
void format_clear_fg_colour( void );
void format_clear_bg_colour( void );
#endif #endif
+46 -75
View File
@@ -12,11 +12,44 @@
#include "stringing.h" #include "stringing.h"
// function signatures // function signatures
void set_type_style( const directory_entry_t* const entry );
void set_file_style( const directory_entry_t* const entry );
bool compare_makefile( const directory_entry_t* const entry ); bool compare_makefile( const directory_entry_t* const entry );
bool compare_archives( const directory_entry_t* const entry ); bool compare_archives( const directory_entry_t* const entry );
bool compare_sources( const directory_entry_t* const entry ); bool compare_sources( const directory_entry_t* const entry );
// module logic // module logic
void display_type( char type )
{
format_set_style( type_style, TYPE_STYLE_NUM );
format_set_colour( type_colour );
fprintf( stdout, "%c", type );
format_clear();
}
void display_permissions( const char* const permissions )
{
format_set_style( permission_style, PERM_STYLE_NUM );
format_set_colour( permission_colour );
fprintf( stdout, "%s", permissions );
format_clear();
}
void display_size( const char* const size )
{
format_set_style( size_style, SIZE_STYLE_NUM );
format_set_colour( size_colour );
fprintf( stdout, "%s", size );
format_clear();
}
void display_entry( const directory_entry_t* const entry )
{
set_type_style( entry );
fprintf( stdout, "%s", entry->name );
format_clear();
}
void display_link_target(const directory_entry_t* const entry) void display_link_target(const directory_entry_t* const entry)
{ {
char link[ PATH_MAX ]; char link[ PATH_MAX ];
@@ -39,12 +72,12 @@ void display_link_target(const directory_entry_t* const entry)
if ( link_invalid ) if ( link_invalid )
{ {
format_set_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM ); format_set_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM );
format_set_fg_colour( brokenlink_colour ); format_set_colour( brokenlink_colour );
} }
else else
{ {
format_set_style( &link_style[ 0 ], LINK_STYLE_NUM ); format_set_style( &link_style[ 0 ], LINK_STYLE_NUM );
format_set_fg_colour( link_colour ); format_set_colour( link_colour );
} }
fprintf( stdout, "%s", link ); fprintf( stdout, "%s", link );
@@ -52,12 +85,12 @@ void display_link_target(const directory_entry_t* const entry)
if ( link_invalid ) if ( link_invalid )
{ {
format_reset_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM ); format_reset_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM );
format_clear_fg_colour(); format_clear_colour();
} }
else else
{ {
format_reset_style( &link_style[ 0 ], LINK_STYLE_NUM ); format_reset_style( &link_style[ 0 ], LINK_STYLE_NUM );
format_clear_fg_colour(); format_clear_colour();
} }
} }
@@ -67,27 +100,27 @@ void set_type_style( const directory_entry_t* const entry )
{ {
case type_block: case type_block:
format_set_style( &block_style[ 0 ], BLK_STYLE_NUM ); format_set_style( &block_style[ 0 ], BLK_STYLE_NUM );
format_set_fg_colour( block_colour ); format_set_colour( block_colour );
break; break;
case type_character: case type_character:
format_set_style( &character_style[ 0 ], CHAR_STYLE_NUM ); format_set_style( &character_style[ 0 ], CHAR_STYLE_NUM );
format_set_fg_colour( character_colour ); format_set_colour( character_colour );
break; break;
case type_directory: case type_directory:
format_set_style( &directory_style[ 0 ], DIR_STYLE_NUM ); format_set_style( &directory_style[ 0 ], DIR_STYLE_NUM );
format_set_fg_colour( directory_colour ); format_set_colour( directory_colour );
break; break;
case type_fifo: case type_fifo:
format_set_style( &fifo_style[ 0 ], FIFO_STYLE_NUM ); format_set_style( &fifo_style[ 0 ], FIFO_STYLE_NUM );
format_set_fg_colour( fifo_colour ); format_set_colour( fifo_colour );
break; break;
case type_link: case type_link:
format_set_style( &link_style[ 0 ], LINK_STYLE_NUM ); format_set_style( &link_style[ 0 ], LINK_STYLE_NUM );
format_set_fg_colour( link_colour ); format_set_colour( link_colour );
break; break;
case type_regular: case type_regular:
@@ -96,47 +129,7 @@ void set_type_style( const directory_entry_t* const entry )
case type_socket: case type_socket:
format_set_style( &socket_style[ 0 ], SOCK_STYLE_NUM ); format_set_style( &socket_style[ 0 ], SOCK_STYLE_NUM );
format_set_fg_colour( socket_colour ); format_set_colour( socket_colour );
break;
}
}
void reset_type_style( const directory_entry_t* const entry )
{
switch ( entry->type )
{
case type_block:
format_reset_style( &block_style[ 0 ], BLK_STYLE_NUM );
format_clear_fg_colour();
break;
case type_character:
format_reset_style( &character_style[ 0 ], CHAR_STYLE_NUM );
format_clear_fg_colour();
break;
case type_directory:
format_reset_style( &directory_style[ 0 ], DIR_STYLE_NUM );
format_clear_fg_colour();
break;
case type_fifo:
format_reset_style( &fifo_style[ 0 ], FIFO_STYLE_NUM );
format_clear_fg_colour();
break;
case type_link:
format_reset_style( &link_style[ 0 ], LINK_STYLE_NUM );
format_clear_fg_colour();
break;
case type_regular:
reset_file_style( entry );
break;
case type_socket:
format_reset_style( &socket_style[ 0 ], SOCK_STYLE_NUM );
format_clear_fg_colour();
break; break;
} }
} }
@@ -149,39 +142,17 @@ void set_file_style( const directory_entry_t* const entry )
if ( compare_makefile( entry ) ) if ( compare_makefile( entry ) )
{ {
format_set_style( &makefile_style[ 0 ], MAKE_STYLE_NUM ); format_set_style( &makefile_style[ 0 ], MAKE_STYLE_NUM );
format_set_fg_colour( makefile_colour ); format_set_colour( makefile_colour );
} }
else if ( compare_archives( entry ) ) else if ( compare_archives( entry ) )
{ {
format_set_style( &archive_style[ 0 ], AR_STYLE_NUM ); format_set_style( &archive_style[ 0 ], AR_STYLE_NUM );
format_set_fg_colour( archive_colour ); format_set_colour( archive_colour );
} }
else if ( compare_sources( entry ) ) else if ( compare_sources( entry ) )
{ {
format_set_style( &source_style[ 0 ], SRC_STYLE_NUM ); format_set_style( &source_style[ 0 ], SRC_STYLE_NUM );
format_set_fg_colour( source_colour ); format_set_colour( source_colour );
}
}
void reset_file_style( const directory_entry_t* const entry )
{
if ( !entry ) return;
// makefiles and readmes
if ( compare_makefile( entry ) )
{
format_reset_style( &makefile_style[ 0 ], MAKE_STYLE_NUM );
format_clear_fg_colour();
}
else if ( compare_archives( entry ) )
{
format_reset_style( &archive_style[ 0 ], AR_STYLE_NUM );
format_clear_fg_colour();
}
else if ( compare_sources( entry ) )
{
format_reset_style( &source_style[ 0 ], SRC_STYLE_NUM );
format_clear_fg_colour();
} }
} }
+18 -22
View File
@@ -8,11 +8,18 @@ const uint8_t STYLE_RESET[ NUMBER_STYLES ] =
{ 22u, 22u, 23u, 24u, 25u, 27u, 28u, 29u }; { 22u, 22u, 23u, 24u, 25u, 27u, 28u, 29u };
/* -- module logic */ /* -- module logic */
/*
* reset colour and font style
*/
void format_clear( void ) void format_clear( void )
{ {
fprintf( stdout, SEQUENCE_PREFIX"0"SEQUENCE_SUFFIX ); fprintf( stdout, SEQUENCE_PREFIX"0"SEQUENCE_SUFFIX );
} }
/*
* set specified font styles
*/
void format_set_style( const style_t* const styles, uint8_t number ) void format_set_style( const style_t* const styles, uint8_t number )
{ {
if ( 0 == number ) return; if ( 0 == number ) return;
@@ -34,6 +41,9 @@ void format_set_style( const style_t* const styles, uint8_t number )
fprintf( stdout, SEQUENCE_SUFFIX ); fprintf( stdout, SEQUENCE_SUFFIX );
} }
/*
* reset specified font styles
*/
void format_reset_style( const style_t* const styles, uint8_t number ) void format_reset_style( const style_t* const styles, uint8_t number )
{ {
if ( 0 == number ) return; if ( 0 == number ) return;
@@ -55,7 +65,10 @@ void format_reset_style( const style_t* const styles, uint8_t number )
fprintf( stdout, SEQUENCE_SUFFIX ); fprintf( stdout, SEQUENCE_SUFFIX );
} }
void format_set_fg_colour( uint32_t colour ) /*
* sets the foreground colour to 0xRRGGBB
*/
void format_set_colour( uint32_t colour )
{ {
uint8_t red = ( colour & 0xFF0000 ) >> 16u; uint8_t red = ( colour & 0xFF0000 ) >> 16u;
uint8_t green = ( colour & 0x00FF00 ) >> 8u; uint8_t green = ( colour & 0x00FF00 ) >> 8u;
@@ -70,27 +83,10 @@ void format_set_fg_colour( uint32_t colour )
); );
} }
void format_set_bg_colour( uint32_t colour ) /*
{ * resets the foreground colour to default
uint8_t red = ( colour & 0xFF0000 ) >> 16u; */
uint8_t green = ( colour & 0x00FF00 ) >> 8u; void format_clear_colour( void )
uint8_t blue = ( colour & 0x0000FF );
fprintf(
stdout,
SEQUENCE_PREFIX"48;2;%d;%d;%d"SEQUENCE_SUFFIX,
red,
green,
blue
);
}
void format_clear_fg_colour( void )
{ {
fprintf( stdout, SEQUENCE_PREFIX"39"SEQUENCE_SUFFIX ); fprintf( stdout, SEQUENCE_PREFIX"39"SEQUENCE_SUFFIX );
} }
void format_clear_bg_colour( void )
{
fprintf( stdout, SEQUENCE_PREFIX"49"SEQUENCE_SUFFIX );
}
+11 -16
View File
@@ -48,7 +48,7 @@ static int get_entries(
const arguments_t* const args const arguments_t* const args
); );
static int is_entry_visible( const arguments_t* const args, char* entry_name ); static int is_entry_visible( const arguments_t* const args, char* entry_name );
static int display_directory_contents( static int handle_directory(
const arguments_t* const args, const arguments_t* const args,
DIR* directory, DIR* directory,
size_t recursion_level, size_t recursion_level,
@@ -56,6 +56,7 @@ static int display_directory_contents(
); );
// program logic // program logic
// TODO: implement display for owner (user) / time (modified)
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
int retval = SUCCESS; int retval = SUCCESS;
@@ -87,7 +88,7 @@ int main( int argc, char** argv )
if ( !chdir( args.path ) ) if ( !chdir( args.path ) )
{ {
// TODO: handle return code // TODO: handle return code
display_directory_contents( handle_directory(
&args, &args,
directory, directory,
0, 0,
@@ -278,7 +279,7 @@ int is_entry_visible( const arguments_t* const args, char* entry_name )
return SUCCESS; return SUCCESS;
} }
int display_directory_contents( int handle_directory(
const arguments_t* const args, const arguments_t* const args,
DIR* directory, DIR* directory,
size_t recursion_level, size_t recursion_level,
@@ -393,26 +394,20 @@ int display_directory_contents(
if ( !args->tree_view ) if ( !args->tree_view )
{ {
fprintf( display_type( file_type );
stdout, display_permissions( file_permissions );
"%c%s%s ", display_size( file_size );
file_type, fputc( ' ', stdout );
file_permissions,
file_size
);
} }
set_type_style( entry ); display_entry( entry );
fprintf( stdout, "%s", entry->name );
reset_type_style( entry );
if ( type_link == entry->type ) if ( type_link == entry->type )
{ {
display_link_target( entry ); display_link_target( entry );
} }
fputc( '\n', stdout ); fputc( '\n', stdout );
if ( args->tree_view if ( args->tree_view
&& recursion_level < args->depth && recursion_level < args->depth
&& entry->type == type_directory ) && entry->type == type_directory )
@@ -426,7 +421,7 @@ int display_directory_contents(
{ {
chdir( entry->name ); chdir( entry->name );
// TODO: handle return code // TODO: handle return code
display_directory_contents( handle_directory(
args, args,
subdirectory, subdirectory,
recursion_level + 1, recursion_level + 1,