NeoMutt  2024-12-12-19-ge4b57e
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 ExpandoRenderCallback *erc, 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 && (fmt->max_cols != -1))
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, erc, tmp, max_cols - total_cols, data, flags);
62 }
63
64 struct Buffer *tmp2 = buf_pool_get();
65 if (fmt)
66 {
67 int max = max_cols;
68 if (fmt->max_cols >= 0)
69 max = MIN(max_cols, fmt->max_cols);
70 int min = MIN(fmt->min_cols, max);
71
72 total_cols = format_string(tmp2, min, max, fmt->justification, ' ',
73 buf_string(tmp), buf_len(tmp), true);
74
75 if (fmt->lower)
77 }
78 else
79 {
80 total_cols = format_string(tmp2, 0, max_cols, JUSTIFY_LEFT, ' ',
81 buf_string(tmp), buf_len(tmp), true);
82 }
83 buf_addstr(buf, buf_string(tmp2));
84 buf_pool_release(&tmp2);
85
86 buf_pool_release(&tmp);
87 return total_cols;
88}
89
95{
96 struct ExpandoNode *node = node_new();
97
98 node->type = ENT_CONTAINER;
100
101 return node;
102}
103
109{
110 if (!ptr || !*ptr)
111 return;
112
113 struct ExpandoNode *node = *ptr;
114
115 if (node->type != ENT_CONTAINER)
116 return;
117
118 struct ExpandoNode *child = NULL;
119 struct ExpandoNode **np = NULL;
120 size_t size = 0;
121 ARRAY_FOREACH(np, &node->children)
122 {
123 if (!np || !*np)
124 continue;
125
126 size++;
127 child = *np;
128 }
129
130 if (size > 1)
131 return;
132
133 if (size == 0)
134 {
135 node_free(ptr);
136 return;
137 }
138
139 ARRAY_FREE(&node->children);
140 node_free(ptr);
141 *ptr = child;
142}
143
149{
150 if (!ptr || !*ptr)
151 return;
152
153 struct ExpandoNode *parent = *ptr;
154
155 struct ExpandoNode **np = NULL;
156 ARRAY_FOREACH(np, &parent->children)
157 {
159 }
160
162}
#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.
@ JUSTIFY_LEFT
Left justify the text.
Definition: format.h:34
int node_container_render(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, 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:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
int node_render(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, 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
struct ExpandoFormat * format
Formatting info.
Definition: node.h:72
int(* render)(const struct ExpandoNode *node, const struct ExpandoRenderCallback *erc, struct Buffer *buf, int max_cols, void *data, MuttFormatFlags flags)
Definition: node.h:92
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:68
struct ExpandoNodeArray children
Children nodes.
Definition: node.h:75