- move the terminal handling in apropos-utils.c since htmp and pager are also
handled there. - underline the name, section, and description so that it is prettier. - change to bold terminal the terminal highlighting to match with less
This commit is contained in:
parent
4adce8f663
commit
6265ee0d3c
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: apropos-utils.c,v 1.8 2013/01/14 18:01:59 christos Exp $ */
|
||||
/* $NetBSD: apropos-utils.c,v 1.9 2013/01/14 21:26:25 christos Exp $ */
|
||||
/*-
|
||||
* Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>
|
||||
* All rights reserved.
|
||||
|
@ -31,7 +31,7 @@
|
|||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__RCSID("$NetBSD: apropos-utils.c,v 1.8 2013/01/14 18:01:59 christos Exp $");
|
||||
__RCSID("$NetBSD: apropos-utils.c,v 1.9 2013/01/14 21:26:25 christos Exp $");
|
||||
|
||||
#include <sys/queue.h>
|
||||
#include <sys/stat.h>
|
||||
|
@ -45,6 +45,8 @@ __RCSID("$NetBSD: apropos-utils.c,v 1.8 2013/01/14 18:01:59 christos Exp $");
|
|||
#include <string.h>
|
||||
#include <util.h>
|
||||
#include <zlib.h>
|
||||
#include <term.h>
|
||||
#undef tab // XXX: manconf.h
|
||||
|
||||
#include "apropos-utils.h"
|
||||
#include "manconf.h"
|
||||
|
@ -711,6 +713,28 @@ run_query_html(sqlite3 *db, query_args *args)
|
|||
return run_query(db, snippet_args, args);
|
||||
}
|
||||
|
||||
/*
|
||||
* underline a string, pager style.
|
||||
*/
|
||||
static char *
|
||||
ul_pager(const char *s)
|
||||
{
|
||||
size_t len;
|
||||
char *dst, *d;
|
||||
|
||||
// a -> _\ba
|
||||
len = strlen(s) * 3 + 1;
|
||||
|
||||
d = dst = emalloc(len);
|
||||
while (*s) {
|
||||
*d++ = '_';
|
||||
*d++ = '\b';
|
||||
*d++ = *s++;
|
||||
}
|
||||
*d = '\0';
|
||||
return dst;
|
||||
}
|
||||
|
||||
/*
|
||||
* callback_pager --
|
||||
* A callback similar to callback_html. It overstrikes the matching text in
|
||||
|
@ -771,12 +795,60 @@ callback_pager(void *data, const char *section, const char *name,
|
|||
}
|
||||
|
||||
psnippet[i] = 0;
|
||||
(orig_data->callback)(orig_data->data, section, name, name_desc, psnippet,
|
||||
psnippet_length);
|
||||
char *ul_section = ul_pager(section);
|
||||
char *ul_name = ul_pager(name);
|
||||
char *ul_name_desc = ul_pager(name_desc);
|
||||
(orig_data->callback)(orig_data->data, ul_section, ul_name,
|
||||
ul_name_desc, psnippet, psnippet_length);
|
||||
free(ul_section);
|
||||
free(ul_name);
|
||||
free(ul_name_desc);
|
||||
free(psnippet);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct term_args {
|
||||
struct orig_callback_data *orig_data;
|
||||
const char *smul;
|
||||
const char *rmul;
|
||||
};
|
||||
|
||||
/*
|
||||
* underline a string, pager style.
|
||||
*/
|
||||
static char *
|
||||
ul_term(const char *s, const struct term_args *ta)
|
||||
{
|
||||
char *dst;
|
||||
|
||||
easprintf(&dst, "%s%s%s", ta->smul, s, ta->rmul);
|
||||
return dst;
|
||||
}
|
||||
|
||||
/*
|
||||
* callback_term --
|
||||
* A callback similar to callback_html. It overstrikes the matching text in
|
||||
* the snippet so that it appears emboldened when viewed using a pager like
|
||||
* more or less.
|
||||
*/
|
||||
static int
|
||||
callback_term(void *data, const char *section, const char *name,
|
||||
const char *name_desc, const char *snippet, size_t snippet_length)
|
||||
{
|
||||
struct term_args *ta = data;
|
||||
struct orig_callback_data *orig_data = ta->orig_data;
|
||||
|
||||
char *ul_section = ul_term(section, ta);
|
||||
char *ul_name = ul_term(name, ta);
|
||||
char *ul_name_desc = ul_term(name_desc, ta);
|
||||
(orig_data->callback)(orig_data->data, ul_section, ul_name,
|
||||
ul_name_desc, snippet, snippet_length);
|
||||
free(ul_section);
|
||||
free(ul_name);
|
||||
free(ul_name_desc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* run_query_pager --
|
||||
* Utility function similar to run_query_html. This function tries to
|
||||
|
@ -795,3 +867,66 @@ run_query_pager(sqlite3 *db, query_args *args)
|
|||
args->callback_data = (void *) &orig_data;
|
||||
return run_query(db, snippet_args, args);
|
||||
}
|
||||
|
||||
static void
|
||||
term_init(int fd, const char *sa[5])
|
||||
{
|
||||
TERMINAL *ti;
|
||||
int error;
|
||||
const char *bold, *sgr0, *smso, *rmso, *smul, *rmul;
|
||||
|
||||
if (ti_setupterm(&ti, NULL, fd, &error) == -1) {
|
||||
bold = sgr0 = NULL;
|
||||
smso = rmso = smul = rmul = "";
|
||||
ti = NULL;
|
||||
} else {
|
||||
bold = ti_getstr(ti, "bold");
|
||||
sgr0 = ti_getstr(ti, "sgr0");
|
||||
if (bold == NULL || sgr0 == NULL) {
|
||||
smso = ti_getstr(ti, "smso");
|
||||
|
||||
if (smso == NULL ||
|
||||
(rmso = ti_getstr(ti, "rmso")) == NULL)
|
||||
smso = rmso = "";
|
||||
bold = sgr0 = NULL;
|
||||
} else
|
||||
smso = rmso = "";
|
||||
|
||||
smul = ti_getstr(ti, "smul");
|
||||
if (smul == NULL || (rmul = ti_getstr(ti, "rmul")) == NULL)
|
||||
smul = rmul = "";
|
||||
}
|
||||
|
||||
sa[0] = estrdup(bold ? bold : smso);
|
||||
sa[1] = estrdup(sgr0 ? sgr0 : rmso);
|
||||
sa[2] = estrdup("...");
|
||||
sa[3] = estrdup(smul);
|
||||
sa[4] = estrdup(rmul);
|
||||
if (ti)
|
||||
del_curterm(ti);
|
||||
}
|
||||
|
||||
/*
|
||||
* run_query_term --
|
||||
* Utility function similar to run_query_html. This function tries to
|
||||
* pre-process the result assuming it will be displayed on a terminal
|
||||
* For this purpose it first calls it's own callback function callback_pager
|
||||
* which then delegates the call to the user supplied callback.
|
||||
*/
|
||||
int
|
||||
run_query_term(sqlite3 *db, query_args *args)
|
||||
{
|
||||
struct orig_callback_data orig_data;
|
||||
struct term_args ta;
|
||||
orig_data.callback = args->callback;
|
||||
orig_data.data = args->callback_data;
|
||||
const char *snippet_args[5];
|
||||
term_init(STDOUT_FILENO, snippet_args);
|
||||
ta.smul = snippet_args[3];
|
||||
ta.rmul = snippet_args[4];
|
||||
ta.orig_data = (void *) &orig_data;
|
||||
|
||||
args->callback = &callback_term;
|
||||
args->callback_data = &ta;
|
||||
return run_query(db, snippet_args, args);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: apropos-utils.h,v 1.4 2012/10/06 15:33:59 wiz Exp $ */
|
||||
/* $NetBSD: apropos-utils.h,v 1.5 2013/01/14 21:26:25 christos Exp $ */
|
||||
/*-
|
||||
* Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>
|
||||
* All rights reserved.
|
||||
|
@ -90,4 +90,5 @@ char *get_dbpath(const char *);
|
|||
int run_query(sqlite3 *, const char *[3], query_args *);
|
||||
int run_query_html(sqlite3 *, query_args *);
|
||||
int run_query_pager(sqlite3 *, query_args *);
|
||||
int run_query_term(sqlite3 *, query_args *);
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: apropos.c,v 1.9 2013/01/14 18:04:58 christos Exp $ */
|
||||
/* $NetBSD: apropos.c,v 1.10 2013/01/14 21:26:25 christos Exp $ */
|
||||
/*-
|
||||
* Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>
|
||||
* All rights reserved.
|
||||
|
@ -31,7 +31,7 @@
|
|||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__RCSID("$NetBSD: apropos.c,v 1.9 2013/01/14 18:04:58 christos Exp $");
|
||||
__RCSID("$NetBSD: apropos.c,v 1.10 2013/01/14 21:26:25 christos Exp $");
|
||||
|
||||
#include <err.h>
|
||||
#include <search.h>
|
||||
|
@ -40,7 +40,6 @@ __RCSID("$NetBSD: apropos.c,v 1.9 2013/01/14 18:04:58 christos Exp $");
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <util.h>
|
||||
#include <term.h>
|
||||
|
||||
#include "apropos-utils.h"
|
||||
#include "sqlite3.h"
|
||||
|
@ -66,40 +65,9 @@ __dead static void usage(void);
|
|||
|
||||
#define _PATH_PAGER "/usr/bin/more -s"
|
||||
|
||||
static int
|
||||
term_init(int fd, const char *sa[3])
|
||||
{
|
||||
if (!isatty(fd))
|
||||
return 0;
|
||||
|
||||
TERMINAL *ti;
|
||||
int error;
|
||||
if (ti_setupterm(&ti, NULL, fd, &error) == -1)
|
||||
return 0;
|
||||
|
||||
const char *rmso = ti_getstr(ti, "rmso");
|
||||
if (rmso == NULL)
|
||||
goto out;
|
||||
|
||||
const char *smso = ti_getstr(ti, "smso");
|
||||
if (smso == NULL)
|
||||
goto out;
|
||||
|
||||
sa[0] = estrdup(smso);
|
||||
sa[1] = estrdup(rmso);
|
||||
sa[2] = estrdup("...");
|
||||
del_curterm(ti);
|
||||
return 1;
|
||||
out:
|
||||
del_curterm(ti);
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
const char *snippet_args[3];
|
||||
query_args args;
|
||||
char *query = NULL; // the user query
|
||||
char *errmsg = NULL;
|
||||
|
@ -205,8 +173,8 @@ main(int argc, char *argv[])
|
|||
args.errmsg = &errmsg;
|
||||
|
||||
|
||||
if (term_init(STDOUT_FILENO, snippet_args))
|
||||
rc = run_query(db, snippet_args, &args);
|
||||
if (isatty(STDOUT_FILENO))
|
||||
rc = run_query_term(db, &args);
|
||||
else
|
||||
rc = run_query_pager(db, &args);
|
||||
|
||||
|
|
Loading…
Reference in New Issue