Initial pass at adding color to output

This commit is contained in:
Joshua Clayton 2016-04-29 04:46:31 -04:00
parent a5230f163e
commit ed72d2405a
2 changed files with 36 additions and 2 deletions

View File

@ -1,12 +1,45 @@
module Main where
import System.Console.ANSI
import Data.List (nub)
import Unused.TermSearch (search)
import Unused.Parser (parseLines)
import Unused.Types
main :: IO ()
main = do
terms <- pure . lines =<< getContents
results <- pure . concat =<< mapM search terms
let matches = parseLines $ unlines results
let groupedMatches = groupBy term $ parseLines $ unlines results
print matches
mapM_ printMatchPair groupedMatches
return ()
printMatchPair :: (String, [TermMatch]) -> IO ()
printMatchPair (term', matches) = do
setSGR [SetColor Foreground Vivid Red]
setSGR [SetConsoleIntensity BoldIntensity]
putStrLn term'
setSGR [Reset]
printMatches matches
putStr "\n"
printMatches :: [TermMatch] -> IO ()
printMatches matches = do
mapM_ printMatch matches
where
printMatch m = do
setSGR [SetColor Foreground Dull Green]
putStr $ " " ++ path m
setSGR [SetColor Foreground Dull Yellow]
putStr $ " " ++ (show . occurrences) m ++ " "
setSGR [Reset]
putStr "\n"
groupBy :: Eq b => (a -> b) -> [a] -> [(b, [a])]
groupBy f l =
fmap (\t -> (t, byTerm t)) uniqueTerms
where
byTerm t = filter (((==) t) . f) l
uniqueTerms = nub $ fmap f l

View File

@ -29,6 +29,7 @@ executable unused
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
build-depends: base
, unused
, ansi-terminal
default-language: Haskell2010
test-suite unused-test