diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2022-05-01 03:55:31 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2022-05-01 03:55:31 -0700 |
commit | df87820818baf166eab847238118372d0d7da87c (patch) | |
tree | 2e0343f5eb696e5470f3f43b2e38b1dfedffb794 | |
parent | 76eff899945b983a30611588ea8ff6fd2ad265ca (diff) | |
download | pw-df87820818baf166eab847238118372d0d7da87c.tar.gz pw-df87820818baf166eab847238118372d0d7da87c.tar.bz2 pw-df87820818baf166eab847238118372d0d7da87c.zip |
New # command to toggle line numbers.
-rw-r--r-- | pw.1 | 11 | ||||
-rw-r--r-- | pw.c | 45 |
2 files changed, 43 insertions, 13 deletions
@@ -373,6 +373,11 @@ opened position must be filled by a line of data from standard input. There is no way to reduce the size dynamically. Resizing is limited to the terminal height, if the height is known. +.IP \fB#\fI +Toggle the display of line numbers. In tail-referenced trigger mode, +the lines are numbered from bottom to top. In all other situations, +top to bottom. + .IP \fBCtrl-Z\fI Suspends .IR pw , @@ -402,13 +407,13 @@ and .BR Ctrl-N . .IP "\fB:w\fP \fIfilename\fP" -Write the current contents of the display into the specified file. +Write the currently displayed lines into the specified file. .IP "\fB:a\fP \fIfilename\fP" -Append the current contents of the display into the specified file. +Append the currently displayed lines into the specified file. .IP "\fB:!\fP \fIfilename\fP" -Pipe the current contents of of the display into the specified command. +Pipe the currently displayed lines into the specified command. .IP "\fB:g\fP \fIpattern\fP, \fB:v\fP \fIpattern\fP" Push a @@ -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; |