Skip to content

Commit

Permalink
safer (total) implementation
Browse files Browse the repository at this point in the history
no more undefined, so the code cannot crash
  • Loading branch information
olivierverdier committed Dec 27, 2014
1 parent 8476e67 commit 9bc2aaa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
33 changes: 20 additions & 13 deletions src/Main.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import System.Process (readProcessWithExitCode)
import System.Exit (ExitCode(ExitSuccess))
import Control.Applicative ((<$>))
import Data.Maybe (fromMaybe)
import BranchParse (Branch, BranchInfo, branchInfo, noBranchInfo)
import Control.Applicative ((<$>), (<*>))
import BranchParse (Branch, BranchInfo, branchInfo, AheadBehind)
import StatusParse (Status(MakeStatus), processStatus)

{- Type aliases -}
Expand All @@ -12,15 +12,21 @@ type Numbers = [String]

{- Combining branch and status parsing -}

processBranch :: String -> BranchInfo
processBranch = either (const noBranchInfo) id . branchInfo . drop 3
rightOrNothing :: Either a b -> Maybe b
rightOrNothing = either (const Nothing) Just

processGitStatus :: [String] -> (BranchInfo, Status Int)
processGitStatus [] = undefined
processGitStatus (branchLine:statusLines) = (processBranch branchLine, processStatus statusLines)
processBranch :: String -> Maybe BranchInfo
processBranch = rightOrNothing . branchInfo . drop 3

allInfo :: (BranchInfo, Status Int) -> (Maybe Branch, Numbers)
allInfo (((branch, _), behead), MakeStatus s x c t) = (branch , fmap show [ahead, behind, s, x, c, t])
processGitStatus :: [String] -> Maybe (BranchInfo, Status Int)
processGitStatus [] = Nothing
processGitStatus (branchLine:statusLines) = (,) <$> (processBranch branchLine) <*> (processStatus statusLines)

showStatusNumbers :: Status Int -> Numbers
showStatusNumbers (MakeStatus s x c t) = show <$> [s, x, c, t]

showBranchNumbers :: Maybe AheadBehind -> Numbers
showBranchNumbers behead = show <$> [ahead, behind]
where
(ahead, behind) = fromMaybe (0,0) behead

Expand Down Expand Up @@ -57,15 +63,16 @@ printBranch branch =
printNumbers :: Numbers -> IO ()
printNumbers = mapM_ putStrLn

allInfo :: (BranchInfo, Status Int) -> (Maybe Branch, Numbers)
allInfo (((branch, _), behead), stat) = (branch, showBranchNumbers behead ++ showStatusNumbers stat)

printAll :: Maybe String -> IO ()
printAll status =
case fmap (allInfo . processGitStatus . lines) status of
printAll gitStatus =
case fmap allInfo . processGitStatus . lines =<< gitStatus of
Nothing -> return ()
Just (branch,numbers) -> printBranch branch >> printNumbers numbers
-- maybe (return ()) (\(b,n) -> printBranch b >> printNumbers n) $ fmap (allInfo . processGitStatus . lines) status

-- main
{- main -}

main :: IO ()
main = gitstatus >>= printAll
12 changes: 6 additions & 6 deletions src/StatusParse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ countStatus l = MakeStatus
untracked=countByType isUntracked l
}

extractMiniStatus :: String -> MiniStatus
extractMiniStatus [] = undefined
extractMiniStatus [_] = undefined
extractMiniStatus (index:work:_) = (index,work)
extractMiniStatus :: String -> Maybe MiniStatus
extractMiniStatus [] = Nothing
extractMiniStatus [_] = Nothing
extractMiniStatus (index:work:_) = Just (index,work)

processStatus :: [String] -> Status Int
processStatus = countStatus . fmap extractMiniStatus
processStatus :: [String] -> Maybe (Status Int)
processStatus = fmap countStatus . sequence . fmap extractMiniStatus

0 comments on commit 9bc2aaa

Please sign in to comment.