From 6e2a4e155a173875034f36ca8949a9c49846841c Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 20:14:36 +0000 Subject: [PATCH] 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. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 7b21f21..67abb19 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -679,19 +679,3 @@ message reads `--budget wants a number from 0 to 9223372036854775807`. The proposal: the message says `a number from 0 up` when `hi` is the largest `I64`; nothing else changes. - -## `Out` a line at a time through a pipe - -`Out` writes at each line feed only when standard output is a -terminal; through a pipe it waits for 4 KB. A command that filters a -stream that comes slowly, `tail -f log | rre error | less`, shows -nothing until 4 KB of matches have come. grep has `--line-buffered` -for this. The proposal, in `Out`: - -``` -proc lines*(on: BOOL); (* write at each line feed, whatever standard output is *) -``` - -It breaks no program. Met in rre, which doesn't yet offer -`--line-buffered` for want of it (or of an `Out.flush` after every -line, which costs a write per line always). diff --git a/doc/raz-lib.md b/doc/raz-lib.md index f00b066..00041de 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -121,12 +121,16 @@ proc int*(n: I64); (* n in decimal *) proc hex*(n: U64; digits: INT); (* n in hexadecimal, digits digits at least *) proc flush*; +proc lines*(on: BOOL); (* written at each line feed, or only when full *) ``` **Out and C's stdio.** `Out` keeps a buffer of `Sys.outSize` bytes, 4 KB on Linux. It writes it out when it is full, at each line feed when standard output is a terminal, -at `Out.flush`, and when the program ends by `Rt.exit`. A lock keeps +at `Out.flush`, and when the program ends by `Rt.exit`. `lines(true)` +has it written at each line feed whatever standard output is, as a +filter of a stream that comes slowly wants through a pipe, as grep's +`--line-buffered`; `lines(false)`, only when full, at a terminal too. A lock keeps what each call writes together when threads write at once. C's `stdio` keeps a buffer of its own for the same standard output. A program that writes with both flushes one before writing with the other, or the diff --git a/lib/Out.raz b/lib/Out.raz index b2b3a3f..60d2a73 100644 --- a/lib/Out.raz +++ b/lib/Out.raz @@ -1,6 +1,7 @@ (* Writing text and numbers to standard output, through a buffer of Sys.outSize bytes. The buffer is written out when it is full, at - each line feed when standard output is a terminal, by flush, and + each line feed when standard output is a terminal or lines asks it, + by flush, and when the program ends by Rt.exit. C's stdio keeps a buffer of its own: a program that writes with both flushes one before writing with the other. A lock keeps what each call writes together when @@ -11,7 +12,7 @@ var buf: [Sys.outSize]U8; n: INT; (* bytes in buf *) - tty: BOOL; (* standard output is a terminal *) + eachLine: BOOL; (* written at each line feed: at first, if standard output is a terminal *) lock: Lock.Lock; (* Writes out the buffer, with the lock held. What can't be written is @@ -37,6 +38,17 @@ Lock.give(ADR(lock)); end; +(* Has the buffer written at each line feed, whatever standard output + is, or, if not on, only when it is full: for a program that filters + a stream that comes slowly, through a pipe, as grep's + --line-buffered. *) +proc lines*(on: BOOL); +begin + Lock.take(ADR(lock)); + eachLine := on; + Lock.give(ADR(lock)); +end; + (* Writes the bytes of s. *) proc str*(s: []U8); var i: INT; @@ -46,7 +58,7 @@ if n = LEN(buf) then out; end; buf[n] := s[i]; INC(n); - if (s[i] = '\n') and tty then out; end; + if (s[i] = '\n') and eachLine then out; end; end; Lock.give(ADR(lock)); end; @@ -100,6 +112,6 @@ end; begin - tty := Sys.isTerminal(Sys.stdout); + eachLine := Sys.isTerminal(Sys.stdout); _ := Rt.atExit(flush); end Out. diff --git a/test/run/OutLines.expect b/test/run/OutLines.expect new file mode 100644 index 0000000..c4593a5 --- /dev/null +++ b/test/run/OutLines.expect @@ -0,0 +1,6 @@ +first +past Out +past Out again +second, half and the rest +past Out, first +kept diff --git a/test/run/OutLines.raz b/test/run/OutLines.raz new file mode 100644 index 0000000..50be240 --- /dev/null +++ b/test/run/OutLines.raz @@ -0,0 +1,20 @@ +module OutLines; +(* Out.lines(true) writes each line as it ends, though standard output + is no terminal here: the lines come in order with what is written + past Out. lines(false) keeps them until the end. *) +import Out, Sys; + +proc main*; +begin + Out.lines(true); + Out.str("first\n"); + _ := Sys.write(Sys.stdout, "past Out\n"); + Out.str("second, half"); + _ := Sys.write(Sys.stdout, "past Out again\n"); + Out.str(" and the rest\n"); + Out.lines(false); + Out.str("kept\n"); + _ := Sys.write(Sys.stdout, "past Out, first\n"); +end; + +end OutLines.