NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
node_padding.c File Reference

Expando Node for Padding. More...

#include "config.h"
#include <stddef.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "gui/lib.h"
#include "node_padding.h"
#include "definition.h"
#include "node.h"
#include "node_container.h"
#include "parse.h"
#include "render.h"
+ Include dependency graph for node_padding.c:

Go to the source code of this file.

Functions

struct NodePaddingPrivatenode_padding_private_new (enum ExpandoPadType pad_type)
 Create new Padding private data.
 
void node_padding_private_free (void **ptr)
 Free Padding private data - Implements ExpandoNode::ndata_free()
 
int pad_string (const struct ExpandoNode *node, struct Buffer *buf, int max_cols)
 Pad a buffer with a character.
 
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() -.
 
struct ExpandoNodenode_padding_new (enum ExpandoPadType pad_type, const char *start, const char *end)
 Creata new Padding ExpandoNode.
 
struct ExpandoNodenode_padding_parse (const char *str, struct ExpandoFormat *fmt, int did, int uid, ExpandoParserFlags flags, const char **parsed_until, struct ExpandoParseError *err)
 Parse a Padding Expando - Implements ExpandoDefinition::parse() -.
 
void node_padding_repad (struct ExpandoNode **ptr)
 Rearrange Padding in a tree of ExpandoNodes.
 

Detailed Description

Expando Node for Padding.

Authors
  • Tóth János
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file node_padding.c.

Function Documentation

◆ node_padding_private_new()

struct NodePaddingPrivate * node_padding_private_new ( enum ExpandoPadType  pad_type)

Create new Padding private data.

Parameters
pad_typePadding type
Return values
ptrNew Padding private data

Definition at line 47 of file node_padding.c.

48{
50
51 priv->pad_type = pad_type;
52
53 return priv;
54}
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
Private data for a Padding Node -.
Definition: node_padding.h:65
enum ExpandoPadType pad_type
Padding type.
Definition: node_padding.h:66
+ Here is the caller graph for this function:

◆ node_padding_private_free()

void node_padding_private_free ( void **  ptr)

Free Padding private data - Implements ExpandoNode::ndata_free()

Parameters
ptrData to free

Definition at line 60 of file node_padding.c.

61{
62 if (!ptr || !*ptr)
63 return;
64
65 FREE(ptr);
66}
#define FREE(x)
Definition: memory.h:55
+ Here is the caller graph for this function:

◆ pad_string()

int pad_string ( const struct ExpandoNode node,
struct Buffer buf,
int  max_cols 
)

Pad a buffer with a character.

Parameters
nodeNode with padding type
bufBuffer to populate
max_colsNumber of screen columns available

Fill buf with the padding char (Node.start) to a maximum of max_cols screen cells.

Definition at line 76 of file node_padding.c.

77{
78 const int pad_len = mutt_str_len(node->text);
79 const int pad_cols = mutt_strnwidth(node->text, pad_len);
80 int total_cols = 0;
81
82 if (pad_len != 0)
83 {
84 while (pad_cols <= max_cols)
85 {
86 buf_addstr_n(buf, node->text, pad_len);
87
88 max_cols -= pad_cols;
89 total_cols += pad_cols;
90 }
91 }
92
93 if (max_cols > 0)
94 {
95 buf_add_printf(buf, "%*s", max_cols, "");
96 total_cols += max_cols;
97 }
98
99 return total_cols;
100}
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:204
size_t buf_addstr_n(struct Buffer *buf, const char *s, size_t len)
Add a string to a Buffer, expanding it if necessary.
Definition: buffer.c:96
size_t mutt_strnwidth(const char *s, size_t n)
Measure a string's width in screen cells.
Definition: curs_lib.c:456
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
const char * text
Node-specific text.
Definition: node.h:73
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_padding_new()

struct ExpandoNode * node_padding_new ( enum ExpandoPadType  pad_type,
const char *  start,
const char *  end 
)

Creata new Padding ExpandoNode.

Parameters
pad_typePadding type
startStart of padding character
endEnd of padding character
Return values
ptrNew Padding ExpandoNode

Definition at line 201 of file node_padding.c.

203{
204 struct ExpandoNode *node = node_new();
205
206 node->type = ENT_PADDING;
207 node->text = mutt_strn_dup(start, end - start);
208
209 switch (pad_type)
210 {
211 case EPT_FILL_EOL:
213 break;
214 case EPT_HARD_FILL:
216 break;
217 case EPT_SOFT_FILL:
219 break;
220 };
221
222 node->ndata = node_padding_private_new(pad_type);
224
225 return node;
226}
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() -.
Definition: node_padding.c:124
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() -.
Definition: node_padding.c:162
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: node_padding.c:105
char * mutt_strn_dup(const char *begin, size_t len)
Duplicate a sub-string.
Definition: string.c:380
struct ExpandoNode * node_new(void)
Create a new empty ExpandoNode.
Definition: node.c:39
@ ENT_PADDING
Padding: soft, hard, EOL.
Definition: node.h:40
void node_padding_private_free(void **ptr)
Free Padding private data - Implements ExpandoNode::ndata_free()
Definition: node_padding.c:60
struct NodePaddingPrivate * node_padding_private_new(enum ExpandoPadType pad_type)
Create new Padding private data.
Definition: node_padding.c:47
@ EPT_FILL_EOL
Fill to the end-of-line.
Definition: node_padding.h:45
@ EPT_SOFT_FILL
Soft-fill: right-hand-side will be truncated.
Definition: node_padding.h:47
@ EPT_HARD_FILL
Hard-fill: left-hand-side will be truncated.
Definition: node_padding.h:46
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
void * ndata
Private node data.
Definition: node.h:77
enum ExpandoNodeType type
Type of Node, e.g. ENT_EXPANDO.
Definition: node.h:68
void(* ndata_free)(void **ptr)
Function to free the private node data.
Definition: node.h:78
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ node_padding_repad()

void node_padding_repad ( struct ExpandoNode **  ptr)

Rearrange Padding in a tree of ExpandoNodes.

Parameters
ptrParent Node

Definition at line 291 of file node_padding.c.

292{
293 if (!ptr || !*ptr)
294 return;
295
296 struct ExpandoNode *parent = *ptr;
297 struct ExpandoNode **np = NULL;
298 ARRAY_FOREACH(np, &parent->children)
299 {
300 if (!np || !*np)
301 continue;
302
303 // Repad any children, recursively
305
306 struct ExpandoNode *node = *np;
307 if (node->type != ENT_PADDING)
308 continue;
309
310 struct ExpandoNode *node_left = node_container_new();
311 struct ExpandoNode *node_right = node_container_new();
312
313 if (ARRAY_FOREACH_IDX > 0)
314 {
315 for (int i = 0; i < ARRAY_FOREACH_IDX; i++)
316 {
317 node_add_child(node_left, node_get_child(parent, i));
318 }
319 }
320
321 size_t count = ARRAY_SIZE(&parent->children);
322 if ((ARRAY_FOREACH_IDX + 1) < count)
323 {
324 for (int i = ARRAY_FOREACH_IDX + 1; i < count; i++)
325 {
326 node_add_child(node_right, node_get_child(parent, i));
327 }
328 }
329
330 // All the children have been transferred
331 ARRAY_FREE(&parent->children);
332
333 node_add_child(node, node_left);
334 node_add_child(node, node_right);
335
336 node_add_child(parent, node);
337
338 break; // Only repad the first padding node
339 }
340}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:212
#define ARRAY_SIZE(head)
The number of elements stored.
Definition: array.h:87
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:204
struct ExpandoNode * node_get_child(const struct ExpandoNode *node, int index)
Get a child of an ExpandoNode.
Definition: node.c:91
void node_add_child(struct ExpandoNode *node, struct ExpandoNode *child)
Add a child to an ExpandoNode.
Definition: node.c:76
struct ExpandoNode * node_container_new(void)
Create a new Container ExpandoNode.
void node_padding_repad(struct ExpandoNode **ptr)
Rearrange Padding in a tree of ExpandoNodes.
Definition: node_padding.c:291
struct ExpandoNodeArray children
Children nodes.
Definition: node.h:75
+ Here is the call graph for this function:
+ Here is the caller graph for this function: