BRL-CAD
column.h
Go to the documentation of this file.
1 /* C O L U M N . H
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2024 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #ifndef BU_COLUMN_H
22 #define BU_COLUMN_H
23 
24 #include "common.h"
25 #include "bu/defines.h"
26 #include "bu/vls.h"
27 
28 __BEGIN_DECLS
29 
30 /** @addtogroup bu_vls
31  *
32  * @brief
33  * Given a series of input strings and formatting parameters,
34  * construct an output version of an input string with contents arranged into
35  * multiple columns.
36  *
37  */
38 /** @{ */
39 /** @file bu/column.h */
40 
41 struct bu_column_state_internal;
42 
44  /* User settings */
45  int terminal_width; /** default is 80 */
46  int fill_order; /** 0 is column first, 1 is row first */
47  int column_cnt; /** normally calculated but can be user specified */
48  const char *delimiter; /** string between columns - defaults to a single space */
49  const char *row_prefix; /** string before first column in row - defaults to empty */
50  const char *row_suffix; /** string after first column in row - defaults to empty */
51  struct bu_column_state_internal *i;
52 };
53 
54 #define BU_COLUMN_ALL -1
55 
56 /**
57  * Generate an output string based on the specified layout and contents in the
58  * bu_column_state s. Return the number of items printed, or -1 if there is
59  * an error.
60  *
61  * Any uninitialized layout state will be calculated according to layout logic
62  * derived from BSD UNIX's column and ls commands. The following defaults are
63  * assumed or calculated, but pre-existing non-empty default values will be
64  * respected if set. Note that if any of the parameters in bu_column_state are
65  * changed, the caller must re-run bu_column_compute to re-generate the new
66  * layout. If column count and width are unset, they will be determined at
67  * print time.
68  *
69  * Terminal width: 80
70  * Column count: input dependent
71  * Column widths: input dependent
72  * Column alignment: left aligned
73  * Column delimiter: space
74  * Row prefix: none
75  * Row suffix: none
76  * Fill order: column first
77  *
78  *
79  */
80 BU_EXPORT extern int bu_column_print(struct bu_vls *o, struct bu_column_state *s);
81 
82 /**
83  * Add a string to the layout. Return codes:
84  *
85  * -1 could not add successfully, str is not in layout
86  * 0 added
87  * n number of characters that could not be added. If column count and/or
88  * width are set to fixed values that prevent the full contents of str from
89  * being added, the return code tells the caller how many characters had to
90  * be truncated to make it fit in the layout. This will not happen with
91  * unset column count and width, since the layout will be finalized based on
92  * the inputs at print time.
93  */
94 BU_EXPORT extern int bu_column_add(struct bu_column_state *s, const char *str);
95 
96 /* Note - column numbering is 0 - n-1 where n is s->column_cnt To set all
97  * alignments simultaneously, pass BU_COLUMN_ALL to colnum. If a column number
98  * is specified that does not correspond to a column present in the layout, or
99  * an unknown alignment is specified, -1 will be returned. */
100 #define BU_COLUMN_ALIGN_LEFT 0
101 #define BU_COLUMN_ALIGN_RIGHT 1
102 #define BU_COLUMN_ALIGN_CENTER_LEFT_BIAS 2
103 #define BU_COLUMN_ALIGN_CENTER_RIGHT_BIAS 3
104 BU_EXPORT extern int bu_column_set_alignment(struct bu_column_state *s, int colnum, int alignment);
105 
106 /* Note - column numbering is 0 - n-1 where n is s->column_cnt To set all
107  * widths simultaneously, pass BU_COLUMN_ALL to colnum. If a column number
108  * is specified that does not correspond to a column present in the layout, -1
109  * will be returned. If a width is specified that is larger than 1/2 the
110  * terminal width, it will be clamped to the nearest value <= 1/2 the terminal
111  * width and a -2 error will be returned. A value of <=0 for width is considered
112  * unset, and the width will be calculated at print time in that case.*/
113 BU_EXPORT extern int bu_column_set_width(struct bu_column_state *s, int colnum, int width);
114 
115 /* Clear all contents in the state, but do not reset the layout parameters */
116 BU_EXPORT extern int bu_column_clear(struct bu_column_state *s);
117 
118 /* Reset the layout parameters, but do not clear the contents. */
119 BU_EXPORT extern int bu_column_reset(struct bu_column_state *s);
120 
121 
122 __END_DECLS
123 
124 /** @} */
125 
126 #endif /* BU_COLUMN_H */
127 
128 /*
129  * Local Variables:
130  * mode: C
131  * tab-width: 8
132  * indent-tabs-mode: t
133  * c-file-style: "stroustrup"
134  * End:
135  * ex: shiftwidth=4 tabstop=8
136  */
Header file for the BRL-CAD common definitions.
int bu_column_reset(struct bu_column_state *s)
int bu_column_clear(struct bu_column_state *s)
int bu_column_add(struct bu_column_state *s, const char *str)
int bu_column_set_width(struct bu_column_state *s, int colnum, int width)
int bu_column_print(struct bu_vls *o, struct bu_column_state *s)
int bu_column_set_alignment(struct bu_column_state *s, int colnum, int alignment)
int terminal_width
Definition: column.h:45
const char * delimiter
Definition: column.h:48
int column_cnt
Definition: column.h:47
int fill_order
Definition: column.h:46
struct bu_column_state_internal * i
Definition: column.h:51
const char * row_prefix
Definition: column.h:49
const char * row_suffix
Definition: column.h:50
Definition: vls.h:53