implemented 'no-colour' feature

This commit is contained in:
root committed 2026-10-09 16:29:48 +02:00
1 parent e37f04a1cd
commit 030ca4584a
3 files changed
+57 -34

No files matched your search

+8 -7
View File
@@ -1,14 +1,15 @@
#ifndef __DISPLAY_H #ifndef __DISPLAY_H
#define __DISPLAY_H #define __DISPLAY_H
#include <stdbool.h>
#include "type_definitions.h" #include "type_definitions.h"
void display_type( entry_type_t type ); void display_type( entry_type_t type, bool no_colour );
void display_permissions( const char* const permissions ); void display_permissions( const char* const permissions, bool no_colour );
void display_size( directory_entry_t* entry ); void display_size( directory_entry_t* entry, bool no_colour );
void display_user( directory_entry_t* entry ); void display_user( directory_entry_t* entry, bool no_colour );
void display_time( time_t time ); void display_time( time_t time, bool no_colour );
void display_entry( const directory_entry_t* const entry ); void display_entry( const directory_entry_t* const entry, bool no_colour );
void display_link_target( const directory_entry_t* const entry ); void display_link_target( const directory_entry_t* const entry, bool no_colour );
#endif #endif
+42 -18
View File
@@ -25,10 +25,14 @@ static void size_to_string(
); );
// module logic // module logic
void display_type( entry_type_t type ) void display_type( entry_type_t type, bool no_colour )
{
if ( !no_colour )
{ {
format_set_style( type_style, TYPE_STYLE_NUM ); format_set_style( type_style, TYPE_STYLE_NUM );
format_set_colour( type_colour ); format_set_colour( type_colour );
}
switch ( type ) switch ( type )
{ {
case type_block: case type_block:
@@ -53,53 +57,69 @@ void display_type( entry_type_t type )
fputc( 's', stdout ); fputc( 's', stdout );
break; break;
} }
format_clear();
format_clear();
fputc( ' ', stdout ); fputc( ' ', stdout );
} }
void display_permissions( const char* const permissions ) void display_permissions( const char* const permissions, bool no_colour )
{
if ( !permissions ) return;
if ( !no_colour )
{ {
format_set_style( permission_style, PERM_STYLE_NUM ); format_set_style( permission_style, PERM_STYLE_NUM );
format_set_colour( permission_colour ); format_set_colour( permission_colour );
}
fprintf( stdout, "%s", permissions ); fprintf( stdout, "%s", permissions );
format_clear(); format_clear();
fputc( ' ', stdout ); fputc( ' ', stdout );
} }
void display_size( directory_entry_t *entry ) void display_size( directory_entry_t *entry, bool no_colour )
{ {
const size_t length = 9; const size_t length = 9;
char size_string[ length ]; char size_string[ length ];
if ( !entry ) return;
size_to_string( entry, size_string, length ); size_to_string( entry, size_string, length );
if ( !no_colour )
{
format_set_style( size_style, SIZE_STYLE_NUM ); format_set_style( size_style, SIZE_STYLE_NUM );
format_set_colour( size_colour ); format_set_colour( size_colour );
}
fprintf( stdout, "%s", size_string ); fprintf( stdout, "%s", size_string );
format_clear(); format_clear();
fputc( ' ', stdout ); 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 ); struct passwd* user = getpwuid( entry->stats.st_uid );
const size_t length = 8; const size_t length = 8;
char string[ length ]; char string[ length ];
if ( !entry ) return;
snprintf( string, length, "%s", user->pw_name ); snprintf( string, length, "%s", user->pw_name );
if ( !no_colour )
{
format_set_style( user_style, USER_STYLE_NUM ); format_set_style( user_style, USER_STYLE_NUM );
format_set_colour( user_colour ); format_set_colour( user_colour );
}
fprintf( stdout, "%-7s", string ); fprintf( stdout, "%-7s", string );
format_clear(); format_clear();
fputc( ' ', stdout ); fputc( ' ', stdout );
} }
void display_time( time_t time ) void display_time( time_t time, bool no_colour )
{ {
const size_t length = 17; const size_t length = 17;
char string[ length ]; char string[ length ];
@@ -107,30 +127,40 @@ void display_time( time_t time )
strftime( string, length, "%Y-%m-%d %H:%M", local ); strftime( string, length, "%Y-%m-%d %H:%M", local );
if ( !no_colour )
{
format_set_style( date_style, DATE_STYLE_NUM ); format_set_style( date_style, DATE_STYLE_NUM );
format_set_colour( date_colour ); format_set_colour( date_colour );
}
fprintf( stdout, "%s", string ); fprintf( stdout, "%s", string );
format_clear(); format_clear();
fputc( ' ', stdout ); fputc( ' ', stdout );
} }
void display_entry( const directory_entry_t* const entry ) void display_entry( const directory_entry_t* const entry, bool no_colour )
{
if ( !entry ) return;
if ( !no_colour )
{ {
set_type_style( entry ); set_type_style( entry );
}
fprintf( stdout, "%s", entry->name ); fprintf( stdout, "%s", entry->name );
format_clear(); format_clear();
fputc( ' ', stdout ); 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 ]; char link[ PATH_MAX ];
ssize_t length = 0; ssize_t length = 0;
struct stat link_stat; struct stat link_stat;
int link_invalid; int link_invalid;
if ( !entry ) return;
length = readlink( entry->name, &link[ 0 ], PATH_MAX ); length = readlink( entry->name, &link[ 0 ], PATH_MAX );
if ( 0 > length ) if ( 0 > length )
@@ -143,6 +173,8 @@ void display_link_target(const directory_entry_t* const entry)
fprintf( stdout, "--> "); fprintf( stdout, "--> ");
if ( !no_colour )
{
if ( link_invalid ) if ( link_invalid )
{ {
format_set_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM ); format_set_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM );
@@ -153,19 +185,11 @@ void display_link_target(const directory_entry_t* const entry)
format_set_style( &link_style[ 0 ], LINK_STYLE_NUM ); format_set_style( &link_style[ 0 ], LINK_STYLE_NUM );
format_set_colour( link_colour ); format_set_colour( link_colour );
} }
}
fprintf( stdout, "%s", link ); fprintf( stdout, "%s", link );
if ( link_invalid ) format_clear();
{
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();
}
} }
void set_type_style( const directory_entry_t* const entry ) void set_type_style( const directory_entry_t* const entry )
+7 -9
View File
@@ -49,7 +49,6 @@ static int handle_directory(
); );
// 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;
@@ -357,21 +356,20 @@ int handle_directory(
fprintf( stdout, "%s", tree ); fprintf( stdout, "%s", tree );
} }
// TODO: implement 'no-color' functionality
if ( args->long_view && !args->tree_view ) if ( args->long_view && !args->tree_view )
{ {
display_type( entry->type ); display_type( entry->type, args->no_colour );
display_permissions( file_permissions ); display_permissions( file_permissions, args->no_colour );
display_size( entry ); display_size( entry, args->no_colour );
display_user( entry ); display_user( entry, args->no_colour );
display_time( entry->stats.st_mtime ); display_time( entry->stats.st_mtime, args->no_colour );
} }
display_entry( entry ); display_entry( entry, args->no_colour );
if ( type_link == entry->type ) if ( type_link == entry->type )
{ {
display_link_target( entry ); display_link_target( entry, args->no_colour );
} }
fputc( '\n', stdout ); fputc( '\n', stdout );