Device list is printed in multiple columns.
This commit is contained in:
parent
6d888fff25
commit
5e39d82784
13
main.c
13
main.c
|
@ -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);
|
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)
|
static int list_devices(void)
|
||||||
{
|
{
|
||||||
struct vector v;
|
struct vector v;
|
||||||
int i;
|
|
||||||
|
|
||||||
vector_init(&v, sizeof(const char *));
|
vector_init(&v, sizeof(const char *));
|
||||||
if (fet_db_enum(add_fet_device, &v) < 0) {
|
if (fet_db_enum(add_fet_device, &v) < 0) {
|
||||||
|
@ -354,13 +348,10 @@ static int list_devices(void)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
qsort(v.ptr, v.size, v.elemsize, cmp_char_ptr);
|
|
||||||
|
|
||||||
printc("Devices supported by FET driver:\n");
|
printc("Devices supported by FET driver:\n");
|
||||||
for (i = 0; i < v.size; i++)
|
namelist_print(&v);
|
||||||
printc(" %s\n", VECTOR_AT(v, i, const char *));
|
|
||||||
|
|
||||||
vector_destroy(&v);
|
vector_destroy(&v);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
51
output.c
51
output.c
|
@ -17,6 +17,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
@ -121,3 +122,53 @@ void capture_end(void)
|
||||||
{
|
{
|
||||||
capture_func = NULL;
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
7
output.h
7
output.h
|
@ -19,6 +19,8 @@
|
||||||
#ifndef OUTPUT_H_
|
#ifndef OUTPUT_H_
|
||||||
#define OUTPUT_H_
|
#define OUTPUT_H_
|
||||||
|
|
||||||
|
#include "vector.h"
|
||||||
|
|
||||||
/* Print output. ANSI colour codes may be embedded, and these will be
|
/* Print output. ANSI colour codes may be embedded, and these will be
|
||||||
* stripped on output if colour output is disabled.
|
* 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_start(capture_func_t, void *user_data);
|
||||||
void capture_end(void);
|
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
|
#endif
|
||||||
|
|
46
stdcmd.c
46
stdcmd.c
|
@ -28,52 +28,6 @@
|
||||||
#include "reader.h"
|
#include "reader.h"
|
||||||
#include "expr.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)
|
static const char *type_text(opdb_type_t type)
|
||||||
{
|
{
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
|
Loading…
Reference in New Issue