Provide an interface for tools to implement SIGINFO handling. - #963
Provide an interface for tools to implement SIGINFO handling.#963grynspan wants to merge 12 commits into
SIGINFO handling.#963Conversation
e9b6474 to
a205653
Compare
| /// user as if it is not implemented. | ||
| nonisolated(nonsending) func provideInfo() async | ||
| #else | ||
| func provideInfo() async |
There was a problem hiding this comment.
This feels a bit repetitive ... InfoProvidingCommand.provideInfo. Would InfoProvidingCommand.information or InfoProvidingCommand.provide might be nicer.
There was a problem hiding this comment.
provide() would be too vague outside the context of the protocol. @rauhul suggested just info().
| siginfoHandler?.register() | ||
| defer { | ||
| siginfoHandler?.unregister() | ||
| } |
There was a problem hiding this comment.
Given that this pattern occurs at least twice, would it be justified to do:
withInfoHandler(...) {
try command.run()
}There was a problem hiding this comment.
Maybe. I'm not too too bothered by it but I can add a helper if other folks want it.
| /// | ||
| /// - Note: This class is responsible for handling `SIGINFO`, `SIGUSR1` (Linux), | ||
| /// and Ctrl+Break (Windows). Naming is hard. | ||
| final class SIGINFOHandler: Sendable { |
There was a problem hiding this comment.
I do wish that we had a better name than SIGINFOHandler (perhaps InfoProviderHandler?).
There was a problem hiding this comment.
It's not API so we can change it. We're thinking about using it for Ctrl+C too to support cancellation. But for now, it's an implementation detail so I'm not sweating it that much.
There was a problem hiding this comment.
ControlKeyObservermaybe?
I really like this if you are okay with the length.
This PR adds a new protocol, `InfoProvidingCommand`, to which root command types can conform to indicate they respond to `SIGINFO` or platform equivalents: - On Darwin and the BSDs, the shell raises `SIGINFO` when you press Ctrl+T; - On Linux, `SIGUSR1` is used by (weak) convention for this purpose and can be raised with `kill` or `pkill`. - On Windows, Ctrl+Break serves the same purpose and triggers a Win32-specific callback.
6333d8c to
29826f6
Compare
Description
This PR adds a new protocol,
InfoProvidingCommand, to which root command types can conform to indicate they respond toSIGINFOor platform equivalents:SIGINFOwhen you press Ctrl+T;SIGUSR1is used by (weak) convention for this purpose and can be raised withkillorpkill.Detailed Design
The new API is an additional protocol that refines
ParsableCommand:Types that conform to this protocol can then implement
provideInfo()to print whatever information is meaningful at that moment. An example program is included to show how you might use it.Documentation Plan
I plan to scrabble something together after the design has been critiqued.
Test Plan
Added a new unit test based on a Swift Testing exit test. Signals are generally hard to test, and I am open to adding more tests if folks have ideas.
The example program can also be run at desk to validate behavior in an interactive way.
Source Impact
There should be no impact on existing code.
Checklist