diff options
Diffstat (limited to 'pw.c')
-rw-r--r-- | pw.c | 45 |
1 files changed, 35 insertions, 10 deletions
@@ -40,7 +40,8 @@ enum status_flags { stat_ttmode = 16, // tail trigger mode stat_trgrd = 32, // triggered flag stat_grep = 64, // grep mode - stat_force = 128 // force refresh even if clean + stat_force = 128, // force refresh even if clean + stat_lino = 256 // render line numbers }; typedef struct grep { @@ -247,10 +248,13 @@ static void clreol(int nl) putchar('\n'); } -static void drawline(const char *line, unsigned hpos, int columns) +static void drawline(const char *line, unsigned hpos, int columns, int lineno) { size_t len = dslen(line); + if (lineno > 0) + columns -= printf("%3d ", lineno); + if ((size_t) hpos <= len) { if (hpos) { line += hpos; @@ -339,6 +343,8 @@ static void freebuf(char **buf, size_t size) static unsigned redraw(char **circbuf, int nlines, unsigned hpos, unsigned hist, int columns, unsigned stat, char *cmd) { + int updln = 0; + if ((stat & (stat_dirty | stat_susp)) == stat_dirty && (stat & (stat_htmode | stat_trgrd)) != stat_htmode && (stat & (stat_ttmode | stat_trgrd)) != stat_ttmode) @@ -351,21 +357,36 @@ static unsigned redraw(char **circbuf, int nlines, unsigned hpos, unsigned hist, memmove(snaplines + 1, snaplines, sizeof *snaplines * (snhistsize - 1)); snapshot[0] = calloc(sizeof *snapshot[0], nlines); snaplines[0] = nlines; - printf("\r\033[%dA", nlines); - for (int i = 0; i < nlines; i++) { + updln = 1; + for (int i = 0; i < nlines; i++) snapshot[0][i] = dsref(circbuf[i]); - drawline(snapshot[hist][i], hpos, columns); - } stat &= ~(stat_dirty | stat_trgrd); + updln = 1; } else if ((stat & stat_force)) { - printf("\r\033[%dA", snaplines[hist]); - for (int i = 0; i < snaplines[hist]; i++) - drawline(snapshot[hist][i], hpos, columns); stat &= ~stat_force; + updln = 1; + } + + if (updln) { + printf("\r\033[%dA", snaplines[hist]); + if ((stat & stat_lino) == 0) { + for (int i = 0; i < snaplines[hist]; i++) + drawline(snapshot[hist][i], hpos, columns, -1); + } else { + int start = 1, step = 1; + if (stat & stat_htmode) { + start = snaplines[hist]; + step = -1; + } + for (int i = 0, l = start; i < snaplines[hist]; i++, l += step) + drawline(snapshot[hist][i], hpos, columns, l); + } } else { clrline(); } + drawstatus(hist, columns, stat, cmd); + return stat; } @@ -789,7 +810,7 @@ int main(int argc, char **argv) snapshot[0] = resizebuf(snapshot[0], snaplines[0], snaplines[0] + 1); snapshot[0][snaplines[0]++] = dsref(line); clrline(); - drawline(line, hpos, columns); + drawline(line, hpos, columns, -1); drawstatus(hist, columns, stat, curcmd); } } @@ -1001,6 +1022,10 @@ int main(int argc, char **argv) } } break; + case '#': + stat ^= stat_lino; + stat |= stat_force; + break; case '0': if (cmdcount == UINT_MAX) { hpos = 0; |