-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
format_string.tea
64 lines (47 loc) · 2.26 KB
/
format_string.tea
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
#!/opt/TeaAgeSolutions/TeaScript/bin/TeaScript
/*
* SPDX-FileCopyrightText: Copyright (C) 2024 Florian Thake, <contact |at| tea-age.solutions>.
* SPDX-License-Identifier: MIT
* see included file MIT_LICENSE.txt
*/
// This script demonstrates the string format feature plus the forall loop and sequences.
// NOTE: this script must run in a TeaScript host with libfmt support (the pre-compiled hosts have this).
##minimum_version 0.12
// fallback for versions older than 0.11 which don't support ##minimum_version.
// they will produce a parse error when raw string literals are used...
// cover this case here first and try to produce a readable error.
// This protects against every new syntax introduced in 0.11 and above!
if( _version_major < 1 and _version_minor < 12 ) {
// return from 'main' exists since 0.8.0 which was the first public release...
return """Version is too old. Need 0.12.0 as minimum."""
}
// quick pre-check
if( not features.color or not features.format ) {
// if the features are unavailable in general the compilation was without libfmt.
return "cprint/format is not available. You must compile with a proper set include path for libfmt."
} else if( not is_defined cprint or not is_defined format ) {
// otherwise most likely the CoreLibrary is not loaded with level >= LevelUtil.
return "cprint/format is not available. Load the CoreLibrary with level >= LevelUtil."
}
const CHAR := "o"
const COLS := 10
const REPEATS := 4
// define some colors....
const colors := ( make_rgb( 255, 255, 255 ),
make_rgb( 255, 255, 0 ),
make_rgb( 255, 0 , 0 ),
make_rgb( 0 , 255, 0 ),
make_rgb( 0 , 0 , 255 ),
make_rgb( 0 , 255, 255 ),
make_rgb( 255, 0 , 255 ) )
const colors_size := _tuple_size( colors )
def loops := 0
forall( i in _seq( 1, REPEATS, 1 ) ) {
forall( col in if( i mod 2 == 1 ) { _seq( 1, COLS, 1 ) } else { _seq( COLS, 1, -1 ) } ) {
cprint( colors[loops mod colors_size], format( "{0:->{1}}", CHAR, col ) )
cprint( colors[loops mod colors_size], format( "{0:-<{1}}\n", "", COLS - col ) )
inc( loops )
}
}
// return nothing ... (otherwise the last inc( loops ) will be returned)
void