module Alfred
( Item (..)
, item
, Icon (..)
, Renderer
, Renderer'
, runScript
, runScript'
, searchRenderer
, Search (..)) where
import Text.XML.Generator
import qualified Data.ByteString as B
import Data.Text (Text)
import qualified Data.Text as T
import Data.ByteString (ByteString)
import Data.Monoid
import System.Environment
import Data.List
import Control.Applicative
import Alfred.Query
data Item = Item {
uid :: Maybe Text
, arg :: Text
, isFile :: Bool
, valid :: Maybe Bool
, autocomplete :: Maybe Text
, title :: Text
, subtitle :: Text
, icon :: Maybe Icon
}
item :: Item
item = Item {uid=Nothing,arg=undefined,isFile=False,valid=Nothing,
autocomplete=Nothing,title=undefined, subtitle=undefined,
icon=Just (IconFile "icon.png")}
type Items = [Item]
data Icon = FileIcon Text | FileType Text | IconFile Text
xmlIcon :: Icon -> Xml Elem
xmlIcon icon = case icon of
FileIcon str -> mk str (xattr "type" "fileicon")
FileType str -> mk str (xattr "type" "filetype")
IconFile str -> mk str mempty
where mk :: Text -> Xml Attr -> Xml Elem
mk str f = xelem "icon" (f <#> xtext str)
xmlItem :: Item -> Xml Elem
xmlItem (Item uid arg file val auto title sub icon) =
xelem "item" $
(uid' <> xattr "arg" arg <> val' <> auto' <> file') <#>
(xelemWithText "title" title <> xelemWithText "subtitle" sub <> icon')
where uid' = case uid of Nothing -> mempty; Just uid -> xattr "uid" uid
val' = case val of
Nothing -> mempty
Just val -> xattr "valid" (if val then "yes" else "no")
file' = if file then xattr "type" "file" else mempty
auto' = case auto of Nothing -> mempty; Just auto -> xattr "autocomplete" auto
icon' = case icon of Nothing -> mempty; Just icon -> xmlIcon icon
xmlItems :: Items -> Xml Elem
xmlItems = xelem "items" . mconcat . map xmlItem
renderItems :: Items -> ByteString
renderItems = xrender . xmlItems
printItems :: Items -> IO ()
printItems = B.putStr . renderItems
type Renderer a = Renderer' Text a
type Renderer' q a = (q -> Either String a -> Items)
runScript' :: ([Text] -> q)
-> Query' q a
-> Renderer' q a
-> IO ()
runScript' inp runQuery mkItems = do
args <- (inp . map T.pack) <$> getArgs
res <- runQuery args
printItems $ mkItems args res
runScript :: Query a
-> Renderer a
-> IO ()
runScript = runScript' (T.concat . intersperse " ")
data Search = Search {searchURL, found, notFound, suggestError :: Text -> Text}
searchRenderer :: Search -> Renderer [Text]
searchRenderer Search {suggestError, searchURL} s (Left _) =
[Item {uid=Nothing,arg=searchURL (escapeText s),isFile=False,
valid=Nothing,autocomplete=Nothing,title=s,
subtitle=suggestError s,icon=Just (IconFile "icon.png")}]
searchRenderer Search {searchURL, found, notFound} s (Right suggs) =
case suggs of
[] -> [Item {uid=Nothing,arg=searchURL2 (escapeText s),isFile=False,
valid=Nothing,autocomplete=Nothing,title=s,
subtitle=notFound s,icon=Just (IconFile "icon.png")}]
res -> map mkItem res
where mkItem :: Text -> Item
mkItem t = Item {uid=Nothing,arg=arg,isFile=False,valid=Nothing,
autocomplete=Just t, title=t, subtitle=found t,icon=Just (IconFile "icon.png")}
where arg = T.concat ["\"",searchURL (escapeText t),"\" \"", searchURL (escapeText s), "\""]
searchURL2 s = T.concat ["\"",url,"\" \"", url, "\""]
where url = searchURL s