factor/extra/irc/ui/ui.factor

216 lines
5.9 KiB
Factor
Raw Normal View History

! Copyright (C) 2008 William Schlieper
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel threads combinators concurrency.mailboxes
sequences strings hashtables splitting fry assocs hashtables
2008-07-14 20:53:08 -04:00
ui ui.gadgets ui.gadgets.panes ui.gadgets.editors
ui.gadgets.scrollers ui.commands ui.gadgets.frames ui.gestures
2008-07-16 19:33:33 -04:00
ui.gadgets.tabs ui.gadgets.grids ui.gadgets.lists ui.gadgets.labels
2008-07-28 21:02:05 -04:00
io io.styles namespaces calendar calendar.format models continuations
irc.client irc.client.private irc.messages irc.messages.private
2008-07-28 21:02:05 -04:00
irc.ui.commandparser irc.ui.load qualified ;
RENAME: join sequences => sjoin
IN: irc.ui
2008-07-14 20:53:08 -04:00
SYMBOL: listener
SYMBOL: client
TUPLE: ui-window client tabs ;
2008-07-16 19:33:33 -04:00
TUPLE: irc-tab < frame listener client listmodel ;
: write-color ( str color -- )
foreground associate format ;
: red { 0.5 0 0 1 } ;
: green { 0 0.5 0 1 } ;
: blue { 0 0 1 1 } ;
2008-07-16 19:33:33 -04:00
: black { 0 0 0 1 } ;
: colors H{ { +operator+ { 0 0.5 0 1 } }
{ +voice+ { 0 0 1 1 } }
{ +normal+ { 0 0 0 1 } } } ;
2008-07-14 20:53:08 -04:00
: dot-or-parens ( string -- string )
dup empty? [ drop "." ]
[ "(" prepend ")" append ] if ;
GENERIC: write-irc ( irc-message -- )
M: privmsg write-irc
"<" blue write-color
2008-07-14 20:53:08 -04:00
[ prefix>> parse-name write ] keep
2008-07-11 17:11:03 -04:00
"> " blue write-color
trailing>> write ;
2008-07-11 17:11:03 -04:00
TUPLE: own-message message nick timestamp ;
: <own-message> ( message nick -- own-message )
now own-message boa ;
M: own-message write-irc
"<" blue write-color
[ nick>> bold font-style associate format ] keep
"> " blue write-color
message>> write ;
M: join write-irc
"* " green write-color
2008-07-14 20:53:08 -04:00
prefix>> parse-name write
" has entered the channel." green write-color ;
M: part write-irc
"* " red write-color
2008-07-14 20:53:08 -04:00
[ prefix>> parse-name write ] keep
" has left the channel" red write-color
trailing>> dot-or-parens red write-color ;
M: quit write-irc
"* " red write-color
2008-07-14 20:53:08 -04:00
[ prefix>> parse-name write ] keep
" has left IRC" red write-color
trailing>> dot-or-parens red write-color ;
2008-07-28 21:02:05 -04:00
: full-mode ( message -- mode )
parameters>> rest " " sjoin ;
2008-07-16 19:33:33 -04:00
M: mode write-irc
"* " blue write-color
2008-07-28 21:02:05 -04:00
[ prefix>> parse-name write ] keep
2008-07-16 19:33:33 -04:00
" has applied mode " blue write-color
2008-07-28 21:02:05 -04:00
[ full-mode write ] keep
2008-07-16 19:33:33 -04:00
" to " blue write-color
channel>> write ;
2008-07-28 21:02:05 -04:00
M: unhandled write-irc
"UNHANDLED: " write
line>> blue write-color ;
M: irc-end write-irc
drop "* You have left IRC" red write-color ;
M: irc-disconnected write-irc
drop "* Disconnected" red write-color ;
M: irc-connected write-irc
drop "* Connected" green write-color ;
2008-07-28 21:02:05 -04:00
M: irc-listener-end write-irc
drop ;
M: irc-message write-irc
drop ; ! catch all unimplemented writes, THIS WILL CHANGE
2008-07-28 21:02:05 -04:00
: time-happened ( irc-message -- timestamp )
[ timestamp>> ] [ 2drop now ] recover ;
: print-irc ( irc-message -- )
2008-07-28 21:02:05 -04:00
[ time-happened timestamp>hms write " " write ]
2008-07-11 17:11:03 -04:00
[ write-irc nl ] bi ;
2008-07-14 20:53:08 -04:00
: send-message ( message -- )
[ print-irc ]
[ listener get write-message ] bi ;
2008-07-16 19:33:33 -04:00
GENERIC: handle-inbox ( tab message -- )
: filter-participants ( assoc val -- alist )
[ >alist ] dip
'[ second , = ] filter ;
: update-participants ( tab -- )
[ listmodel>> ] [ listener>> participants>> ] bi
[ +operator+ filter-participants ]
[ +voice+ filter-participants ]
[ +normal+ filter-participants ] tri
append append swap set-model ;
M: participant-changed handle-inbox
drop update-participants ;
M: object handle-inbox
nip print-irc ;
: display ( stream tab -- )
'[ , [ [ t ]
2008-07-16 19:33:33 -04:00
[ , dup listener>> read-message handle-inbox ]
[ ] while ] with-output-stream ] "ircv" spawn drop ;
2008-07-16 19:33:33 -04:00
: <irc-pane> ( tab -- tab pane )
<scrolling-pane>
2008-07-16 19:33:33 -04:00
[ <pane-stream> swap display ] 2keep ;
TUPLE: irc-editor < editor outstream listener client ;
2008-07-16 19:33:33 -04:00
: <irc-editor> ( tab pane -- tab editor )
over irc-editor new-editor
swap listener>> >>listener swap <pane-stream> >>outstream
over client>> >>client ;
: editor-send ( irc-editor -- )
{ [ outstream>> ]
[ listener>> ]
[ client>> ]
2008-07-14 20:53:08 -04:00
[ editor-string ]
[ "" swap set-editor-string ] } cleave
2008-07-14 20:53:08 -04:00
'[ , listener set , client set , parse-message ] with-output-stream ;
irc-editor "general" f {
{ T{ key-down f f "RET" } editor-send }
{ T{ key-down f f "ENTER" } editor-send }
} define-command-map
2008-07-16 19:33:33 -04:00
: <irc-list> ( -- gadget model )
[ drop ]
[ first2 [ <label> ] dip >>color ]
{ } <model> [ <list> ] keep ;
: <irc-tab> ( listener client -- irc-tab )
irc-tab new-frame
swap client>> >>client swap >>listener
2008-07-25 13:34:41 -04:00
<irc-pane> [ <scroller> @center grid-add ] keep
<irc-editor> <scroller> @bottom grid-add ;
2008-07-16 19:33:33 -04:00
: <irc-channel-tab> ( listener client -- irc-tab )
<irc-tab>
2008-07-25 13:34:41 -04:00
<irc-list> [ <scroller> @right grid-add ] dip >>listmodel
2008-07-16 19:33:33 -04:00
[ update-participants ] keep ;
2008-07-14 20:53:08 -04:00
2008-07-16 19:33:33 -04:00
: <irc-server-tab> ( listener client -- irc-tab )
<irc-tab> ;
2008-07-14 20:53:08 -04:00
2008-07-16 19:33:33 -04:00
M: irc-tab graft*
2008-07-14 20:53:08 -04:00
[ listener>> ] [ client>> ] bi
add-listener ;
2008-07-16 19:33:33 -04:00
M: irc-tab ungraft*
2008-07-14 20:53:08 -04:00
[ listener>> ] [ client>> ] bi
remove-listener ;
: join-channel ( name ui-window -- )
[ dup <irc-channel-listener> ] dip
2008-07-16 19:33:33 -04:00
[ <irc-channel-tab> swap ] keep
2008-07-14 20:53:08 -04:00
tabs>> add-page ;
: irc-window ( ui-window -- )
[ tabs>> ]
[ client>> profile>> server>> ] bi
open-window ;
: ui-connect ( profile -- ui-window )
<irc-client> ui-window new over >>client swap
[ connect-irc ]
2008-07-27 20:15:38 -04:00
[ [ <irc-server-listener> ] dip add-listener ]
2008-07-16 19:33:33 -04:00
[ listeners>> +server-listener+ swap at over <irc-tab>
2008-07-27 20:15:38 -04:00
"Server" associate <tabbed> >>tabs ] tri ;
: server-open ( server port nick password channels -- )
[ <irc-profile> ui-connect [ irc-window ] keep ] dip
2008-07-16 19:33:33 -04:00
[ over join-channel ] each drop ;
: main-run ( -- ) run-ircui ;
MAIN: main-run