From 2ee46c9810181fe1db1ce2f4be72e2e6324575bc Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Sun, 12 Mar 2023 15:48:47 +0100 Subject: [PATCH] Support hyperlinks in search output Add `--hyperlink` flag to search sub command which outputs ANSI escape sequences that supporting terminal emulators can recognize. --- src/Action/CmdLine.hs | 2 ++ src/Action/Search.hs | 10 ++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Action/CmdLine.hs b/src/Action/CmdLine.hs index 30266573..8d033bdb 100644 --- a/src/Action/CmdLine.hs +++ b/src/Action/CmdLine.hs @@ -25,6 +25,7 @@ data CmdLine ,json :: Bool ,jsonl :: Bool ,link :: Bool + ,hyperlink :: Bool ,numbers :: Bool ,info :: Bool ,database :: FilePath @@ -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)" diff --git a/src/Action/Search.hs b/src/Action/Search.hs index e01de4a3..da855eea 100644 --- a/src/Action/Search.hs +++ b/src/Action/Search.hs @@ -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 @@ -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}