Skip to content

Instantly share code, notes, and snippets.

@MichaelBlume
Created October 26, 2012 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelBlume/3960711 to your computer and use it in GitHub Desktop.
Save MichaelBlume/3960711 to your computer and use it in GitHub Desktop.
Cute script for drawing attention to errors in scrolling log output
#!/usr/bin/env runhaskell
import Data.List
import Control.Concurrent
import System.IO
takeBlock :: [String] -> (String, [String])
takeBlock (firstLine:moreLines) = (block, restLines) where
block = intercalate "\n" (firstLine:indLines)
(indLines, restLines) = span (isPrefixOf " ") moreLines
data PrintOrSleep = Print String
| Sleep Int
processBlock :: String -> [PrintOrSleep] -> [PrintOrSleep]
processBlock block acts = Print block : restActs where
restActs = if "ERROR" `isInfixOf` block
then Sleep 5000000 : acts
else acts
makeBlocks :: [String] -> [String]
makeBlocks [] = []
makeBlocks lines = block : makeBlocks remLines where
(block, remLines) = takeBlock lines
processStream :: String -> [PrintOrSleep]
processStream = foldr processBlock [] . makeBlocks . lines
actOn :: PrintOrSleep -> IO()
actOn (Sleep usecs) = threadDelay usecs
actOn (Print block) = putStrLn block
main :: IO ()
main = do
hSetBuffering stdout NoBuffering
input <- getContents
mapM_ actOn $ processStream input
@MichaelBlume
Copy link
Author

Basically just do

yourprogram | ./escroll.hs

mostly it'll just echo the output of your program to the screen, but on any line containing "ERROR", it'll pause for five seconds, then scramble to catch up.

Indented lines are assumed to be part of the line above them -- ie for tracebacks and the like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment