Skip to content

Commit

Permalink
saving my progress (not much progress...)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enoumy committed Oct 11, 2024
1 parent b9e1d5e commit 980e621
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
62 changes: 62 additions & 0 deletions app/capymanga/src/#about.ml#
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
open! Core
open Capytui
module Catpuccin = Capytui_catpuccin
open Bonsai.Let_syntax

let component ~go_back =
(* TODO: Make this prettier. Also figure out a way to nicely center align
text... *)
let%sub dimensions = Capytui.terminal_dimensions in
let%sub flavor = Catpuccin.flavor in
let%sub view =
let%arr flavor = flavor in
Node.vcat
[ Node.hcat
[ Node.text
~attrs:
[ Attr.bold
; Attr.foreground_color (Catpuccin.color ~flavor Mauve)
; Attr.background_color (Catpuccin.color ~flavor Crust)
]
"Capymanga"
; Node.text
~attrs:
[ Attr.foreground_color (Catpuccin.color ~flavor Flamingo)
; Attr.background_color (Catpuccin.color ~flavor Crust)
]
" 2024"
]
; Node.hcat
[ Node.text
~attrs:
[ Attr.foreground_color (Catpuccin.color ~flavor Text)
; Attr.background_color (Catpuccin.color ~flavor Crust)
]
"powered by "
; Node.text
~attrs:
[ Attr.bold
; Attr.foreground_color (Catpuccin.color ~flavor Teal)
; Attr.background_color (Catpuccin.color ~flavor Crust)
]
"MangaDex"
]
]
in
let%sub view =
let%arr dimensions = dimensions
and view = view in
Node.center ~within:dimensions view
in
let%sub handler =
let%arr go_back = go_back in
fun (event : Event.t) ->
match event with
| `Key ((`Backspace | `Escape), []) | `Key (`ASCII ('q' | '?'), []) ->
go_back
| _ -> Effect.Ignore
in
let%arr view = view
and handler = handler in
{ Component.view; handler; images = [] }
;;
76 changes: 76 additions & 0 deletions app/capymanga/src/#capymanga.ml#
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
open! Core
open Bonsai
open Bonsai.Let_syntax
open Capytui
module Catpuccin = Capytui_catpuccin

let backdrop =
let%sub dimensions = Capytui.terminal_dimensions in
let%sub flavor = Catpuccin.flavor in
let%arr { height; width } = dimensions
and flavor = flavor in
Node.vcat
(List.init height ~f:(fun _ ->
Node.text
~attrs:[ Attr.background_color (Catpuccin.color ~flavor Crust) ]
(String.make width ' ')))
;;

let content ~(page : Page.t Value.t) ~set_page ~go_back =
let%sub dimensions = Capytui.terminal_dimensions in
let%sub { view; images; handler } =
let%arr page = page in
match%sub page with
| Manga_search { title } ->
Manga_search.component ~dimensions ~title ~set_page
| Manga_view { manga } ->
Manga_viewer.component ~dimensions ~manga ~set_page ~go_back
| Chapter_view { chapter } ->
Chapter_viewer.component ~dimensions ~chapter ~go_back
| About_page -> About.component ~go_back
in
let%sub () = Capytui.listen_to_events handler in
let%arr view = view
and images = images in
view, images
;;

let app =
Loading_state.register
@@ Scanlation_group_cache.register
@@
let%sub { page; set_page; go_back } =
Navigation.component (Page.Manga_search { title = None })
in
let%sub content, images = content ~page ~set_page ~go_back in
let%sub backdrop = backdrop in
let%arr backdrop = backdrop
and images = images
and content = content in
Node.zcat [ content; backdrop ], images
;;

let command =
let open Async in
Command.async_or_error
~summary:{|Capy manga!|}
[%map_open.Command
let () = return () in
fun () ->
let open Deferred.Or_error.Let_syntax in
let app =
app
|> Outside_world.Manga_cover.register_real
|> Outside_world.Manga_search.register_real
|> Outside_world.Author.register_real
|> Outside_world.Chapter_feed.register_real
|> Outside_world.Chapter_images.register_real
|> Outside_world.Scanlation_group.register_real
in
let%bind () = Capytui.start_with_images app in
Deferred.Or_error.return ()]
;;

module For_testing = struct
module Outside_world = Outside_world
end
57 changes: 57 additions & 0 deletions app/capymanga/src/image_prefetcher.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
open! Core
open Bonsai

type t =
{ write_file : filename:string -> contents:string -> unit Effect.t
; delete_file : filename:string -> unit Effect.t
; curl_uri : uri:Uri.t -> string Effect.t
}

let default_unregistered =
let fail_unregistered () =
failwith
"BUG in capymanga: Image prefetcher dynamic variable not registered!"
in
let write_file ~filename:_ ~contents:_ = fail_unregistered () in
let delete_file ~filename:_ = fail_unregistered () in
let curl_uri ~uri:_ = fail_unregistered () in
{ write_file; delete_file; curl_uri }
;;

let variable =
Dynamic_scope.create
~name:"capymanga-image-prefetcher-outside-world-interactions"
~fallback:default_unregistered
()
;;

let register_real x =
let write_file ~filename ~contents =
let open Async.Deferred.Let_syntax in
Capytui.Effect.of_deferred_fun
(fun () ->
let%bind writer = Async.Writer.open_file filename in
Async.Writer.write writer contents;
let%bind () = Async.Writer.flushed writer in
Async.Writer.close writer)
()
in
let delete_file ~filename =
Capytui.Effect.of_sync_fun (fun () -> Core_unix.remove filename) ()
in
let curl_uri ~uri =
let open Async.Deferred.Let_syntax in
Capytui.Effect.of_deferred_fun
(fun () ->
let%bind _, body = Cohttp_async.Client.get uri in
let%bind string = Cohttp_async.Body.to_string body in
return string)
()
in
let real = { write_file; delete_file; curl_uri } in
Bonsai.Dynamic_scope.set variable (Value.return real) ~inside:x
;;

module For_testing = struct
let register_mock x = x
end
8 changes: 8 additions & 0 deletions app/capymanga/src/image_prefetcher.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
open! Core
open Bonsai

val register_real : 'a Computation.t -> 'a Computation.t

module For_testing : sig
val register_mock : 'a Computation.t -> 'a Computation.t
end

0 comments on commit 980e621

Please sign in to comment.