aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pw.111
-rw-r--r--pw.c45
2 files changed, 43 insertions, 13 deletions
diff --git a/pw.1 b/pw.1
index 95be260..96925ed 100644
--- a/pw.1
+++ b/pw.1
@@ -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
diff --git a/pw.c b/pw.c
index 69898e1..9e2a32a 100644
--- a/pw.c
+++ b/pw.c
@@ -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;