NeoMutt  2024-04-25-91-gb0e085
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 109 of file node_condbool.c.

112{
113 ASSERT(node->type == ENT_CONDBOOL);
114
115 const struct ExpandoRenderData *rd_match = find_get_number(rdata, node->did, node->uid);
116 if (rd_match)
117 {
118 const long num = rd_match->get_number(node, data, flags);
119 return (num != 0); // bool-ify
120 }
121
122 rd_match = find_get_string(rdata, node->did, node->uid);
123 if (rd_match)
124 {
125 struct Buffer *buf_str = buf_pool_get();
126 rd_match->get_string(node, data, flags, max_cols, buf_str);
127 const size_t len = buf_len(buf_str);
128 buf_pool_release(&buf_str);
129
130 return (len > 0); // bool-ify
131 }
132
133 return 0;
134}
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:47
const struct ExpandoRenderData * find_get_string(const struct ExpandoRenderData *rdata, int did, int uid)
Find a get_string() callback function.
Definition: helpers.c:71
@ 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: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 74 of file node_conddate.c.

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

43{
44 ASSERT(node->type == ENT_CONDITION);
45
46 const struct ExpandoNode *node_cond = node_get_child(node, ENC_CONDITION);
47
48 // Discard any text returned, just use the return value as a bool
49 struct Buffer *buf_cond = buf_pool_get();
50 int rc = node_cond->render(node_cond, rdata, buf_cond, max_cols, data, flags);
51 buf_pool_release(&buf_cond);
52
53 if (rc == true)
54 {
55 const struct ExpandoNode *node_true = node_get_child(node, ENC_TRUE);
56 return node_render(node_true, rdata, buf, max_cols, data, flags);
57 }
58 else
59 {
60 const struct ExpandoNode *node_false = node_get_child(node, ENC_FALSE);
61 return node_render(node_false, rdata, buf, max_cols, data, flags);
62 }
63}
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_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 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 total_cols += node_render(*enp, rdata, tmp, max_cols - total_cols, data, flags);
60 }
61
62 if (fmt)
63 {
64 int min_cols = MIN(fmt->min_cols, max_cols);
65 struct Buffer *tmp2 = buf_pool_get();
66 total_cols = format_string(tmp2, min_cols, max_cols, fmt->justification,
67 fmt->leader, buf_string(tmp), buf_len(tmp), true);
68 if (fmt->lower)
70 buf_addstr(buf, buf_string(tmp2));
71 buf_pool_release(&tmp2);
72 }
73 else
74 {
75 buf_addstr(buf, buf_string(tmp));
76 }
77
78 buf_pool_release(&tmp);
79 return total_cols;
80}
#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:176
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 344 of file node_expando.c.

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

44{
45 ASSERT(node->type == ENT_TEXT);
46
47 const int num_bytes = node->end - node->start;
48 return format_string(buf, 0, max_cols, JUSTIFY_LEFT, ' ', node->start, num_bytes, false);
49}
@ 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: