Source file ascii_table.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
open Core
include Ascii_table_kernel

let output_screen ~oc ~screen ~bars =
  Ascii_table_kernel.Screen.render screen ~bars ~close:ignore ~output:(fun attr buf ->
    Console.Ansi.output_string attr oc (Buffer.contents buf);
    Buffer.clear buf)
;;

type ('row, 'rest) renderer =
  ?display:Display.t (* Default: short_box *)
  -> ?spacing:int (* Default: 1 *)
  -> ?limit_width_to:int (* defaults to 90 characters *)
  -> ?header_attr:Attr.t list
  -> ?bars:[ `Ascii | `Unicode ] (* defaults to [`Unicode] *)
  -> ?display_empty_rows:bool (* Default: false *)
  -> ?prefer_split_on_spaces:bool
  -> 'row Column.t list
  -> 'row list
  -> 'rest

let output
  ?display
  ?spacing
  ?limit_width_to
  ?header_attr
  ?(bars = `Unicode)
  ?display_empty_rows
  ?(prefer_split_on_spaces = false)
  cols
  data
  ~oc
  =
  Option.iter
    (Ascii_table_kernel.draw
       ?display
       ?spacing
       ?limit_width_to
       ?header_attr
       ?display_empty_rows
       ~prefer_split_on_spaces
       cols
       data)
    ~f:(fun screen -> output_screen ~screen ~bars ~oc)
;;

let to_string_gen
  ?display
  ?spacing
  ?limit_width_to
  ?header_attr
  ?(bars = `Unicode)
  ?display_empty_rows
  ?(prefer_split_on_spaces = false)
  cols
  data
  ~string_with_attr
  =
  match
    Ascii_table_kernel.draw
      ?display
      ?spacing
      ?limit_width_to
      ?header_attr
      ?display_empty_rows
      ~prefer_split_on_spaces
      cols
      data
  with
  | None -> ""
  | Some screen -> Screen.to_string screen ~bars ~string_with_attr
;;

let to_string_noattr
  ?display
  ?spacing
  ?limit_width_to
  ?header_attr
  ?bars
  ?display_empty_rows
  ?prefer_split_on_spaces
  cols
  data
  =
  to_string_gen
    ?display
    ?spacing
    ?limit_width_to
    ?header_attr
    ?bars
    ?display_empty_rows
    ?prefer_split_on_spaces
    cols
    data
    ~string_with_attr:(fun _attrs str -> str)
;;

let to_string
  ?display
  ?spacing
  ?limit_width_to
  ?header_attr
  ?bars
  ?display_empty_rows
  ?prefer_split_on_spaces
  cols
  data
  =
  to_string_gen
    ?display
    ?spacing
    ?limit_width_to
    ?header_attr
    ?bars
    ?display_empty_rows
    ?prefer_split_on_spaces
    cols
    data
    ~string_with_attr:Console.Ansi.string_with_attr
;;

let default_max_col_width = 90

let simple_list_table_internal
  ?index
  ?(display = Ascii_table_kernel.Display.line)
  ?spacing
  ?(limit_width_to = 160)
  ?min_col_width
  ?(max_col_width = default_max_col_width)
  ?header_attr
  ?bars
  ?display_empty_rows
  ?prefer_split_on_spaces
  cols
  data
  ~(f : (_, _) renderer)
  =
  let cols, data =
    let cols_and_data_of_strings =
      Ascii_table_kernel.cols_and_data_of_strings [@alert "-ascii_table_kernel_internal"]
    in
    cols_and_data_of_strings ?index ?min_col_width ~max_col_width cols data
  in
  f
    ~display
    ?spacing
    ~limit_width_to
    ?header_attr
    ?bars
    ?display_empty_rows
    ?prefer_split_on_spaces
    cols
    data
;;

let simple_list_table
  ?(oc = stdout)
  ?index
  ?display
  ?spacing
  ?limit_width_to
  ?min_col_width
  ?max_col_width
  ?header_attr
  ?bars
  ?display_empty_rows
  ?prefer_split_on_spaces
  cols
  data
  =
  simple_list_table_internal
    ?index
    ?display
    ?spacing
    ?limit_width_to
    ?min_col_width
    ?max_col_width
    ?header_attr
    ?bars
    ?display_empty_rows
    ?prefer_split_on_spaces
    cols
    data
    ~f:(output ~oc)
;;

let simple_list_table_string
  ?index
  ?display
  ?spacing
  ?limit_width_to
  ?min_col_width
  ?max_col_width
  ?header_attr
  ?bars
  ?display_empty_rows
  ?prefer_split_on_spaces
  cols
  data
  =
  simple_list_table_internal
    ?index
    ?display
    ?spacing
    ?limit_width_to
    ?min_col_width
    ?max_col_width
    ?header_attr
    ?bars
    ?display_empty_rows
    ?prefer_split_on_spaces
    cols
    data
    ~f:to_string
;;