Code to query terminal for capabilities

This commit is contained in:
Kovid Goyal
2025-01-03 21:30:05 +05:30
parent e1f60fa71a
commit fc463aaba5
4 changed files with 113 additions and 2 deletions

View File

@@ -57,6 +57,10 @@ type Loop struct {
style_ctx style.Context
atomic_update_active bool
pointer_shapes []PointerShape
waiting_for_capabilities_response bool
// Queried capabilities from terminal
TerminalCapabilities TerminalCapabilities
// Suspend the loop restoring terminal state, and run the provided function. When it returns terminal state is
// put back to what it was before suspending unless the function returns an error or an error occurs saving/restoring state.
@@ -111,6 +115,9 @@ type Loop struct {
// Called on SIGTERM return true if you wish to handle it yourself
OnSIGTERM func() (bool, error)
// Called when capabilities response is received
OnCapabilitiesReceived func(TerminalCapabilities)
}
func New(options ...func(self *Loop)) (*Loop, error) {
@@ -538,3 +545,15 @@ func (self *Loop) CurrentPointerShape() (ans PointerShape, has_shape bool) {
}
return
}
// Query the terminal for various capabilities, the OnCapabilitiesReceived
// callback will be called once the query response is received. This
// function should be called as early as possible ideally in OnInitialize.
func (self *Loop) QueryCapabilities() {
if !self.waiting_for_capabilities_response {
self.waiting_for_capabilities_response = true
self.StartAtomicUpdate()
self.QueueWriteString("\x1b[?u\x1b[?996n\x1b[c")
self.EndAtomicUpdate()
}
}