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