diff --git a/Termdash-API.md b/Termdash-API.md index 7d58633..5c32350 100644 --- a/Termdash-API.md +++ b/Termdash-API.md @@ -135,11 +135,12 @@ if err != nil { } defer ctrl.Close() ``` + ## [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 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 // Create terminal t and container c as in the examples above. @@ -159,4 +160,30 @@ quitter := func(k *terminalapi.Keyboard) { if err := termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter)); err != nil { 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) +} ``` \ No newline at end of file