@@ -21,7 +21,10 @@ import qualified Data.Set as Set
2121import System.Directory
2222import Text.Blaze.Renderer.Utf8
2323import Safe
24- import System.Console.ANSI (hSupportsANSI , hyperlinkCode )
24+ import System.Console.ANSI
25+ (hSupportsANSI , hyperlinkCode , setSGRCode
26+ ,SGR (SetColor ), ConsoleLayer (Foreground )
27+ ,ColorIntensity (Vivid , Dull ), Color (Yellow ))
2528import System.IO (stdout )
2629
2730import Action.CmdLine
@@ -46,19 +49,18 @@ actionSearch Search{..} = replicateM_ repeat_ $ -- deliberately reopen the datab
4649 count' <- pure $ fromMaybe 10 count
4750 (q, res) <- pure $ search store $ parseQuery $ unwords query
4851 whenLoud $ putStrLn $ " Query: " ++ unescapeHTML (LBS. unpack $ renderMarkup $ renderQuery q)
49- hyperlink <- case color of
52+ color' <- case color of
5053 Just b -> pure b
5154 Nothing -> hSupportsANSI stdout
52- let (shown, hidden) = splitAt count' $ nubOrd $ map (targetResultDisplay link hyperlink ) res
55+ let (shown, hidden) = splitAt count' $ nubOrd $ map (targetResultDisplay link color' q ) res
5356 if null res then
5457 putStrLn " No results found"
5558 else if info then do
56- putStr $ targetInfo $ headErr res
59+ putStr $ targetInfo color' q $ headErr res
5760 else do
58- let toShow = if numbers && not info then addCounter shown else shown
5961 if | json -> LBS. putStrLn $ JSON. encode $ maybe id take count $ map unHTMLtargetItem res
6062 | jsonl -> mapM_ (LBS. putStrLn . JSON. encode) $ maybe id take count $ map unHTMLtargetItem res
61- | otherwise -> putStr $ unlines toShow
63+ | otherwise -> putStr $ unlines $ if numbers then addCounter shown else shown
6264 when (hidden /= [] && not json) $ do
6365 whenNormal $ putStrLn $ " -- plus more results not shown, pass --count=" ++ show (count'+ 10 ) ++ " to see more"
6466 else do
@@ -68,22 +70,29 @@ actionSearch Search{..} = replicateM_ repeat_ $ -- deliberately reopen the datab
6870 putStr $ unlines $ searchFingerprintsDebug store (parseType $ unwords query) (map parseType compare_)
6971
7072-- | Returns the details printed out when hoogle --info is called
71- targetInfo :: Target -> String
72- targetInfo Target {.. } =
73- unlines $ [ unHTML targetItem ] ++
73+ targetInfo :: Bool -> [ Query ] -> Target -> String
74+ targetInfo color qs Target {.. } =
75+ unlines $ [ unHTML . ( if color then ansiHighlight qs else id ) $ targetItem ] ++
7476 [ unwords packageModule | not $ null packageModule] ++
7577 [ unHTML targetDocs ]
7678 where packageModule = map fst $ catMaybes [targetPackage, targetModule]
7779
7880-- | Returns the Target formatted as an item to display in the results
79- -- | Bool argument decides whether links are shown
80- targetResultDisplay :: Bool -> Bool -> Target -> String
81- targetResultDisplay link hyperlink Target {.. } = unHTML $ unwords $
81+ -- | Bool arguments decide whether links and colors are shown
82+ targetResultDisplay :: Bool -> Bool -> [ Query ] -> Target -> String
83+ targetResultDisplay link color qs Target {.. } = unHTML $ unwords $
8284 map fst (maybeToList targetModule) ++
83- [if hyperlink then targetItemHyperlink else targetItem] ++
85+ [if color then highlightFull targetItem else targetItem] ++
8486 [" -- " ++ targetURL | link]
8587 where
86- targetItemHyperlink = hyperlinkCode targetURL targetItem
88+ highlightFull = hyperlinkCode targetURL . ansiHighlight qs
89+
90+ ansiHighlight :: [Query ] -> String -> String
91+ ansiHighlight = highlightItem id id ((dull ++ ) . (++ rst)) ((bold ++ ) . (++ rst))
92+ where
93+ dull = setSGRCode [SetColor Foreground Dull Yellow ]
94+ bold = setSGRCode [SetColor Foreground Vivid Yellow ]
95+ rst = setSGRCode []
8796
8897unHTMLtargetItem :: Target -> Target
8998unHTMLtargetItem target = target {targetItem = unHTML $ targetItem target}
0 commit comments