hayoo-cli (empty) → 0.1.0.0
raw patch · 4 files changed
+117/−0 lines, 4 filesdep +aesondep +basedep +bytestringsetup-changed
Dependencies added: aeson, base, bytestring, http-conduit, http-types, optparse-applicative, pandoc, url
Files
- LICENSE +20/−0
- Main.hs +64/−0
- Setup.hs +2/−0
- hayoo-cli.cabal +31/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2014 Max Gonzih++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Main.hs view
@@ -0,0 +1,64 @@+{-# LANGUAGE OverloadedStrings #-}++module Main where++import Paths_hayoo_cli (version)+import Data.Version (showVersion)+import Data.ByteString.Char8 (pack)+import qualified Data.ByteString.Lazy as BSL+import Data.Aeson+import Network.HTTP.Conduit+import Network.HTTP.Types.Header (hUserAgent)+import Network.URL (encString, ok_url)+import Text.Pandoc (def, readHtml, writeAsciiDoc)+import Text.Pandoc.Options (WriterOptions(..))+import CliOptions+import HayooTypes++jsonData :: Opts -> IO BSL.ByteString+jsonData (Opts _ searchQuery) = do+ req <- parseUrl url+ let req' = req { requestHeaders = [userAgentHeader] }+ response <- withManager $ httpLbs req'+ return $ responseBody response+ where url = "http://hayoo.fh-wedel.de/json?query=" ++ encQuery+ encQuery = encString True ok_url searchQuery+ userAgent = pack $ "hayoo-cli/" ++ showVersion version+ userAgentHeader = (hUserAgent, userAgent)++decodeHayooResponse :: BSL.ByteString -> HayooResponse+decodeHayooResponse bs = case eitherDecode bs of+ (Right res) -> res+ (Left err) -> error $ show err++printDelimiter :: Char -> IO ()+printDelimiter = putStrLn . replicate 100++htmlToAscii :: String -> String+htmlToAscii = (writeAsciiDoc def {writerReferenceLinks = True}) . readHtml def++printResultFull :: HayooResult -> IO ()+printResultFull singleResult@(HayooResult _ _ _ _ _ desc _ _ _ _) =+ printResultShort singleResult+ >> putStrLn ""+ >> (putStrLn $ htmlToAscii desc)++printResultShort :: HayooResult -> IO ()+printResultShort (HayooResult _ _ _ name _ _ signature modules _ _) =+ putStrLn $ unwords $ modules ++ nameAndSignaruter name signature+ where nameAndSignaruter n "" = [n]+ nameAndSignaruter n s = [n, "::", s]++printResponse :: Opts -> HayooResponse -> IO ()+printResponse (Opts _ _) (HayooResponse _ _ _ []) = putStrLn "No results found"+printResponse (Opts True _) (HayooResponse _ _ _ (res:_)) = printResultFull res+printResponse (Opts False _) (HayooResponse _ _ _ results) = mapM_ printResultShort results++run :: Opts -> IO ()+run opts =+ (jsonData opts)+ >>= ((printResponse opts) . decodeHayooResponse)++main :: IO ()+main = parseArguments ver run+ where ver = showVersion version
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hayoo-cli.cabal view
@@ -0,0 +1,31 @@+-- Initial hayoo.cabal generated by cabal init. For further documentation,+-- see http://haskell.org/cabal/users-guide/++name: hayoo-cli+version: 0.1.0.0+synopsis: Hayoo CLI+description: Simple console client for hayoo.+license: MIT+license-file: LICENSE+author: Max Gonzih+maintainer: gonzih@gmail.com+-- copyright:+category: Development+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++executable hayoo+ main-is: Main.hs+ -- other-modules:+ other-extensions: OverloadedStrings+ build-depends: base >=4.6 && <4.8+ , pandoc >= 1.12+ , aeson >= 0.7+ , http-conduit >=2.0 && <2.1+ , bytestring >=0.10 && <0.11+ , optparse-applicative >= 0.9+ , url >= 2.1+ , http-types >= 0.8+ -- hs-source-dirs:+ default-language: Haskell2010