shell-pool / shpool

Think tmux, then aim... lower
Apache License 2.0
1.03k stars 13 forks source link

add support for dumping motd on session creation #6

Closed ethanpailes closed 2 months ago

ethanpailes commented 3 months ago

This patch adds support for one of the two motd modes that I plan to support. In this basic "dump" mode, we only display the message of the day when a user first creates a session. Since we are doing it right at the beginning, we can be confident we won't be mangling the restore buffer. Directly injecting the message of the day into the output stream is much more fraught on reconnect.

I was hoping that this would be a simple change, but it would up being quite a bit more involved than I had hoped. The main reason for this is the interaction between the prompt prefix injection and message of the day injection. We need to make sure that motd injection happens after the prompt prefix shell code has finished executing so that it does not get clobbered, but unfortunately this is not as easy as just doing one after the other.

With the naive approach there is a race condition where first we write the prefix injection shell code to the shell process, then we write out the message of the day, then the shell finishes processing the shell code and issues the terminal reset code emitted by the clear command at the end of the prompt prefix shell code.

To deal with this, I started scanning for the control code emitted by clear in the output stream. I was able to re-use the efficient trie I wrote for the keybindings engine to do this.

This addresses the first part of #5, but the issue is not resolved yet. I also realized that two different config variables to control this behavior leaves too much room for weird states. In particular, I worried about doing a direct dump during reattach. I decided it is better to do everything via one variable, where the mode implies when we actually show the motd.

ethanpailes commented 3 months ago

It turns out terminfo is WTFPL licenced, which is not allowed for google projects. I opened https://github.com/meh/rust-terminfo/issues/41 asking the author to dual licence the crate to make things a bit easier. If there is no movement on that issue for a little bit I can switch to https://crates.io/crates/termini

ethanpailes commented 3 months ago

termini turns out to be slightly annoying to integrate since it does not have a clone derivation: https://github.com/pascalkuthe/termini/pull/3