NeoMutt  2024-04-16-36-g75b6fb
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
format.h File Reference

Simple string formatting. More...

#include <stddef.h>
#include <stdbool.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 108 of file format.c.

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