NeoMutt  2024-11-14-34-g5aaf0d
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 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}
90
96{
97 struct ExpandoNode *node = node_new();
98
99 node->type = ENT_CONTAINER;
101
102 return node;
103}
104
110{
111 if (!ptr || !*ptr)
112 return;
113
114 struct ExpandoNode *node = *ptr;
115
116 if (node->type != ENT_CONTAINER)
117 return;
118
119 struct ExpandoNode *child = NULL;
120 struct ExpandoNode **np = NULL;
121 size_t size = 0;
122 ARRAY_FOREACH(np, &node->children)
123 {
124 if (!np || !*np)
125 continue;
126
127 size++;
128 child = *np;
129 }
130
131 if (size > 1)
132 return;
133
134 if (size == 0)
135 {
136 node_free(ptr);
137 return;
138 }
139
140 ARRAY_FREE(&node->children);
141 node_free(ptr);
142 *ptr = child;
143}
144
150{
151 if (!ptr || !*ptr)
152 return;
153
154 struct ExpandoNode *parent = *ptr;
155
156 struct ExpandoNode **np = NULL;
157 ARRAY_FOREACH(np, &parent->children)
158 {
160 }
161
163}
#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:92
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
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