hxbrief: Implement --yield to keep pattern while dropping

This is similar to piping your output to grep, only
our patterns are much weaker (as of yet), you can see
the intermediate output, and you can also still tee the
whole (unfiltered) output to a file at the same time.
master
Lennart Spitzner 2022-02-25 09:44:23 +00:00
parent 78ee1ecb3a
commit b9f038b212
1 changed files with 40 additions and 3 deletions

View File

@ -86,11 +86,13 @@ data StreamKind = StdOut | StdErr
deriving (Eq, Show) deriving (Eq, Show)
data JoinMode data JoinMode
= JoinAll = JoinYield
| JoinAll
| JoinSpecific | JoinSpecific
data JoinedInfo data JoinedInfo
= JoinedNot = JoinedNot
| JoinedYield
| JoinedAll Int | JoinedAll Int
| Joined Int String [String] -- pattern, prefix | Joined Int String [String] -- pattern, prefix
@ -182,6 +184,13 @@ dispatchLine line@(kind, str) = do
when (c_keepStderr conf /= Drop) $ errorConcurrent (fReset ++ str ++ "\n") when (c_keepStderr conf /= Drop) $ errorConcurrent (fReset ++ str ++ "\n")
modify $ \s -> s { s_history = line : s_history s } modify $ \s -> s { s_history = line : s_history s }
dispatchYielded :: (StreamKind, String) -> StateT State IO ()
dispatchYielded line@(kind, str) = do
liftIO $ case kind of
StdOut -> outputConcurrent (fReset ++ str ++ "\n")
StdErr -> errorConcurrent (fReset ++ str ++ "\n")
modify $ \s -> s { s_history = line : s_history s }
showPattern :: String -> String showPattern :: String -> String
showPattern p = p >>= \case showPattern p = p >>= \case
'*' -> setFGColorVivid Ansi.Yellow ++ "" ++ fReset '*' -> setFGColorVivid Ansi.Yellow ++ "" ++ fReset
@ -256,6 +265,7 @@ summarizeLines cur@(kind, line) = do
( cur ( cur
, case match of , case match of
Nothing -> JoinedNot Nothing -> JoinedNot
Just (JoinYield , _ ) -> JoinedYield
Just (JoinAll , _ ) -> JoinedAll 1 Just (JoinAll , _ ) -> JoinedAll 1
Just (JoinSpecific, pat) -> Joined 1 pat (words line) Just (JoinSpecific, pat) -> Joined 1 pat (words line)
) )
@ -267,10 +277,26 @@ summarizeLines cur@(kind, line) = do
( cur ( cur
, case match of , case match of
Nothing -> JoinedNot Nothing -> JoinedNot
Just (JoinYield , _ ) -> JoinedYield
Just (JoinAll , _ ) -> JoinedAll 1 Just (JoinAll , _ ) -> JoinedAll 1
Just (JoinSpecific, pat) -> Joined 1 pat (words line) Just (JoinSpecific, pat) -> Joined 1 pat (words line)
) )
} }
(Just (oldLine, JoinedYield), Nothing) -> do
dispatchYielded oldLine
put s { s_summary = Just (cur, JoinedNot) }
(Just (oldLine, JoinedYield), _) -> do
dispatchYielded oldLine
put s
{ s_summary = Just
( cur
, case match of
Nothing -> JoinedNot
Just (JoinAll , _ ) -> JoinedAll 1
Just (JoinSpecific, pat) -> Joined 1 pat (words line)
Just (JoinYield , _ ) -> JoinedYield
)
}
(Just ((oldKind, _), JoinedAll i), Nothing) -> do (Just ((oldKind, _), JoinedAll i), Nothing) -> do
dispatchSkipped oldKind i dispatchSkipped oldKind i
put s { s_summary = Just (cur, JoinedNot) } put s { s_summary = Just (cur, JoinedNot) }
@ -278,6 +304,9 @@ summarizeLines cur@(kind, line) = do
dispatchPat oldKind i oldPat oldPrefix dispatchPat oldKind i oldPat oldPrefix
put s { s_summary = Just (cur, JoinedNot) } put s { s_summary = Just (cur, JoinedNot) }
(Just ((oldKind, _), JoinedAll i), Just joiner) -> case joiner of (Just ((oldKind, _), JoinedAll i), Just joiner) -> case joiner of
(JoinYield, _pat) -> do
dispatchSkipped oldKind i
put s { s_summary = Just (cur, JoinedYield) }
(JoinAll, _) (JoinAll, _)
| kind == oldKind -> do | kind == oldKind -> do
put s { s_summary = Just (cur, JoinedAll (i + 1)) } put s { s_summary = Just (cur, JoinedAll (i + 1)) }
@ -302,6 +331,7 @@ summarizeLines cur@(kind, line) = do
{ s_summary = Just { s_summary = Just
( cur ( cur
, case joiner of , case joiner of
(JoinYield , _ ) -> JoinedYield
(JoinAll , _ ) -> JoinedAll 1 (JoinAll , _ ) -> JoinedAll 1
(JoinSpecific, pat) -> Joined 1 pat (words line) (JoinSpecific, pat) -> Joined 1 pat (words line)
) )
@ -354,8 +384,12 @@ processLine newPair@(kind, _) = execStateT $ do
++ " lines)" ++ " lines)"
) )
: prettyLines : prettyLines
Just ((StdOut, line), JoinedYield) ->
(fWhiteDis ++ "" ++ fReset ++ ellipse line) : prettyLines
Just ((StdErr, line), JoinedNot) -> Just ((StdErr, line), JoinedNot) ->
(fRedDis ++ "" ++ fReset ++ ellipse line) : prettyLines (fRedDis ++ "" ++ fReset ++ ellipse line) : prettyLines
Just ((StdErr, line), JoinedYield) ->
(fRedDis ++ "" ++ fReset ++ ellipse line) : prettyLines
Just ((StdErr, line), JoinedAll 1) -> Just ((StdErr, line), JoinedAll 1) ->
(fRedDis ++ "" ++ fReset ++ ellipse line) : prettyLines (fRedDis ++ "" ++ fReset ++ ellipse line) : prettyLines
Just ((StdErr, _line), JoinedAll i) -> Just ((StdErr, _line), JoinedAll i) ->
@ -424,6 +458,7 @@ main = B.mainFromCmdParser $ do
summarize <- B.addFlagStringParams "s" ["summarize"] "PATTERN" mempty summarize <- B.addFlagStringParams "s" ["summarize"] "PATTERN" mempty
skip <- B.addFlagStringParams "x" ["skip"] "PATTERN" mempty skip <- B.addFlagStringParams "x" ["skip"] "PATTERN" mempty
label <- B.addFlagStringParams "" ["label"] "STRING" mempty label <- B.addFlagStringParams "" ["label"] "STRING" mempty
yield <- B.addFlagStringParams "y" ["yield"] "PATTERN" mempty
omitSummary <- B.addSimpleBoolFlag "" ["omit-summary"] mempty omitSummary <- B.addSimpleBoolFlag "" ["omit-summary"] mempty
tee <- B.addFlagStringParams tee <- B.addFlagStringParams
"" ""
@ -525,7 +560,8 @@ main = B.mainFromCmdParser $ do
| conflateStderr || conflateBoth -> Conflate | conflateStderr || conflateBoth -> Conflate
| dropStderr || dropBoth -> Drop | dropStderr || dropBoth -> Drop
| otherwise -> Keep | otherwise -> Keep
, c_summarize = (summarize <&> \x -> (JoinSpecific, x)) , c_summarize = (yield <&> \x -> (JoinYield, x))
++ (summarize <&> \x -> (JoinSpecific, x))
++ (skip <&> \x -> (JoinAll, x)) ++ (skip <&> \x -> (JoinAll, x))
, c_outFile = Nothing , c_outFile = Nothing
, c_errFile = Nothing , c_errFile = Nothing
@ -602,6 +638,7 @@ main = B.mainFromCmdParser $ do
gets s_summary >>= \case gets s_summary >>= \case
Nothing -> pure () Nothing -> pure ()
Just (line , JoinedNot ) -> dispatchLine line Just (line , JoinedNot ) -> dispatchLine line
Just (line , JoinedYield) -> dispatchYielded line
Just ((kind, _), JoinedAll i) -> dispatchSkipped kind i Just ((kind, _), JoinedAll i) -> dispatchSkipped kind i
Just ((kind, _), Joined i pat prefix) -> Just ((kind, _), Joined i pat prefix) ->
dispatchPat kind i pat prefix dispatchPat kind i pat prefix