Skip to content

Commit

Permalink
readline-listener: experiment with other ways to get colored prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Nov 14, 2024
1 parent 7130bd7 commit 86a8655
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions extra/readline-listener/readline-listener.factor
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
! Copyright (C) 2011 Erik Charlebois.
! See https://factorcode.org/license.txt for BSD license.

USING: accessors assocs colors combinators editors io
io.streams.256color io.streams.ansi kernel listener
readline sequences sets splitting threads terminfo
io.streams.256color io.streams.ansi io.streams.string kernel
listener readline sequences sets splitting terminfo threads
tools.completion ui.theme ui.theme.switching
ui.tools.listener.history unicode.data vocabs
vocabs.hierarchy ;

ui.tools.listener.history unicode.data vocabs vocabs.hierarchy ;
IN: readline-listener

<PRIVATE

SYMBOL: completions

TUPLE: readline-reader { prompt initial: f } ;
SYMBOLS: +256color+ +ansi+ ;

TUPLE: readline-reader { prompt initial: f } { mode initial: f } ;

: <readline-reader> ( mode -- reader )
readline-reader new swap >>mode ;

: with-readline-reader ( reader quot -- )
over mode>> {
{ +256color+ [ '[ _ with-256color ] with-input-stream* ] }
{ +ansi+ [ '[ _ with-ansi ] with-input-stream* ] }
{ f [ with-input-stream* ] }
} case ; inline

INSTANCE: readline-reader input-stream

M: readline-reader stream-readln
flush
[ dup [ " " append ] when readline f ] change-prompt
drop ;
flush [ readline f ] change-prompt drop ;

M: readline-reader prompt.
>>prompt drop ;
over [
[ [ call-next-method ] with-readline-reader ] with-string-writer
] keep prompt<< ;

: clear-completions ( -- )
f completions tset ;
Expand Down Expand Up @@ -82,12 +92,12 @@ PRIVATE>
] set-completion
history-file [
dark-theme switch-theme-if-default
[ readline-reader new [ listener-main ] with-input-stream* ]
{
{ [ tty-supports-256color? ] [ with-256color ] }
{ [ tty-supports-ansicolor? ] [ with-ansi ] }
[ call ]
{ [ tty-supports-256color? ] [ +256color+ ] }
{ [ tty-supports-ansicolor? ] [ +ansi+ ] }
[ f ]
} cond
<readline-reader> [ listener-main ] with-readline-reader
] with-history ;

: ?readline-listener ( -- )
Expand Down

0 comments on commit 86a8655

Please sign in to comment.