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

+2 -1
View File
@@ -39,7 +39,8 @@ C_SRC := main.c \
arguments.c \ arguments.c \
stringing.c \ stringing.c \
sorting.c \ sorting.c \
format.c format.c \
display.c
C_FLAGS := -std=c11 \ C_FLAGS := -std=c11 \
$(WG) \ $(WG) \
+1
View File
@@ -34,6 +34,7 @@ typedef struct {
bool long_view; bool long_view;
bool reverse_order; bool reverse_order;
bool tree_view; bool tree_view;
bool show_types;
permission_mode_t permission_mode; permission_mode_t permission_mode;
sorting_mode_t sorting_mode; sorting_mode_t sorting_mode;
char* path; char* path;
+24 -34
View File
@@ -1,9 +1,3 @@
/* ------------------------------------------------------------------
* -- project: SCAN
* -- description: abstraction for terminal control sequences
* ------------------------------------------------------------------
*/
#ifndef __CONFIG_H #ifndef __CONFIG_H
#define __CONFIG_H #define __CONFIG_H
@@ -12,69 +6,65 @@
/* -- colour and style configuration */ /* -- colour and style configuration */
// additional information // additional information
// #define PERM_STYLE_NUM ( ( int )1 ) // #define PERM_STYLE_NUM ( ( int )1 )
// const style_t permission_style[PERM_STYLE_NUM] = { style_none }; // const style_t permission_style[ PERM_STYLE_NUM ] = { style_none };
// const uint32_t permission_colour = 0xEEEEEE; // const uint32_t permission_colour = 0xEEEEEE;
// //
// #define DATE_STYLE_NUM ( ( int )1 ) // #define DATE_STYLE_NUM ( ( int )1 )
// const style_t date_style[DATE_STYLE_NUM] = { style_none }; // const style_t date_style[ DATE_STYLE_NUM ] = { style_none };
// const uint32_t date_colour = 0xEEEEEE; // const uint32_t date_colour = 0xEEEEEE;
// //
// #define TYPE_STYLE_NUM ( ( int )1 ) // #define TYPE_STYLE_NUM ( ( int )1 )
// const style_t type_style[TYPE_STYLE_NUM] = { style_none }; // const style_t type_style[ TYPE_STYLE_NUM ] = { style_none };
// const uint32_t type_colour = 0xEEEEEE; // const uint32_t type_colour = 0xEEEEEE;
// //
// #define SIZE_STYLE_NUM ( ( int )1 ) // #define SIZE_STYLE_NUM ( ( int )1 )
// const style_t size_style[SIZE_STYLE_NUM] = { style_none }; // const style_t size_style[ SIZE_STYLE_NUM ] = { style_none };
// const uint32_t size_colour = 0xEEEEEE; // const uint32_t size_colour = 0xEEEEEE;
// files by type // files by type
#define BLK_STYLE_NUM ( ( int )1 ) #define BLK_STYLE_NUM ( ( int )1 )
const style_t block_style[BLK_STYLE_NUM] = { style_none }; const style_t block_style[ BLK_STYLE_NUM ] = { style_none };
const uint32_t block_colour = 0xFFCC13; const uint32_t block_colour = 0xFFCC13;
#define CHAR_STYLE_NUM ( ( int )1 ) #define CHAR_STYLE_NUM ( ( int )1 )
const style_t character_style[CHAR_STYLE_NUM] = { style_none }; const style_t character_style[ CHAR_STYLE_NUM ] = { style_none };
const uint32_t character_colour = 0xEEEEEE; const uint32_t character_colour = 0xEEEEEE;
#define DIR_STYLE_NUM ( ( int )1 ) #define DIR_STYLE_NUM ( ( int )1 )
const style_t directory_style[DIR_STYLE_NUM] = { style_bold }; const style_t directory_style[ DIR_STYLE_NUM ] = { style_bold };
const uint32_t directory_colour = 0x6CA05C; const uint32_t directory_colour = 0x6CA05C;
#define FIFO_STYLE_NUM ( ( int )1 ) #define FIFO_STYLE_NUM ( ( int )1 )
const style_t fifo_style[FIFO_STYLE_NUM] = { style_none }; const style_t fifo_style[ FIFO_STYLE_NUM ] = { style_none };
const uint32_t fifo_colour = 0xEEEEEE; const uint32_t fifo_colour = 0xEEEEEE;
#define LINK_STYLE_NUM ( ( int )1 ) #define LINK_STYLE_NUM ( ( int )1 )
const style_t link_style[LINK_STYLE_NUM] = { style_none }; const style_t link_style[ LINK_STYLE_NUM ] = { style_none };
const uint32_t link_colour = 0x19D1DE; const uint32_t link_colour = 0x19D1DE;
#define BLNK_STYLE_NUM ( ( int )1 ) #define BLNK_STYLE_NUM ( ( int )1 )
const style_t brokenlink_style[BLNK_STYLE_NUM] = { style_none }; const style_t brokenlink_style[ BLNK_STYLE_NUM ] = { style_none };
const uint32_t brokenlink_colour = 0xC00008; const uint32_t brokenlink_colour = 0xC00008;
#define SOCK_STYLE_NUM ( ( int )1 ) #define SOCK_STYLE_NUM ( ( int )1 )
const style_t socket_style[SOCK_STYLE_NUM] = { style_none }; const style_t socket_style[ SOCK_STYLE_NUM ] = { style_none };
const uint32_t socket_colour = 0xEEEEEE; const uint32_t socket_colour = 0xEEEEEE;
#define EXEC_STYLE_NUM ( ( int )1 ) #define EXEC_STYLE_NUM ( ( int )1 )
const style_t exec_style[EXEC_STYLE_NUM] = { style_none }; const style_t exec_style[ EXEC_STYLE_NUM ] = { style_none };
const uint32_t exec_colour = 0xEEEEEE; const uint32_t exec_colour = 0xEEEEEE;
// // files by extension // files by extension
// #define MAKE_STYLE_NUM ( ( int )1 ) #define MAKE_STYLE_NUM ( ( int )1 )
// const style_t makefile_style[MAKE_STYLE_NUM] = { style_none }; const style_t makefile_style[ MAKE_STYLE_NUM ] = { style_none };
// const uint32_t makefile_colour = 0xEAE977; const uint32_t makefile_colour = 0xEAE977;
//
// #define SRC_STYLE_NUM ( ( int )1 ) #define SRC_STYLE_NUM ( ( int )1 )
// const style_t source_style[SRC_STYLE_NUM] = { style_none }; const style_t source_style[ SRC_STYLE_NUM ] = { style_none };
// const uint32_t source_colour = 0x6FADD8; const uint32_t source_colour = 0x6FADD8;
//
// #define MEDIA_STYLE_NUM ( ( int )1 ) #define AR_STYLE_NUM ( ( int )1 )
// const style_t media_style[MEDIA_STYLE_NUM] = { style_none }; const style_t archive_style[ AR_STYLE_NUM ] = { style_none };
// const uint32_t media_colour = 0x6FADD8; const uint32_t archive_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 #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 { typedef struct {
char name[ NAME_MAX ]; char name[ NAME_MAX ];
char* extension;
struct stat stats; struct stat stats;
entry_type_t type; entry_type_t type;
} directory_entry_t; } directory_entry_t;
+17 -3
View File
@@ -20,6 +20,7 @@
#define KEY_REVERSE_ORDER ( ( int )'r' ) #define KEY_REVERSE_ORDER ( ( int )'r' )
#define KEY_SORT_MODE ( ( int )'s' ) #define KEY_SORT_MODE ( ( int )'s' )
#define KEY_TREE_VIEW ( ( int )'t' ) #define KEY_TREE_VIEW ( ( int )'t' )
#define KEY_SHOW_TYPES ( ( int )'T' )
#define KEY_NO_COLOUR ( ( int )0x101 ) #define KEY_NO_COLOUR ( ( int )0x101 )
// function prototypes // function prototypes
@@ -69,7 +70,8 @@ static struct argp_option options[] =
.group = 0 .group = 0
}, },
{ {
.name = "no-color", // TODO: implement this functionality
.name = "no-colour",
.key = KEY_NO_COLOUR, .key = KEY_NO_COLOUR,
.arg = NULL, .arg = NULL,
.flags = 0, .flags = 0,
@@ -132,6 +134,14 @@ static struct argp_option options[] =
.doc = "use tree listing format", .doc = "use tree listing format",
.group = 0 .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 .. { 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->long_view = false;
arguments->reverse_order = false; arguments->reverse_order = false;
arguments->tree_view = false; arguments->tree_view = false;
arguments->permission_mode = no_permissions; arguments->show_types = false;
arguments->sorting_mode = sort_by_name; arguments->permission_mode = octal_permissions;
arguments->sorting_mode = sort_by_extension;
arguments->path = "."; arguments->path = ".";
} }
@@ -225,6 +236,9 @@ static error_t arg_parser( int key, char* arg, struct argp_state* state )
case KEY_TREE_VIEW: case KEY_TREE_VIEW:
arguments->tree_view = true; arguments->tree_view = true;
break; break;
case KEY_SHOW_TYPES:
arguments->show_types = true;
break;
case KEY_REVERSE_ORDER: case KEY_REVERSE_ORDER:
arguments->reverse_order = true; arguments->reverse_order = true;
break; 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 "arguments.h"
#include "type_definitions.h" #include "type_definitions.h"
#include "sorting.h" #include "sorting.h"
#include "format.h" #include "display.h"
#include "config.h"
// macro definitions // macro definitions
#define SUCCESS 0 #define SUCCESS 0
@@ -55,13 +54,11 @@ static int display_directory_contents(
size_t recursion_level, size_t recursion_level,
size_t is_last 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 // program logic
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
int retval = SUCCESS;
arguments_t args; arguments_t args;
initialise_args( &args ); initialise_args( &args );
@@ -75,10 +72,12 @@ int main( int argc, char** argv )
if ( !directory ) if ( !directory )
{ {
if ( ENOENT == errno ) if ( ENOENT == errno )
{
log_error( "no directory `%s`", args.path ); log_error( "no directory `%s`", args.path );
else return -ENOENT;
log_error( "failed to open directory `%s`", args.path ); }
log_error( "failed to open directory `%s`", args.path );
return -EIO; return -EIO;
} }
@@ -95,10 +94,15 @@ int main( int argc, char** argv )
0 0
); );
} }
else
{
log_error( "failed to enter directory '%s'", args.path );
retval = -EIO;
}
closedir( directory ); closedir( directory );
return SUCCESS; return retval;
} }
int count_entries( DIR* directory, const arguments_t* const args ) int count_entries( DIR* directory, const arguments_t* const args )
@@ -141,7 +145,6 @@ int get_entries(
struct dirent* entry; struct dirent* entry;
struct stat file_stats = { 0 }; struct stat file_stats = { 0 };
rewinddir( directory ); rewinddir( directory );
while ( NULL != ( entry = readdir( directory ) ) while ( NULL != ( entry = readdir( directory ) )
@@ -155,6 +158,17 @@ int get_entries(
if ( lstat( entry->d_name, &file_stats ) ) return -EIO; if ( lstat( entry->d_name, &file_stats ) ) return -EIO;
strncpy( new_entry->name, entry->d_name, NAME_MAX ); 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; new_entry->stats = file_stats;
switch ( file_stats.st_mode & S_IFMT ) 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 ); fprintf( stdout, "%s", entry->name );
reset_style( entry ); reset_type_style( entry );
if ( type_link == entry->type ) 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* a = *( directory_entry_t** )operand_a;
directory_entry_t* b = *( directory_entry_t** )operand_b; directory_entry_t* b = *( directory_entry_t** )operand_b;
char* extension_a = strrchr( a->name, '.' ); if ( !a->extension && !b->extension ) return 0;
char* extension_b = strrchr( b->name, '.' ); if ( !a->extension && b->extension ) return -1;
if ( a->extension && !b->extension ) return 1;
if ( extension_a == a->name ) extension_a = NULL; return compare_strings( a->extension, b->extension, NAME_MAX, false );
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 );
} }
int compare_timestamp( int compare_timestamp(
+4
View File
@@ -49,6 +49,10 @@ int string_to_upper( char* const string )
return 0; 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( int compare_strings(
const char* const a, const char* const a,
const char* const b, const char* const b,