Skip to content

Commit e33bb61

Browse files
committed
FEAT: implemented QUERY action on INPUT port to get information about console's size
1 parent fba3b10 commit e33bb61

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

src/boot/sysobj.r

+5
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ standard: context [
254254
none
255255
]
256256

257+
console-info: context [
258+
buffer-size: none
259+
window-size: none
260+
]
261+
257262
extension: context [
258263
lib-base: ; handle to DLL
259264
lib-file: ; file name loaded

src/core/p-console.c

+10
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@
128128
if (IS_OPEN(req)) return R_TRUE;
129129
return R_FALSE;
130130

131+
case A_QUERY:
132+
if (OS_DO_DEVICE(req, RDC_QUERY) < 0) return R_NONE;
133+
REBVAL *spec = Get_System(SYS_STANDARD, STD_CONSOLE_INFO);
134+
if (!IS_OBJECT(spec)) Trap_Arg(spec);
135+
REBSER *obj = CLONE_OBJECT(VAL_OBJ_FRAME(spec));
136+
SET_OBJECT(D_RET, obj);
137+
SET_PAIR(OFV(obj, STD_CONSOLE_INFO_BUFFER_SIZE), req->console.buffer_cols, req->console.buffer_rows);
138+
SET_PAIR(OFV(obj, STD_CONSOLE_INFO_WINDOW_SIZE), req->console.window_cols, req->console.window_rows);
139+
break;
140+
131141
default:
132142
Trap_Action(REB_PORT, action);
133143
}

src/include/reb-device.h

+6
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ struct rebol_devreq {
180180
u32 remote_port; // remote port
181181
void *host_info; // for DNS usage
182182
} net;
183+
struct {
184+
u32 buffer_rows;
185+
u32 buffer_cols;
186+
u32 window_rows;
187+
u32 window_cols;
188+
} console;
183189
};
184190
};
185191
#pragma pack()

src/os/posix/dev-stdio.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <signal.h>
4545
#include <stdlib.h>
4646
#include <string.h>
47+
#include <sys/ioctl.h>
4748

4849
#include "reb-host.h"
4950

@@ -260,6 +261,21 @@ static void Close_Stdio(void)
260261
return DR_DONE;
261262
}
262263

264+
/***********************************************************************
265+
**
266+
*/ DEVICE_CMD Query_IO(REBREQ *req)
267+
/*
268+
** Resolve port information. Currently just size of console.
269+
**
270+
***********************************************************************/
271+
{
272+
struct winsize w;
273+
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
274+
req->console.window_rows =
275+
req->console.buffer_rows = w.ws_row;
276+
req->console.window_cols = w.ws_col;
277+
return DR_DONE;
278+
}
263279

264280
/***********************************************************************
265281
**
@@ -302,7 +318,7 @@ static DEVICE_CMD_FUNC Dev_Cmds[RDC_MAX] =
302318
Write_IO,
303319
0, // poll
304320
0, // connect
305-
0, // query
321+
Query_IO,
306322
0, // modify
307323
Open_Echo, // CREATE used for opening echo file
308324
};

src/os/win32/dev-stdio.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,22 @@ static void close_stdio(void)
439439
return DR_DONE;
440440
}
441441

442+
/***********************************************************************
443+
**
444+
*/ DEVICE_CMD Query_IO(REBREQ *req)
445+
/*
446+
** Resolve console port information. Currently just size of console.
447+
**
448+
***********************************************************************/
449+
{
450+
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
451+
GetConsoleScreenBufferInfo(Std_Out, &csbiInfo);
452+
req->console.buffer_rows = csbiInfo.dwSize.Y;
453+
req->console.buffer_cols = csbiInfo.dwSize.X;
454+
req->console.window_rows = csbiInfo.srWindow.Bottom - csbiInfo.srWindow.Top + 1;
455+
req->console.window_cols = csbiInfo.srWindow.Right - csbiInfo.srWindow.Left + 1;
456+
return DR_DONE;
457+
}
442458

443459
/***********************************************************************
444460
**
@@ -482,7 +498,7 @@ static DEVICE_CMD_FUNC Dev_Cmds[RDC_MAX] =
482498
Write_IO,
483499
0, // poll
484500
0, // connect
485-
0, // query
501+
Query_IO,
486502
0, // modify
487503
Open_Echo, // CREATE used for opening echo file
488504
};

0 commit comments

Comments
 (0)