implemented colours by extension

This commit is contained in:
root committed 2026-10-08 14:10:10 +02:00
1 parent 8bd7c06509
commit 4a8132c0b1
10 files changed
+303 -183

No files matched your search

+213
View File
@@ -0,0 +1,213 @@
#include <linux/limits.h>
#define _XOPEN_SOURCE 700
#include <unistd.h>
#include <stdio.h>
#include "display.h"
#include "type_definitions.h"
#include "log.h"
#include "format.h"
#include "config.h"
#include "stringing.h"
// function signatures
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_link_target(const directory_entry_t* const entry)
{
char link[ PATH_MAX ];
ssize_t length = 0;
struct stat link_stat;
int link_invalid;
length = readlink( entry->name, &link[ 0 ], PATH_MAX );
if ( 0 > length )
{
log_error( "failed to read link '%s'", entry->name );
}
link[ length ] = '\0';
link_invalid = stat( link, &link_stat );
fprintf( stdout, " --> ");
if ( link_invalid )
{
format_set_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM );
format_set_fg_colour( brokenlink_colour );
}
else
{
format_set_style( &link_style[ 0 ], LINK_STYLE_NUM );
format_set_fg_colour( link_colour );
}
fprintf( stdout, "%s", link );
if ( link_invalid )
{
format_reset_style( &brokenlink_style[ 0 ], BLNK_STYLE_NUM );
format_clear_fg_colour();
}
else
{
format_reset_style( &link_style[ 0 ], LINK_STYLE_NUM );
format_clear_fg_colour();
}
}
void set_type_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:
set_file_style( entry );
break;
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();
break;
}
}
void set_file_style( const directory_entry_t* const entry )
{
if ( !entry ) return;
// makefiles and readmes
if ( compare_makefile( entry ) )
{
format_set_style( &makefile_style[ 0 ], MAKE_STYLE_NUM );
format_set_fg_colour( makefile_colour );
}
else if ( compare_archives( entry ) )
{
format_set_style( &archive_style[ 0 ], AR_STYLE_NUM );
format_set_fg_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();
}
}
bool compare_makefile( const directory_entry_t* const entry )
{
return 0 == compare_strings( entry->name, "makefile", NAME_MAX, false )
|| 0 == compare_strings( entry->name, "readme", 6, false )
|| 0 == compare_strings( entry->extension, "mk", 3, false );
}
bool compare_archives( const directory_entry_t* const entry )
{
return 0 == compare_strings( entry->extension, "zip", 4, true )
|| 0 == compare_strings( entry->extension, "tar", 4, true )
|| 0 == compare_strings( entry->extension, "gz", 3, true )
|| 0 == compare_strings( entry->extension, "bzip2", 6, true )
|| 0 == compare_strings( entry->extension, "xz", 3, true );
}
bool compare_sources( const directory_entry_t* const entry )
{
return 0 == compare_strings( entry->extension, "c", 2, true )
|| 0 == compare_strings( entry->extension, "cpp", 4, true )
|| 0 == compare_strings( entry->extension, "h", 2, true )
|| 0 == compare_strings( entry->extension, "rs", 3, true )
|| 0 == compare_strings( entry->extension, "zig", 4, true )
|| 0 == compare_strings( entry->extension, "odin", 5, true )
|| 0 == compare_strings( entry->extension, "py", 3, true );
}