module Hish.SysInfo
( status
, branch
, pwd
, uid
)where
import System.Process
import System.Exit
import Data.List (lines,unlines)
import Data.Char (isSpace)
import qualified Data.String.Utils as S (split,replace,join)
status :: IO String
status = do
(code,out,_) <- readProcessWithExitCode "git" ["status","--porcelain"] ""
case code of
ExitFailure _ -> return ""
ExitSuccess -> return $
((\b->if b then "#" else "*")
.null
.map head.lines
) out
branch :: IO String
branch = do
(code,out,_) <- readProcessWithExitCode "git" ["branch"] ""
case code of
ExitFailure _ -> return ""
ExitSuccess -> return $
(drop 1
.head.takeWhile (('*'==).head)
.lines.filter (/=' ')
) out
pwd :: Int
-> IO String
pwd width = do
(code,out,_) <- readProcessWithExitCode "pwd" [] ""
name <- uid
case code of
ExitFailure _ -> return ""
ExitSuccess -> return $
( (\str -> if (head str) == '~' then str else '/':str )
. (\str -> if (length str) > width
then pwdShorten $ S.split "/" str
else str)
. S.replace ("/Users/"++name) "~"
. filter (/='\n')
) $ out
pwdShorten :: [String] -> String
pwdShorten [] = ""
pwdShorten [l] = l
pwdShorten (x:xs) = (head x) : '/' : pwdShorten xs
uid :: IO String
uid = do
(code,name,_) <- readProcessWithExitCode "whoami" [] ""
case code of
ExitFailure _ -> return ""
ExitSuccess -> return $ filter (/='\n') $ name