1

次のコードのようなものがあります

"file.txt" utf8 <file-reader> [ [ print ] each-line ] with-input-stream* ;

これは の現在のコンテンツに対してうまく機能し、file.txtファイルの終わりに達すると処理 (この場合は印刷など) が終了します。しかし、プロセスがファイルに追加された新しいコンテンツを待機し、これも処理することを望んでいます。つまり、現在のバージョンは Unix を実装していますcatが、それを実行したいのですtail -f

with-input-stream*ドキュメントがストリームが最後に閉じられていないと言うので、私は(アスタリスクに注意して)トリックを行うことを望みました。しかし、私が見逃している何かが他にあるに違いありません。

4

1 に答える 1

2

幸運なことに、私はそのようなユーティリティを少し前に書きました。https://github.com/bjourne/playground-factor/wiki/Tips-and-Tricks-Filesystem#tailing-a-fileを参照してください

USING: accessors io io.encodings.utf8 io.files io.monitors kernel namespaces ;
IN: examples.files.tail

: emit-changes ( monitor -- )
    dup next-change drop
    input-stream get output-stream get stream-copy* flush
    emit-changes ;

: seek-input-end ( -- )
    0 seek-end input-stream get stream>> stream-seek ;

: tail-file ( fname -- )
    [
        dup f <monitor> swap utf8 [
            seek-input-end emit-changes
        ] with-file-reader
    ] with-monitors ;

あなたの問題は、に与えられた引用with-input-stream*が暗黙的にストリームを閉じることです(each-lineそうします)。それがバグかどうかはわかりません。次のような単語を使用すると、ストリームを閉じずにストリーム全体を読み取ることができます。

: my-stream-contents* ( stream -- seq )
    [ [ stream-read1 dup ] curry [ ] ] [ stream-exemplar produce-as nip ] bi ;

それで:

IN: scratchpad "/tmp/foo" utf8 <file-reader> [ my-stream-contents* print ] keep
file contents here
...

--- Data stack:
T{ decoder f ~input-port~ utf8 f }
IN: scratchpad my-stream-contents* print
more file contents here
...
于 2015-11-06T09:38:26.123 に答える