Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Color output with ANSI codes #14

Closed
wants to merge 1 commit into from

2 participants

@agimenez

Colorize special tokens (#hashtags, @mentions, urls) with ANSI codes.
Introduce new configuration variable (colorize).
Check if output is a TTY to avoid redirecting/piping garbage.

Signed-off-by: L. Alberto Giménez agimenez@sysvalve.es


I gave a shot to ncurses but its ending method clears out the screen and I didn't find a way to disable that behaviour (if it is possible at all). Also, implementation with ANSI color codes is simpler and not less standard.

@agimenez agimenez Color output with ANSI codes
Colorize special tokens (#hashtags, @mentions, urls) with ANSI codes.
Introduce new configuration variable (colorize).
Check if output is a TTY to avoid redirecting/piping garbage.

Signed-off-by: L. Alberto Giménez <agimenez@sysvalve.es>
9933d4e
@agimenez

Hi Greg,

Care to take a look at this pull request, please?

@gregkh
Owner

Instead of hard-coding ANSI colorstrings, how about using ncurses, or other libraries to access these properly, so it's portable to other terminal types?

@gregkh gregkh closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 19, 2011
  1. @agimenez

    Color output with ANSI codes

    agimenez authored
    Colorize special tokens (#hashtags, @mentions, urls) with ANSI codes.
    Introduce new configuration variable (colorize).
    Check if output is a TTY to avoid redirecting/piping garbage.
    
    Signed-off-by: L. Alberto Giménez <agimenez@sysvalve.es>
This page is out of date. Refresh to see the latest.
Showing with 77 additions and 2 deletions.
  1. +70 −2 bti.c
  2. +1 −0  bti.h
  3. +6 −0 config.c
View
72 bti.c
@@ -44,6 +44,17 @@
#define zalloc(size) calloc(size, 1)
+#define COLOR_RESET "\033[0m"
+
+/* @mentions */
+#define COLOR_GREEN "\033[32m"
+
+/* #hashtags */
+#define COLOR_BLUE "\033[34m"
+
+/* http[s]://urls */
+#define COLOR_MAGENTA "\033[35m"
+
#define dbg(format, arg...) \
do { \
if (debug) \
@@ -54,6 +65,16 @@
int debug;
+/*
+ * Kind of token that we are in when colorizing output
+ */
+enum color_state {
+ NO_COLOR,
+ MENTION,
+ HASHTAG = MENTION,
+ URL
+};
+
static void display_help(void)
{
fprintf(stdout, "bti - send tweet to twitter or identi.ca\n"
@@ -286,14 +307,61 @@ static CURL *curl_init(void)
return curl;
}
+/*
+ * Handle the string keychars to enable colors
+ */
+static void color_output(char *str)
+{
+ enum color_state in_color = NO_COLOR;
+
+ while (*str) {
+ if (*str == '@') {
+ in_color = MENTION;
+ fputs(COLOR_GREEN, stdout);
+ } else if (*str == '#') {
+ in_color = HASHTAG;
+ fputs(COLOR_BLUE, stdout);
+ } else if (*str == 'h') {
+ if (!strncmp(str, "http://", 7) || !strncmp(str, "https://", 8)) {
+ in_color = URL;
+ fputs(COLOR_MAGENTA, stdout);
+ }
+ } else if ((in_color == URL) && isblank(*str)) {
+ fputs(COLOR_RESET, stdout);
+ in_color = NO_COLOR;
+ } else if ((in_color == HASHTAG || in_color == MENTION) &&
+ !isalnum(*str) && (*str != '_')) {
+ fputs(COLOR_RESET, stdout);
+ in_color = NO_COLOR;
+ }
+ putchar(*str++);
+ }
+
+ puts(COLOR_RESET);
+
+}
+
/* The final place data is sent to the screen/pty/tty */
static void bti_output_line(struct session *session, xmlChar *user,
xmlChar *id, xmlChar *created, xmlChar *text)
{
+ char *str;
+
+ str = zalloc(1024);
if (session->verbose)
- printf("[%s] {%s} (%.16s) %s\n", user, id, created, text);
+ snprintf(str, 1024, "[@%s] {%s} (%.16s) %s", user, id, created, text);
else
- printf("[%s] %s\n", user, text);
+ snprintf(str, 1024, "[@%s] %s", user, text);
+
+ str[1023] = '\0';
+
+ if (!session->colorize || !isatty(1)) {
+ puts(str);
+ } else {
+ color_output(str);
+ }
+
+ free(str);
}
static void parse_statuses(struct session *session,
View
1  bti.h
@@ -68,6 +68,7 @@ struct session {
enum host host;
enum action action;
void *readline_handle;
+ int colorize;
char *(*readline)(const char *);
};
View
6 config.c
@@ -268,6 +268,11 @@ static int shrink_urls_callback(struct session *session, char *value)
return session_bool(&session->shrink_urls, value);
}
+static int colorize_callback(struct session *session, char *value)
+{
+ return session_bool(&session->colorize, value);
+}
+
/*
* List of all of the config file options.
*
@@ -292,6 +297,7 @@ static struct config_function config_table[] = {
{ "action", action_callback },
{ "verbose", verbose_callback },
{ "shrink-urls", shrink_urls_callback },
+ { "colorize", colorize_callback },
{ NULL, NULL }
};
Something went wrong with that request. Please try again.