terminal: adding "terminal-size" vocab.

db4
John Benediktsson 2012-10-24 13:40:39 -07:00
parent 592e4fbf91
commit 4eb4ce395b
8 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1 @@
John Benediktsson

View File

@ -0,0 +1,28 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: accessors arrays classes.struct io.streams.c kernel
system terminal unix unix.ffi ;
QUALIFIED-WITH: alien.c-types c
IN: terminal.linux
<PRIVATE
CONSTANT: TIOCGWINSZ 0x5413
STRUCT: winsize
{ ws_row c:short }
{ ws_col c:short }
{ ws_xpixel c:short }
{ ws_ypixel c:short } ;
PRIVATE>
M: unix (terminal-size)
stdout-handle fileno TIOCGWINSZ winsize <struct>
[ ioctl ] keep swap 0 < [
drop 0 0
] [
[ ws_col>> ] [ ws_row>> ] bi
] if ;

View File

@ -0,0 +1 @@
linux

View File

@ -0,0 +1,44 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: accessors alien.c-types classes.struct io.streams.c
kernel math memoize scratchpad system terminal unix unix.ffi ;
QUALIFIED-WITH: alien.c-types c
IN: terminal.macosx
<PRIVATE
CONSTANT: IOCPARM_MASK 0x1fff ! parameter length, at most 13 bits
CONSTANT: IOC_VOID 0x20000000 ! no parameters
CONSTANT: IOC_OUT 0x40000000 ! copy parameters out
CONSTANT: IOC_IN 0x80000000 ! copy parameters in
: _IOC ( inout group num len -- n )
[ 8 shift ] 2dip IOCPARM_MASK bitand 16 shift
bitor bitor bitor ;
: _IO ( group num -- n ) [ IOC_VOID ] 2dip 0 _IOC ;
: _IOCR ( group num len -- n ) [ IOC_OUT ] 3dip _IOC ;
: _IOCW ( group num len -- n ) [ IOC_IN ] 3dip _IOC ;
STRUCT: winsize
{ ws_row c:short }
{ ws_col c:short }
{ ws_xpixel c:short }
{ ws_ypixel c:short } ;
MEMO: TIOCGWINSZ ( -- x ) CHAR: t 104 winsize heap-size _IOCR ;
PRIVATE>
M: macosx (terminal-size)
stdout-handle fileno TIOCGWINSZ winsize <struct>
[ ioctl ] keep swap 0 < [
drop 0 0
] [
[ ws_col>> ] [ ws_row>> ] bi
] if ;

View File

@ -0,0 +1 @@
macosx

View File

@ -0,0 +1,27 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: arrays combinators environment kernel math math.parser
sequences system vocabs ;
IN: terminal
HOOK: (terminal-size) os ( -- columns lines )
{
{ [ os macosx? ] [ "terminal.macosx" ] }
{ [ os linux? ] [ "terminal.linux" ] }
{ [ os windows? ] [ "terminal.windows" ] }
} cond require
: terminal-size ( -- dim )
"COLUMNS" "LINES"
[ os-env [ string>number ] [ 0 ] if* ] bi@
2dup [ 0 <= ] either? [
(terminal-size)
[ over 0 <= [ nip ] [ drop ] if ] bi-curry@ bi*
] when 2array ;
: terminal-width ( -- width ) terminal-size first ;
: terimal-height ( -- height ) terminal-size second ;

View File

@ -0,0 +1 @@
windows

View File

@ -0,0 +1,18 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: accessors classes.struct kernel math system terminal
windows.kernel32 ;
IN: terminal.windows
M: windows (terminal-size)
STD_OUTPUT_HANDLE GetStdHandle
CONSOLE_SCREEN_BUFFER_INFO <struct>
[ GetConsoleScreenBufferInfo ] keep swap zero? [
drop 0 0
] [
srWindow>>
[ [ Right>> ] [ Left>> ] bi - 1 + ]
[ [ Bottom>> ] [ Top>> ] bi - 1 + ] bi
] if ;