Debugging code cleanup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
diff --git a/linenoise.c b/linenoise.c
index f8c4283..a5c5ee7 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -140,6 +140,25 @@ static void linenoiseAtExit(void);
int linenoiseHistoryAdd(const char *line);
static void refreshLine(struct linenoiseState *l);
+/* Debugging macro. */
+#if 0
+FILE *lndebug_fp = NULL;
+#define lndebug(...) \
+ do { \
+ if (lndebug_fp == NULL) { \
+ lndebug_fp = fopen("/tmp/lndebug.txt","a"); \
+ fprintf(lndebug_fp, \
+ "[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", \
+ (int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos, \
+ (int)l->maxrows,old_rows); \
+ } \
+ fprintf(lndebug_fp, ", " __VA_ARGS__); \
+ fflush(lndebug_fp); \
+ } while (0)
+#else
+#define lndebug(fmt, ...)
+#endif
+
/* ======================= Low level terminal handling ====================== */
/* Set if to use or not the multi line mode. */
@@ -409,36 +428,24 @@ static void refreshMultiLine(struct linenoiseState *l) {
/* Update maxrows if needed. */
if (rows > (int)l->maxrows) l->maxrows = rows;
-#ifdef LN_DEBUG
- FILE *fp = fopen("/tmp/debug.txt","a");
- fprintf(fp,"[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d",
- (int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos,(int)l->maxrows,old_rows);
-#endif
-
/* First step: clear all the lines used before. To do so start by
* going to the last row. */
abInit(&ab);
if (old_rows-rpos > 0) {
-#ifdef LN_DEBUG
- fprintf(fp,", go down %d", old_rows-rpos);
-#endif
+ lndebug("go down %d", old_rows-rpos);
snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
abAppend(&ab,seq,strlen(seq));
}
/* Now for every row clear it, go up. */
for (j = 0; j < old_rows-1; j++) {
-#ifdef LN_DEBUG
- fprintf(fp,", clear+up");
-#endif
+ lndebug("clear+up");
snprintf(seq,64,"\x1b[0G\x1b[0K\x1b[1A");
abAppend(&ab,seq,strlen(seq));
}
/* Clean the top line. */
-#ifdef LN_DEBUG
- fprintf(fp,", clear");
-#endif
+ lndebug("clear");
snprintf(seq,64,"\x1b[0G\x1b[0K");
abAppend(&ab,seq,strlen(seq));
@@ -452,9 +459,7 @@ static void refreshMultiLine(struct linenoiseState *l) {
l->pos == l->len &&
(l->pos+plen) % l->cols == 0)
{
-#ifdef LN_DEBUG
- fprintf(fp,", <newline>");
-#endif
+ lndebug("<newline>");
abAppend(&ab,"\n",1);
snprintf(seq,64,"\x1b[0G");
abAppend(&ab,seq,strlen(seq));
@@ -464,31 +469,23 @@ static void refreshMultiLine(struct linenoiseState *l) {
/* Move cursor to right position. */
rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */
-#ifdef LN_DEBUG
- fprintf(fp,", rpos2 %d", rpos2);
-#endif
+ lndebug("rpos2 %d", rpos2);
+
/* Go up till we reach the expected positon. */
if (rows-rpos2 > 0) {
-#ifdef LN_DEBUG
- fprintf(fp,", go-up %d", rows-rpos2);
-#endif
+ lndebug("go-up %d", rows-rpos2);
snprintf(seq,64,"\x1b[%dA", rows-rpos2);
abAppend(&ab,seq,strlen(seq));
}
+
/* Set column. */
-#ifdef LN_DEBUG
- fprintf(fp,", set col %d", 1+((plen+(int)l->pos) % (int)l->cols));
-#endif
+ lndebug("set col %d", 1+((plen+(int)l->pos) % (int)l->cols));
snprintf(seq,64,"\x1b[%dG", 1+((plen+(int)l->pos) % (int)l->cols));
abAppend(&ab,seq,strlen(seq));
+ lndebug("\n");
l->oldpos = l->pos;
-#ifdef LN_DEBUG
- fprintf(fp,"\n");
- fclose(fp);
-#endif
-
if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */
abFree(&ab);
}