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
+291 -171

No files matched your search

+2 -1
View File
@@ -39,7 +39,8 @@ C_SRC := main.c \
arguments.c \
stringing.c \
sorting.c \
format.c
format.c \
display.c
C_FLAGS := -std=c11 \
$(WG) \
+1
View File
@@ -34,6 +34,7 @@ typedef struct {
bool long_view;
bool reverse_order;
bool tree_view;
bool show_types;
permission_mode_t permission_mode;
sorting_mode_t sorting_mode;
char* path;
+12 -22
View File
@@ -1,9 +1,3 @@
/* ------------------------------------------------------------------
* -- project: SCAN
* -- description: abstraction for terminal control sequences
* ------------------------------------------------------------------
*/
#ifndef __CONFIG_H
#define __CONFIG_H
@@ -60,21 +54,17 @@ const uint32_t socket_colour = 0xEEEEEE;
const style_t exec_style[ EXEC_STYLE_NUM ] = { style_none };
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;
// 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 AR_STYLE_NUM ( ( int )1 )
const style_t archive_style[ AR_STYLE_NUM ] = { style_none };
const uint32_t archive_colour = 0x6FADD8;
#endif
+12
View File
@@ -0,0 +1,12 @@
#ifndef __DISPLAY_H
#define __DISPLAY_H
#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 );
#endif
+1
View File
@@ -22,6 +22,7 @@ typedef enum {
typedef struct {
char name[ NAME_MAX ];
char* extension;
struct stat stats;
entry_type_t type;
} directory_entry_t;
+17 -3
View File
@@ -20,6 +20,7 @@
#define KEY_REVERSE_ORDER ( ( int )'r' )
#define KEY_SORT_MODE ( ( int )'s' )
#define KEY_TREE_VIEW ( ( int )'t' )
#define KEY_SHOW_TYPES ( ( int )'T' )
#define KEY_NO_COLOUR ( ( int )0x101 )
// function prototypes
@@ -69,7 +70,8 @@ static struct argp_option options[] =
.group = 0
},
{
.name = "no-color",
// TODO: implement this functionality
.name = "no-colour",
.key = KEY_NO_COLOUR,
.arg = NULL,
.flags = 0,
@@ -132,6 +134,14 @@ static struct argp_option options[] =
.doc = "use tree listing format",
.group = 0
},
{
.name = "types",
.key = KEY_SHOW_TYPES,
.arg = NULL,
.flags = 0,
.doc = "display file type (block, character, directory, ...)",
.group = 0
},
{ 0 } // this is required to properly end the options array ..
};
@@ -160,8 +170,9 @@ void initialise_args( arguments_t* arguments )
arguments->long_view = false;
arguments->reverse_order = false;
arguments->tree_view = false;
arguments->permission_mode = no_permissions;
arguments->sorting_mode = sort_by_name;
arguments->show_types = false;
arguments->permission_mode = octal_permissions;
arguments->sorting_mode = sort_by_extension;
arguments->path = ".";
}
@@ -225,6 +236,9 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state )
case KEY_TREE_VIEW:
arguments->tree_view = true;
break;
case KEY_SHOW_TYPES:
arguments->show_types = true;
break;
case KEY_REVERSE_ORDER:
arguments->reverse_order = true;
break;
+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 );
}
+25 -135
View File
@@ -18,8 +18,7 @@
#include "arguments.h"
#include "type_definitions.h"
#include "sorting.h"
#include "format.h"
#include "config.h"
#include "display.h"
// macro definitions
#define SUCCESS 0
@@ -55,13 +54,11 @@ static int display_directory_contents(
size_t recursion_level,
size_t is_last
);
void display_link_target(const directory_entry_t* const entry);
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 )
{
int retval = SUCCESS;
arguments_t args;
initialise_args( &args );
@@ -75,10 +72,12 @@ int main( int argc, char** argv )
if ( !directory )
{
if ( ENOENT == errno )
{
log_error( "no directory `%s`", args.path );
else
log_error( "failed to open directory `%s`", args.path );
return -ENOENT;
}
log_error( "failed to open directory `%s`", args.path );
return -EIO;
}
@@ -95,10 +94,15 @@ int main( int argc, char** argv )
0
);
}
else
{
log_error( "failed to enter directory '%s'", args.path );
retval = -EIO;
}
closedir( directory );
return SUCCESS;
return retval;
}
int count_entries( DIR* directory, const arguments_t* const args )
@@ -141,7 +145,6 @@ int get_entries(
struct dirent* entry;
struct stat file_stats = { 0 };
rewinddir( directory );
while ( NULL != ( entry = readdir( directory ) )
@@ -155,6 +158,17 @@ int get_entries(
if ( lstat( entry->d_name, &file_stats ) ) return -EIO;
strncpy( new_entry->name, entry->d_name, NAME_MAX );
new_entry->extension = strrchr( new_entry->name, '.' );
if ( new_entry->extension == new_entry->name )
{
new_entry->extension = NULL;
}
else if ( new_entry->extension )
{
new_entry->extension += 1;
}
new_entry->stats = file_stats;
switch ( file_stats.st_mode & S_IFMT )
{
@@ -388,9 +402,9 @@ int display_directory_contents(
);
}
set_style( entry );
set_type_style( entry );
fprintf( stdout, "%s", entry->name );
reset_style( entry );
reset_type_style( entry );
if ( type_link == entry->type )
{
@@ -485,127 +499,3 @@ void size_to_string(
);
}
}
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_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;
}
}
+4 -10
View File
@@ -182,17 +182,11 @@ int compare_extension(
directory_entry_t* a = *( directory_entry_t** )operand_a;
directory_entry_t* b = *( directory_entry_t** )operand_b;
char* extension_a = strrchr( a->name, '.' );
char* extension_b = strrchr( b->name, '.' );
if ( !a->extension && !b->extension ) return 0;
if ( !a->extension && b->extension ) return -1;
if ( a->extension && !b->extension ) return 1;
if ( extension_a == a->name ) extension_a = NULL;
if ( extension_b == b->name ) extension_b = NULL;
if ( !extension_a && !extension_b ) return 0;
if ( !extension_a && extension_b ) return -1;
if ( extension_a && !extension_b ) return 1;
return compare_strings( extension_a, extension_b, NAME_MAX, false );
return compare_strings( a->extension, b->extension, NAME_MAX, false );
}
int compare_timestamp(
+4
View File
@@ -49,6 +49,10 @@ int string_to_upper( char* const string )
return 0;
}
// compares strings and returns `a - b`
// -1 if a < b (lower ascii value)
// 0 if a = b
// 1 if a > b (higher ascii value)
int compare_strings(
const char* const a,
const char* const b,