Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / console.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8structure Console: CONSOLE =
9struct
10
11(* Information from Chapter 20 of Linux Application Development,
12 * by Johnson and Troan.
13 *)
14
15structure Background =
16 struct
17 datatype t = Black | Red | Green | Brown | Blue | Magenta | Cyan | Gray
18 end
19
20structure Foreground =
21 struct
22 datatype t =
23 DarkGray | BrightRed | BrightGreen | Yellow | BrightBlue
24 | BrightMagenta | BrightCyan | White
25 end
26
27val esc = "\027["
28
29structure CharRendition =
30 struct
31 datatype t =
32 Default
33 | Bold
34 | Dim
35 | Normal
36 | UnderlineOn
37 | UnderlineOff
38 | UnderlineOnDefaultForeground
39 | UnderlineOffDefaultForeground
40 | BlinkOn
41 | BlinkOff
42 | ReverseVideoOn
43 | ReverseVideoOff
44 | Foreground of Foreground.t
45 | Background of Background.t
46
47 fun set(l: t list): string =
48 concat(esc
49 :: List.fold(rev l, [], fn (c, l) =>
50 let
51 val n =
52 case c of
53 Default => "0"
54 | Bold => "1"
55 | Dim => "2"
56 | Normal => "21"
57 | UnderlineOn => "4"
58 | UnderlineOff => "24"
59 | UnderlineOnDefaultForeground => "38"
60 | UnderlineOffDefaultForeground => "39"
61 | BlinkOn => "5"
62 | BlinkOff => "25"
63 | ReverseVideoOn => "7"
64 | ReverseVideoOff => "27"
65 | Foreground f =>
66 let datatype z = datatype Foreground.t
67 in case f of
68 DarkGray => "30"
69 | BrightRed => "31"
70 | BrightGreen => "32"
71 | Yellow => "33"
72 | BrightBlue => "34"
73 | BrightMagenta => "35"
74 | BrightCyan => "36"
75 | White => "37"
76 end
77 | Background b =>
78 let datatype z = datatype Background.t
79 in case b of
80 Black => "40"
81 | Red => "41"
82 | Green => "42"
83 | Brown => "43"
84 | Blue => "44"
85 | Magenta => "45"
86 | Cyan => "46"
87 | Gray => "47"
88 end
89 in case l of
90 [] => [n, "m"]
91 | _ => n :: ";" :: l
92 end))
93 end
94
95fun moveToColumn c =
96 let
97 val columns =
98 case Process.getEnv "COLUMNS" of
99 NONE => 80
100 | SOME c => valOf(Int.fromString c)
101 (* 300 is kind of arbitrary, but it's what they do in
102 * /etc/sysconfig/init.
103 *)
104 in concat[esc, "300C", esc, Int.toString(columns - c), "D"]
105 end
106
107end