Skip to content

Commit

Permalink
Split off the textbox file into its own library. Tests TBD.
Browse files Browse the repository at this point in the history
  • Loading branch information
Enoumy committed Oct 15, 2024
1 parent 11e6bff commit 5857d58
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/capymanga/src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
capytui
capytui_catpuccin
capytui_scroller
capytui_textbox
mangadex_api
bonsai.extra)
(preprocess
Expand Down
8 changes: 5 additions & 3 deletions app/capymanga/src/manga_search.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ let component ~dimensions ~set_page ~title =
; set
}
=
let%sub extra_attrs =
let%sub text_attrs =
let%arr flavor = flavor in
[ Attr.background_color (Catpuccin.color ~flavor Base) ]
[ Attr.background_color (Catpuccin.color ~flavor Base)
; Attr.foreground_color (Catpuccin.color ~flavor Text)
]
in
let%map.Computation input =
Text_box.component ~extra_attrs ~is_focused:textbox_is_focused
Capytui_textbox.component ~text_attrs ~is_focused:textbox_is_focused ()
and flavor = return flavor in
let space =
Node.text
Expand Down
8 changes: 5 additions & 3 deletions app/capymanga/src/manga_viewer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,13 @@ let component ~dimensions ~(manga : Manga.t Value.t) ~set_page ~go_back =
; set = _
}
=
let%sub extra_attrs =
let%sub text_attrs =
let%arr flavor = flavor in
[ Attr.background_color (Catpuccin.color ~flavor Base) ]
[ Attr.background_color (Catpuccin.color ~flavor Base)
; Attr.foreground_color (Catpuccin.color ~flavor Text)
]
in
Text_box.component ~extra_attrs ~is_focused:is_textbox_focused
Capytui_textbox.component ~text_attrs ~is_focused:is_textbox_focused ()
in
let%sub top_bar =
Top_bar.component
Expand Down
18 changes: 18 additions & 0 deletions lib/capytui/tui/textbox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Capytui Textbox

This is a primitive text box component. It is currently a single line
and has very limited functionality and poor performance.

If you would like to edit something more complex/non-ephemeral, consider
instead programmatically opening a proper text editor / some other alternative.

## TODO.

- Consider supporting a multi-line text box
- Consider being able to move the cursor around (currently you can only type at the end.)
- Consider making character insertions more performant by using something like a rope.
- Consider supporting more complex editor functionality like an undo stack...
- Consider physically moving the terminal cursor around as you type. (this is
a minor thing - technically the current cursor is not the real cursor so if
you ctrl+b + [ in tmux, the cursor won't be wherever your fake capytui "cursor"
currently is.)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ type action =
| Clear
| Set of string

let component ?(extra_attrs = Value.return []) ~is_focused =
let component
?(cursor_attrs = Value.return [])
?(text_attrs = Value.return [])
~is_focused
()
=
let%sub string, inject =
Bonsai.state_machine0
~default_model:""
Expand Down Expand Up @@ -46,6 +51,7 @@ let component ?(extra_attrs = Value.return []) ~is_focused =
let%sub handler =
let%arr inject = inject in
fun (event : Event.t) ->
(* TODO: Implement the abilityof moving the cursor around. *)
match event with
| `Mouse _ | `Paste _ -> Effect.Ignore
| `Key (`ASCII char, []) -> inject (Char char)
Expand All @@ -55,15 +61,15 @@ let component ?(extra_attrs = Value.return []) ~is_focused =
| _ -> Effect.Ignore
in
let%sub view =
let%sub text = Text.component in
let%arr string = string
and text = text
and is_focused = is_focused
and extra_attrs = extra_attrs in
and cursor_attrs = cursor_attrs
and text_attrs = text_attrs in
Node.hcat
[ text ~attrs:extra_attrs string
[ Node.text ~attrs:text_attrs string
; (if is_focused
then text ~attrs:[ Attr.reverse; Attr.blink ] " "
then
Node.text ~attrs:(cursor_attrs @ [ Attr.reverse; Attr.blink ]) " "
else Node.none)
]
in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type t =
}

val component
: ?extra_attrs:Attr.t list Value.t
: ?cursor_attrs:Attr.t list Value.t
-> ?text_attrs:Attr.t list Value.t
-> is_focused:bool Value.t
-> unit
-> t Computation.t
5 changes: 5 additions & 0 deletions lib/capytui/tui/textbox/src/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name capytui_textbox)
(libraries core bonsai capytui)
(preprocess
(pps ppx_jane bonsai.ppx_bonsai)))

0 comments on commit 5857d58

Please sign in to comment.