wybor-0.1.0: Console line fuzzy search

Safe HaskellNone
LanguageHaskell2010

Wybor

Contents

Description

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 (like Enter)
  • C-u clears the line
  • C-w deletes last word
  • C-h deletes last character (as does Backspace)
  • C-n focuses the next alternative
  • C-p focuses the previous alternative
  • C-d aborts the selection

Synopsis

Documentation

select :: Wybor a -> IO (Either TTYException (Maybe a)) Source

Select an item from Wybor once

The user can interrupt the process with C-d and then you get Nothing. Exceptions result in Left _

selections :: MonadResource m => Wybor a -> Source m a Source

Continuously select items from Wybor

Exceptions (see TTYException) aren't caught

data Wybor a Source

The description of the alternative choices, see HasWybor

Instances

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 Nothing the queue will never be read from again and it can return Just Nothing when there's nothing to add to the choices yet

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

Minimal complete definition

wybor

Methods

wybor :: Lens' t (Wybor a) Source

visible :: Lens' t Int Source

How many alternative choices to show at once? (default: 10)

height :: Lens' t Int Source

How many lines every alternative takes on the screen? (default: 1)

initial :: Lens' t Text Source

Initial search string (default: "")

prefix :: Lens' t Text Source

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

Instances

HasWybor (Wybor a) a 

newtype TTYException Source

Exceptions thrown while manipulating /dev/tty device

A bunch of helpers to use with focused and normal

reset :: Text Source

Sets all attributes off

bold :: Text Source

Set bold font style

regular :: Text Source

Set regular font style

underlining :: Underlining -> Text Source

Set underlining style

swap :: Text Source

Swap foreground and background colors

unswap :: Text Source

Unswap foreground and background colors

fgcolor :: ColorIntensity -> Color -> Text Source

Set foreground color

bgcolor :: ColorIntensity -> Color -> Text Source

Set background color

data Color :: *

ANSI colors: come in various intensities, which are controlled by ColorIntensity

Constructors

Black 
Red 
Green 
Yellow 
Blue 
Magenta 
Cyan 
White