Skip to content

Commit 2ee46c9

Browse files
committed
Support hyperlinks in search output
Add `--hyperlink` flag to search sub command which outputs ANSI escape sequences that supporting terminal emulators can recognize.
1 parent 5b34a0d commit 2ee46c9

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/Action/CmdLine.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ data CmdLine
2525
,json :: Bool
2626
,jsonl :: Bool
2727
,link :: Bool
28+
,hyperlink :: Bool
2829
,numbers :: Bool
2930
,info :: Bool
3031
,database :: FilePath
@@ -114,6 +115,7 @@ search_ = Search
114115
,json = def &= name "json" &= help "Get result as JSON"
115116
,jsonl = def &= name "jsonl" &= help "Get result as JSONL (JSON Lines)"
116117
,link = def &= help "Give URL's for each result"
118+
,hyperlink = def &= help "Hyperlink results with ANSI escape sequences"
117119
,numbers = def &= help "Give counter for each result"
118120
,info = def &= help "Give extended information about the first result"
119121
,database = def &= typFile &= help "Name of database to use (use .hoo extension)"

src/Action/Search.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ actionSearch Search{..} = replicateM_ repeat_ $ -- deliberately reopen the datab
4343
count' <- pure $ fromMaybe 10 count
4444
(q, res) <- pure $ search store $ parseQuery $ unwords query
4545
whenLoud $ putStrLn $ "Query: " ++ unescapeHTML (LBS.unpack $ renderMarkup $ renderQuery q)
46-
let (shown, hidden) = splitAt count' $ nubOrd $ map (targetResultDisplay link) res
46+
let (shown, hidden) = splitAt count' $ nubOrd $ map (targetResultDisplay link hyperlink) res
4747
if null res then
4848
putStrLn "No results found"
4949
else if info then do
@@ -71,11 +71,13 @@ targetInfo Target{..} =
7171

7272
-- | Returns the Target formatted as an item to display in the results
7373
-- | Bool argument decides whether links are shown
74-
targetResultDisplay :: Bool -> Target -> String
75-
targetResultDisplay link Target{..} = unHTML $ unwords $
74+
targetResultDisplay :: Bool -> Bool -> Target -> String
75+
targetResultDisplay link hyperlink Target{..} = unHTML $ unwords $
7676
map fst (maybeToList targetModule) ++
77-
[targetItem] ++
77+
[if hyperlink then targetItemHyperlink else targetItem] ++
7878
["-- " ++ targetURL | link]
79+
where
80+
targetItemHyperlink = "\ESC]8;;" ++ targetURL ++ "\BEL" ++ targetItem ++ "\ESC]8;;\BEL"
7981

8082
unHTMLtargetItem :: Target -> Target
8183
unHTMLtargetItem target = target {targetItem = unHTML $ targetItem target}

0 commit comments

Comments
 (0)