From 5fa1d54ce27ca93ac116ed765b8e9a5a6161dd56 Mon Sep 17 00:00:00 2001 From: mbays Date: Wed, 24 Mar 2021 00:00:00 +0000 Subject: [PATCH] add option link_desc_first --- Command.hs | 5 ++++- TextGemini.hs | 16 +++++++++------- diohsc.hs | 16 ++++++++++++---- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/Command.hs b/Command.hs index be5a391..a0f93d3 100644 --- a/Command.hs +++ b/Command.hs @@ -129,7 +129,7 @@ helpText = topics :: [String] topics = ["targets", "queue", "pager", "trust", "configuration" , "default_action", "proxies", "geminators", "render_filter" - , "pre_display", "log_length", "max_wrap_width", "no_confirm" + , "pre_display", "link_desc_first", "log_length", "max_wrap_width", "no_confirm" , "verbose_connection", "copying", "topics"] helpOn :: String -> [String] @@ -308,6 +308,9 @@ helpOn s = [ "{set} {pre_display} pre: suppress alt text of preformatted blocks" , "{set} {pre_display} alt: display only alt text of preformatted blocks" , "{set} {pre_display} both: display alt text and contents of preformatted blocks" ] + "link_desc_first" -> + [ "{set} {link_desc_first} true: show link description before uri" + , "{set} {link_desc_first} false: show uri before link description" ] "log_length" -> [ "{set} {log_length} N: set number of items to store in log" , "{set} {log_length} 0: clear log and disable logging" diff --git a/TextGemini.hs b/TextGemini.hs index 83e513e..a35b62c 100644 --- a/TextGemini.hs +++ b/TextGemini.hs @@ -60,15 +60,16 @@ data GemRenderOpts = GemRenderOpts { grOptsAnsi :: Bool , grOptsPre :: PreOpt , grOptsWrapWidth :: Int + , grOptsLinkDescFirst :: Bool } deriving (Eq,Ord,Show) printGemDoc :: GemRenderOpts -> (URIRef -> T.Text) -> GeminiDocument -> [T.Text] -printGemDoc (GemRenderOpts ansi preOpt width) +printGemDoc (GemRenderOpts ansi preOpt width linkDescFirst) showUri (GeminiDocument ls) = concatMap printLine ls where printLine (TextLine line) = wrap width line printLine (LinkLine n link) = (:[]) $ - printGemLinkLine ansi showUri (n+1) link + printGemLinkLine ansi linkDescFirst showUri (n+1) link printLine (AltTextLine line) | preOpt == PreOptPre = [] | preOpt == PreOptBoth && T.null line = [] @@ -104,13 +105,14 @@ printGemDoc (GemRenderOpts ansi preOpt width) then l : wrap' w nw ws else wrap' l' n' ws -printGemLinkLine :: Bool -> (URIRef -> T.Text) -> Int -> Link -> T.Text -printGemLinkLine ansi showUri n (Link uri desc) = +printGemLinkLine :: Bool -> Bool -> (URIRef -> T.Text) -> Int -> Link -> T.Text +printGemLinkLine ansi linkDescFirst showUri n (Link uri desc) = applyIf ansi withBoldStr (T.pack $ '[' : show n ++ "]") <> " " - <> showUri uri - <> (if T.null desc then "" else - applyIf ansi (withColourStr Cyan) $ " " <> desc) + <> (if T.null desc then id + else (if linkDescFirst then id else flip) (\a b -> a <> " " <> b) + $ applyIf ansi (withColourStr Cyan) desc) + (showUri uri) data GeminiParseState = GeminiParseState { numLinks :: Int, preformatted :: Bool } diff --git a/diohsc.hs b/diohsc.hs index e111bcd..5f63120 100644 --- a/diohsc.hs +++ b/diohsc.hs @@ -147,6 +147,7 @@ data ClientConfig = ClientConfig , clientConfGeminators :: [(String,(Regex,String))] , clientConfRenderFilter :: Maybe String , clientConfPreOpt :: PreOpt + , clientConfLinkDescFirst :: Bool , clientConfMaxLogLen :: Int , clientConfMaxWrapWidth :: Int , clientConfNoConfirm :: Bool @@ -154,7 +155,7 @@ data ClientConfig = ClientConfig } defaultClientConfig :: ClientConfig -defaultClientConfig = ClientConfig ("page", []) M.empty [] Nothing PreOptPre 1000 80 False False +defaultClientConfig = ClientConfig ("page", []) M.empty [] Nothing PreOptPre False 1000 80 False False data QueueItem = QueueItem { queueOrigin :: Maybe HistoryOrigin @@ -411,7 +412,7 @@ handleCommandLine :: ClientOptions -> ClientState -> CommandLine -> ClientM () handleCommandLine cOpts@(ClientOptions userDataDir interactive ansi ghost restrictedMode requestContext logH) cState@(ClientState curr jumpBack cLog visited queue _ marks sessionMarks aliases _ - (ClientConfig defaultAction proxies geminators renderFilter preOpt maxLogLen maxWrapWidth confNoConfirm verboseConnection)) + (ClientConfig defaultAction proxies geminators renderFilter preOpt linkDescFirst maxLogLen maxWrapWidth confNoConfirm verboseConnection)) = \(CommandLine mt mcas) -> case mcas of Just (c,args) | Just (Alias _ (CommandLine mt' mcas')) <- lookupAlias c aliases -> let mcas'' = (, drop 1 args) . commandArgArg <$> headMay args @@ -745,6 +746,7 @@ handleCommandLine printMap id $ second snd <$> geminators putStrLn $ expand "{render_filter}: " <> fromMaybe "" renderFilter putStrLn $ expand "{pre_display}: " <> showPreOpt preOpt + putStrLn $ expand "{link_desc_first}: " <> show linkDescFirst putStrLn $ expand "{log_length}: " <> show maxLogLen putStrLn $ expand "{max_wrap_width}: " <> show maxWrapWidth putStrLn $ expand "{no_confirm}: " <> show noConfirm @@ -785,6 +787,12 @@ handleCommandLine [CommandArg s _] | map toLower s `isPrefixOf` "alt" -> modifyCConf $ \c -> c { clientConfPreOpt = PreOptAlt } _ -> printErr "Require \"both\" or \"pre\" or \"alt\" for pre_display" + | opt `isPrefixOf` "link_desc_first" = case val of + [CommandArg s _] | map toLower s `isPrefixOf` "true" -> + modifyCConf $ \c -> c { clientConfLinkDescFirst = True } + [CommandArg s _] | map toLower s `isPrefixOf` "false" -> + modifyCConf $ \c -> c { clientConfLinkDescFirst = False } + _ -> printErr "Require \"true\" or \"false\" as value for link_desc_first" | opt `isPrefixOf` "log_length" = case val of [CommandArg s _] | Just n <- readMay s, n >= 0 -> do modifyCConf $ \c -> c { clientConfMaxLogLen = n } @@ -894,7 +902,7 @@ handleCommandLine let cl = childLink =<< historyChild item linkLine n link = applyIf (cl == Just (n-1)) (bold "* " <>) $ - printGemLinkLine ansi (showUriRefFull ansi ais $ historyUri item) n link + printGemLinkLine ansi linkDescFirst (showUriRefFull ansi ais $ historyUri item) n link doPage . zipWith linkLine [1..] . extractLinksMimed . historyGeminatedMimedData $ item actionOfCommand ("mark", CommandArg mark _ : _) | @@ -1147,7 +1155,7 @@ handleCommandLine where appendNewline = (`BL.snoc` 10) (Right <$>) . applyFilter . (sanitiseNonCSI <$>) $ case textType of "gemini" -> - let opts = GemRenderOpts ansi' preOpt pageWidth + let opts = GemRenderOpts ansi' preOpt pageWidth linkDescFirst in printGemDoc opts (showUriRefFull ansi' ais uri) $ parseGemini bodyText _ -> T.stripEnd <$> T.lines bodyText mimeType -> -- 2.11.4.GIT