larkery / zsh-histdb

A slightly better history for zsh
MIT License
1.25k stars 74 forks source link

What is the purpose of buffering output when in a pipeline? #98

Closed mafredri closed 3 years ago

mafredri commented 3 years ago

Hi, I've been looking at https://github.com/larkery/zsh-histdb/commit/01452348e15536b3efad83e6d9ae7979c9f46bed and https://github.com/larkery/zsh-histdb/commit/a26afec15aa2586389aadb70fd005c07f32457e4 and trying to figure out why this is needed. But I haven't figured it out.

The issue I've run into is that it's not possible to exit the pipeline early. Consider the following command:

histdb --limit 99999 | head -n 10

The | buffer in this case (via temp file) prevents early exit via head.

This can easily be demonstrated by artificially slowing down the tabulation command:

_histdb_tabulate_cmd() {
    while read -r line; do
        print $line
        sleep 0.1
    done
}
HISTDB_TABULATE_CMD=(_histdb_tabulate_cmd)

It will take forever to complete. Removing | buffer (or replacing it with buffer() { cat }) produces the expected result (exit within the second).

mafredri commented 3 years ago

As soon as I wrote it up, I believe I figured it out. It's purpose seems to be to avoid triggering the timeout defined in _histdb_query, yes? Although I still don't fully understand the purpose of the temp file, I created #99 to fix the issue defined here.

larkery commented 3 years ago

It is to prevent the query timeout. There might be a better way to do it. Your PR looks OK - I can't recall if there is any subtlety to having it the way round it it at the moment.