summaryrefslogtreecommitdiffstats
path: root/include/column-list-table.h
blob: 1629336bc5cd5c977368e76d5a405436070233f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * column-list-table.h - helper functions for implementing -H/--list-columns option
 *
 * Copyright (C) 2023 Red Hat, Inc. All rights reserved.
 * Written by Masatake YAMATO <yamato@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it would 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, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef UTIL_LINUX_COLUMN_LIST_TABLE_H
#define UTIL_LINUX_COLUMN_LIST_TABLE_H

#include "libsmartcols.h"
#include <stdio.h>

enum { CLT_COL_HOLDER, CLT_COL_TYPE, CLT_COL_DESC };

static inline struct libscols_table *xcolumn_list_table_new(const char *table_name,
							    FILE *out,
							    int raw,
							    int json)
{
	struct clt_colinfo {
		const char *name;
		int flags;
	};
	struct libscols_table *tb;

	scols_init_debug(0);

	tb = scols_new_table();
	if (!tb)
		err(EXIT_FAILURE, _("failed to allocate output table"));

	scols_table_set_name(tb, table_name);
	scols_table_set_stream(tb, out);
	scols_table_enable_noheadings(tb, 1);
	scols_table_enable_raw(tb, raw);
	scols_table_enable_json(tb, json);

	if (!scols_table_new_column(tb, "HOLDER", 0, SCOLS_FL_RIGHT))
		goto failed;
	if (!scols_table_new_column(tb, "TYPE", 0, 0))
		goto failed;
	if (!scols_table_new_column(tb, "DESCRIPTION", 0, 0))
		goto failed;
	return tb;

 failed:
	err(EXIT_FAILURE, _("failed to allocate output column"));
}

static inline void xcolumn_list_table_append_line(struct libscols_table *tb,
						  const char *name,
						  int json_type, const char *fallback_typename,
						  const char *desc)
{
	struct libscols_line *ln = scols_table_new_line(tb, NULL);
	if (!ln)
		err(EXIT_FAILURE, _("failed to allocate output line"));

	if (scols_line_set_data(ln, CLT_COL_HOLDER, name))
		err(EXIT_FAILURE, _("failed to add output data"));
	if (scols_line_set_data(ln, CLT_COL_TYPE, (json_type == SCOLS_JSON_STRING ?        "<string>":
						   json_type == SCOLS_JSON_ARRAY_STRING ?  "<string>":
						   json_type == SCOLS_JSON_ARRAY_NUMBER ?  "<string>":
						   json_type == SCOLS_JSON_NUMBER ?        "<integer>":
						   json_type == SCOLS_JSON_FLOAT ?         "<float>":
						   json_type == SCOLS_JSON_BOOLEAN ?       "<boolean>":
						   fallback_typename ?: "<string>")))
		err(EXIT_FAILURE, _("failed to add output data"));
	if (scols_line_set_data(ln, CLT_COL_DESC, desc))
		err(EXIT_FAILURE, _("failed to add output data"));
}

#endif