NeoMutt  2024-04-25-34-g585158
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 111 of file node_condbool.c.

114{
115 assert(node->type == ENT_CONDBOOL);
116
117 const struct ExpandoRenderData *rd_match = find_get_number(rdata, node->did, node->uid);
118 if (rd_match)
119 {
120 const long num = rd_match->get_number(node, data, flags);
121 return (num != 0); // bool-ify
122 }
123
124 rd_match = find_get_string(rdata, node->did, node->uid);
125 if (rd_match)
126 {
127 struct Buffer *buf_str = buf_pool_get();
128 rd_match->get_string(node, data, flags, max_cols, buf_str);
129 const size_t len = buf_len(buf_str);
130 buf_pool_release(&buf_str);
131
132 return (len > 0); // bool-ify
133 }
134
135 return 0;
136}
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:48
const struct ExpandoRenderData * find_get_string(const struct ExpandoRenderData *rdata, int did, int uid)
Find a get_string() callback function.
Definition: helpers.c:72
@ 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
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:73
int did
Domain ID, e.g. ED_EMAIL.
Definition: node.h:72
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:70
void(* get_string)(const struct ExpandoNode *node, void *data, MuttFormatFlags flags, int max_cols, struct Buffer *buf)
Definition: render.h:65
long(* get_number)(const struct ExpandoNode *node, void *data, MuttFormatFlags flags)
Definition: render.h:79
+ 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 75 of file node_conddate.c.

78{
79 assert(node->type == ENT_CONDDATE);
80
81 const struct ExpandoRenderData *rd_match = find_get_number(rdata, node->did, node->uid);
82 assert(rd_match && "Unknown UID");
83
84 const long t_test = rd_match->get_number(node, data, flags);
85
86 const struct NodeCondDatePrivate *priv = node->ndata;
87
88 time_t t = mutt_date_now();
89 struct tm tm = { 0 };
90 gmtime_r(&t, &tm);
91
92 switch (priv->period)
93 {
94 case 'y':
95 tm.tm_year -= priv->count;
96 break;
97
98 case 'm':
99 tm.tm_mon -= priv->count;
100 break;
101
102 case 'w':
103 tm.tm_mday -= (7 * priv->count);
104 break;
105
106 case 'd':
107 tm.tm_mday -= priv->count;
108 break;
109
110 case 'H':
111 tm.tm_hour -= priv->count;
112 break;
113
114 case 'M':
115 tm.tm_min -= priv->count;
116 break;
117 }
118
119 const time_t t_cutoff = mktime(&tm);
120
121 return (t_test > t_cutoff); // bool-ify
122}
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:455
@ ENT_CONDDATE
True/False date condition.
Definition: node.h:43
void * ndata
Private node data.
Definition: node.h:82
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 41 of file node_condition.c.

44{
45 assert(node->type == ENT_CONDITION);
46
47 const struct ExpandoNode *node_cond = node_get_child(node, ENC_CONDITION);
48
49 // Discard any text returned, just use the return value as a bool
50 struct Buffer *buf_cond = buf_pool_get();
51 int rc = node_cond->render(node_cond, rdata, buf_cond, max_cols, data, flags);
52 buf_pool_release(&buf_cond);
53
54 if (rc == true)
55 {
56 const struct ExpandoNode *node_true = node_get_child(node, ENC_TRUE);
57 return node_tree_render(node_true, rdata, buf, max_cols, data, flags);
58 }
59 else
60 {
61 const struct ExpandoNode *node_false = node_get_child(node, ENC_FALSE);
62 return node_tree_render(node_false, rdata, buf, max_cols, data, flags);
63 }
64}
struct ExpandoNode * node_get_child(const struct ExpandoNode *node, int index)
Get a child of an ExpandoNode.
Definition: node.c:99
@ 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_tree_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
Basic Expando Node.
Definition: node.h:69
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderData *rdata, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:96
+ 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 44 of file node_container.c.

47{
48 assert(node->type == ENT_CONTAINER);
49
50 const struct ExpandoFormat *fmt = node->format;
51 if (fmt)
53
54 int total_cols = 0;
55
56 struct Buffer *tmp = buf_pool_get();
57 struct ExpandoNode **enp = NULL;
58 ARRAY_FOREACH(enp, &node->children)
59 {
60 total_cols += node_tree_render(*enp, rdata, tmp, max_cols - total_cols, data, flags);
61 }
62
63 if (fmt)
64 {
65 int min_cols = MIN(fmt->min_cols, max_cols);
66 struct Buffer *tmp2 = buf_pool_get();
67 total_cols = format_string(tmp2, min_cols, max_cols, fmt->justification,
68 fmt->leader, buf_string(tmp), buf_len(tmp), true);
69 if (fmt->lower)
71 buf_addstr(buf, buf_string(tmp2));
72 buf_pool_release(&tmp2);
73 }
74 else
75 {
76 buf_addstr(buf, buf_string(tmp));
77 }
78
79 buf_pool_release(&tmp);
80 return total_cols;
81}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
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:177
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
@ ENT_CONTAINER
Container for other nodes.
Definition: node.h:44
Formatting information for an Expando.
Definition: node.h:53
char leader
Leader character, 0 or space.
Definition: node.h:57
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
struct ExpandoFormat * format
Formatting info.
Definition: node.h:75
struct ExpandoNodeArray children
Children nodes.
Definition: node.h:77
+ 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 345 of file node_expando.c.

348{
349 assert(node->type == ENT_EXPANDO);
350
351 struct Buffer *buf_expando = buf_pool_get();
352
353 const struct ExpandoRenderData *rd_match = find_get_string(rdata, node->did, node->uid);
354 if (rd_match)
355 {
356 rd_match->get_string(node, data, flags, max_cols, buf_expando);
357 }
358 else
359 {
360 rd_match = find_get_number(rdata, node->did, node->uid);
361 assert(rd_match && "Unknown UID");
362
363 const long num = rd_match->get_number(node, data, flags);
364 buf_printf(buf_expando, "%ld", num);
365 }
366
367 int total_cols = 0;
368
369 const struct NodeExpandoPrivate *priv = node->ndata;
370
371 if (priv->color > -1)
372 add_color(buf, priv->color);
373
374 struct Buffer *tmp = buf_pool_get();
375 const struct ExpandoFormat *fmt = node->format;
376 if (fmt)
377 {
378 max_cols = MIN(max_cols, fmt->max_cols);
379 int min_cols = MIN(max_cols, fmt->min_cols);
380 total_cols += format_string(tmp, min_cols, max_cols, fmt->justification,
381 fmt->leader, buf_string(buf_expando),
382 buf_len(buf_expando), priv->has_tree);
383 if (fmt->lower)
385 }
386 else
387 {
388 total_cols += format_string(tmp, 0, max_cols, JUSTIFY_LEFT, 0, buf_string(buf_expando),
389 buf_len(buf_expando), priv->has_tree);
390 }
391 buf_addstr(buf, buf_string(tmp));
392 buf_pool_release(&tmp);
393
394 if (priv->color > -1)
396
397 buf_pool_release(&buf_expando);
398 return total_cols;
399}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
@ MT_COLOR_INDEX
Index: default colour.
Definition: color.h:83
@ 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:334
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 102 of file node_padding.c.

105{
106 struct ExpandoNode *left = node_get_child(node, ENP_LEFT);
107
108 int total_cols = node_tree_render(left, rdata, buf, max_cols, data, flags);
109
110 total_cols += pad_string(node, buf, max_cols - total_cols);
111
112 return total_cols;
113}
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:56
+ 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 121 of file node_padding.c.

124{
125 struct Buffer *buf_left = buf_pool_get();
126 struct Buffer *buf_pad = buf_pool_get();
127 struct Buffer *buf_right = buf_pool_get();
128
129 int cols_used = 0;
130
131 struct ExpandoNode *left = node_get_child(node, ENP_LEFT);
132 if (left)
133 cols_used += node_tree_render(left, rdata, buf_left, max_cols - cols_used, data, flags);
134
135 struct ExpandoNode *right = node_get_child(node, ENP_RIGHT);
136 if (right)
137 cols_used += node_tree_render(right, rdata, buf_right, max_cols - cols_used, data, flags);
138
139 if (max_cols > cols_used)
140 cols_used += pad_string(node, buf_pad, max_cols - cols_used);
141
142 buf_addstr(buf, buf_string(buf_left));
143 buf_addstr(buf, buf_string(buf_pad));
144 buf_addstr(buf, buf_string(buf_right));
145
146 buf_pool_release(&buf_left);
147 buf_pool_release(&buf_pad);
148 buf_pool_release(&buf_right);
149
150 return cols_used;
151}
@ ENP_RIGHT
Index of Right-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_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 159 of file node_padding.c.

162{
163 struct Buffer *buf_left = buf_pool_get();
164 struct Buffer *buf_pad = buf_pool_get();
165 struct Buffer *buf_right = buf_pool_get();
166
167 int cols_used = 0;
168
169 struct ExpandoNode *right = node_get_child(node, ENP_RIGHT);
170 if (right)
171 cols_used += node_tree_render(right, rdata, buf_right, max_cols - cols_used, data, flags);
172
173 struct ExpandoNode *left = node_get_child(node, ENP_LEFT);
174 if (left)
175 cols_used += node_tree_render(left, rdata, buf_left, max_cols - cols_used, data, flags);
176
177 if (max_cols > cols_used)
178 cols_used += pad_string(node, buf_pad, max_cols - cols_used);
179
180 buf_addstr(buf, buf_string(buf_left));
181 buf_addstr(buf, buf_string(buf_pad));
182 buf_addstr(buf, buf_string(buf_right));
183
184 buf_pool_release(&buf_left);
185 buf_pool_release(&buf_pad);
186 buf_pool_release(&buf_right);
187
188 return cols_used;
189}
+ 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 43 of file node_text.c.

46{
47 assert(node->type == ENT_TEXT);
48
49 const int num_bytes = node->end - node->start;
50 return format_string(buf, 0, max_cols, JUSTIFY_LEFT, ' ', node->start, num_bytes, false);
51}
@ ENT_TEXT
Plain text.
Definition: node.h:38
const char * end
End of string data.
Definition: node.h:80
const char * start
Start of string data.
Definition: node.h:79
+ Here is the call graph for this function:
+ Here is the caller graph for this function: