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.
 
 

94 lines
2.4 KiB

  1. open Nottui
  2. (* Put the UI here *)
  3. (*let node title ~f =
  4. let vopened = Lwd.var false in
  5. let label =
  6. Lwd.map' (Lwd.get vopened) @@ fun opened ->
  7. let text = if opened then "[-]" else "[+]" in
  8. Ui.mouse_area (fun ~x:_ ~y:_ -> function
  9. | `Left -> Lwd.set vopened (not opened); `Handled
  10. | _ -> `Unhandled
  11. ) (Ui.atom Notty.(I.string A.empty text))
  12. in
  13. let content = Lwd.bind (Lwd.get vopened) @@ function
  14. | true -> f ()
  15. | false -> Lwd.pure Ui.empty
  16. in
  17. Lwd.map2' label content (fun lbl content ->
  18. Ui.join_x lbl
  19. (Ui.join_y (Ui.atom Notty.(I.string A.empty title)) content)
  20. )
  21. let rec count_to_10 () =
  22. Lwd_utils.pack Ui.pack_y (
  23. List.map
  24. (fun i -> node (string_of_int i) ~f:count_to_10)
  25. [1;2;3;4;5;6;7;8;9;10]
  26. )
  27. let root = count_to_10 ()*)
  28. let f_to_c x = (x -. 32.0) *. 5.0/.9.0
  29. let c_to_f x = x *. 9.0/.5.0 +. 32.0
  30. let degrees = Lwd.var 0.0
  31. let farenheit = Lwd.var (nan, ("", 0))
  32. let farenheit_text =
  33. Lwd.map2' (Lwd.get degrees) (Lwd.get farenheit)
  34. (fun d (d', f) ->
  35. if d = d' then f else (string_of_float (c_to_f d), 0))
  36. let farenheit_edit =
  37. Nottui_widgets.edit_field
  38. farenheit_text
  39. ~on_change:(fun (text, _ as state) ->
  40. let d = match float_of_string_opt text with
  41. | None -> Lwd.peek degrees
  42. | Some d -> let d = f_to_c d in Lwd.set degrees d; d
  43. in
  44. Lwd.set farenheit (d, state)
  45. )
  46. ~on_submit:ignore
  47. let celsius = Lwd.var (nan, ("", 0))
  48. let celsius_text =
  49. Lwd.map2' (Lwd.get degrees) (Lwd.get celsius)
  50. (fun d (d', f) -> if d = d' then f else (string_of_float d, 0))
  51. let celsius_edit =
  52. Nottui_widgets.edit_field
  53. celsius_text
  54. ~on_change:(fun (text, _ as state) ->
  55. let d = match float_of_string_opt text with
  56. | None -> Lwd.peek degrees
  57. | Some d -> Lwd.set degrees d; d
  58. in
  59. Lwd.set celsius (d, state)
  60. )
  61. ~on_submit:ignore
  62. let root =
  63. let base =
  64. Lwd_utils.pack Ui.pack_y [
  65. Lwd.pure (Nottui_widgets.string "Celsius:");
  66. celsius_edit;
  67. Lwd.pure (Nottui_widgets.string "Farenheight:");
  68. farenheit_edit;
  69. ]
  70. in
  71. let base = Lwd.map2 Ui.join_y base base in
  72. let base = Lwd.map2 Ui.join_y base base in
  73. let base = Lwd.map2 Ui.join_y base base in
  74. let base = Lwd.map2 Ui.join_y base base in
  75. let base = Lwd.map2 Ui.join_y base base in
  76. base
  77. (*let () = Statmemprof_emacs.start 1E-4 30 5*)
  78. let () = Ui_loop.run ~tick_period:0.2 root