Debian Codegraph @

deps/clic/src/clic-tty.ads

raw
1 with ANSI;
2
3 package CLIC.TTY
4 with Preelaborate
5 is
6
7 -- Color/Formatting related subprograms. These won't have any
8 -- effect if a redirection of output is detected, or if global
9 -- flag Simple_Logging.Is_TTY is false.
10
11 -- Re-expose for clients
12 package ANSI renames Standard.ANSI;
13
14 function Is_TTY return Boolean;
15
16 procedure Force_Disable_TTY
17 with Post => not Is_TTY;
18 -- Disable TTY support even if availabe
19
20 --------------------
21 -- Color enabling --
22 --------------------
23
24 function Color_Enabled return Boolean;
25
26 procedure Disable_Color;
27 -- Disables color/formatting output even when TTY is capable
28
29 procedure Enable_Color (Force : Boolean := False);
30 -- Prepares colors for the logging messages. Unless Force, will do nothing
31 -- if a console redirection is detected.
32
33 function Format (Text : String;
34 Fore : ANSI.Colors := ANSI.Default;
35 Back : ANSI.Colors := ANSI.Default;
36 Style : ANSI.Styles := ANSI.Default)
37 return String;
38 -- Wrap text with the appropriate ANSI sequences. Following text will be
39 -- unaffected. Default colors are interpreted as no change of color (will
40 -- result in no color sequences), not as setting the default color (which
41 -- is always set after a color change).
42
43 ------------------------
44 -- Predefined formats --
45 ------------------------
46
47 function Text_With_Fallback (Text : String; Fallback : String)
48 return String
49 with Post => Text_With_Fallback'Result =
50 (if Color_Enabled and then Is_TTY then Text else Fallback);
51 -- Intended to have a rich text and a safe alternative
52
53 function Info (Text : String := "") return String;
54 -- Prepends Text with a Emph ("🛈") or "Note: " if no tty color enabled
55
56 function Success (Text : String := "") return String;
57 -- Prepends Text (in normal formatting) with a green check mark, or a
58 -- simple Success: text if no tty or color enabled.
59
60 function OK (Text : String) return String;
61 -- Bold Light_Green
62
63 function Emph (Text : String) return String;
64 -- Something to highlight not negatively, bold cyan
65
66 function Error (Text : String) return String;
67 -- Bold Red
68
69 function Warn (Text : String) return String;
70 -- Bold Yellow
71
72 function Bold (Text : String) return String;
73
74 function Dim (Text : String) return String;
75
76 function Italic (Text : String) return String;
77
78 function Underline (Text : String) return String;
79
80 function Description (Text : String) return String;
81 -- Not bold cyan for crate descriptions
82
83 function Terminal (Text : String) return String;
84 -- For showing commands that the user can run; mimics old amber displays.
85
86 function URL (Text : String) return String;
87
88 function Version (Text : String) return String;
89 -- For versions/version sets, bold magenta
90
91 private
92
93 function Text_With_Fallback (Text : String; Fallback : String)
94 return String
95 is (if Color_Enabled and then Is_TTY
96 then Text
97 else Fallback);
98
99 function Info (Text : String := "") return String is
100 (if Color_Enabled and then Is_TTY
101 then Emph ("ⓘ") & " " & Text
102 else "Note: " & Text);
103
104 function Success (Text : String := "") return String is
105 (if Color_Enabled and then Is_TTY
106 then OK ("✓") & " " & Text
107 else "Success: " & Text);
108
109 function OK (Text : String) return String is
110 (Format (Text,
111 Fore => ANSI.Light_Green,
112 Style => ANSI.Bright));
113
114 function Emph (Text : String) return String is
115 (Format (Text,
116 Fore => ANSI.Cyan,
117 Style => ANSI.Bright));
118
119 function Error (Text : String) return String is
120 (Format (Text,
121 Fore => ANSI.Red,
122 Style => ANSI.Bright));
123
124 function Warn (Text : String) return String is
125 (Format (Text,
126 Fore => ANSI.Yellow,
127 Style => ANSI.Bright));
128
129 function Bold (Text : String) return String is
130 (Format (Text,
131 Style => ANSI.Bright));
132
133 function Dim (Text : String) return String is
134 (Format (Text,
135 Style => ANSI.Dim));
136
137 function Italic (Text : String) return String is
138 (Format (Text,
139 Style => ANSI.Italic));
140
141 function Underline (Text : String) return String is
142 (Format (Text,
143 Style => ANSI.Underline));
144
145 function Name (Text : String) return String is
146 (Bold (Text));
147
148 function Description (Text : String) return String is
149 (Format (Text,
150 Fore => ANSI.Light_Cyan));
151
152 function Terminal (Text : String) return String is
153 (if Color_Enabled and then Is_TTY
154 then ANSI.Color_Wrap (Text, ANSI.Palette_Fg (5, 3, 0))
155 else Text);
156
157 function URL (Text : String) return String renames Version;
158
159 function Version (Text : String) return String is
160 (Format (Text,
161 Fore => ANSI.Magenta,
162 Style => ANSI.Bright));
163
164 end CLIC.TTY;