]>
git.ipfire.org Git - thirdparty/git.git/blob - column.c
3 #include "string-list.h"
4 #include "parse-options.h"
6 /* Display without layout when not enabled */
7 static void display_plain(const struct string_list
*list
,
8 const char *indent
, const char *nl
)
12 for (i
= 0; i
< list
->nr
; i
++)
13 printf("%s%s%s", indent
, list
->items
[i
].string
, nl
);
16 void print_columns(const struct string_list
*list
, unsigned int colopts
,
17 const struct column_options
*opts
)
19 struct column_options nopts
;
23 assert((colopts
& COL_ENABLE_MASK
) != COL_AUTO
);
25 memset(&nopts
, 0, sizeof(nopts
));
26 nopts
.indent
= opts
&& opts
->indent
? opts
->indent
: "";
27 nopts
.nl
= opts
&& opts
->nl
? opts
->nl
: "\n";
28 nopts
.padding
= opts
? opts
->padding
: 1;
29 nopts
.width
= opts
&& opts
->width
? opts
->width
: term_columns() - 1;
30 if (!column_active(colopts
)) {
31 display_plain(list
, "", "\n");
34 switch (COL_LAYOUT(colopts
)) {
36 display_plain(list
, nopts
.indent
, nopts
.nl
);
39 die("BUG: invalid layout mode %d", COL_LAYOUT(colopts
));
43 int finalize_colopts(unsigned int *colopts
, int stdout_is_tty
)
45 if ((*colopts
& COL_ENABLE_MASK
) == COL_AUTO
) {
46 if (stdout_is_tty
< 0)
47 stdout_is_tty
= isatty(1);
48 *colopts
&= ~COL_ENABLE_MASK
;
50 *colopts
|= COL_ENABLED
;
64 static int parse_option(const char *arg
, int len
, unsigned int *colopts
,
67 struct colopt opts
[] = {
68 { "always", COL_ENABLED
, COL_ENABLE_MASK
},
69 { "never", COL_DISABLED
, COL_ENABLE_MASK
},
70 { "auto", COL_AUTO
, COL_ENABLE_MASK
},
71 { "plain", COL_PLAIN
, COL_LAYOUT_MASK
},
75 for (i
= 0; i
< ARRAY_SIZE(opts
); i
++) {
76 int arg_len
= len
, name_len
;
77 const char *arg_str
= arg
;
79 name_len
= strlen(opts
[i
].name
);
80 if (arg_len
!= name_len
||
81 strncmp(arg_str
, opts
[i
].name
, name_len
))
84 switch (opts
[i
].mask
) {
86 *group_set
|= ENABLE_SET
;
89 *group_set
|= LAYOUT_SET
;
94 *colopts
= (*colopts
& ~opts
[i
].mask
) | opts
[i
].value
;
98 return error("unsupported option '%s'", arg
);
101 static int parse_config(unsigned int *colopts
, const char *value
)
103 const char *sep
= " ,";
107 int len
= strcspn(value
, sep
);
109 if (parse_option(value
, len
, colopts
, &group_set
))
114 value
+= strspn(value
, sep
);
117 * Setting layout implies "always" if neither always, never
118 * nor auto is specified.
120 * Current value in COL_ENABLE_MASK is disregarded. This means if
121 * you set column.ui = auto and pass --column=row, then "auto"
122 * will become "always".
124 if ((group_set
& LAYOUT_SET
) && !(group_set
& ENABLE_SET
))
125 *colopts
= (*colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
129 static int column_config(const char *var
, const char *value
,
130 const char *key
, unsigned int *colopts
)
133 return config_error_nonbool(var
);
134 if (parse_config(colopts
, value
))
135 return error("invalid column.%s mode %s", key
, value
);
139 int git_column_config(const char *var
, const char *value
,
140 const char *command
, unsigned int *colopts
)
142 const char *it
= skip_prefix(var
, "column.");
146 if (!strcmp(it
, "ui"))
147 return column_config(var
, value
, "ui", colopts
);
149 if (command
&& !strcmp(it
, command
))
150 return column_config(var
, value
, it
, colopts
);
155 int parseopt_column_callback(const struct option
*opt
,
156 const char *arg
, int unset
)
158 unsigned int *colopts
= opt
->value
;
159 *colopts
|= COL_PARSEOPT
;
160 *colopts
&= ~COL_ENABLE_MASK
;
161 if (unset
) /* --no-column == never */
163 /* --column == always unless "arg" states otherwise */
164 *colopts
|= COL_ENABLED
;
166 return parse_config(colopts
, arg
);