You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SmallBASIC supports a number of escape codes for controlling the display. The codes allow you to set foreground and background colors, change the font and also set underline and inverse text display.
3
+
SmallBASIC supports a number of escape codes for controlling the display. The codes allow you to set foreground and background colors, change the font and also set underline and inverse text display. The escape codes are based on [ANSI Codes](http://en.wikipedia.org/wiki/ANSI_escape_code).
4
4
5
-
The escape codes are based on [ANSI Codes](http://en.wikipedia.org/wiki/ANSI_escape_code). SmallBASIC also support a number of additional codes which are not part of the standard.
6
-
7
-
Escape sequences start with the characters ESC (ASCII 27d / 1Bh / 033o ) and [ (left bracket). This sequence is called CSI for "Control Sequence Introducer".
8
-
9
-
The supported standard codes are:
5
+
## The supported standard codes are:
10
6
11
7
```
12
8
\a beep
13
9
\t tab (20 px)
14
10
\r return
15
11
\n next line
16
-
\xC clear screen (new page)
12
+
\" quote "
13
+
\\ Backslash \
17
14
\e[K clear to end of line
18
15
\e[nG move to column n
19
16
\e[s save cursor position
@@ -30,23 +27,69 @@ The supported standard codes are:
30
27
\e[nm n colors - 30..37 foreground, 40..47 background
31
28
```
32
29
33
-
Useful non-standard codes include:
30
+
## Using the escape codes directly
34
31
35
-
```
36
-
\003 end of text (flush buffer)
37
-
\m scroll to the top
38
-
\<1 select backscreen 1
39
-
\<2 select backscreen 2
40
-
\>1 select frontscreen 1
41
-
\<2 select frontscreen 2
32
+
The instead of "\e" the CHR command is useful for obtaining and printing the escape character (ASCII 27)
33
+
34
+
```Freebasic
35
+
PRINTCHR(27)+"[1mTHIS IS BOLD"+CHR(27)+"[0m"
36
+
PRINTCHR(27)+"[3mThis is italic"+CHR(27)+"[0m"
37
+
PRINTCHR(27)+"[4mThis is underline"
38
+
39
+
PRINT"\e[32mGreen text"
40
+
PRINT"\e[32m\e[47mGreen text on white background"
41
+
PRINT"First line\nSecond Line"
42
42
```
43
43
44
-
The CHR command is useful for obtaining and printing the escape character (ASCII 27)
44
+
## Using the EscapeCode Unit
45
45
46
-
For example:
46
+
The EscapeCode Unit makes it easier to use the escape codes and to deal with the different colors for foreground and background. The uint can be downloaded or copy pasted from the [SmallBASIC Github website](https://github.com/smallbasic/smallbasic.plugins/blob/master/units/EscapeCodes.bas). Please save the unit in the same directory as you basic file.
47
47
48
+
Here an example on how to use the unit.
49
+
50
+
```Freebasic
51
+
' SmallBASIC 12.25
52
+
' Example for using UNIT "EscapeCodes"
53
+
' For more information see: https://smallbasic.github.io/pages/escape.html
54
+
55
+
importEscapeCodesasesc
56
+
57
+
print"FORMATING TEXT:"
58
+
print
59
+
printesc.NORMAL+"WITHOUT ANY FORMAT "+esc.ITALIC+"ITALIC "+esc.ITALIC_OFF+esc.BOLD+"BOLD "+esc.BOLD_OFF+esc.UNDERLINE+"UNDERLINE "+esc.UNDERLINE_OFF+esc.REVERSE+"REVERSE"+esc.REVERSE_OFF
60
+
print
61
+
print"USE COLORS:"
62
+
print
63
+
printesc.BG_BLACK+esc.BLACK+" BLACK "+esc.RED+" RED "+esc.GREEN+" GREEN "+esc.YELLOW+" YELLOW "+esc.BLUE+" BLUE "+esc.MAGENTA+" MAGENTA "+esc.CYAN+" CYAN "+esc.WHITE+" WHITE "+esc.NORMAL
64
+
printesc.BG_RED+esc.BLACK+" BLACK "+esc.RED+" RED "+esc.GREEN+" GREEN "+esc.YELLOW+" YELLOW "+esc.BLUE+" BLUE "+esc.MAGENTA+" MAGENTA "+esc.CYAN+" CYAN "+esc.WHITE+" WHITE "+esc.NORMAL
65
+
printesc.BG_GREEN+esc.BLACK+" BLACK "+esc.RED+" RED "+esc.GREEN+" GREEN "+esc.YELLOW+" YELLOW "+esc.BLUE+" BLUE "+esc.MAGENTA+" MAGENTA "+esc.CYAN+" CYAN "+esc.WHITE+" WHITE "+esc.NORMAL
66
+
printesc.BG_YELLOW+esc.BLACK+" BLACK "+esc.RED+" RED "+esc.GREEN+" GREEN "+esc.YELLOW+" YELLOW "+esc.BLUE+" BLUE "+esc.MAGENTA+" MAGENTA "+esc.CYAN+" CYAN "+esc.WHITE+" WHITE "+esc.NORMAL
67
+
printesc.BG_BLUE+esc.BLACK+" BLACK "+esc.RED+" RED "+esc.GREEN+" GREEN "+esc.YELLOW+" YELLOW "+esc.BLUE+" BLUE "+esc.MAGENTA+" MAGENTA "+esc.CYAN+" CYAN "+esc.WHITE+" WHITE "+esc.NORMAL
68
+
printesc.BG_MAGENTA+esc.BLACK+" BLACK "+esc.RED+" RED "+esc.GREEN+" GREEN "+esc.YELLOW+" YELLOW "+esc.BLUE+" BLUE "+esc.MAGENTA+" MAGENTA "+esc.CYAN+" CYAN "+esc.WHITE+" WHITE "+esc.NORMAL
69
+
printesc.BG_CYAN+esc.BLACK+" BLACK "+esc.RED+" RED "+esc.GREEN+" GREEN "+esc.YELLOW+" YELLOW "+esc.BLUE+" BLUE "+esc.MAGENTA+" MAGENTA "+esc.CYAN+" CYAN "+esc.WHITE+" WHITE "+esc.NORMAL
70
+
printesc.BG_WHITE+esc.BLACK+" BLACK "+esc.RED+" RED "+esc.GREEN+" GREEN "+esc.YELLOW+" YELLOW "+esc.BLUE+" BLUE "+esc.MAGENTA+" MAGENTA "+esc.CYAN+" CYAN "+esc.WHITE+" WHITE "+esc.NORMAL
In the console version of SmallBASIC (sbasic.exe or sbasic) most of the escape codes, for example [ANSI Codes at wikipedia](http://en.wikipedia.org/wiki/ANSI_escape_code), can be used in version 12.25 or later. The support of the escape codes depends on the operating system and the terminal you are using.
Copy file name to clipboardExpand all lines: _build/reference/1425-language-try.markdown
+39-6Lines changed: 39 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,12 +2,39 @@
2
2
3
3
> TRY
4
4
5
-
The TRY statement introduces a TRY/CATCH BLOCK
5
+
__TRY__
6
6
7
+
The TRY statement introduces a TRY/CATCH block.
7
8
9
+
__CATCH [var | expr]__
8
10
9
-
Note:
10
-
If this demo program crashes... then run it again. It seems that TRY / CATCH block might be unstable within a function or sub... (?)
11
+
The CATCH statement is used to CATCH an run-time error. This is typically used with errors raised when calling a file system command that cannot be completed, for example attempting to open a non-existent file.
12
+
13
+
The CATCH statement has two modes. You can supply a variable argument to store the error string. Alternatively you can supply an expression. When the raised error matches the (String) expression, the error will be caught.
14
+
15
+
When using the expression mode, you can supply a succession of CATCH statements to handle various error messages separately.
16
+
17
+
__END TRY__
18
+
19
+
The END TRY statement marks the end of a TRY/CATCH block.
20
+
21
+
22
+
### Example 1: Opening a non-existing file for reading
23
+
24
+
```
25
+
try
26
+
' DON'T use existing file for demo.
27
+
open "try demo.tmp" for input as #1
28
+
catch err
29
+
print err; " "
30
+
' Some error handling could be implemented here
31
+
' i.e: if(err = "...") then ...
32
+
end try
33
+
34
+
print "This point is reach, even if opening the file was not possible"
35
+
```
36
+
37
+
### Example 2: Advanced error handling for opening files
11
38
12
39
~~~
13
40
@@ -21,7 +48,7 @@ Func opens(filename, mode)
21
48
Case "input" : Open filename For Input As #fn
22
49
Case "output": Open filename For Output As #fn
23
50
Case "append": Open filename For Append As #fn
24
-
Case Else: ? "opens(): Bad open mode at line " + Progline: Pause: Stop
51
+
Case Else: ? "opens(): Bad open mode at line " + Progline: Stop
Copy file name to clipboardExpand all lines: _build/reference/1426-language-catch.markdown
+3-5Lines changed: 3 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,9 @@
2
2
3
3
> CATCH [var | expr]
4
4
5
-
The CATCH statement is used to CATCH an run-time error.
5
+
The CATCH statement is used to CATCH a run-time error. This is typically used with errors raised when calling a file system command that cannot be completed, for example attempting to open a non-existent file.
6
6
7
-
This is typically used with errors raised when calling a file system command that cannot be completed, for example attempting to open a non-existent file.
7
+
The CATCH statement has two modes. You can supply a variable argument to store the error string. Alternatively you can supply an expression. When the raised error matches the (String) expression, the error will be caught. When using the expression mode, you can supply a succession of CATCH statements to handle various error messages separately.
8
8
9
-
The CATCH statement has two modes. You can supply a variable argument to store the error string. Alternatively you can supply an expression.
10
-
11
-
When the raised error matches the (String) expression, the error will be caught. When using the expression mode, you can supply a succession of CATCH statements to handle various error messages separately.
0 commit comments