NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_container.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdbool.h>
32#include <stddef.h>
33#include "mutt/lib.h"
34#include "node_container.h"
35#include "format.h"
36#include "helpers.h"
37#include "node.h"
38#include "render.h"
39
43int node_container_render(const struct ExpandoNode *node,
44 const struct ExpandoRenderData *rdata, struct Buffer *buf,
45 int max_cols, void *data, MuttFormatFlags flags)
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}
81
87{
88 struct ExpandoNode *node = node_new();
89
90 node->type = ENT_CONTAINER;
92
93 return node;
94}
95
101{
102 if (!ptr || !*ptr)
103 return;
104
105 struct ExpandoNode *node = *ptr;
106
107 if (node->type != ENT_CONTAINER)
108 return;
109
110 struct ExpandoNode *child = NULL;
111 struct ExpandoNode **np = NULL;
112 size_t size = 0;
113 ARRAY_FOREACH(np, &node->children)
114 {
115 if (!np || !*np)
116 continue;
117
118 size++;
119 child = *np;
120 }
121
122 if (size > 1)
123 return;
124
125 if (size == 0)
126 {
127 node_free(ptr);
128 return;
129 }
130
131 ARRAY_FREE(&node->children);
132 node_free(ptr);
133 *ptr = child;
134}
135
141{
142 if (!ptr || !*ptr)
143 return;
144
145 struct ExpandoNode *parent = *ptr;
146
147 struct ExpandoNode **np = NULL;
148 ARRAY_FOREACH(np, &parent->children)
149 {
151 }
152
154}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:204
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:491
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
Shared code.
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
Simple string formatting.
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() -.
#define MIN(a, b)
Definition: memory.h:32
Convenience wrapper for the library headers.
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition: node.c:39
void node_free(struct ExpandoNode **ptr)
Free an ExpandoNode and its private data.
Definition: node.c:48
Basic Expando Node.
@ ENT_CONTAINER
Container for other nodes.
Definition: node.h:44
struct ExpandoNode * node_container_new(void)
Create a new Container ExpandoNode.
void node_container_collapse_all(struct ExpandoNode **ptr)
Remove unnecessary Containers.
void node_container_collapse(struct ExpandoNode **ptr)
Remove an unnecessary Container.
Expando Node for a Container.
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
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
Render Expandos using Data.
uint8_t MuttFormatFlags
Flags for expando_render(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: render.h:32
#define ASSERT(COND)
Definition: signal2.h:58
String manipulation buffer.
Definition: buffer.h:36
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
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
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:68
struct ExpandoNodeArray children
Children nodes.
Definition: node.h:75