package wax-lib

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file colors.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
type flag = Never | Always | Auto

let should_use_color ~color ~out_channel =
  match color with
  | Never -> false
  | Always -> true
  | Auto -> (
      match out_channel with
      | None -> false
      | Some oc -> (
          Option.is_none (Sys.getenv_opt "NO_COLOR")
          && (match Sys.getenv_opt "TERM" with
            | None | Some "dumb" -> false
            | _ -> true)
          &&
            try Unix.isatty (Unix.descr_of_out_channel oc)
            with Unix.Unix_error _ -> false))

let update_flag ~color =
  if should_use_color ~color ~out_channel:(Some stdout) then Always else Never

module Ansi = struct
  let reset = "\027[0m"
  let bold = "\027[1m"
  let red = "\027[31m"
  let high_red = "\027[91m"
  let green = "\027[32m"
  let yellow = "\027[33m"
  let high_yellow = "\027[93m"
  let blue = "\027[34m"
  let magenta = "\027[35m"
  let cyan = "\027[36m"
  let white = "\027[37m"
  let grey = "\027[90m"
end

type style =
  | Keyword (* func / fn *)
  | Instruction (* i32.const *)
  | Attribute (* offset=4 *)
  | Type (* i32 *)
  | Identifier
  | Constant
  | String
  | Operator (* + *)
  | Annotation (* (@...), rust macro *)
  | Comment
  | Punctuation (* { ( : *)

type theme = {
  keyword : string;
  instruction : string;
  attribute : string;
  type_ : string;
  identifier : string;
  constant : string;
  string : string;
  operator : string;
  annotation : string;
  comment : string;
  punctuation : string;
  reset : string;
}

let escape_sequence theme style =
  match style with
  | Keyword -> theme.keyword
  | Instruction -> theme.instruction
  | Attribute -> theme.attribute
  | Type -> theme.type_
  | Identifier -> theme.identifier
  | Constant -> theme.constant
  | String -> theme.string
  | Operator -> theme.operator
  | Annotation -> theme.annotation
  | Comment -> theme.comment
  | Punctuation -> theme.punctuation

(* The two source palettes, one per surface language. Each is the single source
   of truth: the language's source printer ([Wax_lang.Output] / [Wax_wasm.Output])
   uses it to render whole modules, and diagnostics use it to colour an AST
   fragment (a type, an identifier) embedded in a message, so a diagnostic about
   Wax code and one about WebAssembly code colour their types the same way that
   language's source does (e.g. Wax types cyan, WAT types red). *)
let wax_theme =
  {
    keyword = Ansi.bold ^ Ansi.magenta;
    operator = Ansi.bold ^ Ansi.white;
    annotation = Ansi.blue;
    attribute = Ansi.magenta;
    type_ = Ansi.cyan;
    identifier = Ansi.yellow;
    constant = Ansi.bold ^ Ansi.blue;
    string = Ansi.green;
    comment = Ansi.grey;
    punctuation = Ansi.white;
    instruction = "";
    reset = Ansi.reset;
  }

let wat_theme =
  {
    keyword = Ansi.bold ^ Ansi.magenta;
    instruction = Ansi.white;
    attribute = Ansi.magenta;
    type_ = Ansi.red;
    identifier = Ansi.yellow;
    constant = Ansi.bold ^ Ansi.blue;
    string = Ansi.green;
    annotation = Ansi.blue;
    comment = Ansi.grey;
    punctuation = Ansi.cyan;
    operator = "";
    reset = Ansi.reset;
  }

let no_color =
  {
    keyword = "";
    instruction = "";
    attribute = "";
    type_ = "";
    identifier = "";
    constant = "";
    string = "";
    annotation = "";
    comment = "";
    punctuation = "";
    operator = "";
    reset = "";
  }