NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
Expando Render API

Render an Expando. More...

Functions

int node_condbool_render (const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Callback for every bool node - Implements ExpandoNode::render() -.
 
int node_conddate_render (const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Render a CondDate Node - Implements ExpandoNode::render() -.
 
static int node_condition_render (const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Render a Conditional Node - Implements ExpandoNode::render() -.
 
int node_container_render (const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Callback for a Container Node - Implements ExpandoNode::render() -.
 
int node_expando_render (const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Render an Expando Node - Implements ExpandoNode::render() -.
 
int node_padding_render_eol (const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Render End-of-Line Padding - Implements ExpandoNode::render() -.
 
int node_padding_render_hard (const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Render Hard Padding - Implements ExpandoNode::render() -.
 
int node_padding_render_soft (const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Render Soft Padding - Implements ExpandoNode::render() -.
 
static int node_text_render (const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
 Render a Text Node - Implements ExpandoNode::render() -.
 

Detailed Description

Render an Expando.

Parameters
[in]nodeNode to render
[out]bufBuffer in which to save string
[in]max_colsMaximum number of screen columns to use
[in]dataPrivate data
[in]flagsFlags, see MuttFormatFlags
Return values
numNumber of screen columns used

Function Documentation

◆ node_condbool_render()

int node_condbool_render ( const struct ExpandoNode node,
const struct ExpandoRenderData rdata,
struct Buffer buf,
int  max_cols,
void *  data,
MuttFormatFlags  flags 
)

Callback for every bool node - Implements ExpandoNode::render() -.

Definition at line 41 of file node_condbool.c.

44{
45 ASSERT(node->type == ENT_CONDBOOL);
46
47 const struct ExpandoRenderData *rd_match = find_get_number(rdata, node->did, node->uid);
48 if (rd_match)
49 {
50 const long num = rd_match->get_number(node, data, flags);
51 return (num != 0); // bool-ify
52 }
53
54 rd_match = find_get_string(rdata, node->did, node->uid);
55 if (rd_match)
56 {
57 struct Buffer *buf_str = buf_pool_get();
58 rd_match->get_string(node, data, flags, buf_str);
59 const size_t len = buf_len(buf_str);
60 buf_pool_release(&buf_str);
61
62 return (len > 0); // bool-ify
63 }
64
65 return 0;
66}
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:491
const struct ExpandoRenderData * find_get_number(const struct ExpandoRenderData *rdata, int did, int uid)
Find a get_number() callback function.
Definition: helpers.c:45
const struct ExpandoRenderData * find_get_string(const struct ExpandoRenderData *rdata, int did, int uid)
Find a get_string() callback function.
Definition: helpers.c:69
@ ENT_CONDBOOL
True/False boolean condition.
Definition: node.h:42
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
#define ASSERT(COND)
Definition: signal2.h:58
String manipulation buffer.
Definition: buffer.h:36
char * data
Pointer to data.
Definition: buffer.h:37
int uid
Unique ID, e.g. ED_EMA_SIZE.
Definition: node.h:70
int did
Domain ID, e.g. ED_EMAIL.
Definition: node.h:69
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:68
long(* get_number)(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
Definition: render.h:78
void(* get_string)(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, struct Buffer *buf)
Definition: render.h:64
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_conddate_render()

int node_conddate_render ( const struct ExpandoNode node,
const struct ExpandoRenderData rdata,
struct Buffer buf,
int  max_cols,
void *  data,
MuttFormatFlags  flags 
)

Render a CondDate Node - Implements ExpandoNode::render() -.

Definition at line 169 of file node_conddate.c.

172{
173 ASSERT(node->type == ENT_CONDDATE);
174
175 const struct ExpandoRenderData *rd_match = find_get_number(rdata, node->did, node->uid);
176 ASSERT(rd_match && "Unknown UID");
177
178 const long t_test = rd_match->get_number(node, data, flags);
179
180 const struct NodeCondDatePrivate *priv = node->ndata;
181
182 time_t t_cutoff;
183 if (priv->count == 0)
184 t_cutoff = cutoff_this(priv->period);
185 else
186 t_cutoff = cutoff_number(priv->period, priv->count);
187
188 return (t_test > t_cutoff); // bool-ify
189}
@ ENT_CONDDATE
True/False date condition.
Definition: node.h:43
time_t cutoff_number(char period, int count)
Calculate the cutoff time for n units.
Definition: node_conddate.c:79
time_t cutoff_this(char period)
Calculcate the cutoff time of this unit.
void * ndata
Private node data.
Definition: node.h:77
Private data for a Conditional Date -.
Definition: node_conddate.h:33
int count
Number of 'units' to count.
Definition: node_conddate.h:34
char period
Units, e.g. 'd' Day or 'm' Month.
Definition: node_conddate.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_condition_render()

static int node_condition_render ( const struct ExpandoNode node,
const struct ExpandoRenderData rdata,
struct Buffer buf,
int  max_cols,
void *  data,
MuttFormatFlags  flags 
)
static

Render a Conditional Node - Implements ExpandoNode::render() -.

Definition at line 49 of file node_condition.c.

52{
53 ASSERT(node->type == ENT_CONDITION);
54
55 const struct ExpandoNode *node_cond = node_get_child(node, ENC_CONDITION);
56
57 // Discard any text returned, just use the return value as a bool
58 struct Buffer *buf_cond = buf_pool_get();
59 int rc_cond = node_cond->render(node_cond, rdata, buf_cond, max_cols, data, flags);
60
61 int rc = 0;
62 buf_reset(buf_cond);
63
64 if (rc_cond == true)
65 {
66 const struct ExpandoNode *node_true = node_get_child(node, ENC_TRUE);
67 rc = node_render(node_true, rdata, buf_cond, max_cols, data, flags);
68 }
69 else
70 {
71 const struct ExpandoNode *node_false = node_get_child(node, ENC_FALSE);
72 rc = node_render(node_false, rdata, buf_cond, max_cols, data, flags);
73 }
74
75 const struct ExpandoFormat *fmt = node->format;
76 if (!fmt)
77 {
78 buf_addstr(buf, buf_string(buf_cond));
79 buf_pool_release(&buf_cond);
80 return rc;
81 }
82
83 struct Buffer *tmp = buf_pool_get();
84
85 int min_cols = MAX(fmt->min_cols, fmt->max_cols);
86 min_cols = MIN(min_cols, max_cols);
87 if (fmt->max_cols >= 0)
88 max_cols = MIN(max_cols, fmt->max_cols);
89 rc = format_string(tmp, min_cols, max_cols, fmt->justification, ' ',
90 buf_string(buf_cond), buf_len(buf_cond), true);
91 if (fmt->lower)
93
94 buf_addstr(buf, buf_string(tmp));
95 buf_pool_release(&tmp);
96 buf_pool_release(&buf_cond);
97
98 return rc;
99}
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
void buf_lower_special(struct Buffer *buf)
Convert to lowercase, excluding special characters.
Definition: helpers.c:92
int format_string(struct Buffer *buf, int min_cols, int max_cols, enum FormatJustify justify, char pad_char, const char *str, size_t n, bool arboreal)
Format a string, like snprintf()
Definition: format.c:108
#define MIN(a, b)
Definition: memory.h:32
#define MAX(a, b)
Definition: memory.h:31
struct ExpandoNode * node_get_child(const struct ExpandoNode *node, int index)
Get a child of an ExpandoNode.
Definition: node.c:91
@ ENT_CONDITION
True/False condition.
Definition: node.h:41
@ ENC_CONDITION
Index of Condition Node.
@ ENC_FALSE
Index of False Node.
@ ENC_TRUE
Index of True Node.
int node_render(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Render a tree of ExpandoNodes into a string.
Definition: render.c:45
Formatting information for an Expando.
Definition: node.h:53
enum FormatJustify justification
Justification: left, centre, right.
Definition: node.h:56
int min_cols
Minimum number of screen columns.
Definition: node.h:54
int max_cols
Maximum number of screen columns.
Definition: node.h:55
bool lower
Display in lower case.
Definition: node.h:58
Basic Expando Node.
Definition: node.h:67
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:91
struct ExpandoFormat * format
Formatting info.
Definition: node.h:72
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_container_render()

int node_container_render ( const struct ExpandoNode node,
const struct ExpandoRenderData rdata,
struct Buffer buf,
int  max_cols,
void *  data,
MuttFormatFlags  flags 
)

Callback for a Container Node - Implements ExpandoNode::render() -.

Definition at line 43 of file node_container.c.

46{
47 ASSERT(node->type == ENT_CONTAINER);
48
49 const struct ExpandoFormat *fmt = node->format;
50 if (fmt)
52
53 int total_cols = 0;
54
55 struct Buffer *tmp = buf_pool_get();
56 struct ExpandoNode **enp = NULL;
57 ARRAY_FOREACH(enp, &node->children)
58 {
59 if (total_cols >= max_cols)
60 break;
61 total_cols += node_render(*enp, rdata, tmp, max_cols - total_cols, data, flags);
62 }
63
64 if (fmt)
65 {
66 struct Buffer *tmp2 = buf_pool_get();
67
68 int max = max_cols;
69 if (fmt->max_cols >= 0)
70 max = MIN(max_cols, fmt->max_cols);
71 int min = MIN(fmt->min_cols, max);
72
73 total_cols = format_string(tmp2, min, max, fmt->justification, ' ',
74 buf_string(tmp), buf_len(tmp), true);
75
76 if (fmt->lower)
78
79 buf_addstr(buf, buf_string(tmp2));
80 buf_pool_release(&tmp2);
81 }
82 else
83 {
84 buf_addstr(buf, buf_string(tmp));
85 }
86
87 buf_pool_release(&tmp);
88 return total_cols;
89}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
@ ENT_CONTAINER
Container for other nodes.
Definition: node.h:44
struct ExpandoNodeArray children
Children nodes.
Definition: node.h:75
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_expando_render()

int node_expando_render ( const struct ExpandoNode node,
const struct ExpandoRenderData rdata,
struct Buffer buf,
int  max_cols,
void *  data,
MuttFormatFlags  flags 
)

Render an Expando Node - Implements ExpandoNode::render() -.

Definition at line 404 of file node_expando.c.

407{
408 ASSERT(node->type == ENT_EXPANDO);
409
410 struct Buffer *buf_expando = buf_pool_get();
411 struct Buffer *buf_format = buf_pool_get();
412
413 const struct ExpandoFormat *fmt = node->format;
414 const struct NodeExpandoPrivate *priv = node->ndata;
415
416 // ---------------------------------------------------------------------------
417 // Numbers and strings get treated slightly differently. We prefer strings.
418 // This allows dates to be stored as 1729850182, but displayed as "2024-10-25".
419
420 const struct ExpandoRenderData *rd_match = find_get_string(rdata, node->did, node->uid);
421 if (rd_match)
422 {
423 rd_match->get_string(node, data, flags, buf_expando);
424
425 if (fmt && fmt->lower)
426 buf_lower_special(buf_expando);
427 }
428 else
429 {
430 rd_match = find_get_number(rdata, node->did, node->uid);
431 ASSERT(rd_match && "Unknown UID");
432
433 const long num = rd_match->get_number(node, data, flags);
434
435 int precision = 1;
436
437 if (fmt)
438 {
439 precision = fmt->max_cols;
440 if ((precision < 0) && (fmt->leader == '0'))
441 precision = fmt->min_cols;
442 }
443
444 if (num < 0)
445 precision--; // Allow space for the '-' sign
446
447 buf_printf(buf_expando, "%.*ld", precision, num);
448 }
449
450 // ---------------------------------------------------------------------------
451
452 int max = max_cols;
453 int min = 0;
454
455 if (fmt)
456 {
457 min = fmt->min_cols;
458 if (fmt->max_cols > 0)
459 max = MIN(max_cols, fmt->max_cols);
460 }
461
462 const enum FormatJustify just = fmt ? fmt->justification : JUSTIFY_LEFT;
463
464 int total_cols = format_string(buf_format, min, max, just, ' ', buf_string(buf_expando),
465 buf_len(buf_expando), priv->has_tree);
466
467 if (!buf_is_empty(buf_format))
468 {
469 if (priv->color > -1)
470 add_color(buf, priv->color);
471
472 buf_addstr(buf, buf_string(buf_format));
473
474 if (priv->color > -1)
476 }
477
478 buf_pool_release(&buf_format);
479 buf_pool_release(&buf_expando);
480
481 return total_cols;
482}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
@ MT_COLOR_INDEX
Index: default colour.
Definition: color.h:83
FormatJustify
Alignment for format_string()
Definition: format.h:33
@ JUSTIFY_LEFT
Left justify the text.
Definition: format.h:34
@ ENT_EXPANDO
Expando, e.g. 'n'.
Definition: node.h:39
void add_color(struct Buffer *buf, enum ColorId cid)
Add a colour code to a buffer.
Definition: node_expando.c:393
char leader
Leader character, 0 or space.
Definition: node.h:57
Private data for an Expando -.
Definition: node_expando.h:40
int color
ColorId to use.
Definition: node_expando.h:41
bool has_tree
Contains tree characters, used in $index_format's s.
Definition: node_expando.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_padding_render_eol()

int node_padding_render_eol ( const struct ExpandoNode node,
const struct ExpandoRenderData rdata,
struct Buffer buf,
int  max_cols,
void *  data,
MuttFormatFlags  flags 
)

Render End-of-Line Padding - Implements ExpandoNode::render() -.

Definition at line 105 of file node_padding.c.

108{
109 struct ExpandoNode *left = node_get_child(node, ENP_LEFT);
110
111 int total_cols = node_render(left, rdata, buf, max_cols, data, flags);
112
113 total_cols += pad_string(node, buf, max_cols - total_cols);
114
115 return total_cols;
116}
int pad_string(const struct ExpandoNode *node, struct Buffer *buf, int max_cols)
Pad a buffer with a character.
Definition: node_padding.c:76
@ ENP_LEFT
Index of Left-Hand Nodes.
Definition: node_padding.h:57
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_padding_render_hard()

int node_padding_render_hard ( const struct ExpandoNode node,
const struct ExpandoRenderData rdata,
struct Buffer buf,
int  max_cols,
void *  data,
MuttFormatFlags  flags 
)

Render Hard Padding - Implements ExpandoNode::render() -.

Text to the left of the padding is hard and will be preserved if possible. Text to the right of the padding will be truncated.

Definition at line 124 of file node_padding.c.

127{
128 struct Buffer *buf_left = buf_pool_get();
129 struct Buffer *buf_pad = buf_pool_get();
130 struct Buffer *buf_right = buf_pool_get();
131
132 int cols_used = 0;
133
134 struct ExpandoNode *left = node_get_child(node, ENP_LEFT);
135 if (left)
136 cols_used += node_render(left, rdata, buf_left, max_cols - cols_used, data, flags);
137
138 struct ExpandoNode *right = node_get_child(node, ENP_RIGHT);
139 if (right)
140 cols_used += node_render(right, rdata, buf_right, max_cols - cols_used, data, flags);
141
142 if (max_cols > cols_used)
143 cols_used += pad_string(node, buf_pad, max_cols - cols_used);
144
145 buf_addstr(buf, buf_string(buf_left));
146 buf_addstr(buf, buf_string(buf_pad));
147 buf_addstr(buf, buf_string(buf_right));
148
149 buf_pool_release(&buf_left);
150 buf_pool_release(&buf_pad);
151 buf_pool_release(&buf_right);
152
153 return cols_used;
154}
@ ENP_RIGHT
Index of Right-Hand Nodes.
Definition: node_padding.h:58
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_padding_render_soft()

int node_padding_render_soft ( const struct ExpandoNode node,
const struct ExpandoRenderData rdata,
struct Buffer buf,
int  max_cols,
void *  data,
MuttFormatFlags  flags 
)

Render Soft Padding - Implements ExpandoNode::render() -.

Text to the right of the padding is hard and will be preserved if possible. Text to the left of the padding will be truncated.

Definition at line 162 of file node_padding.c.

165{
166 struct Buffer *buf_left = buf_pool_get();
167 struct Buffer *buf_pad = buf_pool_get();
168 struct Buffer *buf_right = buf_pool_get();
169
170 int cols_used = 0;
171
172 struct ExpandoNode *right = node_get_child(node, ENP_RIGHT);
173 if (right)
174 cols_used += node_render(right, rdata, buf_right, max_cols - cols_used, data, flags);
175
176 struct ExpandoNode *left = node_get_child(node, ENP_LEFT);
177 if (left)
178 cols_used += node_render(left, rdata, buf_left, max_cols - cols_used, data, flags);
179
180 if (max_cols > cols_used)
181 cols_used += pad_string(node, buf_pad, max_cols - cols_used);
182
183 buf_addstr(buf, buf_string(buf_left));
184 buf_addstr(buf, buf_string(buf_pad));
185 buf_addstr(buf, buf_string(buf_right));
186
187 buf_pool_release(&buf_left);
188 buf_pool_release(&buf_pad);
189 buf_pool_release(&buf_right);
190
191 return cols_used;
192}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_text_render()

static int node_text_render ( const struct ExpandoNode node,
const struct ExpandoRenderData rdata,
struct Buffer buf,
int  max_cols,
void *  data,
MuttFormatFlags  flags 
)
static

Render a Text Node - Implements ExpandoNode::render() -.

Definition at line 42 of file node_text.c.

45{
46 ASSERT(node->type == ENT_TEXT);
47
48 return format_string(buf, 0, max_cols, JUSTIFY_LEFT, ' ', node->text,
49 mutt_str_len(node->text), false);
50}
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
@ ENT_TEXT
Plain text.
Definition: node.h:38
const char * text
Node-specific text.
Definition: node.h:73
+ Here is the call graph for this function:
+ Here is the caller graph for this function: