Skip to content

Ocaml Library to Capture IO in a manner similar to perl's IO::Capture

Notifications You must be signed in to change notification settings

sylvarant/captureio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CaptureIO

Build Status Coverage Status artistic

What is CaptureIO

CaptureIO is an OCaml library that captures the terminal print outs of a sequence of code or function, in a manner similar to Perl's IO::Capture.

Usage

To capture the output of a term wrap it in a lambda that takes unit as argument.

let str = capture (fun () -> print_string "hello") in
assert(str = "hello");

Alternatively, you can start a capture session to capture the output of a block of code.

let session = start_capture() in
prerr_string ("error1");
prerr_string ("error2");
let str = (finish_capture session) in
assert(str = "error1error2");

It is also possible to capture the output to a specific file descriptor.

let file_chan = Pervasives.open_out "somefile" in
let file_descr = Unix.descr_of_out_channel file_chan in
let session = start_capture_descr [file_descr] in
  print_string("not captured");
  Pervasives.output_string file_chan "is captured";
let result = finish_capture session in
assert(result = "is captured"); 

For the full API see the file lib/capturio.mli which is fully documented. For more examples see the tests in t/01-capture_test.ml.

License

Artistic License 2.0

About

Ocaml Library to Capture IO in a manner similar to perl's IO::Capture

Resources

Stars

Watchers

Forks

Packages

No packages published