+cloogle :: String *World -> (String, *World)
+cloogle data w
+# (mer, w) = doRequestL
+ { newHTTPRequest
+ & req_path = "/api.php"
+ , req_query = "?str=" + urlEncode data
+ , req_headers = 'DM'.fromList [("User-Agent", "cloogle-irc")]
+ , server_name = "cloogle.org"
+ , server_port = 80} 10 w
+| isError mer = ("request failed: " + fromError mer, w)
+# resp = fromOk mer
+= case fromJSON $ fromString resp.HTTPResponse.rsp_data of
+ Nothing = ("couldn't parse json", w)
+ Just clr = ("Results for " + data + " -- https://cloogle.org/#" + replaceSubString "+" "%20" (urlEncode data) + "\n" +
+ processResults clr, w)
+ where
+ processResults :: Response -> String
+ processResults resp
+ | resp.return > 127 = "Failed: return code: " + toString resp.return + ", " + resp.msg
+ = join "\n" $ map processResult $ take 3 resp.data
+
+ processResult :: Result -> String
+ processResult (FunctionResult (br, {func}))
+ = "Function in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ func
+ processResult (TypeResult (br, {type}))
+ = "Type in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ limitResults type
+ processResult (ClassResult (br, {class_name,class_funs}))
+ = "Class in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ class_name +++ " with "
+ +++ toString (length class_funs) +++ " class functions"
+ processResult (MacroResult (br, {macro_name}))
+ = "Macro in " +++ br.library +++ ": " +++ br.modul +++ "\n" +++ macro_name
+ processResult (ModuleResult (br, _))
+ = "Module in " +++ br.library +++ ": " +++ br.modul
+
+ limitResults :: String -> String
+ limitResults s
+ # lines = split "\n" s
+ | length lines > 4 = limitResults (join "\n" (take 3 lines) + "\n...")
+ = join "\n" (map maxWidth lines)
+
+ maxWidth :: String -> String
+ maxWidth s
+ | size s > 80 = subString 0 77 s + "..."
+ = s
+