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.
| @@ -679,19 +679,3 @@ | ||
| 679 | 679 | message reads `--budget wants a number from 0 to |
| 680 | 680 | 9223372036854775807`. The proposal: the message says `a number from |
| 681 | 681 | 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). |
| @@ -121,12 +121,16 @@ | ||
| 121 | 121 | proc int*(n: I64); (* n in decimal *) |
| 122 | 122 | proc hex*(n: U64; digits: INT); (* n in hexadecimal, digits digits at least *) |
| 123 | 123 | proc flush*; |
| 124 | +proc lines*(on: BOOL); (* written at each line feed, or only when full *) | |
| 124 | 125 | ``` |
| 125 | 126 | |
| 126 | 127 | **Out and C's stdio.** `Out` keeps a buffer of `Sys.outSize` bytes, |
| 127 | 128 | 4 KB on Linux. It writes it out |
| 128 | 129 | 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 | |
| 130 | 134 | what each call writes together when threads write at once. C's `stdio` |
| 131 | 135 | keeps a buffer of its own for the same standard output. A program that |
| 132 | 136 | writes with both flushes one before writing with the other, or the |
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | (* Writing text and numbers to standard output, through a buffer of |
| 2 | 2 | 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 | |
| 4 | 5 | when the program ends by Rt.exit. C's stdio keeps a buffer of its |
| 5 | 6 | own: a program that writes with both flushes one before writing |
| 6 | 7 | with the other. A lock keeps what each call writes together when |
| @@ -11,7 +12,7 @@ | ||
| 11 | 12 | var |
| 12 | 13 | buf: [Sys.outSize]U8; |
| 13 | 14 | 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 *) | |
| 15 | 16 | lock: Lock.Lock; |
| 16 | 17 | |
| 17 | 18 | (* Writes out the buffer, with the lock held. What can't be written is |
| @@ -37,6 +38,17 @@ | ||
| 37 | 38 | Lock.give(ADR(lock)); |
| 38 | 39 | end; |
| 39 | 40 | |
| 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 | + | |
| 40 | 52 | (* Writes the bytes of s. *) |
| 41 | 53 | proc str*(s: []U8); |
| 42 | 54 | var i: INT; |
| @@ -46,7 +58,7 @@ | ||
| 46 | 58 | if n = LEN(buf) then out; end; |
| 47 | 59 | buf[n] := s[i]; |
| 48 | 60 | INC(n); |
| 49 | - if (s[i] = '\n') and tty then out; end; | |
| 61 | + if (s[i] = '\n') and eachLine then out; end; | |
| 50 | 62 | end; |
| 51 | 63 | Lock.give(ADR(lock)); |
| 52 | 64 | end; |
| @@ -100,6 +112,6 @@ | ||
| 100 | 112 | end; |
| 101 | 113 | |
| 102 | 114 | begin |
| 103 | - tty := Sys.isTerminal(Sys.stdout); | |
| 115 | + eachLine := Sys.isTerminal(Sys.stdout); | |
| 104 | 116 | _ := Rt.atExit(flush); |
| 105 | 117 | end Out. |
| @@ -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 |
| @@ -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. |