Skip to content

Commit

Permalink
Support hyperlinks in search output
Browse files Browse the repository at this point in the history
Add `--hyperlink` flag to search sub command which outputs ANSI escape sequences
that supporting terminal emulators can recognize.
  • Loading branch information
avdv committed Mar 12, 2023
1 parent 5b34a0d commit 2ee46c9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Action/CmdLine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data CmdLine
,json :: Bool
,jsonl :: Bool
,link :: Bool
,hyperlink :: Bool
,numbers :: Bool
,info :: Bool
,database :: FilePath
Expand Down Expand Up @@ -114,6 +115,7 @@ search_ = Search
,json = def &= name "json" &= help "Get result as JSON"
,jsonl = def &= name "jsonl" &= help "Get result as JSONL (JSON Lines)"
,link = def &= help "Give URL's for each result"
,hyperlink = def &= help "Hyperlink results with ANSI escape sequences"
,numbers = def &= help "Give counter for each result"
,info = def &= help "Give extended information about the first result"
,database = def &= typFile &= help "Name of database to use (use .hoo extension)"
Expand Down
10 changes: 6 additions & 4 deletions src/Action/Search.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ actionSearch Search{..} = replicateM_ repeat_ $ -- deliberately reopen the datab
count' <- pure $ fromMaybe 10 count
(q, res) <- pure $ search store $ parseQuery $ unwords query
whenLoud $ putStrLn $ "Query: " ++ unescapeHTML (LBS.unpack $ renderMarkup $ renderQuery q)
let (shown, hidden) = splitAt count' $ nubOrd $ map (targetResultDisplay link) res
let (shown, hidden) = splitAt count' $ nubOrd $ map (targetResultDisplay link hyperlink) res
if null res then
putStrLn "No results found"
else if info then do
Expand Down Expand Up @@ -71,11 +71,13 @@ targetInfo Target{..} =

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

unHTMLtargetItem :: Target -> Target
unHTMLtargetItem target = target {targetItem = unHTML $ targetItem target}
Expand Down

0 comments on commit 2ee46c9

Please sign in to comment.