Skip to content

Instantly share code, notes, and snippets.

@ncaq
Created December 1, 2016 05:04
Show Gist options
  • Save ncaq/4782362c1d735b8e88a23b88b536ae71 to your computer and use it in GitHub Desktop.
Save ncaq/4782362c1d735b8e88a23b88b536ae71 to your computer and use it in GitHub Desktop.
2015-08に記述,コマンドの使用頻度を計測する
import Control.Applicative ()
import Control.Monad
import Data.Char
import Data.List
import Data.List.Split
import qualified Data.Map as M
import Data.Monoid
import Data.Ord ()
import qualified Data.Set as S
import System.Directory
import System.Process
main = mapM_ (\(c, i) -> putStrLn $ (show i) <> ": " <> (show c)) .
sortBy (\(_, a) (_, b) -> b `compare` a) . M.toList =<<
historyCommands
historyCommands = do
h <- groupWithSize . wordsBy (not . isCommandChar) <$> zshHistory
zshWords <- S.fromList . concat <$> sequence [zshCommands, zshFunctions, zshAliases]
return $ M.filterWithKey (\k _ -> S.member k zshWords) h
zshCommands = return . map (last . wordsBy ('/' ==)) =<<
filterM (\fp -> not <$> doesDirectoryExist fp) =<<
return . wordsBy (' ' ==) =<<
readProcess "zsh" ["-c", "source ~/.zshrc && echo $commands"] []
zshFunctions = lines <$> readProcess "zsh" ["-c", "source ~/.zshrc && print -l ${(ok)functions}"] []
zshAliases = map (takeWhile ('=' /=)) . lines <$>
readProcess "zsh" ["-c", "source ~/.zshrc && alias"] []
zshHistory = readProcess "zsh" ["-c", "HISTFILE=~/.zsh_history && HISTSIZE=1000000 && fc -R && history -n 1"] []
isCommandChar c = any (\f -> f c) [isAlphaNum, (\c -> elem c "_-/.")]
groupWithSize :: (Ord a, Num n) => [a] -> M.Map a n
groupWithSize = foldr count M.empty
where count k m = case M.lookup k m of
Nothing -> M.insert k 1 m
Just _ -> M.update (Just . (1 +)) k m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment