Project

General

Profile

A console in Scol » History » Version 1

iri, 09/24/2012 10:04 PM

1 1 iri
h1. A console in Scol
2
3
The Scol console is very simple. It allows to display values and messages for debugging.
4
You must define a mask from the Scol settings (launch Scol, click right on the icon et choose Settings. Select the item "Mintenance").
5
6
To show the console : *_showconsole*
7
To hide the console : *_hideconsole*
8
9
To display a string in the console : *_fooS*
10
To display an integer in the console : *_fooId*
11
To display an hexa in the console : *_fooI*
12
To display a list in the console : *_fooIList* or *_fooSList*
13
14
Others functions *_fooXXX* are availables from the non-standard library Syspack.
15
16
h2. How to have a customized console ?
17
18
This is an example, you can adapt at your case.
19
20
The code (by Loïc Berthelot, Scol language developer, 2000-2005) is :
21
22
<pre>
23
/* By Loïc Berthelot, aka Neywen */
24
25
var CONSOLE_HIDE_LINENUM = 0;;
26
var CONSOLE_SHOW_LINENUM = 1;;
27
28
struct CONSOLE =
29
[
30
  win_CSL : ObjWin,		// console window
31
  txt_CSL : S,			// content
32
  objtxt_CSL : ObjText,	// 2d text object
33
  font_CSL : ObjFont,	// font object
34
  flag_CSL : I			// flag
35
] mkCONSOLE;;
36
37
38
typeof lastcsl = CONSOLE;;	// console object
39
var LINECOUNT = 0;;			// initial line count = 0
40
41
/* When the console window is destroyed, font and text object are destroyed before the window */
42
fun console_destroy (a, csl) = 
43
	_DSfont csl.font_CSL;
44
 	_DStext csl.objtxt_CSL;
45
  	1;;
46
47
/* When the window object is resized, the text object is resized too */
48
fun console_resize (win, csl, w, h) = 
49
  	let _GETtextPositionSize csl.objtxt_CSL -> [x y _ _] in
50
  	_SIZEtext csl.objtxt_CSL (w-20) (h-20) x y;
51
  	1;;
52
53
54
fun console_getLineNum () = 
55
	let
56
  		if (LINECOUNT < 10)	then 
57
  			strcat strcat "  " (itoa LINECOUNT) " : "
58
  		else if (LINECOUNT < 100) then 
59
  			strcat strcat " " (itoa LINECOUNT) " : "
60
  		else 
61
  			strcat (itoa LINECOUNT) " : "
62
	-> linenum in
63
	(
64
  	set LINECOUNT = LINECOUNT+1;
65
  	linenum;
66
	);;
67
68
  
69
fun console_clear (csl) = 
70
  	let if (csl == nil) then lastcsl else csl -> csl in
71
  	(
72
  		set csl.txt_CSL = "";
73
 		_SETtext csl.objtxt_CSL csl.txt_CSL;
74
   	);;
75
76
/* print a message in the console */
77
fun console_print (csl, msg) = 
78
  	let if (csl == nil) then lastcsl else csl -> csl in
79
  	(
80
   	if ((csl.flag_CSL == CONSOLE_SHOW_LINENUM) || (csl.flag_CSL == nil)) then
81
    	set csl.txt_CSL = strcat strcat strcat csl.txt_CSL "\n" console_getLineNum msg
82
    else 
83
    	set csl.txt_CSL = strcat csl.txt_CSL  msg;
84
85
    _SETtext csl.objtxt_CSL csl.txt_CSL;
86
    _SCROLLtext csl.objtxt_CSL 0 (_GETlineCount csl.objtxt_CSL)-1;
87
88
  	);
89
  	1;;
90
91
/* create the console window :
92
x, y : its position on the screen
93
w, h : its width and its height
94
flag : CONSOLE_HIDE_LINENUM ot CONSOLE_SHOW_LINENUM
95
the new console object is returned */
96
fun console_create (x, y, w, h, flag) = 
97
  	let mkCONSOLE [nil nil nil nil flag] -> csl in
98
  	(
99
    set csl.win_CSL = _CRwindow _channel nil x y w h WN_MENU|WN_SIZEBOX|WN_MINBOX "console";
100
    _CBwinDestroy csl.win_CSL @console_destroy csl;
101
   	_CBwinSize csl.win_CSL @console_resize csl;
102
103
    set csl.font_CSL = _CRfont _channel 12 0 FF_WEIGHT "arial";
104
105
    set csl.objtxt_CSL = _CRtext _channel csl.win_CSL 10 10 w-20 h-20
106
                    ET_AHSCROLL|ET_AVSCROLL|ET_ALIGN_LEFT|ET_BORDER|ET_HSCROLL|ET_VSCROLL nil;
107
108
    set lastcsl = csl;
109
110
    csl;
111
  	);;
112
  	
113
Here is a very basic example for use this console :
114
115
fun main ()=
116
	_showconsole;
117
	
118
	// we create the console
119
	console_create 50 50 500 300 CONSOLE_SHOW_LINENUM;
120
	
121
	let 50 -> counter in
122
	while counter >= 0 do
123
	(
124
		// we write a message inside
125
		console_print nil strcat "count : " itoa counter;
126
		set counter = counter-1;
127
	);
128
		
129
	0;;
130
</pre>
131
	
132
	
133
134
135
License : "CC-BY-SA-2.0":https://creativecommons.org/licenses/by-sa/2.0/
136
Tutorial by iri
137
Updated by /