aboutsummaryrefslogtreecommitdiffstats
path: root/pw.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-05-13 19:40:16 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-05-13 19:40:16 -0700
commit72fd3ddba37a2369d52cdacf4f6b29ffba81a0bd (patch)
tree3ddb83cb3db0e9a283645834cb19ca6bfdf064ff /pw.c
parent7614514f0b7cd17e1f65eb691a1a21c14afa3353 (diff)
downloadpw-72fd3ddba37a2369d52cdacf4f6b29ffba81a0bd.tar.gz
pw-72fd3ddba37a2369d52cdacf4f6b29ffba81a0bd.tar.bz2
pw-72fd3ddba37a2369d52cdacf4f6b29ffba81a0bd.zip
Preparation for diff-showing feature.
Diffstat (limited to 'pw.c')
-rw-r--r--pw.c74
1 files changed, 44 insertions, 30 deletions
diff --git a/pw.c b/pw.c
index ae6545d..d1a652f 100644
--- a/pw.c
+++ b/pw.c
@@ -81,7 +81,8 @@ enum status_flags {
stat_bkgnd = 0x0200, // running in the background
stat_hlite = 0x0400, // running in the background
stat_oneshot = 0x0800, // running in the background
- stat_save = stat_susp | stat_lino | stat_hlite
+ stat_diff = 0x1000, // highlight differences
+ stat_save = stat_susp | stat_lino | stat_hlite | stat_diff
};
typedef enum execode {
@@ -93,7 +94,7 @@ typedef struct pwstate {
int nlines, maxlines;
int hpos;
int vsplit1, vsplit2, vs2pos;
- int hist;
+ int hist, prevhist;
int columns;
int tstop;
unsigned stat;
@@ -384,38 +385,43 @@ static void clreol(int nl)
putchar('\n');
}
-static void hlon(void)
+static void hlon(int *phlite)
{
- printf("\033[7m");
+ if (!*phlite) {
+ printf("\033[7m");
+ *phlite = 1;
+ }
}
-static void hloff(void)
+static void hloff(int *phlite)
{
- printf("\033[m");
+ if (*phlite) {
+ printf("\033[m");
+ *phlite = 0;
+ }
}
-#define with_hl(pw, expr) do { \
- if ((pw)->stat & stat_hlite) \
- hlon(); \
- expr; \
- if ((pw)->stat & stat_hlite) \
- hloff(); \
-} while (0)
-
-static void hlchar(pwstate *pw, int ch)
-{
- with_hl(pw, putchar(ch));
-}
+#define with_hl(on, keep, hlite, expr) \
+ do { \
+ if (on) \
+ hlon(&hlite); \
+ expr; \
+ if (!keep) \
+ hloff(&hlite); \
+ } while (0)
-static void drawline(pwstate *pw, const char *line, int lineno)
+static void drawline(pwstate *pw, const char *line, int lineno, char *diffline)
{
const char *oline = line;
- int olen = (int) dslen(line), len = olen;
+ int olen = (int) dslen(line), len = olen, pos = 0;
int columns = pw->columns;
int vsplit1 = pw->vsplit1;
int vsplit2 = pw->vsplit2;
int vs2pos = pw->vs2pos;
int endmark = 0;
+ int hlpanel = (pw->stat & stat_hlite) != 0;
+ int hldiff = (pw->stat & stat_diff) != 0 && diffline != 0;
+ int hlite = 0;
if (lineno >= 0)
columns -= printf("%3d ", lineno);
@@ -449,7 +455,7 @@ static void drawline(pwstate *pw, const char *line, int lineno)
int i = 0;
if (vsplit1 || vs2pos) {
- hlchar(pw, '|');
+ with_hl (hlpanel, hldiff, hlite, putchar('|'));
i++;
}
@@ -480,7 +486,7 @@ static void drawline(pwstate *pw, const char *line, int lineno)
if (pw->hpos || vsplit1 || vsplit2) {
pos += pw->hpos + 1;
len -= pw->hpos + 1;
- hlchar(pw, '>');
+ with_hl (hlpanel, hldiff, hlite, putchar('>'));
columns--;
}
if (len < columns) {
@@ -489,14 +495,16 @@ static void drawline(pwstate *pw, const char *line, int lineno)
} else {
for (int i = 0; i < columns - 1; i++)
putchar(line[pos + i]);
- hlchar(pw, '<');
+ with_hl (hlpanel, hldiff, hlite, putchar('<'));
putchar('\n');
}
} else {
if (endmark)
- hlchar(pw, '>');
+ with_hl (hlpanel, hldiff, hlite, putchar('>'));
clreol(1);
}
+
+ hloff(&hlite);
}
static void drawstatus(pwstate *pw)
@@ -602,7 +610,7 @@ static void redraw(pwstate *pw)
printf("\r\033[%dA", snaplines[pw->hist]);
if ((pw->stat & stat_lino) == 0) {
for (int i = 0; i < snaplines[pw->hist]; i++)
- drawline(pw, snapshot[pw->hist][i], -1);
+ drawline(pw, snapshot[pw->hist][i], -1, snapshot[pw->prevhist][i]);
} else {
int start = 1, step = 1;
if (pw->stat & stat_htmode) {
@@ -610,7 +618,7 @@ static void redraw(pwstate *pw)
step = -1;
}
for (int i = 0, l = start; i < snaplines[pw->hist]; i++, l += step)
- drawline(pw, snapshot[pw->hist][i], l);
+ drawline(pw, snapshot[pw->hist][i], l, snapshot[pw->prevhist][i]);
}
} else {
clrline(pw->stat);
@@ -1420,7 +1428,7 @@ int main(int argc, char **argv)
snaplines[0] + 1);
snapshot[0][snaplines[0]++] = dsref(line);
clrline(pw.stat);
- drawline(&pw, line, (pw.stat & stat_lino) ? 0: -1);
+ drawline(&pw, line, (pw.stat & stat_lino) ? 0: -1, 0);
drawstatus(&pw);
}
}
@@ -1440,7 +1448,7 @@ int main(int argc, char **argv)
pw.maxlines = ws.ws_row - 1;
}
if (maxed) {
- pw.hist = 0;
+ pw.prevhist = pw.hist = 0;
pw.circbuf = resizebuf(pw.circbuf, oldmax, pw.maxlines);
snapshot[0] = resizebuf(snapshot[0], oldmax, pw.maxlines);
if (pw.nlines > pw.maxlines)
@@ -1620,15 +1628,19 @@ int main(int argc, char **argv)
pw.stat ^= stat_hlite;
pw.stat |= stat_force;
break;
+ case ctrl('d'):
+ pw.stat ^= stat_diff;
+ pw.stat |= stat_force;
+ break;
case 'j':
if (pw.hist > 0) {
- pw.hist--;
+ pw.prevhist = pw.hist--;
pw.stat |= stat_force;
}
break;
case 'k':
if (pw.hist < snhistsize - 1 && snapshot[pw.hist + 1]) {
- pw.hist++;
+ pw.prevhist = pw.hist++;
pw.stat |= stat_force;
}
break;
@@ -1706,6 +1718,8 @@ int main(int argc, char **argv)
free(snapshot[i]);
snapshot[i] = 0;
}
+
+ pw.prevhist = 0;
}
break;
case '#':