Debian Codegraph @

texmf-dist/doc/support/cluttex/src/message.sml

raw
1 structure Message : sig
2 datatype mode = ALWAYS | AUTO | NEVER
3 val setColors : mode -> unit
4 val exec : string -> unit
5 val error : string -> unit
6 val warn : string -> unit
7 val diag : string -> unit
8 val info : string -> unit
9 val getVerbosity : unit -> int
10 val beMoreVerbose : unit -> unit
11 val setTypeStyle : ANSIStyle.style -> unit
12 val setExecuteStyle : ANSIStyle.style -> unit
13 val setErrorStyle : ANSIStyle.style -> unit
14 val setWarningStyle : ANSIStyle.style -> unit
15 val setDiagnosticStyle : ANSIStyle.style -> unit
16 val setInformationStyle : ANSIStyle.style -> unit
17 end = struct
18 val () = Lua.setGlobal ("CLUTTEX_VERBOSITY", Lua.fromInt 0)
19 fun getVerbosity () : int = Lua.unsafeFromValue (Lua.global "CLUTTEX_VERBOSITY")
20 fun beMoreVerbose () = let val verbosity = Lua.global "CLUTTEX_VERBOSITY"
21 in Lua.setGlobal ("CLUTTEX_VERBOSITY", Lua.+ (verbosity, Lua.fromInt 1))
22 end
23
24 datatype mode = ALWAYS | AUTO | NEVER
25 val useColors = ref false
26 fun setColors ALWAYS = let val isatty = Lua.call1 Lua.Lib.require #[Lua.fromString "texrunner.isatty"]
27 val enableVirtualTerminal = Lua.field (isatty, "enable_virtual_terminal")
28 val stderr = Lua.field (Lua.global "io", "stderr")
29 in useColors := true
30 ; if not (Lua.isFalsy enableVirtualTerminal) then
31 let val succ = Lua.call1 enableVirtualTerminal #[stderr]
32 in if Lua.isFalsy succ andalso getVerbosity () >= 2 then
33 TextIO.output (TextIO.stdErr, "ClutTeX: Failed to enable virtual terminal\n")
34 else
35 ()
36 end
37 else
38 ()
39 end
40 | setColors AUTO = let val isatty = Lua.call1 Lua.Lib.require #[Lua.fromString "texrunner.isatty"]
41 val enableVirtualTerminal = Lua.field (isatty, "enable_virtual_terminal")
42 val stderr = Lua.field (Lua.global "io", "stderr")
43 val u = not (Lua.isFalsy (Lua.call1 (Lua.field (isatty, "isatty")) #[stderr]))
44 in useColors := u
45 ; if u andalso not (Lua.isFalsy enableVirtualTerminal) then
46 let val succ : bool = Lua.unsafeFromValue (Lua.call1 enableVirtualTerminal #[stderr])
47 in useColors := succ
48 ; if not succ andalso getVerbosity () >= 2 then
49 TextIO.output (TextIO.stdErr, "ClutTeX: Failed to enable virtual terminal\n")
50 else
51 ()
52 end
53 else
54 ()
55 end
56 | setColors NEVER = useColors := false
57
58 structure CMD = struct
59 (* ESCAPE: hex 1B = dec 27 = oct 33 *)
60 val reset = "\027[0m"
61 val underline = "\027[4m"
62 val fg_black = "\027[30m"
63 val fg_red = "\027[31m"
64 val fg_green = "\027[32m"
65 val fg_yellow = "\027[33m"
66 val fg_blue = "\027[34m"
67 val fg_magenta = "\027[35m"
68 val fg_cyan = "\027[36m"
69 val fg_white = "\027[37m"
70 val fg_reset = "\027[39m"
71 val bg_black = "\027[40m"
72 val bg_red = "\027[41m"
73 val bg_green = "\027[42m"
74 val bg_yellow = "\027[43m"
75 val bg_blue = "\027[44m"
76 val bg_magenta = "\027[45m"
77 val bg_cyan = "\027[46m"
78 val bg_white = "\027[47m"
79 val bg_reset = "\027[49m"
80 val fg_x_black = "\027[90m"
81 val fg_x_red = "\027[91m"
82 val fg_x_green = "\027[92m"
83 val fg_x_yellow = "\027[93m"
84 val fg_x_blue = "\027[94m"
85 val fg_x_magenta = "\027[95m"
86 val fg_x_cyan = "\027[96m"
87 val fg_x_white = "\027[97m"
88 val bg_x_black = "\027[100m"
89 val bg_x_red = "\027[101m"
90 val bg_x_green = "\027[102m"
91 val bg_x_yellow = "\027[103m"
92 val bg_x_blue = "\027[104m"
93 val bg_x_magenta = "\027[105m"
94 val bg_x_cyan = "\027[106m"
95 val bg_x_white = "\027[107m"
96 end
97
98 val typeStyle : string ref
99 = ref (ANSIStyle.toString { foreground = SOME ANSIColor.BRIGHT_WHITE
100 , background = SOME ANSIColor.RED
101 , bold = false
102 , dim = false
103 , underline = false
104 , blink = false
105 , reverse = false
106 , italic = false
107 , strike = false
108 }
109 )
110 val executeStyle : string ref
111 = ref (ANSIStyle.toString { foreground = SOME ANSIColor.CYAN
112 , background = NONE
113 , bold = false
114 , dim = false
115 , underline = false
116 , blink = false
117 , reverse = false
118 , italic = false
119 , strike = false
120 }
121 )
122 val errorStyle : string ref
123 = ref (ANSIStyle.toString { foreground = SOME ANSIColor.RED
124 , background = NONE
125 , bold = false
126 , dim = false
127 , underline = false
128 , blink = false
129 , reverse = false
130 , italic = false
131 , strike = false
132 }
133 )
134 val warningStyle : string ref
135 = ref (ANSIStyle.toString { foreground = SOME ANSIColor.BLUE
136 , background = NONE
137 , bold = false
138 , dim = false
139 , underline = false
140 , blink = false
141 , reverse = false
142 , italic = false
143 , strike = false
144 }
145 )
146 val diagnosticStyle : string ref
147 = ref (ANSIStyle.toString { foreground = SOME ANSIColor.BLUE
148 , background = NONE
149 , bold = false
150 , dim = false
151 , underline = false
152 , blink = false
153 , reverse = false
154 , italic = false
155 , strike = false
156 }
157 )
158 val informationStyle : string ref
159 = ref (ANSIStyle.toString { foreground = SOME ANSIColor.MAGENTA
160 , background = NONE
161 , bold = false
162 , dim = false
163 , underline = false
164 , blink = false
165 , reverse = false
166 , italic = false
167 , strike = false
168 }
169 )
170
171 fun setTypeStyle style = typeStyle := ANSIStyle.toString style
172 fun setExecuteStyle style = executeStyle := ANSIStyle.toString style
173 fun setErrorStyle style = errorStyle := ANSIStyle.toString style
174 fun setWarningStyle style = warningStyle := ANSIStyle.toString style
175 fun setDiagnosticStyle style = diagnosticStyle := ANSIStyle.toString style
176 fun setInformationStyle style = informationStyle := ANSIStyle.toString style
177
178 fun exec commandline = if !useColors then
179 TextIO.output (TextIO.stdErr, !typeStyle ^ "[EXEC]" ^ ANSIStyle.resetAll ^ " " ^ !executeStyle ^ commandline ^ ANSIStyle.resetAll ^ "\n")
180 else
181 TextIO.output (TextIO.stdErr, "[EXEC] " ^ commandline ^ "\n")
182 fun error message = if !useColors then
183 TextIO.output (TextIO.stdErr, !typeStyle ^ "[ERROR]" ^ ANSIStyle.resetAll ^ " " ^ !errorStyle ^ message ^ ANSIStyle.resetAll ^ "\n")
184 else
185 TextIO.output (TextIO.stdErr, "[ERROR] " ^ message ^ "\n")
186 fun warn message = if !useColors then
187 TextIO.output (TextIO.stdErr, !typeStyle ^ "[WARN]" ^ ANSIStyle.resetAll ^ " " ^ !warningStyle ^ message ^ ANSIStyle.resetAll ^ "\n")
188 else
189 TextIO.output (TextIO.stdErr, "[WARN] " ^ message ^ "\n")
190 fun diag message = if !useColors then
191 TextIO.output (TextIO.stdErr, !typeStyle ^ "[DIAG]" ^ ANSIStyle.resetAll ^ " " ^ !diagnosticStyle ^ message ^ ANSIStyle.resetAll ^ "\n")
192 else
193 TextIO.output (TextIO.stdErr, "[DIAG] " ^ message ^ "\n")
194 fun info message = if !useColors then
195 TextIO.output (TextIO.stdErr, !typeStyle ^ "[INFO]" ^ ANSIStyle.resetAll ^ " " ^ !informationStyle ^ message ^ ANSIStyle.resetAll ^ "\n")
196 else
197 TextIO.output (TextIO.stdErr, "[INFO] " ^ message ^ "\n")
198 end;