NeoMutt  2024-10-02-37-gfa9146
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 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 if (iswspace(wc))
147 {
148 w = 1;
149 }
150 else
151 {
152#ifdef HAVE_ISWBLANK
153 if (iswblank(wc))
154 wc = ' '; // LCOV_EXCL_LINE
155 else
156#endif
157 if (!IsWPrint(wc))
158 wc = '?';
159 w = wcwidth(wc);
160 }
161
162 // It'll require _some_ space
163 if (w >= 0)
164 {
165 if (w > max_cols)
166 continue;
167 size_t k2 = wcrtomb(scratch, wc, &mbstate2);
168 if (k2 == ICONV_ILLEGAL_SEQ)
169 continue; // LCOV_EXCL_LINE
170
171 used_cols += w;
172
173 min_cols -= w;
174 max_cols -= w;
175 buf_addstr_n(buf, scratch, k2);
176 }
177 }
178
179 // How much space is left?
180 w = min_cols;
181 if (w > 0)
182 {
183 used_cols += w;
184 }
185
186 if (w > 0)
187 buf_justify(buf, justify, buf_len(buf) + w, pad_char);
188
189 return used_cols;
190}
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 buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:491
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:107
#define ICONV_ILLEGAL_SEQ
Error value for iconv() - Illegal sequence.
Definition: charset.h:105
@ 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: