1 ! Copyright (C) 2011 Erik Charlebois.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs colors.constants combinators fry io
4 kernel listener readline sequences splitting system threads
5 tools.completion unicode.data vocabs vocabs.hierarchy ;
12 TUPLE: readline-reader { prompt initial: f } ;
13 INSTANCE: readline-reader input-stream
15 M: readline-reader stream-readln
16 flush [ prompt>> dup [ " " append ] [ ] if readline ]
17 keep f >>prompt drop ;
19 M: readline-reader prompt.
22 : clear-completions ( -- )
25 : prefixed ( prefix seq -- seq' )
26 swap '[ _ head? ] filter ;
28 : prefixed-words ( prefix -- words )
29 all-words [ name>> ] map! prefixed ;
31 : prefixed-vocabs ( prefix -- vocabs )
32 disk-vocabs-recursive filter-vocabs [ name>> ] map! prefixed ;
34 : prefixed-colors ( prefix -- colors )
35 named-colors prefixed ;
37 : prefixed-chars ( prefix -- chars )
38 name-map keys prefixed ;
40 : get-completions ( prefix -- completions )
41 completions tget [ nip ] [
42 completion-line " \r\n" split {
43 { [ dup complete-vocab? ] [ drop prefixed-vocabs ] }
44 { [ dup complete-char? ] [ drop prefixed-chars ] }
45 { [ dup complete-color? ] [ drop prefixed-colors ] }
46 [ drop prefixed-words ]
47 } cond dup completions tset
52 : readline-listener ( -- )
54 swap get-completions ?nth
55 [ clear-completions f ] unless*
57 readline-reader new [ listener ] with-input-stream* ;
59 : readline-listener-main ( -- )
60 version-info print flush readline-listener ;
62 MAIN: readline-listener-main