raz / commit 6e2a4e15

commit
6e2a4e155a173875034f36ca8949a9c49846841c
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
0bc2d2a8
Out.lines: the buffer written at each line feed, through a pipe too

A filter of a stream that comes slowly, tail -f log | rre error | less,
showed nothing until 4 KB of matches had come; grep has --line-buffered
for it. lines(true) writes at each line feed whatever standard output
is, lines(false) only when full; at first, at each line feed only at a
terminal, as before. Tested in OutLines; its proposal is removed.

patch · log

5 files changed, +47 -21

PROPOSALS.md modified +0 -16

@@ -679,19 +679,3 @@
679679 message reads `--budget wants a number from 0 to
680680 9223372036854775807`. The proposal: the message says `a number from
681681 0 up` when `hi` is the largest `I64`; nothing else changes.
682-
683-## `Out` a line at a time through a pipe
684-
685-`Out` writes at each line feed only when standard output is a
686-terminal; through a pipe it waits for 4 KB. A command that filters a
687-stream that comes slowly, `tail -f log | rre error | less`, shows
688-nothing until 4 KB of matches have come. grep has `--line-buffered`
689-for this. The proposal, in `Out`:
690-
691-```
692-proc lines*(on: BOOL); (* write at each line feed, whatever standard output is *)
693-```
694-
695-It breaks no program. Met in rre, which doesn't yet offer
696-`--line-buffered` for want of it (or of an `Out.flush` after every
697-line, which costs a write per line always).

doc/raz-lib.md (history) modified +5 -1

@@ -121,12 +121,16 @@
121121 proc int*(n: I64); (* n in decimal *)
122122 proc hex*(n: U64; digits: INT); (* n in hexadecimal, digits digits at least *)
123123 proc flush*;
124+proc lines*(on: BOOL); (* written at each line feed, or only when full *)
124125 ```
125126
126127 **Out and C's stdio.** `Out` keeps a buffer of `Sys.outSize` bytes,
127128 4 KB on Linux. It writes it out
128129 when it is full, at each line feed when standard output is a terminal,
129-at `Out.flush`, and when the program ends by `Rt.exit`. A lock keeps
130+at `Out.flush`, and when the program ends by `Rt.exit`. `lines(true)`
131+has it written at each line feed whatever standard output is, as a
132+filter of a stream that comes slowly wants through a pipe, as grep's
133+`--line-buffered`; `lines(false)`, only when full, at a terminal too. A lock keeps
130134 what each call writes together when threads write at once. C's `stdio`
131135 keeps a buffer of its own for the same standard output. A program that
132136 writes with both flushes one before writing with the other, or the

lib/Out.raz (history) modified +16 -4

@@ -1,6 +1,7 @@
11 (* Writing text and numbers to standard output, through a buffer of
22 Sys.outSize bytes. The buffer is written out when it is full, at
3- each line feed when standard output is a terminal, by flush, and
3+ each line feed when standard output is a terminal or lines asks it,
4+ by flush, and
45 when the program ends by Rt.exit. C's stdio keeps a buffer of its
56 own: a program that writes with both flushes one before writing
67 with the other. A lock keeps what each call writes together when
@@ -11,7 +12,7 @@
1112 var
1213 buf: [Sys.outSize]U8;
1314 n: INT; (* bytes in buf *)
14- tty: BOOL; (* standard output is a terminal *)
15+ eachLine: BOOL; (* written at each line feed: at first, if standard output is a terminal *)
1516 lock: Lock.Lock;
1617
1718 (* Writes out the buffer, with the lock held. What can't be written is
@@ -37,6 +38,17 @@
3738 Lock.give(ADR(lock));
3839 end;
3940
41+(* Has the buffer written at each line feed, whatever standard output
42+ is, or, if not on, only when it is full: for a program that filters
43+ a stream that comes slowly, through a pipe, as grep's
44+ --line-buffered. *)
45+proc lines*(on: BOOL);
46+begin
47+ Lock.take(ADR(lock));
48+ eachLine := on;
49+ Lock.give(ADR(lock));
50+end;
51+
4052 (* Writes the bytes of s. *)
4153 proc str*(s: []U8);
4254 var i: INT;
@@ -46,7 +58,7 @@
4658 if n = LEN(buf) then out; end;
4759 buf[n] := s[i];
4860 INC(n);
49- if (s[i] = '\n') and tty then out; end;
61+ if (s[i] = '\n') and eachLine then out; end;
5062 end;
5163 Lock.give(ADR(lock));
5264 end;
@@ -100,6 +112,6 @@
100112 end;
101113
102114 begin
103- tty := Sys.isTerminal(Sys.stdout);
115+ eachLine := Sys.isTerminal(Sys.stdout);
104116 _ := Rt.atExit(flush);
105117 end Out.

test/run/OutLines.expect (history) added +6 -0

@@ -0,0 +1,6 @@
1+first
2+past Out
3+past Out again
4+second, half and the rest
5+past Out, first
6+kept

test/run/OutLines.raz (history) added +20 -0

@@ -0,0 +1,20 @@
1+module OutLines;
2+(* Out.lines(true) writes each line as it ends, though standard output
3+ is no terminal here: the lines come in order with what is written
4+ past Out. lines(false) keeps them until the end. *)
5+import Out, Sys;
6+
7+proc main*;
8+begin
9+ Out.lines(true);
10+ Out.str("first\n");
11+ _ := Sys.write(Sys.stdout, "past Out\n");
12+ Out.str("second, half");
13+ _ := Sys.write(Sys.stdout, "past Out again\n");
14+ Out.str(" and the rest\n");
15+ Out.lines(false);
16+ Out.str("kept\n");
17+ _ := Sys.write(Sys.stdout, "past Out, first\n");
18+end;
19+
20+end OutLines.