• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16package std.core;
17
18/**
19 * The maximum depth of printing nested objects
20 * Useful for cyclic linked objects
21 */
22const MAX_CONSOLE_PRINT_DEPTH: int = 10
23
24/**
25 * Standard console, that provides access to standard output stream
26 */
27export final class Console {
28    internal constructor() {}
29
30    /**
31     * Prints an object to the console
32     *
33     * @param i value to print
34     */
35    public print(o: NullishType): void {
36        this.print(Value.of(o).toPrint(MAX_CONSOLE_PRINT_DEPTH))
37    }
38
39    /**
40     * Prints a string to the console
41     *
42     * @param i value to print
43     */
44    public native print(i: String): void
45
46    /**
47     * Prints a boolean to the console
48     *
49     * @param i value to print
50     */
51    public native print(i: boolean): void
52
53    /**
54     * Prints a byte to the console
55     *
56     * @param i value to print
57     */
58    public native print(i: byte): void
59
60    /**
61     * Prints a short to the console
62     *
63     * @param i value to print
64     */
65    public native print(i: short): void
66
67    /**
68     * Prints a char to the console
69     *
70     * @param i value to print
71     */
72    public native print(i: char): void
73
74    /**
75     * Prints a int to the console
76     *
77     * @param i value to print
78     */
79    public native print(i: int): void
80
81    /**
82     * Prints a long to the console
83     *
84     * @param i value to print
85     */
86    public native print(i: long): void
87
88    /**
89     * Prints a float to the console
90     *
91     * @param i value to print
92     */
93    public print(f: float): void {
94        let boxed: Float = f;
95        this.print(boxed.toString());
96    }
97
98    /**
99     * Prints a double to the console
100     *
101     * @param i value to print
102     */
103    public print(d: double): void {
104        let boxed: Double = d;
105        this.print(boxed.toString());
106    }
107
108    /**
109     * Puts newline (\n) to the console
110     */
111    public native println(): void
112
113    /**
114     * Prints an object to the console and puts newline
115     *
116     * @param i value to print
117     */
118    public println(i: Object): void {
119        this.print(i);
120        this.println();
121    }
122
123    /**
124     * Prints a string to the console and puts newline
125     *
126     * @param i value to print
127     */
128    public println(i: String): void {
129        this.print(i);
130        this.println();
131    }
132
133    /**
134     * Prints a boolean to the console
135     *
136     * @param i value to print
137     */
138    public println(i: boolean): void {
139        this.print(i);
140        this.println();
141    }
142
143    /**
144     * Prints a byte to the console and puts newline
145     *
146     * @param i value to print
147     */
148    public println(i: byte): void {
149        this.print(i);
150        this.println();
151    }
152
153    /**
154     * Prints a short to the console and puts newline
155     *
156     * @param i value to print
157     */
158    public println(i: short): void {
159        this.print(i);
160        this.println();
161    }
162
163    /**
164     * Prints a char to the console and puts newline
165     *
166     * @param i value to print
167     */
168    public println(i: char): void {
169        this.print(i);
170        this.println();
171    }
172
173    /**
174     * Prints a int to the console and puts newline
175     *
176     * @param i value to print
177     */
178    public println(i: int): void {
179        this.print(i);
180        this.println();
181    }
182
183    /**
184     * Prints a long to the console and puts newline
185     *
186     * @param i value to print
187     */
188    public println(i: long): void {
189        this.print(i);
190        this.println();
191    }
192
193    /**
194     * Prints a float to the console and puts newline
195     *
196     * @param i value to print
197     */
198    public println(i: float): void {
199        this.print(i);
200        this.println();
201    }
202
203    /**
204     * Prints a double to the console and puts newline
205     *
206     * @param i value to print
207     */
208    public println(i: double): void {
209        this.print(i);
210        this.println();
211    }
212
213    // TODO(ivan-tyulyandin): move this to escompat part
214    // Now in core part to provide subset
215    //----------------------------------------
216    //           escompat log
217    //----------------------------------------
218
219    /**
220     * Prints an nullish object to the console and puts newline
221     *
222     * @param i value to print
223     */
224    public log(...vals: NullishType[]): void {
225        const len = vals.length
226        for (let i = 0; i < len; i++) {
227            this.print(vals[i])
228            if (i != len - 1) {
229                this.print(" ")
230            }
231        }
232        this.println()
233    }
234
235    /**
236     * Prints a string to the console and puts newline
237     *
238     * @param i value to print
239     */
240    public log(i: String): void {
241        this.print(i);
242        this.println();
243    }
244
245    /**
246     * Prints a boolean to the console and puts newline
247     *
248     * @param i value to print
249     */
250    public log(i: boolean): void {
251        this.print(i);
252        this.println();
253    }
254
255    /**
256     * Prints a byte to the console and puts newline
257     *
258     * @param i value to print
259     */
260    public log(i: byte): void {
261        this.print(i);
262        this.println();
263    }
264
265    /**
266     * Prints a short to the console and puts newline
267     *
268     * @param i value to print
269     */
270    public log(i: short): void {
271        this.print(i);
272        this.println();
273    }
274
275    /**
276     * Prints a char to the console and puts newline
277     *
278     * @param i value to print
279     */
280    public log(i: char): void {
281        this.print(i);
282        this.println();
283    }
284
285    /**
286     * Prints a int to the console and puts newline
287     *
288     * @param i value to print
289     */
290    public log(i: int): void {
291        this.print(i);
292        this.println();
293    }
294
295    /**
296     * Prints a long to the console and puts newline
297     *
298     * @param i value to print
299     */
300    public log(i: long): void {
301        this.print(i);
302        this.println();
303    }
304
305    /**
306     * Prints a float to the console and puts newline
307     *
308     * @param i value to print
309     */
310    public log(i: float): void {
311        this.print(i);
312        this.println();
313    }
314
315    /**
316     * Prints a double to the console and puts newline
317     *
318     * @param i value to print
319     */
320    public log(i: double): void {
321        this.print(i);
322        this.println();
323    }
324}
325
326export const console = new Console();
327