forked from haskell-github/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchRepos.hs
More file actions
59 lines (53 loc) · 1.97 KB
/
SearchRepos.hs
File metadata and controls
59 lines (53 loc) · 1.97 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified GitHub
import Control.Monad (forM_)
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Data.List (intercalate)
import System.Environment (getArgs)
import Text.Printf (printf)
import Data.Time.Clock (getCurrentTime, UTCTime(..))
import Data.Time.LocalTime (utc,utcToLocalTime,localDay)
import Data.Time.Calendar (toGregorian)
import Data.Text (Text)
import qualified Data.Text as T
main :: IO ()
main = do
args <- getArgs
date <- case args of
(x:_) -> return $ T.pack x
_ -> today
let query = ("language:haskell created:>" <> date) :: Text
result <- GitHub.github' GitHub.searchReposR query 1000
case result of
Left e -> putStrLn $ "Error: " ++ show e
Right r -> do
forM_ (GitHub.searchResultResults r) $ \r -> do
putStrLn $ formatRepo r
putStrLn ""
putStrLn $ "Count: " ++ show (GitHub.searchResultTotalCount r)
++ " Haskell repos created since " ++ T.unpack date
-- | return today (in UTC) formatted as YYYY-MM-DD
today :: IO Text
today = do
now <- getCurrentTime
let day = localDay $ utcToLocalTime utc now
(y,m,d) = toGregorian day
in return $ T.pack $ printf "%d-%02d-%02d" y m d
formatRepo :: GitHub.Repo -> String
formatRepo r =
let fields = [ ("Name", show . GitHub.repoName)
,("URL", show . GitHub.repoHtmlUrl)
,("Description", show . orEmpty . GitHub.repoDescription)
,("Created-At", formatMaybeDate . GitHub.repoCreatedAt)
,("Pushed-At", formatMaybeDate . GitHub.repoPushedAt)
,("Stars", show . GitHub.repoStargazersCount)
]
in intercalate "\n" $ map fmt fields
where fmt (s,f) = fill 12 (s ++ ":") ++ " " ++ f r
orEmpty = fromMaybe ""
fill n s = s ++ replicate n' ' '
where n' = max 0 (n - length s)
formatMaybeDate :: Maybe UTCTime -> String
formatMaybeDate = maybe "???" show