forked from haskell-github/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchCode.hs
More file actions
33 lines (29 loc) · 1.05 KB
/
SearchCode.hs
File metadata and controls
33 lines (29 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified GitHub
import Control.Monad (forM_)
import Data.List (intercalate)
import qualified Data.Text as T
main :: IO ()
main = do
let query = "Code repo:haskell-github/github"
result <- GitHub.github' GitHub.searchCodeR query 1000
case result of
Left e -> putStrLn $ "Error: " ++ show e
Right r -> do
forM_ (GitHub.searchResultResults r) $ \r -> do
putStrLn $ formatCode r
putStrLn ""
putStrLn $ "Count: " ++ show (GitHub.searchResultTotalCount r)
++ " matches for the query: \"" ++ T.unpack query ++ "\""
formatCode :: GitHub.Code -> String
formatCode r =
let fields = [ ("Name", show . GitHub.codeName)
, ("Path", show . GitHub.codePath)
, ("Sha", show . GitHub.codeSha)
, ("URL", show . GitHub.codeHtmlUrl)
]
in intercalate "\n" $ map fmt fields
where fmt (s,f) = fill 12 (s ++ ":") ++ " " ++ f r
fill n s = s ++ replicate n' ' '
where n' = max 0 (n - length s)