-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcon_readline.bas
246 lines (195 loc) · 7.05 KB
/
con_readline.bas
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/' comfortable INPUT function '/
#include "fb.bi"
extern "C"
private sub DoAdjust( x as long ptr, y as long ptr, dx as long, dy as long, cols as long , rows as long )
DBG_ASSERT( x <> NULL and y <> NULL )
*x -= 1
*y -= 1
*x += dx
if ( *x < 0 ) then
*x = -*x + cols
*y -= *x / cols
*x = cols - (*x mod cols)
end if
*y += *x / cols
*x mod= cols
*y += dy
*x += 1
*y += 1
end sub
private sub DoMove( x as long ptr, y as long ptr, dx as long, dy as long, cols as long, rows as long )
DoAdjust( x, y, dx, dy, cols, rows )
if ( *y = (rows+1) and *x = 1 ) then
fb_Locate( rows, cols, -1, 0, 0 )
fb_PrintBufferEx( cast(const any ptr, @FB_NEWLINE), sizeof(FB_NEWLINE)-1, 0 )
else
fb_Locate( *y, *x, -1, 0, 0 )
end if
end sub
function fb_ConReadLine FBCALL ( soft_cursor as long ) as FBSTRING ptr
dim as FBSTRING result = ( 0, 0, 0 )
dim as long current_x, current_y
dim as long cols, rows
dim as size_t _pos, _len, tmp_buffer_len = 0
dim as long cursor_visible
dim as long k
dim as ubyte tmp_buffer(0 to 11)
fb_GetSize(@cols, @rows)
cursor_visible = (fb_Locate( 0, 0, -1, 0, 0 ) and &h10000) <> 0
fb_Locate( 0, 0, FALSE, 0, 0 )
_pos = 0
_len = 0
fb_PrintBufferEx( NULL, 0, 0 )
/' Ensure that the cursor is visible during INPUT '/
fb_Locate( 0, 0, (soft_cursor = FALSE), 0, 0 )
do
dim as size_t delete_char_count = 0, add_char = FALSE
dim as FBSTRING ptr s
fb_GetXY( @current_x, @current_y )
if ( soft_cursor <> NULL ) then
fb_PrintFixString( 0, sadd("\377"), 0 )
fb_Locate( current_y, current_x, FALSE, 0, 0 )
end if
while( fb_KeyHit( ) = 0 )
fb_Delay( 25 ) /' release time slice '/
wend
s = fb_Inkey( )
if ( s->data <> NULL ) then
if ( FB_STRSIZE( s ) = 2 ) then
k = FB_MAKE_EXT_KEY( FB_CHAR_TO_INT( s->data[1] ) )
else
k = FB_CHAR_TO_INT( s->data[0] )
end if
fb_hStrDelTemp( s )
else
k = 0
continue do
end if
if ( soft_cursor <> 0 ) then
dim as ubyte mask(0 to 1) = { iif((result.data <> NULL) and (_pos < _len), result.data[_pos], asc(" ")), 0 }
fb_PrintFixString( 0, cast(const ubyte ptr, @mask(0)), 0 )
fb_Locate( current_y, current_x, FALSE, 0, 0 )
end if
select case( k )
case 8: /' Backspace '/
if ( _pos <> 0 ) then
DoMove( @current_x, @current_y, -1, 0, cols, rows )
_pos -= 1
delete_char_count = 1
end if
case 9: /' TAB '/
tmp_buffer_len = ((_pos + 8) / 8 * 8) - _pos
memset( @tmp_buffer(0), 32, tmp_buffer_len )
add_char = TRUE
case 27: /' ESC '/
DoMove( @current_x, @current_y, -pos, 0, cols, rows )
_pos = 0
delete_char_count = _len
case KEY_DEL: /' Delete following char '/
/' not at EOL already? '/
if ( _len <> _pos ) then
delete_char_count = 1
else
fb_Beep()
end if
case KEY_LEFT: /' Move cursor left '/
/' not at begin-of-line already? '/
if ( _pos <> 0 ) then
DoMove( @current_x, @current_y, -1, 0, cols, rows )
_pos -= 1
end if
case KEY_RIGHT: /' Move cursor right '/
/' not at EOL already? '/
if ( _pos <> _len ) then
DoMove( @current_x, @current_y, 1, 0, cols, rows )
_pos += 1
end if
case KEY_HOME: /' Move cursor to begin-of-line '/
DoMove( @current_x, @current_y, -_pos, 0, cols, rows )
_pos = 0
case KEY_END: /' Move cursor to EOL '/
DoMove( @current_x, @current_y, _len-_pos, 0, cols, rows )
_pos = _len
case KEY_UP: /' Move cursor up '/
if ( _pos >= cast(size_t, cols) ) then
DoMove( @current_x, @current_y, -cols, 0, cols, rows )
_pos -= cols
end if
case KEY_DOWN: /' Move cursor down '/
if ( (_pos + cols) <= _len ) then
DoMove( @current_x, @current_y, cols, 0, cols, rows )
_pos += cols
end if
case else:
if ( (k >= 32) and (k <= 255) ) then
tmp_buffer(0) = cast(ubyte, k)
tmp_buffer_len = 1
add_char = TRUE
/' DoMove( ¤t_x, ¤t_y, 1, 0, cols ); '/
end if
end select
if ( (delete_char_count <> 0) or add_char <> 0 ) then
/' Turn off the cursor during output (speed-up) '/
fb_Locate( 0, 0, FALSE, 0, 0 )
end if
if ( delete_char_count <> 0 ) then
dim as FBSTRING ptr str_fill
dim as FBSTRING ptr str_left = fb_StrMid( @result, 1, _pos )
dim as FBSTRING ptr str_right = fb_StrMid( @result, _pos + 1 + delete_char_count, _len - _pos - delete_char_count)
fb_StrAssign( @result, -1, str_left, -1, FALSE )
fb_StrConcatAssign( @result, -1, str_right, -1, FALSE )
_len -= delete_char_count
FB_LOCK()
fb_PrintBufferEx( result.data + _pos, _len - _pos, 0 )
/' Overwrite all deleted characters with SPC's '/
str_fill = fb_StrFill1 ( delete_char_count, 32 )
fb_PrintBufferEx( str_fill->data, delete_char_count, 0 )
fb_hStrDelTemp( str_fill )
fb_Locate( current_y, current_x, -1, 0, 0 )
FB_UNLOCK()
end if
if ( add_char <> 0 ) then
tmp_buffer(tmp_buffer_len) = 0
end if
if ( add_char <> 0 ) then
dim as long old_x = current_x, old_y = current_y
dim as FBSTRING ptr str_add = fb_StrAllocTempDescF( cast(ubyte ptr, @tmp_buffer(0)), tmp_buffer_len + 1 )
dim as FBSTRING ptr str_left = fb_StrMid( @result, 1, _pos )
dim as FBSTRING ptr str_right = fb_StrMid( @result, _pos + 1, _len - _pos)
fb_StrAssign( @result, -1, str_left, -1, FALSE )
fb_StrConcatAssign( @result, -1, str_add, -1, FALSE )
fb_StrConcatAssign( @result, -1, str_right, -1, FALSE )
_len += tmp_buffer_len
FB_LOCK()
fb_PrintBufferEx( result.data + _pos, _len - _pos, 0 )
fb_GetXY(@current_x, @current_y)
if ( _pos = (_len-tmp_buffer_len) ) then
current_x = old_x
current_y = old_y
DoMove( @current_x, @current_y, tmp_buffer_len, 0, cols, rows )
else
dim as long tmp_x_2 = old_x, tmp_y_2 = old_y
DoAdjust( @tmp_x_2, @tmp_y_2, _len - _pos, 0, cols, rows )
if ( tmp_y_2 > (rows + 1) or (tmp_y_2 = (rows + 1) and tmp_x_2 > 1) ) then
DoMove( @current_x, @current_y, -(_len - _pos - tmp_buffer_len), 0, cols, rows )
else
current_x = old_x
current_y = old_y
DoMove( @current_x, @current_y, tmp_buffer_len, 0, cols, rows )
end if
end if
_pos += tmp_buffer_len
FB_UNLOCK()
end if
fb_Locate( 0, 0, (soft_cursor = FALSE), 0, 0 )
loop while (k <> ASC(!"\n") and k <> ASC(!"\r"))
FB_LOCK()
/' set cursor to end of line '/
fb_GetXY(@current_x, @current_y)
DoMove( @current_x, @current_y, _len - _pos, 0, cols, rows )
/' Restore old cursor visibility '/
fb_Locate( 0, 0, cursor_visible, 0, 0 )
FB_UNLOCK()
return fb_StrAllocTempResult( @result )
end function
end extern