A lightweight reactive document library.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

76 lines
2.2 KiB

  1. open Nottui
  2. module P = Nottui_pretty
  3. let string ?attr text = P.ui (Nottui_widgets.string ?attr text)
  4. let (^^) = P.(^^)
  5. let (^/^) a b = P.(a ^^ break 1 ^^ b)
  6. let spring = P.ui (Ui.resize ~sw:1 Ui.empty)
  7. let selector text f choices =
  8. Nottui_widgets.main_menu_item text (fun () ->
  9. Lwd.pure @@ Ui.vcat (
  10. List.map
  11. (fun choice ->
  12. Nottui_widgets.sub_entry choice (fun () -> f choice))
  13. choices
  14. )
  15. )
  16. let fruit =
  17. let fruits = ["Apple"; "Orange"; "Strawberry"] in
  18. let choice = Lwd.var (List.hd fruits) in
  19. Lwd.join (
  20. Lwd.map' (Lwd.get choice) (fun current ->
  21. selector current (Lwd.set choice) fruits
  22. )
  23. )
  24. let doc = Lwd_table.make ()
  25. let () =
  26. for _ = 0 to 99 do
  27. List.iter (fun doc' -> Lwd_table.append' doc (Lwd.pure doc'))
  28. [
  29. P.group (string "This" ^/^ string "is" ^/^ string "pretty.");
  30. P.hardline; P.ui (Nottui.Ui.void 0 1); P.hardline;
  31. P.group (P.group (string "This" ^/^ string "is") ^/^ string "pretty.");
  32. P.hardline; P.ui (Nottui.Ui.void 0 1); P.hardline;
  33. P.group (string "This" ^/^ P.group (string "is" ^/^ string "pretty."));
  34. P.hardline; P.ui (Nottui.Ui.void 0 1); P.hardline;
  35. P.group (spring ^^ string "This" ^^ spring ^/^
  36. P.group (string "is" ^^ spring ^/^ string "pretty.") ^^ spring);
  37. P.hardline; P.ui (Nottui.Ui.void 0 1); P.hardline;
  38. ];
  39. Lwd_table.append' doc
  40. (Lwd.map' fruit (fun fruit ->
  41. P.group (spring ^^ string "I" ^^ spring ^/^
  42. P.group (string "like" ^^ spring ^/^
  43. P.ui fruit ^^ spring ^/^
  44. string "more.") ^^ spring);
  45. ))
  46. done
  47. let varying_width f =
  48. let width = Lwd.var 0 in
  49. Lwd.map'
  50. (f (Lwd.get width))
  51. (fun ui ->
  52. Nottui.Ui.size_sensor
  53. (fun ~w ~h:_ -> if Lwd.peek width <> w then Lwd.set width w)
  54. (Nottui.Ui.resize ~sw:1 ~sh:1 ~w:0 ui))
  55. let doc =
  56. Lwd.join (Lwd_table.reduce (Lwd_utils.lift_monoid (P.empty, P.(^^))) doc)
  57. let contents width = Lwd.map2' width doc P.pretty
  58. let () =
  59. Nottui.Ui_loop.run (
  60. Nottui_widgets.h_pane
  61. (Nottui_widgets.scroll_area (varying_width contents))
  62. (Lwd.pure Nottui.Ui.empty)
  63. )