From 030ca4584a7b5d5537858506755dee68652662a9 Mon Sep 17 00:00:00 2001 From: zegonix Date: Fri, 9 Oct 2026 16:29:48 +0200 Subject: [PATCH] implemented 'no-colour' feature --- inc/display.h | 15 ++++---- src/display.c | 98 ++++++++++++++++++++++++++++++++------------------- src/main.c | 16 ++++----- 3 files changed, 76 insertions(+), 53 deletions(-) diff --git a/inc/display.h b/inc/display.h index 6f7def0..10ca993 100644 --- a/inc/display.h +++ b/inc/display.h @@ -1,14 +1,15 @@ #ifndef __DISPLAY_H #define __DISPLAY_H +#include #include "type_definitions.h" -void display_type( entry_type_t type ); -void display_permissions( const char* const permissions ); -void display_size( directory_entry_t* entry ); -void display_user( directory_entry_t* entry ); -void display_time( time_t time ); -void display_entry( const directory_entry_t* const entry ); -void display_link_target( const directory_entry_t* const entry ); +void display_type( entry_type_t type, bool no_colour ); +void display_permissions( const char* const permissions, bool no_colour ); +void display_size( directory_entry_t* entry, bool no_colour ); +void display_user( directory_entry_t* entry, bool no_colour ); +void display_time( time_t time, bool no_colour ); +void display_entry( const directory_entry_t* const entry, bool no_colour ); +void display_link_target( const directory_entry_t* const entry, bool no_colour ); #endif diff --git a/src/display.c b/src/display.c index 9193fe1..9c5fbaa 100644 --- a/src/display.c +++ b/src/display.c @@ -25,10 +25,14 @@ static void size_to_string( ); // module logic -void display_type( entry_type_t type ) +void display_type( entry_type_t type, bool no_colour ) { - format_set_style( type_style, TYPE_STYLE_NUM ); - format_set_colour( type_colour ); + if ( !no_colour ) + { + format_set_style( type_style, TYPE_STYLE_NUM ); + format_set_colour( type_colour ); + } + switch ( type ) { case type_block: @@ -53,53 +57,69 @@ void display_type( entry_type_t type ) fputc( 's', stdout ); break; } - format_clear(); + format_clear(); fputc( ' ', stdout ); } -void display_permissions( const char* const permissions ) +void display_permissions( const char* const permissions, bool no_colour ) { - format_set_style( permission_style, PERM_STYLE_NUM ); - format_set_colour( permission_colour ); + if ( !permissions ) return; + + if ( !no_colour ) + { + format_set_style( permission_style, PERM_STYLE_NUM ); + format_set_colour( permission_colour ); + } + fprintf( stdout, "%s", permissions ); format_clear(); fputc( ' ', stdout ); } -void display_size( directory_entry_t *entry ) +void display_size( directory_entry_t *entry, bool no_colour ) { const size_t length = 9; char size_string[ length ]; + if ( !entry ) return; + size_to_string( entry, size_string, length ); - format_set_style( size_style, SIZE_STYLE_NUM ); - format_set_colour( size_colour ); + if ( !no_colour ) + { + format_set_style( size_style, SIZE_STYLE_NUM ); + format_set_colour( size_colour ); + } fprintf( stdout, "%s", size_string ); format_clear(); fputc( ' ', stdout ); } -void display_user( directory_entry_t* entry ) +void display_user( directory_entry_t* entry, bool no_colour ) { struct passwd* user = getpwuid( entry->stats.st_uid ); const size_t length = 8; char string[ length ]; + if ( !entry ) return; + snprintf( string, length, "%s", user->pw_name ); - format_set_style( user_style, USER_STYLE_NUM ); - format_set_colour( user_colour ); + if ( !no_colour ) + { + format_set_style( user_style, USER_STYLE_NUM ); + format_set_colour( user_colour ); + } fprintf( stdout, "%-7s", string ); format_clear(); fputc( ' ', stdout ); } -void display_time( time_t time ) +void display_time( time_t time, bool no_colour ) { const size_t length = 17; char string[ length ]; @@ -107,30 +127,40 @@ void display_time( time_t time ) strftime( string, length, "%Y-%m-%d %H:%M", local ); - format_set_style( date_style, DATE_STYLE_NUM ); - format_set_colour( date_colour ); + if ( !no_colour ) + { + format_set_style( date_style, DATE_STYLE_NUM ); + format_set_colour( date_colour ); + } fprintf( stdout, "%s", string ); format_clear(); fputc( ' ', stdout ); } -void display_entry( const directory_entry_t* const entry ) +void display_entry( const directory_entry_t* const entry, bool no_colour ) { - set_type_style( entry ); + if ( !entry ) return; + + if ( !no_colour ) + { + set_type_style( entry ); + } fprintf( stdout, "%s", entry->name ); format_clear(); fputc( ' ', stdout ); } -void display_link_target(const directory_entry_t* const entry) +void display_link_target(const directory_entry_t* const entry, bool no_colour ) { char link[ PATH_MAX ]; ssize_t length = 0; struct stat link_stat; int link_invalid; + if ( !entry ) return; + length = readlink( entry->name, &link[ 0 ], PATH_MAX ); if ( 0 > length ) @@ -143,29 +173,23 @@ void display_link_target(const directory_entry_t* const entry) fprintf( stdout, "--> "); - if ( link_invalid ) + if ( !no_colour ) { - format_set_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM ); - format_set_colour( brokenlink_colour ); - } - else - { - format_set_style( &link_style[ 0 ], LINK_STYLE_NUM ); - format_set_colour( link_colour ); + if ( link_invalid ) + { + format_set_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM ); + format_set_colour( brokenlink_colour ); + } + else + { + format_set_style( &link_style[ 0 ], LINK_STYLE_NUM ); + format_set_colour( link_colour ); + } } fprintf( stdout, "%s", link ); - if ( link_invalid ) - { - format_reset_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM ); - format_clear_colour(); - } - else - { - format_reset_style( &link_style[ 0 ], LINK_STYLE_NUM ); - format_clear_colour(); - } + format_clear(); } void set_type_style( const directory_entry_t* const entry ) diff --git a/src/main.c b/src/main.c index 633a38a..189960c 100644 --- a/src/main.c +++ b/src/main.c @@ -49,7 +49,6 @@ static int handle_directory( ); // program logic -// TODO: implement display for owner (user) / time (modified) int main( int argc, char** argv ) { int retval = SUCCESS; @@ -357,21 +356,20 @@ int handle_directory( fprintf( stdout, "%s", tree ); } - // TODO: implement 'no-color' functionality if ( args->long_view && !args->tree_view ) { - display_type( entry->type ); - display_permissions( file_permissions ); - display_size( entry ); - display_user( entry ); - display_time( entry->stats.st_mtime ); + display_type( entry->type, args->no_colour ); + display_permissions( file_permissions, args->no_colour ); + display_size( entry, args->no_colour ); + display_user( entry, args->no_colour ); + display_time( entry->stats.st_mtime, args->no_colour ); } - display_entry( entry ); + display_entry( entry, args->no_colour ); if ( type_link == entry->type ) { - display_link_target( entry ); + display_link_target( entry, args->no_colour ); } fputc( '\n', stdout );