Permalink
Browse files

Highlight escape sequences in input line

  • Loading branch information...
1 parent 8040568 commit 2b3007c2cf963d1e92eefda03ad1b5380b1bf53d Sound and Fury committed Aug 21, 2010
Showing with 111 additions and 4 deletions.
  1. +2 −0 Makefile
  2. +11 −0 bits.c
  3. +1 −0 bits.h
  4. +72 −2 buffer.c
  5. +2 −0 buffer.h
  6. +1 −1 colour.h
  7. +19 −0 ttyesc.c
  8. +3 −1 ttyesc.h
View
2 Makefile
@@ -35,6 +35,8 @@ README: readme.htm
%.o: %.c %.h
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
+ttyesc.o: bits.h
+
irc.o: irc.c irc.h bits.h buffer.h colour.h config.h numeric.h
bits.o: bits.c bits.h ttyesc.h colour.h config.h
View
11 bits.c
@@ -134,6 +134,17 @@ int wordline(char *msg, int x, char **out)
return(x);
}
+void append_char(char **buf, int *l, int *i, char c)
+{
+ (*buf)[(*i)++]=c;
+ if((*i)>=(*l))
+ {
+ *l=*l*2;
+ *buf=(char *)realloc(*buf, *l);
+ }
+ (*buf)[*i]=0;
+}
+
void crush(char **buf, int len)
{
if(strlen(*buf)>len)
View
1 bits.h
@@ -21,5 +21,6 @@
char * fgetl(FILE *); // gets a line of string data; returns a malloc-like pointer (preserves trailing \n)
int wordline(char *, int x, char **); // prepares a string for printing, breaking lines in between words
+void append_char(char **buf, int *l, int *i, char c); // adds a character to a string buffer in heap (and realloc()s if needed)
void crush(char **buf, int len);
void scrush(char **buf, int len);
View
74 buffer.c
@@ -240,15 +240,85 @@ void in_update(char *inp)
if(ino>width-2)
{
int off=20*max((ino+27-width)/20, 0);
- printf("%.10s ... %s" CLR, inp, inp+off+10);
+ char l[11];
+ sprintf(l, "%.10s", inp);
+ char *lh=highlight(lh);
+ char *rh=highlight(inp+off+10);
+ printf("%s ... %s" CLR, lh, rh);
+ free(lh);
+ free(rh);
}
else
{
- printf("%s" CLR, inp?inp:"");
+ char *h=highlight(inp?inp:"");
+ printf("%s" CLR, h);
+ free(h);
}
fflush(stdout);
}
+char *highlight(char *src)
+{
+ int l=80;
+ char *rv=(char *)malloc(l);
+ int i=0;
+ while(*src)
+ {
+ if(*src=='\\')
+ {
+ switch(src[1])
+ {
+ case 'n':
+ s_setcol(7, 0, 1, 0, &rv, &l, &i);
+ append_char(&rv, &l, &i, '\\');
+ append_char(&rv, &l, &i, 'n');
+ s_setcol(7, 0, 0, 0, &rv, &l, &i);
+ src++;
+ break;
+ case 'r':
+ s_setcol(7, 0, 1, 0, &rv, &l, &i);
+ append_char(&rv, &l, &i, '\\');
+ append_char(&rv, &l, &i, 'r');
+ s_setcol(7, 0, 0, 0, &rv, &l, &i);
+ src++;
+ break;
+ case '\\':
+ s_setcol(3, 0, 1, 0, &rv, &l, &i);
+ append_char(&rv, &l, &i, '\\');
+ append_char(&rv, &l, &i, '\\');
+ s_setcol(7, 0, 0, 0, &rv, &l, &i);
+ src++;
+ break;
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ s_setcol(6, 0, 1, 0, &rv, &l, &i);
+ append_char(&rv, &l, &i, '\\');
+ append_char(&rv, &l, &i, *++src);
+ int digits=0;
+ while(isdigit(src[1])&&(src[1]<'8')&&(++digits<3))
+ {
+ append_char(&rv, &l, &i, *++src);
+ }
+ s_setcol(7, 0, 0, 0, &rv, &l, &i);
+ break;
+ default:
+ s_setcol(1, 0, 1, 0, &rv, &l, &i);
+ append_char(&rv, &l, &i, '\\');
+ s_setcol(7, 0, 0, 0, &rv, &l, &i);
+ break;
+ }
+ }
+ else
+ {
+ append_char(&rv, &l, &i, *src);
+ }
+ src++;
+ }
+ return(rv);
+}
+
int w_buf_print(int buf, colour lc, char *lt, char *lead)
{
char *ltd=strdup(lt);
View
2 buffer.h
@@ -15,6 +15,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
+#include <ctype.h>
#include "ttyesc.h"
#include "colour.h"
#include "bits.h"
@@ -62,3 +63,4 @@ int redraw_buffer(void);
int buf_print(int buf, colour lc, char *lt); // don't include trailing \n, because buf_print appends CLR \n
int w_buf_print(int buf, colour lc, char *lt, char *lead);
void in_update(char *inp);
+char *highlight(char *src); // use ANSI-colours to highlight \escapes. Returns a malloc-like pointer
View
2 colour.h
@@ -1,3 +1,4 @@
+#pragma once
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010 Edward Cree
@@ -9,7 +10,6 @@
#include <stdlib.h>
#include <stdbool.h>
#include "ttyesc.h"
-#pragma once
typedef struct
{
View
19 ttyesc.c
@@ -28,6 +28,25 @@ int setcol(int fore, int back, bool hi, bool ul)
return(0);
}
+int s_setcol(int fore, int back, bool hi, bool ul, char **rv, int *l, int *i)
+{
+ if((fore<0)||(fore>7))
+ return(1);
+ if((back<0)||(back>7))
+ return(2);
+ append_char(rv, l, i, '\033');
+ append_char(rv, l, i, '[');
+ append_char(rv, l, i, '0'+(hi?1:0)+(ul?4:0));
+ append_char(rv, l, i, ';');
+ append_char(rv, l, i, '3');
+ append_char(rv, l, i, '0'+fore);
+ append_char(rv, l, i, ';');
+ append_char(rv, l, i, '4');
+ append_char(rv, l, i, '0'+back);
+ append_char(rv, l, i, 'm');
+ return(0);
+}
+
int resetcol(void)
{
return(setcol(7, 0, false, false));
View
4 ttyesc.h
@@ -1,11 +1,11 @@
+#pragma once
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010 Edward Cree
See quirc.c for license information
ttyesc: ANSI Terminal Escape Sequences
*/
-#pragma once
#define CLS "\033[2J" // You might recognise these two names...
#define LOCATE "\033[%d;%dH" // as being very similar to some basic keywords... :p
@@ -15,7 +15,9 @@
#include <stdio.h>
#include <stdbool.h>
+#include "bits.h"
int setcol(int fore, int back, bool hi, bool ul); // sets the text colour
+int s_setcol(int fore, int back, bool hi, bool ul, char **rv, int *l, int *i); // writes a setcol-like string with append_char (see bits.h)
int resetcol(void); // default setcol() values
int settitle(char *newtitle); // sets the window title if running in a term in a window system (eg. xterm)

0 comments on commit 2b3007c

Please sign in to comment.