Restrict fifo buffer maximum size

Add a global option (or any other suitable mechanism) which restricts the number of lines kakoune’s fifo buffer keeps, which is 0 by default (meaning no limit, like current behaviour). If maximum number of lines is reached, the older text (lines at the beginning of the buffer) are removed. For correctness, this can also happen in case of user input, although user input isn’t the main problem.

Usecase: When running programs in kakoune’s fifo buffers which generate a lot of text, kakoune keeps all the text, unlike terminal emulators which only keep the last n lines of output (1000 by default in case of foot terminal). Although it takes an order of millions of lines to make an impact on memory usage or speed, it still means you cannot have a process outputting text indefinitely.

Issues: Terminal emulators split lines once text reaches the end of the line, so the logic works even for very long single lines. Kakoune doesn’t do that, so this feature will not help in the case the fifo recieves very long line, although that is unusual case.

What do you guys think about this? Is this reasonable?

1 Like

This sounds good. It can probably be handled like any other kakoune option that can be set in multiple scopes were the lowest one has priority. See :doc options

Then a default global can be set to something like 10 000

You can write this yourself as a plugin.

When a FIFO buffer receives new data at the end of the file, Kakoune triggers the BufReadFifo hook, so you could do something like this to only preserve the last 1000 lines:

hook global BufReadFifo .* %{
    execute-keys -draft gj 1001k Gg <a-d>
}

The execute-keys command works like this:

  • -draft means these keys will be executed in a “draft context”, so they won’t affect the cursor position the user actually sees
  • gj goes to the bottom of the buffer
  • 1001k moves the cursor up 1001 lines
  • Gg selects from that line to the beginning of the buffer
  • <a-d> deletes without putting the deleted text on the clipboard

You might worry that just deleting lines from the buffer doesn’t save any memory since the old data will still be in the undo history. Luckily, when a FIFO buffer receives new data, Kakoune flushes the undo history, so it should do just what you want.

Note that I haven’t actually tested this myself, but I’m pretty sure it’ll do what you want.

4 Likes

You also need x to select whole lines. If the buffer has less than 1001 lines, the command will delete the first line of the buffer.

2 Likes