tested and fixed the module 'format'

This commit is contained in:
root committed 2026-10-05 23:46:12 +02:00
1 parent 1327ff8b0e
commit 38130ab1d9
2 files changed
+114 -85

No files matched your search

+28 -76
View File
@@ -7,88 +7,40 @@
#ifndef __FORMAT_H #ifndef __FORMAT_H
#define __FORMAT_H #define __FORMAT_H
/* -- type definitions */ #include "stdint.h"
typedef struct {
unsigned char black;
unsigned char red;
unsigned char green;
unsigned char yellow;
unsigned char blue;
unsigned char magenta;
unsigned char cyan;
unsigned char white;
unsigned char extended;
unsigned char normal;
} colours_t;
typedef struct {
unsigned char bold; /* -- defines */
unsigned char dim; #define NUMBER_STYLES ( ( int )8 )
unsigned char italic; #define NUMBER_COLOURS ( ( int )9 )
unsigned char underlined; #define ESCAPE "\x1b"
unsigned char blinking; #define SEQUENCE_PREFIX ESCAPE"["
unsigned char reversed; #define SEQUENCE_SUFFIX "m"
unsigned char invisible;
unsigned char strikethrough; /* -- type definitions */
typedef enum {
style_bold = 0u,
style_dim,
style_italic,
style_underlined,
style_blinking,
style_reversed,
style_invisible,
style_strikethrough
} style_t; } style_t;
/* -- constant definitions */ /* -- constant definitions */
const char* ESCAPE = "\x1b"; extern const uint8_t STYLE_SET[ NUMBER_STYLES ];
const char* SEQUENCE_PREFIX = "\x1b["; extern const uint8_t STYLE_RESET[ NUMBER_STYLES ];
const char* SEQUENCE_SUFFIX = "m";
const style_t STYLE_SET = {
.bold = 1u,
.dim = 2u,
.italic = 3u,
.underlined = 4u,
.blinking = 5u,
.reversed = 7u,
.invisible = 8u,
.strikethrough = 9u,
};
const style_t STYLE_RESET = {
.bold = 22u,
.dim = 22u,
.italic = 23u,
.underlined = 24u,
.blinking = 25u,
.reversed = 27u,
.invisible = 28u,
.strikethrough = 29u,
};
const colours_t COLOUR_FG = {
.black = 30u,
.red = 31u,
.green = 32u,
.yellow = 33u,
.blue = 34u,
.magenta = 35u,
.cyan = 36u,
.white = 37u,
.extended = 38u,
.normal = 39u,
};
const colours_t COLOUR_BG = {
.black = 40u,
.red = 41u,
.green = 42u,
.yellow = 43u,
.blue = 44u,
.magenta = 45u,
.cyan = 46u,
.white = 47u,
.extended = 48u,
.normal = 49u,
};
/* -- function prototypes */ /* -- function prototypes */
void format_reset( void ); void format_clear( void );
void format_set_style( unsigned char style ); void format_set_style( style_t* styles, uint8_t number_styles );
void format_reset_style( unsigned char style ); void format_reset_style( style_t* styles, uint8_t number_styles );
void format_set_fg_colour( uint32_t colour );
void format_set_bg_colour( uint32_t colour );
void format_clear_fg_colour( void );
void format_clear_bg_colour( void );
#endif #endif
+86 -9
View File
@@ -1,19 +1,96 @@
#include <stdio.h> #include <stdio.h>
#include "format.h" #include "format.h"
void format_reset( void ) /* -- constant declarations */
const uint8_t STYLE_SET[ NUMBER_STYLES ] =
{ 1u, 2u, 3u, 4u, 5u, 7u, 8u, 9u };
const uint8_t STYLE_RESET[ NUMBER_STYLES ] =
{ 22u, 22u, 23u, 24u, 25u, 27u, 28u, 29u };
/* -- module logic */
void format_clear( void )
{ {
printf( "%s0%s", SEQUENCE_PREFIX, SEQUENCE_SUFFIX ); fprintf( stdout, SEQUENCE_PREFIX"0"SEQUENCE_SUFFIX );
} }
void format_set_style( unsigned char style ) void format_set_style( style_t* styles, uint8_t number )
{ {
if ( COLOUR_FG.extended == style ) return; if ( NUMBER_STYLES < number )
if ( COLOUR_BG.extended == style ) return; return;
printf( "%s%d%s", SEQUENCE_PREFIX, style, SEQUENCE_SUFFIX ); fprintf( stdout, SEQUENCE_PREFIX );
for ( int n = 0; n < number; n++ )
{
style_t style = styles[ n ];
if ( n >= number - 1 )
{
fprintf( stdout, "%d", STYLE_SET[ style ] );
}
else
{
fprintf( stdout, "%d;", STYLE_SET[ style ] );
}
}
fprintf( stdout, SEQUENCE_SUFFIX );
} }
// void format_reset_style( unsigned char style ) void format_reset_style( style_t* styles, uint8_t number )
// { {
// } if ( NUMBER_STYLES < number )
return;
fprintf( stdout, SEQUENCE_PREFIX );
for ( int n = 0; n < number; n++ )
{
style_t style = styles[ n ];
if ( n >= number - 1 )
{
fprintf( stdout, "%d", STYLE_RESET[ style ] );
}
else
{
fprintf( stdout, "%d;", STYLE_RESET[ style ] );
}
}
fprintf( stdout, SEQUENCE_SUFFIX );
}
void format_set_fg_colour( uint32_t colour )
{
uint8_t red = ( colour & 0xFF0000 ) >> 16u;
uint8_t green = ( colour & 0x00FF00 ) >> 8u;
uint8_t blue = ( colour & 0x0000FF );
fprintf(
stdout,
SEQUENCE_PREFIX"38;2;%d;%d;%d"SEQUENCE_SUFFIX,
red,
green,
blue
);
}
void format_set_bg_colour( uint32_t colour )
{
uint8_t red = ( colour & 0xFF0000 ) >> 16u;
uint8_t green = ( colour & 0x00FF00 ) >> 8u;
uint8_t blue = ( colour & 0x0000FF );
fprintf(
stdout,
SEQUENCE_PREFIX"48;2;%d;%d;%d"SEQUENCE_SUFFIX,
red,
green,
blue
);
}
void format_clear_fg_colour( void )
{
fprintf( stdout, SEQUENCE_PREFIX"39"SEQUENCE_SUFFIX );
}
void format_clear_bg_colour( void )
{
fprintf( stdout, SEQUENCE_PREFIX"49"SEQUENCE_SUFFIX );
}