Source file dsr.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
open! Core

type query =
  | Device_status
  | Cursor_position
  | Other of int
[@@deriving compare ~localize, equal ~localize, quickcheck, sexp]

type t = { query : query }
[@@deriving compare ~localize, equal ~localize, quickcheck, sexp]

let quickcheck_generator =
  let open Base_quickcheck.Generator in
  let query_gen =
    union
      [ return Device_status
      ; return Cursor_position
      ; map (Int.gen_incl 0 99) ~f:(fun n -> Other n)
      ]
  in
  map query_gen ~f:(fun query -> { query })
;;

let of_params = function
  | [ Some 5 ] -> Some { query = Device_status }
  | [ Some 6 ] -> Some { query = Cursor_position }
  | [ Some n ] -> Some { query = Other n }
  | _ -> None
;;

let query_to_param = function
  | Device_status -> 5
  | Cursor_position -> 6
  | Other n -> n
;;

let to_string { query } = sprintf "\027[%dn" (query_to_param query)

let to_string_hum { query } =
  match query with
  | Device_status -> "(DSR:device-status)"
  | Cursor_position -> "(DSR:cursor-position)"
  | Other n -> sprintf "(DSR:%d)" n
;;