Skip to content

Commit 3618c04

Browse files
OhadRaulet-def
authored andcommitted
Nottui.Ui_loop: add option to opt-out of default quit bindings in run
By default, [Nottui.Ui_loop.run] quits whenever the user types [Ctrl+Q] and [Escape]. There was previously no way to opt-out of this, and since the [run] loop intercepts all key events, this meant that there was no way to rebind [Escape] to other actions when using [run].
1 parent 3aea714 commit 3618c04

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lib/nottui/nottui.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,13 +849,15 @@ struct
849849
ignore (Lwd.quick_release quit)
850850

851851
let run ?tick_period ?tick ?term ?(renderer=Renderer.make ())
852-
?quit t =
852+
?quit ?(quit_on_escape=true) ?(quit_on_ctrl_q=true) t =
853853
let quit = match quit with
854854
| Some quit -> quit
855855
| None -> Lwd.var false
856856
in
857857
let t = Lwd.map t ~f:(Ui.event_filter (function
858-
| `Key (`ASCII 'Q', [`Ctrl]) | `Key (`Escape, []) ->
858+
| `Key (`ASCII 'Q', [`Ctrl]) when quit_on_ctrl_q ->
859+
Lwd.set quit true; `Handled
860+
| `Key (`Escape, []) when quit_on_escape ->
859861
Lwd.set quit true; `Handled
860862
| _ -> `Unhandled
861863
))

lib/nottui/nottui.mli

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,13 @@ sig
353353
val run :
354354
?tick_period:float -> ?tick:(unit -> unit) ->
355355
?term:Term.t -> ?renderer:Renderer.t ->
356-
?quit:bool Lwd.var -> ui Lwd.t -> unit
356+
?quit:bool Lwd.var -> ?quit_on_escape:bool ->
357+
?quit_on_ctrl_q:bool -> ui Lwd.t -> unit
357358
(** Repeatedly run steps of the main loop, until either:
358359
- [quit] becomes true,
359360
- the ui computation raises an exception,
360-
- if [quit] was not provided, wait for Ctrl-Q event
361+
- if [quit_on_ctrl_q] was true or not provided, wait for Ctrl-Q event
362+
- if [quit_on_escape] was true or not provided, wait for Escape event
361363
362364
Specific [term] or [renderer] instances can be provided, otherwise new
363365
ones will be allocated and released.

0 commit comments

Comments
 (0)