diff --git a/inc/config.h b/inc/config.h index f0a098d..f94203a 100644 --- a/inc/config.h +++ b/inc/config.h @@ -10,12 +10,71 @@ #include "format.h" /* -- 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; + +// files by type +#define BLK_STYLE_NUM ( ( int )1 ) +const style_t block_style[BLK_STYLE_NUM] = { style_none }; +const uint32_t block_colour = 0xFFCC13; + +#define CHAR_STYLE_NUM ( ( int )1 ) +const style_t character_style[CHAR_STYLE_NUM] = { style_none }; +const uint32_t character_colour = 0xEEEEEE; + #define DIR_STYLE_NUM ( ( int )1 ) const style_t directory_style[DIR_STYLE_NUM] = { style_bold }; const uint32_t directory_colour = 0x6CA05C; +#define FIFO_STYLE_NUM ( ( int )1 ) +const style_t fifo_style[FIFO_STYLE_NUM] = { style_none }; +const uint32_t fifo_colour = 0xEEEEEE; + +#define LINK_STYLE_NUM ( ( int )1 ) +const style_t link_style[LINK_STYLE_NUM] = { style_none }; +const uint32_t link_colour = 0x19D1DE; + +// #define BLNK_STYLE_NUM ( ( int )1 ) +// const style_t brokenlink_style[BLNK_STYLE_NUM] = { style_none }; +// const uint32_t brokenlink_colour = 0xEEEEEE; + +#define SOCK_STYLE_NUM ( ( int )1 ) +const style_t socket_style[SOCK_STYLE_NUM] = { style_none }; +const uint32_t socket_colour = 0xEEEEEE; + #define EXEC_STYLE_NUM ( ( int )1 ) const style_t exec_style[EXEC_STYLE_NUM] = { style_none }; -const uint32_t exec_colour = 0x34AD1F; +const uint32_t exec_colour = 0xEEEEEE; + +// // files by extension +// #define MAKE_STYLE_NUM ( ( int )1 ) +// const style_t makefile_style[MAKE_STYLE_NUM] = { style_none }; +// const uint32_t makefile_colour = 0xEAE977; +// +// #define SRC_STYLE_NUM ( ( int )1 ) +// const style_t source_style[SRC_STYLE_NUM] = { style_none }; +// const uint32_t source_colour = 0x6FADD8; +// +// #define MEDIA_STYLE_NUM ( ( int )1 ) +// const style_t media_style[MEDIA_STYLE_NUM] = { style_none }; +// const uint32_t media_colour = 0x6FADD8; +// +// #define AR_STYLE_NUM ( ( int )1 ) +// const style_t archive_style[AR_STYLE_NUM] = { style_none }; +// const uint32_t archive_colour = 0x6FADD8; #endif diff --git a/src/format.c b/src/format.c index fb95206..0aa8298 100644 --- a/src/format.c +++ b/src/format.c @@ -15,6 +15,7 @@ void format_clear( void ) void format_set_style( const style_t* const styles, uint8_t number ) { + if ( 0 == number ) return; if ( NUMBER_STYLES < number ) return; if ( style_none == styles[ 0 ] ) return; @@ -26,20 +27,16 @@ void format_set_style( const style_t* const styles, uint8_t number ) if ( style_none <= style ) continue; - if ( n >= number - 1 ) - { - fprintf( stdout, "%d", STYLE_SET[ style ] ); - } - else - { - fprintf( stdout, "%d;", STYLE_SET[ style ] ); - } + fprintf( stdout, "%d", STYLE_SET[ style ] ); + if ( n < number - 1 ) + fputc( ';', stdout ); } fprintf( stdout, SEQUENCE_SUFFIX ); } void format_reset_style( const style_t* const styles, uint8_t number ) { + if ( 0 == number ) return; if ( NUMBER_STYLES < number ) return; if ( style_none == styles[ 0 ] ) return; @@ -47,14 +44,13 @@ void format_reset_style( const style_t* const styles, uint8_t number ) for ( int n = 0; n < number; n++ ) { style_t style = styles[ n ]; - if ( n >= number - 1 ) - { - fprintf( stdout, "%d", STYLE_RESET[ style ] ); - } - else - { - fprintf( stdout, "%d;", STYLE_RESET[ style ] ); - } + + if ( NUMBER_STYLES <= style ) + continue; + + fprintf( stdout, "%d", STYLE_RESET[ style ] ); + if ( n < number - 1 ) + fputc( ';', stdout ); } fprintf( stdout, SEQUENCE_SUFFIX ); } diff --git a/src/main.c b/src/main.c index 983616e..b8e31db 100644 --- a/src/main.c +++ b/src/main.c @@ -55,6 +55,8 @@ static int display_directory_contents( size_t recursion_level, size_t is_last ); +void set_style( const directory_entry_t* const entry ); +void reset_style( const directory_entry_t* const entry ); // program logic int main( int argc, char** argv ) @@ -335,10 +337,10 @@ int display_directory_contents( } ( void )permissions_to_string( - args, - entry->stats.st_mode, - file_permissions - ); + args, + entry->stats.st_mode, + file_permissions + ); size_to_string( entry, &file_size[ 0 ], LENGTH_SIZE ); if ( args->tree_view ) @@ -374,50 +376,64 @@ int display_directory_contents( fprintf( stdout, "%s", tree ); } - if ( entry->type == type_directory ) + if ( !args->tree_view ) { - format_set_style( directory_style, DIR_STYLE_NUM ); - format_set_fg_colour( directory_colour ); + fprintf( + stdout, + "%c%s%s ", + file_type, + file_permissions, + file_size + ); } + + set_style( entry ); fprintf( stdout, "%s", entry->name ); - if ( entry->type == type_directory ) + reset_style( entry ); + + if ( type_link == entry->type ) { - format_reset_style( directory_style, DIR_STYLE_NUM ); + char link[ PATH_MAX ]; + ssize_t length = 0; + + length = readlink( entry->name, &link[ 0 ], PATH_MAX ); + + if ( 0 > length ) + { + log_error( "failed to read link '%s'", entry->name ); + } + link[ length ] = '\0'; + + fprintf( stdout, " --> "); + + format_set_style( &link_style[ 0 ], LINK_STYLE_NUM ); + format_set_fg_colour( link_colour ); + fprintf( stdout, "%s", link ); + format_reset_style( &link_style[ 0 ], LINK_STYLE_NUM ); format_clear_fg_colour(); } - fprintf( stdout, "\n" ); - // if ( !args->tree_view ) - // { - // log_info( - // "%c%s%s %s", - // file_type, - // file_permissions, - // file_size, - // entry->name - // ); - // } - // else - // { + fputc( '\n', stdout ); - if ( recursion_level < args->depth - && entry->type == type_directory ) + if ( args->tree_view + && recursion_level < args->depth + && entry->type == type_directory ) { DIR* subdirectory = opendir(entry->name); if ( !subdirectory ) { - log_error( "failed to open directory `%s`", entry->name ); + log_error( "failed to open directory '%s'", entry->name ); } else { chdir( entry->name ); // TODO: handle return code display_directory_contents( - args, - subdirectory, - recursion_level + 1, - is_last | (size_t)( num_entries == n + 1 ) << recursion_level - ); + args, + subdirectory, + recursion_level + 1, + is_last | (size_t)( num_entries == n + 1 ) << recursion_level + ); closedir( subdirectory ); chdir( ".." ); } @@ -485,3 +501,83 @@ void size_to_string( ); } } + +void set_style( const directory_entry_t* const entry ) +{ + switch ( entry->type ) + { + case type_block: + format_set_style( &block_style[ 0 ], BLK_STYLE_NUM ); + format_set_fg_colour( block_colour ); + break; + + case type_character: + format_set_style( &character_style[ 0 ], CHAR_STYLE_NUM ); + format_set_fg_colour( character_colour ); + break; + + case type_directory: + format_set_style( &directory_style[ 0 ], DIR_STYLE_NUM ); + format_set_fg_colour( directory_colour ); + break; + + case type_fifo: + format_set_style( &fifo_style[ 0 ], FIFO_STYLE_NUM ); + format_set_fg_colour( fifo_colour ); + break; + + case type_link: + format_set_style( &link_style[ 0 ], LINK_STYLE_NUM ); + format_set_fg_colour( link_colour ); + break; + + case type_regular: + // TODO: handle files by extension + break; + + case type_socket: + format_set_style( &socket_style[ 0 ], SOCK_STYLE_NUM ); + format_set_fg_colour( socket_colour ); + break; + } +} + +void reset_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: + // TODO: handle files by extension + break; + + case type_socket: + format_reset_style( &socket_style[ 0 ], SOCK_STYLE_NUM ); + format_clear_fg_colour(); + break; + } +}