NeoMutt  2025-12-11-1039-g550ac6
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
format.h File Reference

Simple string formatting. More...

#include <stdbool.h>
#include <stddef.h>
Include dependency graph for format.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  FormatJustify { JUSTIFY_LEFT = -1 , JUSTIFY_CENTER = 0 , JUSTIFY_RIGHT = 1 }
 Alignment for format_string(). More...

Functions

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().

Detailed Description

Simple string formatting.

Authors
  • 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 format.h.

Enumeration Type Documentation

◆ FormatJustify

Alignment for format_string().

Enumerator
JUSTIFY_LEFT 

Left justify the text.

JUSTIFY_CENTER 

Centre the text.

JUSTIFY_RIGHT 

Right justify the text.

Definition at line 32 of file format.h.

33{
34 JUSTIFY_LEFT = -1,
35 JUSTIFY_CENTER = 0,
36 JUSTIFY_RIGHT = 1,
37};
@ JUSTIFY_RIGHT
Right justify the text.
Definition format.h:36
@ JUSTIFY_LEFT
Left justify the text.
Definition format.h:34
@ JUSTIFY_CENTER
Centre the text.
Definition format.h:35

Function Documentation

◆ format_string()

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().

Parameters
[out]bufBuffer in which to save string
[in]min_colsMinimum number of screen columns to use
[in]max_colsMaximum number of screen columns to use
[in]justifyJustification, e.g. JUSTIFY_RIGHT
[in]pad_charPadding character
[in]strString to format
[in]nNumber of bytes of string to format
[in]arborealIf true, string contains graphical tree characters
Return values
objNumber of bytes and screen columns used

This formats a string, a bit like sprintf(buf, "%-*.*s", min_cols, max_cols, str), except that the max_cols refer to the number of character cells when printed.

Definition at line 107 of file format.c.

109{
110 wchar_t wc = 0;
111 int w = 0;
112 size_t k = 0;
113 char scratch[MB_LEN_MAX] = { 0 };
114 mbstate_t mbstate1 = { 0 };
115 mbstate_t mbstate2 = { 0 };
116 bool escaped = false;
117 int used_cols = 0;
118
119 for (; (n > 0) && (k = mbrtowc(&wc, str, n, &mbstate1)); str += k, n -= k)
120 {
121 if ((k == ICONV_ILLEGAL_SEQ) || (k == ICONV_BUF_TOO_SMALL))
122 {
123 if ((k == ICONV_ILLEGAL_SEQ) && (errno == EILSEQ))
124 memset(&mbstate1, 0, sizeof(mbstate1));
125
126 k = (k == ICONV_ILLEGAL_SEQ) ? 1 : n;
127 wc = ReplacementChar;
128 }
129
130 // How many screen cells will the character require?
131 if (escaped)
132 {
133 escaped = false;
134 w = 0;
135 }
136 else if (arboreal && (wc == MUTT_SPECIAL_INDEX))
137 {
138 escaped = true;
139 w = 0;
140 }
141 else if (arboreal && (wc < MUTT_TREE_MAX))
142 {
143 w = 1; /* hack */
144 }
145 else if (iswspace(wc))
146 {
147 w = MAX(1, wcwidth(wc));
148 }
149 else
150 {
151#ifdef HAVE_ISWBLANK
152 if (iswblank(wc))
153 wc = ' '; // LCOV_EXCL_LINE
154 else
155#endif
156 if (!IsWPrint(wc))
157 wc = '?';
158 w = wcwidth(wc);
159 }
160
161 // It'll require _some_ space
162 if (w >= 0)
163 {
164 if (w > max_cols)
165 continue;
166 size_t k2 = wcrtomb(scratch, wc, &mbstate2);
167 if (k2 == ICONV_ILLEGAL_SEQ)
168 continue; // LCOV_EXCL_LINE
169
170 used_cols += w;
171
172 min_cols -= w;
173 max_cols -= w;
174 buf_addstr_n(buf, scratch, k2);
175 }
176 }
177
178 // How much space is left?
179 w = min_cols;
180 if (w > 0)
181 {
182 used_cols += w;
183 }
184
185 if (w > 0)
186 buf_justify(buf, justify, buf_len(buf) + w, pad_char);
187
188 return used_cols;
189}
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:109
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition buffer.c:497
void buf_justify(struct Buffer *buf, enum FormatJustify justify, size_t max_cols, char pad_char)
Justify a string.
Definition format.c:49
@ MUTT_TREE_MAX
Definition thread.h:70
@ MUTT_SPECIAL_INDEX
Colour indicator.
Definition thread.h:72
#define IsWPrint(wc)
Definition mbyte.h:40
#define MAX(a, b)
Return the maximum of two values.
Definition memory.h:38
wchar_t ReplacementChar
When a Unicode character can't be displayed, use this instead.
Definition charset.c:62
#define EILSEQ
Definition charset.c:56
#define ICONV_BUF_TOO_SMALL
Error value for iconv() - Buffer too small.
Definition charset.h:116
#define ICONV_ILLEGAL_SEQ
Error value for iconv() - Illegal sequence.
Definition charset.h:114
Here is the call graph for this function:
Here is the caller graph for this function: