MiniDevil As beautiful as a shell
drawing_text.c File Reference

Text utilities: truncates lines & handles UTF-8 display width. More...

#include "minishell_ui.h"
+ Include dependency graph for drawing_text.c:

Detailed Description

Text utilities: truncates lines & handles UTF-8 display width.

Definition in file drawing_text.c.

Functions

static int utf8_byte_len (unsigned char c)
 Return the length in bytes of an UTF-8 character. More...
 
char * truncate_line (const char *line, int max_width)
 Copy up to max_width visible characters into a static buffer. More...
 
int visual_strlen (const char *s)
 Calculate the visual display width of a UTF-8 string in terminal cols. More...
 

Function Documentation

◆ utf8_byte_len()

static int utf8_byte_len ( unsigned char  c)
static

Return the length in bytes of an UTF-8 character.

Parameters
cFirst byte of the UTF-8 sequence
Returns
4, 3, 2 or 1 for ASCII

Definition at line 21 of file drawing_text.c.

Referenced by truncate_line(), and visual_strlen().

+ Here is the caller graph for this function:

◆ truncate_line()

char* truncate_line ( const char *  line,
int  max_width 
)

Copy up to max_width visible characters into a static buffer.

Handles UTF-8 sequences

Parameters
lineSource string to truncate (could be NULL)
max_widthMaximum number to include
Returns
Pointer to static buffer holding the truncated result
Note
The static buffer gets overwritten on every call

Definition at line 42 of file drawing_text.c.

References BUF_SIZE, and utf8_byte_len().

Referenced by draw_line_content().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ visual_strlen()

int visual_strlen ( const char *  s)

Calculate the visual display width of a UTF-8 string in terminal cols.

4 byte (emoji range) are counted as 2 columns, the others as 1

Parameters
sUTF-8 string (NULL terminated)
Returns
Visual column width of the string

Definition at line 75 of file drawing_text.c.

References utf8_byte_len().

Referenced by draw_box_title().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Go to the source code of this file.