packages feed

nptools-0.1: git-prompt.hs

import System.Console.ANSI
import System.Exit
import HSH
import Control.Applicative
import Control.Monad
import Data.Char
import Data.Maybe

color :: Color -> String
color c = setSGRCode [SetColor Foreground Dull c]

blue, magenta, green, noColor :: String
blue      = color Blue
magenta   = color Magenta
green     = color Green
noColor  = setSGRCode []

main :: IO ()
main = do 
  ref <- runSL "git symbolic-ref HEAD 2> /dev/null"
  when (null ref) exitFailure
  gitstat <-  fmap lines . runSL $ "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]