diff --git a/.ocamlformat b/.ocamlformat new file mode 100644 index 0000000..e69de29 diff --git a/basics_I_above_another.ml b/basics_I_above_another.ml index 4db8739..4f85bbc 100644 --- a/basics_I_above_another.ml +++ b/basics_I_above_another.ml @@ -1,7 +1,5 @@ open Notty -open Notty_unix -(* ocamlbuild -pkg notty -pkg notty.unix basics_I_above_another.native *) let () = let bar = I.uchar A.(fg lightred) (Uchar.of_int 0x2502) 3 1 in let img1 = I.string A.(fg lightgreen) "image1" in diff --git a/basics_I_char.ml b/basics_I_char.ml index 266884e..0801fa2 100644 --- a/basics_I_char.ml +++ b/basics_I_char.ml @@ -1,8 +1,5 @@ open Notty -open Notty_unix + (* ocamlbuild -pkg notty -pkg notty.unix basics_I_char.native *) let () = - I.char A.(fg lightred) 'o' 4 4 - |> Notty_unix.eol - |> Notty_unix.output_image - + I.char A.(fg lightred) 'o' 4 4 |> Notty_unix.eol |> Notty_unix.output_image diff --git a/basics_I_hcropping.ml b/basics_I_hcropping.ml index fe1425e..dc08793 100644 --- a/basics_I_hcropping.ml +++ b/basics_I_hcropping.ml @@ -1,10 +1,10 @@ open Notty -open Notty_unix -(* ocamlfind ocamlc -o basics_cropping -package notty,notty.unix -linkpkg -g basics_I_cropping.ml *) -let long_line_str = "This is a line that will be cropped 2 unit left and 5 unit right" +let long_line_str = + "This is a line that will be cropped 2 unit left and 5 unit right" let () = let long_line = I.string A.(fg lightgreen ++ bg black) long_line_str in let long_line_cropped = I.hcrop 2 5 long_line in - I.(long_line <-> long_line_cropped) |> Notty_unix.output_image_endline + I.(long_line <-> long_line_cropped) + |> Notty_unix.eol |> Notty_unix.output_image diff --git a/basics_I_negative_vcropping.ml b/basics_I_negative_vcropping.ml index 043a315..2b99649 100644 --- a/basics_I_negative_vcropping.ml +++ b/basics_I_negative_vcropping.ml @@ -1,21 +1,21 @@ open Notty -open Notty_unix -(* ocamlfind ocamlc -o basics_negative_vcropping -package notty,notty.unix -linkpkg -g basics_I_negative_vcropping.ml *) -let line_str num = - "line number " ^ (string_of_int num) +let line_str num = "line number " ^ string_of_int num let build_5_lines () = let rec _build img remain = if remain = 0 then img - else let str = line_str (6 - remain) in - _build I.(img <-> string A.(fg lightgreen ++ bg black) str) (remain - 1) - in _build (I.string A.(fg lightgreen ++ bg black) (line_str 1)) 4 + else + let str = line_str (6 - remain) in + _build I.(img <-> string A.(fg lightgreen ++ bg black) str) (remain - 1) + in + _build (I.string A.(fg lightgreen ++ bg black) (line_str 1)) 4 let description = - I.string A.(fg lightyellow ++ bg lightblack) "Negative crop -2 at top and -1 at bottom" + I.string + A.(fg lightyellow ++ bg lightblack) + "Negative crop -2 at top and -1 at bottom" let () = - I.(build_5_lines () <-> - description <-> - vcrop (-2) (-1) (build_5_lines ())) |> Notty_unix.output_image_endline + I.(build_5_lines () <-> description <-> vcrop (-2) (-1) (build_5_lines ())) + |> Notty_unix.eol |> Notty_unix.output_image diff --git a/basics_I_overlay.ml b/basics_I_overlay.ml index 66e8834..8f7c569 100644 --- a/basics_I_overlay.ml +++ b/basics_I_overlay.ml @@ -1,8 +1,6 @@ open Notty -open Notty_unix -(* ocamlbuild -pkg notty -pkg notty.unix basic_I_overlay.native *) let () = - let bar = I.uchar A.(fg lightred) 0x2502 3 1 in + let bar = I.uchar A.(fg lightred) (Uchar.of_int 0x2502) 3 1 in let img1 = I.string A.(fg lightgreen) "image1" in - I.(img1 bar) |> Notty_unix.output_image_endline + I.(img1 bar) |> Notty_unix.eol |> Notty_unix.output_image diff --git a/basics_I_padding.ml b/basics_I_padding.ml index 3b29247..664ef82 100644 --- a/basics_I_padding.ml +++ b/basics_I_padding.ml @@ -1,21 +1,23 @@ open Notty -open Notty_unix -(* ocamlfind ocamlc -o basics_padding -package notty,notty.unix -linkpkg -g basics_I_padding.ml *) -let line_str num = - "line number " ^ (string_of_int num) +let line_str num = "line number " ^ string_of_int num let build_5_lines () = let rec _build img remain = if remain = 0 then img - else let str = line_str (6 - remain) in - _build I.(img <-> string A.(fg lightgreen ++ bg black) str) (remain - 1) - in _build (I.string A.(fg lightgreen ++ bg black) (line_str 1)) 4 + else + let str = line_str (6 - remain) in + _build I.(img <-> string A.(fg lightgreen ++ bg black) str) (remain - 1) + in + _build (I.string A.(fg lightgreen ++ bg black) (line_str 1)) 4 let description = - I.string A.(fg lightyellow ++ bg lightblack) "Padding left = 2, right = 3, top = 4 and 1 at bottom" + I.string + A.(fg lightyellow ++ bg lightblack) + "Padding left = 2, right = 3, top = 4 and 1 at bottom" let () = - I.(build_5_lines () <-> - description <-> - pad ~l:2 ~r:3 ~t:4 ~b:1 (build_5_lines ())) |> Notty_unix.output_image_endline + I.( + build_5_lines () <-> description + <-> pad ~l:2 ~r:3 ~t:4 ~b:1 (build_5_lines ())) + |> Notty_unix.eol |> Notty_unix.output_image diff --git a/basics_I_side_by_side.ml b/basics_I_side_by_side.ml index d125a4f..2ee78ce 100644 --- a/basics_I_side_by_side.ml +++ b/basics_I_side_by_side.ml @@ -1,10 +1,6 @@ open Notty -open Notty_unix -(* ocamlbuild -pkg notty -pkg notty.unix basic_I_side_by_side *) let () = - let bar = I.uchar A.(fg lightred) 0x2502 3 1 in + let bar = I.uchar A.(fg lightred) (Uchar.of_int 0x2502) 3 1 in let img1 = I.string A.(fg lightgreen) "image1" in - I.(img1 <|> bar) |> Notty_unix.output_image_endline - - + I.(img1 <|> bar) |> Notty_unix.eol |> Notty_unix.output_image diff --git a/basics_I_string.ml b/basics_I_string.ml index 740027a..dde671c 100644 --- a/basics_I_string.ml +++ b/basics_I_string.ml @@ -1,7 +1,5 @@ open Notty open Notty_unix + (* ocamlbuild -pkg notty -pkg notty.unix basics_I_string.native *) -let () = -I.string A.(fg lightred) "Wow!" -|> eol -|> Notty_unix.output_image +let () = I.string A.(fg lightred) "Wow!" |> eol |> Notty_unix.output_image diff --git a/basics_I_uchar.ml b/basics_I_uchar.ml index d40a459..048002e 100644 --- a/basics_I_uchar.ml +++ b/basics_I_uchar.ml @@ -1,8 +1,5 @@ open Notty -open Notty_unix -(* ocamlbuild -pkg notty -pkg notty.unix basics_I_uchar.native *) -let () = - I.uchar A.(fg lightred) (Uchar.of_int 0x2022) 4 4 - |> Notty_unix.eol - |> Notty_unix.output_image +let () = + I.uchar A.(fg lightred) (Uchar.of_int 0x2022) 4 4 + |> Notty_unix.eol |> Notty_unix.output_image diff --git a/basics_I_uchars.ml b/basics_I_uchars.ml index 9aad3fd..fc30d55 100644 --- a/basics_I_uchars.ml +++ b/basics_I_uchars.ml @@ -1,11 +1,19 @@ open Notty -open Notty_unix -(* ocamlbuild -pkg notty -pkg notty.unix basics_I_uchars.native *) + let () = let my_unicode_chars = - [|0x2500; 0x2502; 0x2022; 0x2713; 0x25cf; - 0x256d; 0x256e; 0x256f; 0x2570; 0x253c|] in - I.uchars A.(fg lightred) (Array.map Uchar.of_int my_unicode_chars) - |> Notty_unix.eol - |> Notty_unix.output_image - + [| + 0x2500; + 0x2502; + 0x2022; + 0x2713; + 0x25cf; + 0x256d; + 0x256e; + 0x256f; + 0x2570; + 0x253c; + |] + in + I.uchars A.(fg lightred) (Array.map Uchar.of_int my_unicode_chars) + |> Notty_unix.eol |> Notty_unix.output_image diff --git a/basics_I_vcropping.ml b/basics_I_vcropping.ml index 202a8c2..2ae6e24 100644 --- a/basics_I_vcropping.ml +++ b/basics_I_vcropping.ml @@ -1,21 +1,19 @@ open Notty -open Notty_unix -(* ocamlfind ocamlc -o basics_vcropping -package notty,notty.unix -linkpkg -g basics_I_vcropping.ml *) -let line_str num = - "line number " ^ (string_of_int num) +let line_str num = "line number " ^ string_of_int num let build_5_lines () = let rec _build img remain = if remain = 0 then img - else let str = line_str (6 - remain) in - _build I.(img <-> string A.(fg lightgreen ++ bg black) str) (remain - 1) - in _build (I.string A.(fg lightgreen ++ bg black) (line_str 1)) 4 + else + let str = line_str (6 - remain) in + _build I.(img <-> string A.(fg lightgreen ++ bg black) str) (remain - 1) + in + _build (I.string A.(fg lightgreen ++ bg black) (line_str 1)) 4 let description = I.string A.(fg lightyellow ++ bg lightblack) "crop 2 at top and 1 at bottom" let () = - I.(build_5_lines () <-> - description <-> - vcrop 2 1 (build_5_lines ())) |> Notty_unix.output_image_endline + I.(build_5_lines () <-> description <-> vcrop 2 1 (build_5_lines ())) + |> Notty_unix.eol |> Notty_unix.output_image diff --git a/basics_Lwt_Term_simple_terminal_resize.ml b/basics_Lwt_Term_simple_terminal_resize.ml index 1afd0f6..ceb086b 100644 --- a/basics_Lwt_Term_simple_terminal_resize.ml +++ b/basics_Lwt_Term_simple_terminal_resize.ml @@ -1,44 +1,47 @@ open Notty -open Notty_lwt open Lwt (* ocamlfind ocamlc -o simple_lwt_terminal_resize -package notty.lwt -linkpkg -g common.ml basics_Lwt_Term_simple_terminal_resize.ml*) - module LwtTerm = Notty_lwt.Term let grid xxs = xxs |> List.map I.hcat |> I.vcat let outline attr t = - let (w, h) = LwtTerm.size t in + let w, h = LwtTerm.size t in let chr x = I.uchar attr x 1 1 - and hbar = I.uchar attr 0x2500 (w - 2) 1 - and vbar = I.uchar attr 0x2502 1 (h - 2) in - let (a, b, c, d) = (chr 0x256d, chr 0x256e, chr 0x256f, chr 0x2570) in - grid [ [a; hbar; b]; [vbar; I.void (w - 2) 1; vbar]; [d; hbar; c] ] + and hbar = I.uchar attr (Uchar.of_int 0x2500) (w - 2) 1 + and vbar = I.uchar attr (Uchar.of_int 0x2502) 1 (h - 2) in + let a, b, c, d = + ( chr (Uchar.of_int 0x256d), + chr (Uchar.of_int 0x256e), + chr (Uchar.of_int 0x256f), + chr (Uchar.of_int 0x2570) ) + in + grid [ [ a; hbar; b ]; [ vbar; I.void (w - 2) 1; vbar ]; [ d; hbar; c ] ] let size_box cols rows = - let cols_str = string_of_int cols in let rows_str = string_of_int rows in - let label = (cols_str ^ "x" ^ rows_str) in + let cols_str = string_of_int cols in + let rows_str = string_of_int rows in + let label = cols_str ^ "x" ^ rows_str in let box = I.string A.(fg lightgreen ++ bg lightblack) label in let top_margin = (rows - I.height box) / 2 in let left_margin = (cols - I.width box) / 2 in I.pad ~t:top_margin ~l:left_margin box -let rec main t (x, y as pos) = - let img = I.((outline A.(fg lightred ) t) (size_box x y)) in - LwtTerm.image t img - >>= fun () -> - LwtTerm.cursor t (Some pos) - >>= fun () -> - Lwt_stream.get ( LwtTerm.events t) - >>= fun event -> - match event with - | None -> LwtTerm.release t >>= fun () -> Lwt.return_unit - | Some (`Resize _ | #Unescape.event as x) -> match x with - | `Key (`Escape, []) | `Key (`Uchar 67, [`Ctrl]) -> LwtTerm.release t >>= fun () -> Lwt.return_unit - | `Resize (cols, rows) -> main t (cols, rows) - | _ ->Lwt.return () >>= fun () -> main t pos +let rec main t ((x, y) as pos) = + let img = I.(outline A.(fg lightred) t size_box x y) in + LwtTerm.image t img >>= fun () -> + LwtTerm.cursor t (Some pos) >>= fun () -> + Lwt_stream.get (LwtTerm.events t) >>= fun event -> + match event with + | None -> LwtTerm.release t >>= fun () -> Lwt.return_unit + | Some ((`Resize _ | #Unescape.event) as x) -> ( + match x with + | `Key (`Escape, []) -> LwtTerm.release t + | `Key (`Uchar u, [ `Ctrl ]) when Uchar.to_int u = 67 -> LwtTerm.release t + | `Resize (cols, rows) -> main t (cols, rows) + | _ -> Lwt.return () >>= fun () -> main t pos ) let () = let t = LwtTerm.create () in diff --git a/basics_Lwt_Term_simple_terminal_timeout.ml b/basics_Lwt_Term_simple_terminal_timeout.ml index c5f9b31..4c6f141 100644 --- a/basics_Lwt_Term_simple_terminal_timeout.ml +++ b/basics_Lwt_Term_simple_terminal_timeout.ml @@ -1,61 +1,40 @@ -(* ocamlfind ocamlc -o double_threads -package lwt,notty.lwt -linkpkg -g double_threads.ml*) -open Lwt open Lwt.Infix open Notty -open Notty_lwt -open Notty.Infix - module Term = Notty_lwt.Term let counter = ref 0 let rec increase_counter () = - Lwt_unix.sleep 0.1 - >>= fun () -> - ( - if !counter < max_int then counter := (!counter + 1) - else counter := 0 - ); - Lwt.return () - >>= fun () -> - increase_counter () - -let render (w, h) = - I.(strf ~attr:A.(fg lightblack) "[counter %d]" !counter) + Lwt_unix.sleep 0.1 >>= fun () -> + if !counter < max_int then counter := !counter + 1 else counter := 0; + Lwt.return () >>= fun () -> increase_counter () + +let render (_, _) = I.(strf ~attr:A.(fg lightblack) "[counter %d]" !counter) let timer () = Lwt_unix.sleep 0.1 >|= fun () -> `Timer -let event term = Lwt_stream.get (Term.events term) >|= function - | Some (`Resize _ | #Unescape.event as x) -> x +let event term = + Lwt_stream.get (Term.events term) >|= function + | Some ((`Resize _ | #Unescape.event) as x) -> x | None -> `End let rec loop term (e, t) dim = - (e t) >>= function - | `End | `Key (`Escape, []) -> - Lwt.return_unit + e t >>= function + | `End | `Key (`Escape, []) -> Lwt.return_unit | `Timer -> - Term.image term (render dim) - >>= fun () -> - loop term (e, timer ()) dim - | `Mouse ((`Press _|`Drag), (x, y), _) -> - loop term (event term, t) dim + Term.image term (render dim) >>= fun () -> loop term (e, timer ()) dim + | `Mouse ((`Press _ | `Drag), (_, _), _) -> loop term (event term, t) dim | `Resize dim -> - Term.image term (render dim) - >>= fun () -> - loop term (event term, t) dim + Term.image term (render dim) >>= fun () -> loop term (event term, t) dim | _ -> loop term (event term, t) dim let interface () = let tc = Unix.(tcgetattr stdin) in Unix.(tcsetattr stdin TCSANOW { tc with c_isig = false }); - let term = Term.create () in + let term = Term.create () in let size = Term.size term in loop term (event term, timer ()) size -let main () = - Lwt.choose [ - increase_counter (); - interface (); - ] +let main () = Lwt.choose [ increase_counter (); interface () ] let () = Lwt_main.run (main ()) diff --git a/basics_Term_simple_terminal.ml b/basics_Term_simple_terminal.ml index 577d2ec..0f91d70 100644 --- a/basics_Term_simple_terminal.ml +++ b/basics_Term_simple_terminal.ml @@ -1,15 +1,14 @@ open Notty open Notty_unix -(* ocamlbuild -pkg notty -pkg notty.unix basics_Term_simple_terminal.native - * or - * ocamlfind ocamlc -o basics_simple_terminal -package notty,notty.unix -linkpkg -g basics_Term_simple_terminal.ml*) let rec main_loop t = let img = I.(string A.(bg lightred ++ fg black) "This is a simple example") in - Term.image t img; - match Term.event t with - | `End | `Key (`Escape, []) | `Key (`Uchar 67, [`Ctrl]) -> () - | _ -> main_loop t + Term.image t img; + match Term.event t with + | `End | `Key (`Escape, []) -> () + | `Key (`Uchar u, [ `Ctrl ]) when Uchar.to_int u = 67 -> () + | _ -> main_loop t let () = - let t = Term.create () in main_loop t + let t = Term.create () in + main_loop t diff --git a/basics_Term_simple_terminal_resize.ml b/basics_Term_simple_terminal_resize.ml index 095a9e7..a588e0a 100644 --- a/basics_Term_simple_terminal_resize.ml +++ b/basics_Term_simple_terminal_resize.ml @@ -1,34 +1,37 @@ open Notty open Notty_unix -(* ocamlbuild -pkg notty -pkg notty.unix basics_Term_simple_terminal_resize.native - * or - * ocamlfind ocamlc -o simple_terminal_resize -package notty.unix -linkpkg -g common.ml basics_Term_simple_terminal_resize.ml*) - let grid xxs = xxs |> List.map I.hcat |> I.vcat let outline attr t = - let (w, h) = Term.size t in + let w, h = Term.size t in let chr x = I.uchar attr x 1 1 - and hbar = I.uchar attr 0x2500 (w - 2) 1 - and vbar = I.uchar attr 0x2502 1 (h - 2) in - let (a, b, c, d) = (chr 0x256d, chr 0x256e, chr 0x256f, chr 0x2570) in - grid [ [a; hbar; b]; [vbar; I.void (w - 2) 1; vbar]; [d; hbar; c] ] + and hbar = I.uchar attr (Uchar.of_int 0x2500) (w - 2) 1 + and vbar = I.uchar attr (Uchar.of_int 0x2502) 1 (h - 2) in + let a, b, c, d = + ( chr (Uchar.of_int 0x256d), + chr (Uchar.of_int 0x256e), + chr (Uchar.of_int 0x256f), + chr (Uchar.of_int 0x2570) ) + in + grid [ [ a; hbar; b ]; [ vbar; I.void (w - 2) 1; vbar ]; [ d; hbar; c ] ] let size_box cols rows = - let cols_str = string_of_int cols in let rows_str = string_of_int rows in - let label = (cols_str ^ "x" ^ rows_str) in + let cols_str = string_of_int cols in + let rows_str = string_of_int rows in + let label = cols_str ^ "x" ^ rows_str in let box = I.string A.(fg lightgreen ++ bg lightblack) label in let top_margin = (rows - I.height box) / 2 in let left_margin = (cols - I.width box) / 2 in I.pad ~t:top_margin ~l:left_margin box -let rec main t (x, y as pos) = - let img = I.((outline A.(fg lightred ) t) (size_box x y)) in +let rec main t ((x, y) as pos) = + let img = I.(outline A.(fg lightred) t size_box x y) in Term.image t img; Term.cursor t (Some pos); match Term.event t with - | `End | `Key (`Escape, []) | `Key (`Uchar 67, [`Ctrl]) -> () + | `End | `Key (`Escape, []) -> () + | `Key (`Uchar u, [ `Ctrl ]) when Uchar.to_int u = 67 -> () | `Resize (cols, rows) -> main t (cols, rows) | _ -> main t pos diff --git a/basics_wow.ml b/basics_wow.ml index 6e597d5..53212da 100644 --- a/basics_wow.ml +++ b/basics_wow.ml @@ -1,11 +1,8 @@ open Notty -open Notty_unix (* * ocamlbuild -pkg notty -pkg notty.unix basics_wow.native *) let () = -let wow = I.string A.(fg lightred) "Wow!" in -I.(wow <-> (void 2 0 <|> wow)) -|> Notty_unix.eol -|> Notty_unix.output_image + let wow = I.string A.(fg lightred) "Wow!" in + I.(wow <-> (void 2 0 <|> wow)) |> Notty_unix.eol |> Notty_unix.output_image diff --git a/common.ml b/common.ml index efcc9c9..0191278 100644 --- a/common.ml +++ b/common.ml @@ -3,7 +3,6 @@ open Notty let pow n e = int_of_float (float n ** float e) module List = struct - include List let rec replicate n a = if n < 1 then [] else a :: replicate (n - 1) a @@ -11,46 +10,45 @@ module List = struct let rec range a b = if a > b then [] else a :: range (a + 1) b let rec intersperse a = function - | [] | [_] as t -> t - | x::xs -> x :: a :: intersperse a xs + | ([] | [ _ ]) as t -> t + | x :: xs -> x :: a :: intersperse a xs let rec take n = function - | x::xs when n > 0 -> x :: take (pred n) xs + | x :: xs when n > 0 -> x :: take (pred n) xs | _ -> [] let rec splitat n = function - | x::xs when n > 0 -> - let (a, b) = splitat (pred n) xs in (x::a, b) + | x :: xs when n > 0 -> + let a, b = splitat (pred n) xs in + (x :: a, b) | xs -> ([], xs) let rec chunks n xs = - match splitat n xs with - | (a, []) -> [a] - | (a, b) -> a :: chunks n b - - let rec zip xs ys = match (xs, ys) with - | ([], _) | (_, []) -> [] - | (x::xs, y::ys) -> (x, y) :: zip xs ys + match splitat n xs with a, [] -> [ a ] | a, b -> a :: chunks n b + let rec zip xs ys = + match (xs, ys) with + | [], _ | _, [] -> [] + | x :: xs, y :: ys -> (x, y) :: zip xs ys end module String = struct - include String let repeat n str = let b = Buffer.create 16 in - for _ = 1 to n do Buffer.add_string b str done; + for _ = 1 to n do + Buffer.add_string b str + done; Buffer.contents b end (** A few images used in several places. *) module Images = struct - let i1 = let open I in - (string A.(fg lightblack) "omgbbq" <-> - string A.(fg white ++ bg red) "@") + string A.(fg lightblack) "omgbbq" + <-> string A.(fg white ++ bg red) "@" <|> pad ~t:2 @@ string A.(fg green) "xo" let i2 = I.(hpad 1 1 (hcrop 1 1 @@ tile 3 3 i1) <|> i1) @@ -63,41 +61,41 @@ module Images = struct let i5 = let open I in - tile 5 1 List.( - range 0 15 |> map (fun i -> pad ~t:i ~l:(i*2) i2) |> zcat - ) - + tile 5 1 List.(range 0 15 |> map (fun i -> pad ~t:i ~l:(i * 2) i2) |> zcat) let square color = I.string (A.fg color) "◾" - let rec cantor = let open I in function + let rec cantor = + let open I in + function | 0 -> square A.lightblue | n -> let sub = cantor (pred n) in - hcat (List.replicate (pow 3 n) (square A.lightblue)) <-> - (sub <|> void (pow 3 (n - 1)) 0 <|> sub) + hcat (List.replicate (pow 3 n) (square A.lightblue)) + <-> (sub <|> void (pow 3 (n - 1)) 0 <|> sub) let checker n m i = let w = I.width i in - I.(tile (n/2) (m/2) (hpad 0 w i <-> hpad w 0 i)) + I.(tile (n / 2) (m / 2) (hpad 0 w i <-> hpad w 0 i)) let checker1 = checker 20 20 I.(char A.(bg magenta) ' ' 2 1) - let rec sierp c n = I.( - if n > 1 then - let ss = sierp c (pred n) in ss <-> (ss <|> ss) - else hpad 1 0 (string (A.fg c) "◾") - ) + let rec sierp c n = + I.( + if n > 1 then + let ss = sierp c (pred n) in + ss <-> (ss <|> ss) + else hpad 1 0 (string (A.fg c) "◾")) let grid xxs = xxs |> List.map I.hcat |> I.vcat let outline attr i = - let (w, h) = I.(width i, height i) in + let w, h = I.(width i, height i) in let chr x = I.uchar attr x 1 1 - and hbar = I.uchar attr 0x2500 w 1 - and vbar = I.uchar attr 0x2502 1 h in - let (a, b, c, d) = (chr 0x256d, chr 0x256e, chr 0x256f, chr 0x2570) in - grid [ [a; hbar; b]; [vbar; i; vbar]; [d; hbar; c] ] + and hbar = I.uchar attr 0x2500 w 1 + and vbar = I.uchar attr 0x2502 1 h in + let a, b, c, d = (chr 0x256d, chr 0x256e, chr 0x256f, chr 0x2570) in + grid [ [ a; hbar; b ]; [ vbar; i; vbar ]; [ d; hbar; c ] ] end module Term = Notty_unix.Term @@ -105,12 +103,13 @@ module Term = Notty_unix.Term let simpleterm ~imgf ~f ~s = let term = Term.create () in let imgf (w, h) s = - I.(string A.(fg lightblack) "[ESC quits.]" <-> imgf (w, h - 1) s) in + I.(string A.(fg lightblack) "[ESC quits.]" <-> imgf (w, h - 1) s) + in let rec go s = Term.image term (imgf (Term.size term) s); match Term.event term with - | `End | `Key (`Escape, []) | `Key (`Uchar 67, [`Ctrl]) -> () + | `End | `Key (`Escape, []) | `Key (`Uchar 67, [ `Ctrl ]) -> () | `Resize _ -> go s - | #Unescape.event as e -> - match f s e with Some s -> go s | _ -> () - in go s + | #Unescape.event as e -> ( match f s e with Some s -> go s | _ -> () ) + in + go s diff --git a/common_lwt.ml b/common_lwt.ml index f191bbf..363ed1e 100644 --- a/common_lwt.ml +++ b/common_lwt.ml @@ -1,49 +1,49 @@ open Notty open Lwt.Infix - include Common - module T = Notty_lwt.Term let simpleterm_lwt ~imgf ~f ~s = let term = T.create () in let imgf (w, h) s = - I.(string A.(fg lightblack) "[ESC quits.]" <-> imgf (w, h - 1) s) in - let step e s = match e with - | `Key (`Escape, []) | `Key (`Uchar 67, [`Ctrl]) -> + I.(string A.(fg lightblack) "[ESC quits.]" <-> imgf (w, h - 1) s) + in + let step e s = + match e with + | `Key (`Escape, []) | `Key (`Uchar 67, [ `Ctrl ]) -> T.release term >|= fun () -> s | `Resize dim -> T.image term (imgf dim s) >|= fun () -> s - | #Unescape.event as e -> + | #Unescape.event as e -> ( match f s e with | Some s -> T.image term (imgf (T.size term) s) >|= fun () -> s - | None -> T.release term >|= fun () -> s + | None -> T.release term >|= fun () -> s ) in - ( T.image term (imgf (T.size term) s) - >>= fun () -> Lwt_stream.fold_s step (T.events term) s ) + T.image term (imgf (T.size term) s) + >>= (fun () -> Lwt_stream.fold_s step (T.events term) s) |> Lwt_main.run |> ignore - let timer = function - | None -> Lwt.wait () |> fst + | None -> Lwt.wait () |> fst | Some t -> Lwt_unix.sleep t >|= fun _ -> `Timer -let event e = Lwt_stream.get (T.events e) >|= function - | Some (`Resize _ | #Unescape.event as x) -> x +let event e = + Lwt_stream.get (T.events e) >|= function + | Some ((`Resize _ | #Unescape.event) as x) -> x | None -> `End let simpleterm_lwt_timed ?delay ~f s0 = let term = T.create () in let rec loop (e, t) dim s = - (e t) >>= function - | `End | `Key (`Escape, []) | `Key (`Uchar 67, [`Ctrl]) -> - Lwt.return_unit - | `Resize dim as evt -> invoke (event term, t) dim s evt + e t >>= function + | `End | `Key (`Escape, []) | `Key (`Uchar 67, [ `Ctrl ]) -> Lwt.return_unit + | `Resize dim as evt -> invoke (event term, t) dim s evt | #Unescape.event as evt -> invoke (event term, t) dim s evt - | `Timer as evt -> invoke (e, timer delay) dim s evt + | `Timer as evt -> invoke (e, timer delay) dim s evt and invoke es dim s e = match f dim s e with - | `Continue s -> loop es dim s + | `Continue s -> loop es dim s | `Redraw (s, i) -> T.image term i >>= fun () -> loop es dim s - | `Stop -> Lwt.return_unit in + | `Stop -> Lwt.return_unit + in let size = T.size term in loop (event term, timer delay) size s0 diff --git a/dune b/dune new file mode 100644 index 0000000..e6d562b --- /dev/null +++ b/dune @@ -0,0 +1,19 @@ +(executables + (libraries notty notty.unix lwt lwt.unix notty.lwt) + (names + basics_I_above_another + basics_I_char + basics_I_hcropping + basics_I_negative_vcropping + basics_I_overlay + basics_I_padding + basics_I_side_by_side + basics_I_string + basics_I_uchar + basics_I_uchars + basics_I_vcropping + basics_Lwt_Term_simple_terminal_resize + basics_Lwt_Term_simple_terminal_timeout + basics_Term_simple_terminal + basics_Term_simple_terminal_resize + basics_wow)) diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..c2e4660 --- /dev/null +++ b/dune-project @@ -0,0 +1 @@ +(lang dune 2.8)