Device list is printed in multiple columns.

This commit is contained in:
Daniel Beer 2010-10-12 12:02:17 +13:00
parent 6d888fff25
commit 5e39d82784
4 changed files with 60 additions and 57 deletions

13
main.c
View File

@ -337,15 +337,9 @@ static int add_fet_device(void *user_data, const struct fet_db_record *r)
return vector_push(v, &r->name, 1);
}
static int cmp_char_ptr(const void *a, const void *b)
{
return strcmp(*(const char **)a, *(const char **)b);
}
static int list_devices(void)
{
struct vector v;
int i;
vector_init(&v, sizeof(const char *));
if (fet_db_enum(add_fet_device, &v) < 0) {
@ -354,13 +348,10 @@ static int list_devices(void)
return -1;
}
qsort(v.ptr, v.size, v.elemsize, cmp_char_ptr);
printc("Devices supported by FET driver:\n");
for (i = 0; i < v.size; i++)
printc(" %s\n", VECTOR_AT(v, i, const char *));
namelist_print(&v);
vector_destroy(&v);
return 0;
}

View File

@ -17,6 +17,7 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
@ -121,3 +122,53 @@ void capture_end(void)
{
capture_func = NULL;
}
/************************************************************************
* Name lists
*/
static int namelist_cmp(const void *a, const void *b)
{
return strcasecmp(*(const char **)a, *(const char **)b);
}
void namelist_print(struct vector *v)
{
int i;
int max_len = 0;
int rows, cols;
qsort(v->ptr, v->size, v->elemsize, namelist_cmp);
for (i = 0; i < v->size; i++) {
const char *text = VECTOR_AT(*v, i, const char *);
int len = strlen(text);
if (len > max_len)
max_len = len;
}
max_len += 2;
cols = 72 / max_len;
rows = (v->size + cols - 1) / cols;
for (i = 0; i < rows; i++) {
int j;
printc(" ");
for (j = 0; j < cols; j++) {
int k = j * rows + i;
const char *text;
if (k >= v->size)
break;
text = VECTOR_AT(*v, k, const char *);
printc("%s", text);
for (k = strlen(text); k < max_len; k++)
printc(" ");
}
printc("\n");
}
}

View File

@ -19,6 +19,8 @@
#ifndef OUTPUT_H_
#define OUTPUT_H_
#include "vector.h"
/* Print output. ANSI colour codes may be embedded, and these will be
* stripped on output if colour output is disabled.
*
@ -43,4 +45,9 @@ typedef void (*capture_func_t)(void *user_data, const char *text);
void capture_start(capture_func_t, void *user_data);
void capture_end(void);
/* Name lists. This function is used for printing multi-column sorted
* lists of constant strings. Expected is a vector of const char *.
*/
void namelist_print(struct vector *v);
#endif

View File

@ -28,52 +28,6 @@
#include "reader.h"
#include "expr.h"
static int namelist_cmp(const void *a, const void *b)
{
return strcasecmp(*(const char **)a, *(const char **)b);
}
static void namelist_print(struct vector *v)
{
int i;
int max_len = 0;
int rows, cols;
qsort(v->ptr, v->size, v->elemsize, namelist_cmp);
for (i = 0; i < v->size; i++) {
const char *text = VECTOR_AT(*v, i, const char *);
int len = strlen(text);
if (len > max_len)
max_len = len;
}
max_len += 2;
cols = 72 / max_len;
rows = (v->size + cols - 1) / cols;
for (i = 0; i < rows; i++) {
int j;
printc(" ");
for (j = 0; j < cols; j++) {
int k = j * rows + i;
const char *text;
if (k >= v->size)
break;
text = VECTOR_AT(*v, k, const char *);
printc("%s", text);
for (k = strlen(text); k < max_len; k++)
printc(" ");
}
printc("\n");
}
}
static const char *type_text(opdb_type_t type)
{
switch (type) {