package matrix

Build not yet checked
  1. Overview
  2. Docs
Fast, modern terminal toolkit for OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

mosaic-0.3.0.tbz
sha256=a92cbbcbe016fb19fb0a749222da0e459a9567aef3f4c7340d9bd6b71c53ed6f
sha512=6a7ab054605538bb31a2b73a1a6d093981a2704c9f0af5852221c05735290d74aca7c7282298c4d0f91e86a800e2ccd94b4918af883cd963449c758eb6b272b3

doc/src/matrix.ansi/writer.ml.html

Source file writer.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(* [fixed] writers write into the caller's bytes and never replace them. *)
type t = { mutable bytes : bytes; mutable pos : int; fixed : bool }

exception Buffer_full

let create capacity = { bytes = Bytes.create capacity; pos = 0; fixed = false }
let of_bytes bytes = { bytes; pos = 0; fixed = true }
let[@inline] len w = w.pos
let[@inline] reset_pos w = w.pos <- 0
let[@inline] buffer w = w.bytes
let contents w = Bytes.sub_string w.bytes 0 w.pos

let grow w additional =
  if w.fixed then raise Buffer_full;
  if additional > Sys.max_string_length - w.pos then
    invalid_arg "Ansi.Writer: output too large";
  let capacity = Bytes.length w.bytes in
  let doubled =
    if capacity > Sys.max_string_length / 2 then Sys.max_string_length
    else max 16 (capacity * 2)
  in
  let bytes = Bytes.create (max (w.pos + additional) doubled) in
  Bytes.blit w.bytes 0 bytes 0 w.pos;
  w.bytes <- bytes

let[@inline] write_char w c =
  if w.pos = Bytes.length w.bytes then grow w 1;
  Bytes.unsafe_set w.bytes w.pos c;
  w.pos <- w.pos + 1

let[@inline] write_string w s =
  let len = String.length s in
  if len > Bytes.length w.bytes - w.pos then grow w len;
  Bytes.blit_string s 0 w.bytes w.pos len;
  w.pos <- w.pos + len

let[@inline] write_subbytes w bytes off len =
  if len < 0 || off < 0 || off > Bytes.length bytes - len then
    invalid_arg "Ansi.Writer: invalid source slice";
  if len > Bytes.length w.bytes - w.pos then grow w len;
  Bytes.blit bytes off w.bytes w.pos len;
  w.pos <- w.pos + len