This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* Assume a module called [Proc.Cmd] with a [run] function is loaded. *) | |
| (* Compile with the [unix.cma/cmxa] module. *) | |
| module Timer = struct | |
| let time f x = | |
| let start = Unix.gettimeofday () in | |
| let result = f x in | |
| let stop = Unix.gettimeofday () in | |
| let delta = stop -. start in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Logs = struct | |
| let hash = Hashtbl.create 256 | |
| let log name msg = | |
| let ch = Hashtbl.find hash name in | |
| Printf.fprintf ch "%s\n%!" msg | |
| let close name = | |
| let ch = Hashtbl.find hash name in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Files = struct | |
| let line c = | |
| try | |
| let result = input_line c in | |
| Some result | |
| with End_of_file -> None | |
| let rec read c acc = | |
| match line c with |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Logs = struct | |
| let hash : (string, string) Hashtbl.t = Hashtbl.create 256 | |
| let show_all = fun () -> | |
| Printf.printf "- - - HASH CONTENTS:\n%!"; | |
| Hashtbl.iter (fun key value -> Printf.printf "- - - '%s': '%s'\n%!" key value) hash | |
| let register_msg key value = | |
| Printf.printf "- Registering '%s': '%s'\n%!" key value; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Logs = struct | |
| exception NoSuchLog of string | |
| let file_opts = [Unix.O_WRONLY; Unix.O_APPEND; Unix.O_CREAT] | |
| let file_perm = 0o600 | |
| let hash : (string, out_channel) Hashtbl.t = Hashtbl.create 256 | |
| let register key oc = Hashtbl.add hash key oc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| exception Foo of string | |
| let main () = | |
| print_endline "Running"; | |
| raise (Foo "Something went wrong") | |
| let handle_errors f () = | |
| Printexc.register_printer | |
| (function | |
| | Foo msg -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let has_match tests = | |
| let matches = List.filter (fun s -> s = true) tests in | |
| List.length matches > 0 | |
| let is_exact_match str_1 str_2 = str_1 = str_2 | |
| let is_regex_match str_1 str_2 = | |
| let r = Str.regexp str_1 in | |
| Str.string_match r str_2 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type ttyfmt = | |
| | Plain | |
| | Red | |
| | Green | |
| | Warn | |
| | Emph | |
| let reset_tag = "\x1b[0m" | |
| let start_of fmt = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Tty_str = struct | |
| type ttyfmt = | |
| | Plain | |
| | Red | |
| | Green | |
| | Yellow | |
| | Bold | |
| | Dim | |
| | Italic |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* A utility for building a table that can be printed on a TTY. | |
| Use it by calling [create], and passing it a list of string lists. | |
| The argument is a list of rows, where each row is a list of strings. | |
| The [create] function returns a string you can print to a TTY. *) | |
| let rec longest items res = | |
| match items with | |
| | [] -> res | |
| | hd :: tl -> | |
| match hd > res with |