Updated Termdash API (markdown)

Jakub Sobon 2019-02-24 18:36:28 -05:00
parent 93a662fb4a
commit de2f3174df
1 changed files with 28 additions and 1 deletions

@ -135,11 +135,12 @@ if err != nil {
} }
defer ctrl.Close() defer ctrl.Close()
``` ```
## [termdash.KeyboardSubscriber](https://github.com/mum4k/termdash/blob/275d95ad418b6ecca8817de4b013b440fc9abbe6/termdash.go#L74-L81): ## [termdash.KeyboardSubscriber](https://github.com/mum4k/termdash/blob/275d95ad418b6ecca8817de4b013b440fc9abbe6/termdash.go#L74-L81):
The **termdash.KeyboardSubscriber** function can be used by the developer to provide an additional subscriber to keyboard events. A keyboard subscriber is a function (a callback) that processes keyboard events. This function must be thread-safe, since Termdash delivers events concurrently. Providing an additional keyboard subscriber is completely optional and doesn't affect any of Termdash functionalities. The **termdash.KeyboardSubscriber** function can be used by the developer to provide an additional subscriber to keyboard events. A keyboard subscriber is a function (a callback) that processes keyboard events. This function must be thread-safe, since Termdash delivers events concurrently. Providing an additional keyboard subscriber is completely optional and doesn't affect any of Termdash functionalities.
The following example shows initialisation of Termdash with a keyboard subscriber that terminates the application when the letter 'q' or 'Q' is pressed on the keyboard. The following example shows initialisation of Termdash with a keyboard subscriber that terminates the application when the letter 'q' or 'Q' is pressed on the keyboard:
```go ```go
// Create terminal t and container c as in the examples above. // Create terminal t and container c as in the examples above.
@ -160,3 +161,29 @@ if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter)); err !=
return fmt.Errorf("termdash.Run => %v", err) return fmt.Errorf("termdash.Run => %v", err)
} }
``` ```
## [termdash.MouseSubscriber](https://github.com/mum4k/termdash/blob/275d95ad418b6ecca8817de4b013b440fc9abbe6/termdash.go#L83-L90):
The **termdash.MouseSubscriber** function can be used by the developer to provide an additional subscriber to mouse events. A mouse subscriber is a function (a callback) that processes mouse events. This function must be thread-safe, since Termdash delivers events concurrently. Providing an additional mouse subscriber is completely optional and doesn't affect any of Termdash functionalities.
The following example shows initialisation of Termdash with a mouse subscriber that terminates the application when the right mouse button is pressed:
```go
// Create terminal t and container c as in the examples above.
// Context for Termdash, the application quits when this expires.
// Since the context has no deadline, it will only expire when cancel() is called.
ctx, cancel := context.WithCancel(context.Background())
// A mouse subscriber that terminates the application by cancelling the context.
quitter := func(m *terminalapi.Mouse) {
if m.Button == mouse.ButtonRight {
cancel()
}
}
// Termdash will run until the right mouse button is pressed.
if err := termdash.Run(ctx, t, c, termdash.MouseSubscriber(quitter)); err != nil {
return fmt.Errorf("termdash.Run => %v", err)
}
```