Safe Haskell | None |
---|---|
Language | Haskell2010 |
Console line fuzzy search as a library.
It's probably easier to toy with the library through
the wybor
executable, although this example uses ghci
, which is
also a pretty capable environment for toying
First, we need a bit of setup. Both extensions aren't strictly necessary but they make our life considerably easier. Imports, on the other hand, are essential.
Note: -XOverloadedLists
requires GHC >= 7.8
>>> :set -XOverloadedStrings >>> :set -XOverloadedLists >>> import Control.Lens >>> import Wybor
So, this starts a sligtly customized selection process (more settings are avaliable,
please see HasWybor
class):
>>>select
(fromTexts
["foo", "bar", "baz"] &prefix
.~ "λ> ") λ> foo bar baz
This new prompt (λ>
) expects us to enter symbols to narrow the list of outcomes, e.g.:
λ> b bar baz
λ> bz baz
At this point there is only one acceptable outcome, so we press Enter
and end up with:
Right (Just "baz")
Of course, we can press Enter
at any point of the process, selecting
the focused alternative (marked bold here). Other hotkeys:
C-j
selects the focused alternative (likeEnter
)C-u
clears the lineC-w
deletes last wordC-h
deletes last character (as doesBackspace
)C-n
focuses the next alternativeC-p
focuses the previous alternativeC-d
aborts the selection
- select :: Wybor a -> IO (Either TTYException (Maybe a))
- selections :: MonadResource m => Wybor a -> Source m a
- data Wybor a
- fromAssoc :: NonEmpty (Text, a) -> Wybor a
- fromTexts :: NonEmpty Text -> Wybor Text
- fromIO :: IO (Maybe (Maybe (NonEmpty (Text, a)))) -> Wybor a
- class HasWybor t a | t -> a where
- newtype TTYException = TTYIOException IOException
- reset :: Text
- bold :: Text
- regular :: Text
- underlining :: Underlining -> Text
- swap :: Text
- unswap :: Text
- fgcolor :: ColorIntensity -> Color -> Text
- bgcolor :: ColorIntensity -> Color -> Text
- data Underlining :: *
- data ColorIntensity :: *
- data Color :: *
Documentation
selections :: MonadResource m => Wybor a -> Source m a Source
Continuously select items from Wybor
Exceptions (see TTYException
) aren't caught
The description of the alternative choices, see HasWybor
fromAssoc :: NonEmpty (Text, a) -> Wybor a Source
Construct Wybor
from the nonempty list of key-value pairs
fromTexts :: NonEmpty Text -> Wybor Text Source
Construct Wybor
from the nonempty list of strings
The strings are used both as keys and values
fromIO :: IO (Maybe (Maybe (NonEmpty (Text, a)))) -> Wybor a Source
Construct Wybor
from the IO
action that streams choices
It's useful when the list of alternatives is populated over time from multiple sources (for instance, from HTTP responses)
The interface is tailored for the use with closeable queues from the "stm-chans" package:
>>>
q <- newTMQueueIO
>>>
... {- a bunch of threads populating and eventually closing the queue -}
>>>
c <- 'select' ('fromIO' (atomically (tryReadTMQueue q)))
>>>
print c
That is, if the IO
action returns
the queue will never be read from again
and it can return Nothing
when there's nothing to add to the choices yetJust
Nothing
It's still possible to use non-fancy queues:
>>>
q <- newTQueueIO
>>>
... {- a bunch of threads populating the queue -}
>>>
c <- 'select' ('fromIO' (fmap Just (atomically (tryReadTQueue q))))
>>>
print c
If choices are static, you will be served better by fromAssoc
and fromTexts
class HasWybor t a | t -> a where Source
A bunch of lenses to pick and configure Wybor
wybor :: Lens' t (Wybor a) Source
How many alternative choices to show at once? (default: 10
)
How many lines every alternative takes on the screen? (default: 1
)
initial :: Lens' t Text Source
Initial search string (default: ""
)
Prompt prefix (default: ">>> "
)
focused :: Lens' t (Text -> Text) Source
Decoration applied to the focused item (swaps foreground and background colors by default)
Note: should not introduce any printable symbols
normal :: Lens' t (Text -> Text) Source
Decoration applied to other items (is id
by default)
Note: should not introduce any printable symbols
newtype TTYException Source
Exceptions thrown while manipulating /dev/tty
device
A bunch of helpers to use with focused
and normal
underlining :: Underlining -> Text Source
Set underlining style
fgcolor :: ColorIntensity -> Color -> Text Source
Set foreground color
bgcolor :: ColorIntensity -> Color -> Text Source
Set background color
data Underlining :: *
ANSI text underlining
SingleUnderline | |
DoubleUnderline | Not widely supported |
NoUnderline |
data ColorIntensity :: *
ANSI colors come in two intensities