Last active
November 20, 2016 01:05
-
-
Save sleexyz/72598fa7790dea4e93bc42b1d251d016 to your computer and use it in GitHub Desktop.
visualize bytebeat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE BangPatterns #-} | |
-- | Visualize bytebeat. currently doesn't print fast enough... | |
module Main where | |
import Control.Monad | |
import Data.Bits | |
import qualified Data.Char as C | |
import Data.List | |
import System.IO | |
import System.Environment | |
import qualified Data.ByteString as BS | |
visualize :: Int -> String | |
visualize !byte = (mconcat $ replicate byte " ") ++ "#" | |
visualizeBits :: Int -> Int -> String | |
visualizeBits len byte = foldr f "" [1..len] where | |
f n str = c : str where | |
c = if testBit byte n then '█' else ' ' | |
foo :: Handle -> String -> IO () | |
foo h str = do | |
putStr $ mconcat $ (visualizeBits 8 . C.ord) <$> str | |
hPutStr h str | |
main :: IO () | |
main = do | |
(fifopath:_) <- getArgs | |
h <- openFile fifopath WriteMode | |
forever $ (replicateM 256 getChar) >>= foo h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ls bytebeat.hs vis.hs | entr -cr bash -c "runghc bytebeat.hs | tee >(aplay > /dev/null) | runghc vis.hs"
ls bytebeat.hs vis.hs | entr -cr bash -c "runghc bytebeat.hs | runghc vis.hs >(aplay &> /dev/null)"