Skip to content

Instantly share code, notes, and snippets.

@jkramer
Created October 27, 2009 14:21
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 jkramer/219594 to your computer and use it in GitHub Desktop.
Save jkramer/219594 to your computer and use it in GitHub Desktop.
-- ZChart
-- Find the 10 most popular commands from your ZSH history and build a Google
-- Chart from it. Prints the charts URL to stdout and saves it as PNG in
-- "history.png".
import System.IO
import System.Environment
import Data.List
import Data.Char
import Control.Arrow
import Graphics.Google.Chart
import Data.ByteString.Char8 (unpack)
import Network.Download
main = getEnv "HISTFILE" >>= readFile >>= googleChart . topTen . parse
parse = map (takeWhile (not . isSpace) . dropWhile (not . isAlpha)) . lines
topTen full = take 10 $ sortByUsage $ uniqC full
where
uniqC = map (id &&& count) . nub
count = length . flip elemIndices full
sortByUsage = sortBy (flip (compare . snd) . snd)
googleChart values =
putStrLn uri >> openURI uri >>= writeFile "history.png" . check
where
check = either error unpack
uri = chartURL
$ setSize 600 400
$ setTitle "ZSH History Top 10"
$ setData (encodeDataExtended [map snd values])
$ setLabels (map fst values)
$ newPieChart Pie2D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment