Skip to content

Commit 75cdc1f

Browse files
committed
Fixes for io module.
1 parent ee4fced commit 75cdc1f

File tree

8 files changed

+96
-104
lines changed

8 files changed

+96
-104
lines changed

apps/nyssa/.blade/libs/qi/app/lib.b

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,21 @@ class expect {
219219

220220
if is_function(self.value) {
221221
self._run('to throw', e, @(x, y) {
222+
223+
var res
222224
catch {
223-
x()
225+
res = x()
224226
return false
225227
} as ex
226228

227229
if ex {
228230
if is_string(e) return ex.message.match(e)
229231
if is_class(e) return instance_of(ex, e)
230-
if instance_of(e, Exception) and ex == e return true
232+
if instance_of(e, Exception) return ex == e
231233
return true
232234
}
235+
236+
return res
233237
})
234238
} else {
235239
self._run('to throw', e, @(x, y) { return false })

tests/io.b renamed to examples/vim.b

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import io
33
# simple test
44
var tty = io.TTY(io.stdin)
55
6-
var keywords = ['default', 'def', 'class', 'echo', 'for', 'if', 'in',
7-
'as', 'import', 'let', 'using', 'when', 'while', 'iter', 'raise', 'break', 'continue']
6+
var keywords = [
7+
'and', 'as', 'assert', 'break', 'catch', 'class',
8+
'continue', 'def', 'default', 'do', 'echo','else',
9+
'false', 'for', 'if', 'import','in', 'iter', 'nil',
10+
'or', 'parent','raise', 'return', 'self', 'static',
11+
'true', 'using', 'var', 'when', 'while'
12+
]
813
914
def highlight(str) {
1015
if str.length() == 0
@@ -15,19 +20,18 @@ def highlight(str) {
1520
return str.replace(regex, '\x1b[32m$1\x1b[0m')
1621
}
1722
18-
# go full screen by clearning output
19-
# stdout().write("\x1b[2J");
20-
# stdout().write("\x1b[H");
23+
# go full screen by cleaning output
24+
io.stdout.write("\x1b[2J")
25+
io.stdout.write("\x1b[H")
2126
22-
print('\x1b[33m')
23-
print('A simple TTY implementation for Bladey io module demonstration')
24-
print('Note that your cursor can move left and right.')
25-
print('Nope! That\'s not how the terminal works by default.')
26-
print('And we have a few key words too: ' + ', '.join(keywords))
27-
print('That\'s the power of Bladey!')
28-
print('Press Ctrl+C to quit')
29-
print('-------------------------------------------------------------')
30-
print('\x1b[0m')
27+
echo '\x1b[33m'
28+
echo 'A simple TTY based editor with syntax highlighting for Blade using the io module'
29+
echo 'Note that your cursor can move left and right.'
30+
echo 'Nope! That\'s not how the terminal works by default.'
31+
echo 'That\'s the power of Blade!'
32+
echo 'Press Ctrl+C to quit'
33+
echo '-------------------------------------------------------------'
34+
echo '\x1b[0m'
3135
3236
if !tty.set_raw() {
3337
echo 'Failed to enter raw mode for STDIN'
@@ -40,7 +44,6 @@ var input = ''
4044
var index = 0
4145
4246
var s
43-
4447
while s = io.stdin.read() {
4548
if ord(s) == 3 { # ctrl + c
4649
io.stdout.write('\x1b[1000D\n')
@@ -59,11 +62,11 @@ while s = io.stdin.read() {
5962
history.append(input)
6063
history_index++
6164
# you can take this out to see the output
62-
# print('\nechoing...', input)
65+
# echo '\nechoing...', input
6366
input = ''
6467
index = 0
6568
} else if ord(s) == 27 { # arrow keys
66-
var next1 = ord(io.stdin.read())
69+
var next1 = ord(io.stdin.read())
6770
var next2 = ord(io.stdin.read())
6871
if next1 == 91 {
6972
if next2 == 68 #left

libs/io.b

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,18 +564,18 @@ class TTY {
564564

565565
/**
566566
* Sets the current tty to raw mode.
567-
*
567+
*
568568
* @returns bool
569569
*/
570570
set_raw() {
571-
var new_attr = _io.TTY.tcgetattr(self.std)
571+
var new_attr = _io.TTY.tcgetattr(self.std, false)
572572

573573
new_attr[TTY.TTY_IFLAG] &= ~(TTY.IGNBRK | TTY.BRKINT | TTY.PARMRK | TTY.ISTRIP | TTY.INLCR | TTY.IGNCR | TTY.ICRNL | TTY.IXON)
574574
new_attr[TTY.TTY_OFLAG] &= ~TTY.OPOST
575575
new_attr[TTY.TTY_LFLAG] &= ~(TTY.ECHO | TTY.ECHONL | TTY.ICANON | TTY.ISIG | TTY.IEXTEN)
576576
new_attr[TTY.TTY_CFLAG] &= ~(TTY.CSIZE | TTY.PARENB)
577577
new_attr[TTY.TTY_CFLAG] |= TTY.CS8
578-
578+
579579
return self.set_attr(TTY.TCSAFLUSH, new_attr)
580580
}
581581

@@ -585,7 +585,8 @@ class TTY {
585585
* @returns bool
586586
*/
587587
exit_raw() {
588-
_io.TTY.exit_raw()
588+
# return _io.TTY.exit_raw(self.std)
589+
return self.set_attr(TTY.TCSAFLUSH, _io.TTY.tcgetattr(self.std, true))
589590
}
590591

591592
/**
@@ -655,7 +656,7 @@ def getch() {
655656
}
656657

657658
/**
658-
* Reads an entire line from standard input. If a _messagge_ is given, the
659+
* Reads an entire line from standard input. If a _message_ is given, the
659660
* message will be printed before it begins to wait for a user input. If
660661
* _secure_ is `true`, the user's input will not be printing and _obscure_text_
661662
* will be printed instead.

src/bunistd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef bladey_compart_unistd_h
2-
#define bladey_compart_unistd_h
1+
#ifndef blade_compart_unistd_h
2+
#define blade_compart_unistd_h
33

44
#include "common.h"
55

src/file.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,22 @@ DECLARE_FILE_METHOD(read) {
215215

216216
size_t bytes_read = fread(buffer, sizeof(char), file_size, file->file);
217217

218-
if (bytes_read == 0 && file_size != 0 && file_size == file_size_real) {
218+
if (bytes_read == 0 && file_size != 0 && file_size == file_size_real && !file->is_std) {
219219
FILE_ERROR(Read, "could not read file contents");
220220
}
221221

222-
// we made use of +1 so we can terminate the string.
222+
if(file->is_std && bytes_read == 0) {
223+
RETURN_VALUE(EMPTY_STRING_VAL);
224+
}
225+
226+
// we made use of +1, so we can terminate the string.
223227
if (buffer != NULL)
224228
buffer[bytes_read] = '\0';
225229

226230
// close file
227-
file_close(file);
231+
if(!file->is_std) {
232+
file_close(file);
233+
}
228234

229235
if (!in_binary_mode) {
230236
RETURN_T_STRING(buffer, bytes_read);
@@ -294,8 +300,12 @@ DECLARE_FILE_METHOD(gets) {
294300

295301
size_t bytes_read = fread(buffer, sizeof(char), length, file->file);
296302

297-
if (bytes_read == 0 && length != 0) {
298-
FILE_ERROR(Read, "could not read file contents");
303+
if (bytes_read == 0 && length != 0 && !file->is_std) {
304+
FILE_ERROR(Read, "could not read file contents %d, %d");
305+
}
306+
307+
if(file->is_std && bytes_read == 0) {
308+
RETURN_VALUE(EMPTY_STRING_VAL);
299309
}
300310

301311
// we made use of +1, so we can terminate the string.

src/file.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,4 @@ DECLARE_FILE_METHOD(seek);
234234
*/
235235
DECLARE_FILE_METHOD(tell);
236236

237-
bool is_std_file(b_obj_file *file);
238-
239237
#endif

src/object.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,6 @@ static inline bool is_obj_type(b_value v, b_obj_type t) {
279279
return IS_OBJ(v) && AS_OBJ(v)->type == t;
280280
}
281281

282-
static inline bool is_std_file(b_obj_file *file) { return file->mode->length == 0; }
283-
284282
#define ALLOCATE_OBJ(type, obj_type) \
285283
(type *)allocate_object(vm, sizeof(type), obj_type)
286284

0 commit comments

Comments
 (0)