talash-0.1.0.0: Line oriented fast enough text search
Safe HaskellNone
LanguageHaskell2010

Talash.Piped

Description

This module provides a split searcher and seeker, a simple server-client version of the search in which searcher runs in the background and can communicate with clients using named pipes. The searcher reads input as a UTF8 encoded bytestring from one named pipe and outputs the search results to another named pipe. It quits when it receives an empty bytestring as input. The main function for starting the server is runSearch while a simple client is provided by askSearcher. One motivation for this is be able to use this library as search backend for some searches in Emacs (though the implementation may have to wait for a massive improvement in my elisp skills).

Synopsis

Types and Lenses

data SearchResult Source #

Constructors

SearchResult 

Fields

Instances

Instances details
Eq SearchResult Source # 
Instance details

Defined in Talash.Piped

Show SearchResult Source # 
Instance details

Defined in Talash.Piped

data IOPipes Source #

Constructors

IOPipes 

Fields

  • input :: Handle

    The handle to the named piped on which the server receives input to search for.

  • output :: Handle

    The handle to the named piped on which the searcher outputs the search results.

data SearchFunctions a Source #

Constructors

SearchFunctions 

Fields

searchFunctionsOL :: SearchFunctions Int Source #

Search functions that match the words in i.e. space separated substring in any order. "talash best" will match "be as" with the split ["tal","as","h","be","st"] but "talash best" will not match "bet".

searchFunctionsFuzzy :: SearchFunctions MatchPart Source #

Search functions suitable for fuzzy matching. The candidate c will match the query s if c contains all the characters in s in order. In general there can be several ways of matching. This tries to find a match with the minimum number of parts. It does not find the minimum number of parts, if that requires reducing the extent of the partial match during search. E.g. matching "as" against "talash" the split will be ["tal","as","h"] and not ["t","a","la","s","h"]. While matching "talash best match testing hat" against "tea" will not result in ["talash best match ","te","sting h","a","t"] since "te" occurs only after we have match all three letters and we can't know if we will find the "a" without going through the string.

Searcher

searchLoop :: SearchFunctions a -> (Handle -> [Text] -> IO ()) -> Vector Text -> IO () Source #

Starts with the dummy initialSearchResult and handles event in a loop until the searcher receives an empty input and exits.

runSearch :: SearchFunctions a -> (Handle -> [Text] -> IO ()) -> Vector Text -> IO () Source #

Run search create a new session for the searcher to run in, forks a process in which the searchLoop is run in the background and exits.

runSearchStdIn :: SearchFunctions a -> (Handle -> [Text] -> IO ()) -> IO () Source #

Version of runSearch in which the vector of candidates is built by reading lines from stdin.

runSearchStdInDef :: SearchFunctions a -> IO () Source #

Version of runSearchStdIn which uses showMatch to put the output on the handle.

runSearchStdInColor :: SearchFunctions a -> IO () Source #

Version of runSearchStdIn for viewing the matches on a terminal which uses showMatchColor to put the output on the handle.

showMatch :: Handle -> [Text] -> IO () Source #

Outputs the parts of a matching candidate to handle as space separated double quoted strings alternating between a match and a gap. The first text is always a gap and can be empty the rest should be non-empty

showMatchColor :: Handle -> [Text] -> IO () Source #

Outputs a matching candidate for the terminal with the matches highlighted in blue. Uses the Colored Text monoid from `colorful-monoids` for coloring.

Seeker

askSearcher Source #

Arguments

:: String

Path to the input named pipe to which to write the query.

-> String

Path to the output named pipe from which to read the results.

-> Text

Th qeury itself.

-> IO () 

Default program

run :: IO () Source #

run is a small demo program for the piped search. Run `talash piped` to see usage information.

run' :: [String] -> IO () Source #

run' is the backend of run which is just `run' =<< getArgs`

Exposed Internals

response Source #

Arguments

:: SearchFunctions a

The functions determining how to much.

-> Vector Text

The vector of candidates.

-> Text

The text to match

-> SearchResult

The last result result. This is used to determine which candidates to search among.

-> SearchResult 

event Source #

Arguments

:: SearchFunctions a 
-> (Handle -> [Text] -> IO ())

The functions that determines how a results is presented. Must not introduce newlines.

-> IOPipes

The handles to the named pipes

-> Vector Text

The candidates

-> SearchResult

The last search result

-> IO (Maybe SearchResult)

The final result. The Nothing is for the case if the input was empty signalling that the searcher should exit.

One search event consisiting of the searcher reading a bytestring from the input named pipe. If the bytestring is empty the searcher exists. If not it outputs the search results to the output handle and also returns them.

The first line of the output of results is the query. The second is an decimal integer n which is the number of results to follow. There are n more lines each contaning a result presented according the function supplied.

withIOPipes :: (IOPipes -> IO a) -> IO a Source #

Run an IO action that needs two handles to named pipes by creating two named pipes, opening the handles to them performing the action and then cleaning up by closing the handles and deleting the named pipes created. The names of the pipes are printed on the stdout and are of the form /tmp/talash-input-pipe or /tmp/talash-input-pipe<n> where n is an integer for the input-pipe and /tmp/talash-output-pipe or /tmp/talash-output-pipe<n> for the output pipe. The integer n will be the same for both.

send :: String -> Text -> IO () Source #

searchWithMatcher Source #

Arguments

:: SearchFunctions a

The configuration to use to carry out the search.

-> Vector Text

The vector v of candidates.

-> Maybe Text

The query string

-> Vector Int

The subset of indices of v to search against. If input changes from talas to talash we only search among candidates that matched talas.

-> (Vector Int, (Int, Vector [Text]))

The indices of the matched candidates (see the note above) and the matched candidates broken up according to the match.

searchWithMatcher carries out one step of the search. Note that the search can stops before going through the whole vector of text. In that case the returned vector of indices should contain not only the indices matched candidates but also the indices of candidates that weren't tested for a match.

readVectorStdIn :: IO (Vector Text) Source #

Read a vector of newline separated candidates from the stdin.

readVectorHandle :: Handle -> IO (Vector Text) Source #

Read a vector of newline separated candidates from a handle.

readVectorHandleWith Source #

Arguments

:: (Text -> Text)

The function to transform the candidates.

-> (Vector Text -> Vector Text)

The function to apply to the constructed vector before compacting.

-> Handle

The handle to read from

-> IO (Vector Text) 

A generalized version of readVectorHandle allowing for the transformation of candidates and the resulting vector. See fileNamesSorted for an example of use.