#ifndef UTIL_LINUX_PAGER
#define UTIL_LINUX_PAGER
+#include <stddef.h>
+
void pager_open(void);
+void pager_open_header(int header_lines, size_t first_col_width);
void pager_close(void);
#endif
close(fd[1]);
}
+static int pager_header_lines;
+static size_t pager_header_width;
+
static void pager_preexec(void)
{
- if (getenv("LESS") == NULL && setenv("LESS", "FRSX", 0) != 0)
- warn(_("failed to set the %s environment variable"), "LESS");
+ if (getenv("LESS") == NULL) {
+ if (pager_header_lines > 0) {
+ char less_env[256];
+
+ if (pager_header_width > 0)
+ snprintf(less_env, sizeof(less_env),
+ "FRSX --header %d,%zu",
+ pager_header_lines,
+ pager_header_width);
+ else
+ snprintf(less_env, sizeof(less_env),
+ "FRSX --header %d",
+ pager_header_lines);
+
+ if (setenv("LESS", less_env, 0) != 0)
+ warn(_("failed to set the %s environment variable"), "LESS");
+ } else {
+ if (setenv("LESS", "FRSX", 0) != 0)
+ warn(_("failed to set the %s environment variable"), "LESS");
+ }
+ }
+
if (getenv("LV") == NULL && setenv("LV", "-c", 0) != 0)
warn(_("failed to set the %s environment variable"), "LV");
}
}
}
+/* Setup pager with "less --header" support to freeze header lines and
+ * optionally freeze the first column. The @header_lines specifies the
+ * number of header lines to freeze (typically 1 for table header).
+ * The @first_col_width specifies the number of character columns to
+ * freeze (width of first column including separator), or 0 to not
+ * freeze any column.
+ */
+void pager_open_header(int header_lines, size_t first_col_width)
+{
+ pager_header_lines = header_lines;
+ pager_header_width = first_col_width;
+ pager_open();
+ pager_header_lines = 0;
+ pager_header_width = 0;
+}
+
/* Close pager and restore original std{out,err}.
*/
void pager_close(void)
/* table_print.c */
+extern int scols_table_calculate(struct libscols_table *tb);
extern int scols_print_table(struct libscols_table *tb);
extern int scols_print_table_to_string(struct libscols_table *tb, char **data);
scols_column_get_headercolor;
scols_column_refer_annotation;
scols_column_get_annotation;
+ scols_table_calculate;
} SMARTCOLS_2.41;
}
#endif
+/**
+ * scols_table_calculate:
+ * @tb: table
+ *
+ * Force column width calculation without printing. After this call,
+ * scols_column_get_width() returns valid column widths. This is useful
+ * when you need to know column widths before printing, for example to
+ * set up a pager with "less --header" to freeze the header row and
+ * first column.
+ *
+ * Note that scols_print_table() will skip recalculation if this
+ * function has already been called.
+ *
+ * Returns: 0, a negative value in case of an error.
+ *
+ * Since: 2.42
+ */
+int scols_table_calculate(struct libscols_table *tb)
+{
+ struct ul_buffer buf = UL_INIT_BUFFER;
+ int rc;
+
+ if (!tb)
+ return -EINVAL;
+ if (list_empty(&tb->tb_columns))
+ return -EINVAL;
+ if (list_empty(&tb->tb_lines))
+ return 0;
+
+ DBG(TAB, ul_debugobj(tb, "pre-calculate"));
+
+ rc = __scols_initialize_printing(tb, &buf);
+ __scols_cleanup_printing(tb, &buf);
+
+ if (rc == 0)
+ tb->is_calculated = 1;
+
+ return rc;
+}
+
static int do_print_table(struct libscols_table *tb, int *is_empty)
{
int rc = 0;
}
done:
__scols_cleanup_printing(tb, &buf);
+ tb->is_calculated = 0;
return rc;
}
if (has_groups(tb) && scols_table_is_tree(tb))
scols_groups_fix_members_order(tb);
- if (tb->format == SCOLS_FMT_HUMAN) {
+ if (tb->format == SCOLS_FMT_HUMAN && !tb->is_calculated) {
rc = __scols_calculate(tb, buf);
if (rc != 0)
goto err;
no_headings , /* don't print header */
no_encode , /* don't care about control and non-printable chars */
no_linesep , /* don't print line separator */
- no_wrap ; /* never wrap lines */
+ no_wrap , /* never wrap lines */
+ is_calculated ; /* column widths already calculated */
};
#define IS_ITER_FORWARD(_i) ((_i)->direction == SCOLS_ITER_FORWARD)