]> gitweb.factorcode.org Git - factor.git/commitdiff
terminal: adding "terminal-size" vocab.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 24 Oct 2012 20:40:39 +0000 (13:40 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 24 Oct 2012 20:40:39 +0000 (13:40 -0700)
extra/terminal/authors.txt [new file with mode: 0644]
extra/terminal/linux/linux.factor [new file with mode: 0644]
extra/terminal/linux/platforms.txt [new file with mode: 0644]
extra/terminal/macosx/macosx.factor [new file with mode: 0644]
extra/terminal/macosx/platforms.txt [new file with mode: 0644]
extra/terminal/terminal.factor [new file with mode: 0644]
extra/terminal/windows/platforms.txt [new file with mode: 0644]
extra/terminal/windows/windows.factor [new file with mode: 0644]

diff --git a/extra/terminal/authors.txt b/extra/terminal/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/extra/terminal/linux/linux.factor b/extra/terminal/linux/linux.factor
new file mode 100644 (file)
index 0000000..c8cefbe
--- /dev/null
@@ -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 ;
diff --git a/extra/terminal/linux/platforms.txt b/extra/terminal/linux/platforms.txt
new file mode 100644 (file)
index 0000000..a08e1f3
--- /dev/null
@@ -0,0 +1 @@
+linux
diff --git a/extra/terminal/macosx/macosx.factor b/extra/terminal/macosx/macosx.factor
new file mode 100644 (file)
index 0000000..0c58e03
--- /dev/null
@@ -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 ;
diff --git a/extra/terminal/macosx/platforms.txt b/extra/terminal/macosx/platforms.txt
new file mode 100644 (file)
index 0000000..6e806f4
--- /dev/null
@@ -0,0 +1 @@
+macosx
diff --git a/extra/terminal/terminal.factor b/extra/terminal/terminal.factor
new file mode 100644 (file)
index 0000000..3c56922
--- /dev/null
@@ -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 ;
diff --git a/extra/terminal/windows/platforms.txt b/extra/terminal/windows/platforms.txt
new file mode 100644 (file)
index 0000000..8e1a559
--- /dev/null
@@ -0,0 +1 @@
+windows
diff --git a/extra/terminal/windows/windows.factor b/extra/terminal/windows/windows.factor
new file mode 100644 (file)
index 0000000..ce7d2b7
--- /dev/null
@@ -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 ;