Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh project build #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .ocamlformat
Empty file.
2 changes: 0 additions & 2 deletions basics_I_above_another.ml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 2 additions & 5 deletions basics_I_char.ml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions basics_I_hcropping.ml
Original file line number Diff line number Diff line change
@@ -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
22 changes: 11 additions & 11 deletions basics_I_negative_vcropping.ml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions basics_I_overlay.ml
Original file line number Diff line number Diff line change
@@ -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
24 changes: 13 additions & 11 deletions basics_I_padding.ml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 2 additions & 6 deletions basics_I_side_by_side.ml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions basics_I_string.ml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 3 additions & 6 deletions basics_I_uchar.ml
Original file line number Diff line number Diff line change
@@ -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
24 changes: 16 additions & 8 deletions basics_I_uchars.ml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 8 additions & 10 deletions basics_I_vcropping.ml
Original file line number Diff line number Diff line change
@@ -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
49 changes: 26 additions & 23 deletions basics_Lwt_Term_simple_terminal_resize.ml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
51 changes: 15 additions & 36 deletions basics_Lwt_Term_simple_terminal_timeout.ml
Original file line number Diff line number Diff line change
@@ -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 ())
15 changes: 7 additions & 8 deletions basics_Term_simple_terminal.ml
Original file line number Diff line number Diff line change
@@ -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
Loading