import System.Console.ANSI import System.Exit import HSH import Control.Monad import Control.Applicative import Data.Char import Data.Maybe -- tell Zsh to not count the contents for the length noLength :: String -> String noLength = ("%{"++) . (++"%}") color :: Color -> String color c = noLength $ setSGRCode [ SetConsoleIntensity BoldIntensity , SetColor Foreground Dull c] -- I didn't found how to generate sgr0 from Sys.Console.ANSI sgr0 :: String sgr0 = "\SI" blue, magenta, green, noColor :: String blue = color Blue magenta = color Magenta green = color Green noColor = noLength $ setSGRCode [] ++ sgr0 main :: IO () main = do ref <- head . lines <$> run "git symbolic-ref HEAD 2> /dev/null" when (null ref) exitFailure gitstat <- fmap lines . run $ "git status 2> /dev/null" -|- egrep "(# Untracked|# Changes|# Changed but not updated:|# Your branch)" let f msg = listToMaybe . map (takeWhile isDigit . dropWhile (not . isDigit)) $ egrep msg gitstat ahead = f "# Your branch is ahead of " behind = f "# Your branch is behind " putStrLn . concat $ ["±" ,blue,":",magenta,drop (length "refs/heads/") ref ,green ,['!' | "# Changes to be committed:" `elem` gitstat] ,['?' | "# Untracked files:" `elem` gitstat || "# Changed but not updated:" `elem` gitstat] ,maybe "" ((blue++":"++green++"-")++) behind ,maybe "" ((blue++":"++green++"+")++) ahead ,noColor]