mirror of
https://github.com/joshuaclayton/unused.git
synced 2024-10-05 16:47:19 +03:00
Haskell styles
This includes cosmetic changes to improve styles
This commit is contained in:
parent
51f9ae7992
commit
2eeb54fe58
48
app/App.hs
48
app/App.hs
@ -11,6 +11,7 @@ import qualified Data.Bifunctor as B
|
||||
import Control.Monad.Reader
|
||||
import Control.Monad.Except
|
||||
import Data.Maybe (isJust)
|
||||
import Data.Bool (bool)
|
||||
import Unused.Grouping (CurrentGrouping(..), groupedResponses)
|
||||
import Unused.Types (TermMatchSet, RemovalLikelihood(..))
|
||||
import Unused.TermSearch (SearchResults(..), fromResults)
|
||||
@ -48,7 +49,8 @@ data Options = Options
|
||||
|
||||
runProgram :: Options -> IO ()
|
||||
runProgram options = withRuntime $
|
||||
runExceptT (runReaderT (runApp run) options) >>= either renderError return
|
||||
either renderError return
|
||||
=<< runExceptT (runReaderT (runApp run) options)
|
||||
|
||||
run :: App ()
|
||||
run = do
|
||||
@ -72,36 +74,33 @@ renderError (InvalidConfigError e) = V.invalidConfigError e
|
||||
renderError (CacheError e) = V.fingerprintError e
|
||||
|
||||
retrieveGitContext :: TermMatchSet -> App TermMatchSet
|
||||
retrieveGitContext tms = do
|
||||
commitCount <- numberOfCommits
|
||||
case commitCount of
|
||||
Just c -> liftIO $ loadGitContext c tms
|
||||
Nothing -> return tms
|
||||
retrieveGitContext tms =
|
||||
maybe (return tms) (liftIO . flip loadGitContext tms)
|
||||
=<< numberOfCommits
|
||||
|
||||
printResults :: TermMatchSet -> App ()
|
||||
printResults ts = do
|
||||
filters <- optionFilters ts
|
||||
printResults tms = do
|
||||
filters <- optionFilters tms
|
||||
grouping <- groupingOptions
|
||||
formatter <- resultFormatter
|
||||
liftIO $ V.searchResults formatter $ groupedResponses grouping filters
|
||||
|
||||
loadAllConfigs :: App [LanguageConfiguration]
|
||||
loadAllConfigs = do
|
||||
configs <- liftIO (B.first InvalidConfigError <$> loadAllConfigurations)
|
||||
either throwError return configs
|
||||
loadAllConfigs =
|
||||
either throwError return
|
||||
=<< B.first InvalidConfigError <$> liftIO loadAllConfigurations
|
||||
|
||||
calculateTagInput :: App [String]
|
||||
calculateTagInput = do
|
||||
tags <- liftIO . fmap (B.first TagError) . loadTags =<< readFromStdIn
|
||||
either throwError return tags
|
||||
where
|
||||
loadTags b = if b then loadTagsFromPipe else loadTagsFromFile
|
||||
calculateTagInput =
|
||||
either throwError return
|
||||
=<< liftIO .
|
||||
fmap (B.first TagError) .
|
||||
bool loadTagsFromFile loadTagsFromPipe =<< readFromStdIn
|
||||
|
||||
withCache :: IO SearchResults -> App SearchResults
|
||||
withCache f =
|
||||
operateCache =<< runWithCache
|
||||
bool (liftIO f) (withCache' f) =<< runWithCache
|
||||
where
|
||||
operateCache b = if b then withCache' f else liftIO f
|
||||
withCache' :: IO SearchResults -> App SearchResults
|
||||
withCache' r =
|
||||
either (throwError . CacheError) (return . SearchResults) =<<
|
||||
@ -118,11 +117,8 @@ optionFilters tms = foldl (>>=) (pure tms) matchSetFilters
|
||||
]
|
||||
|
||||
singleOccurrenceFilter :: AppConfig m => TermMatchSet -> m TermMatchSet
|
||||
singleOccurrenceFilter tms = do
|
||||
allowsSingleOccurrence <- asks oSingleOccurrenceMatches
|
||||
return $ if allowsSingleOccurrence
|
||||
then withOneOccurrence tms
|
||||
else tms
|
||||
singleOccurrenceFilter tms =
|
||||
bool tms (withOneOccurrence tms) <$> asks oSingleOccurrenceMatches
|
||||
|
||||
likelihoodsFilter :: AppConfig m => TermMatchSet -> m TermMatchSet
|
||||
likelihoodsFilter tms =
|
||||
@ -152,8 +148,4 @@ numberOfCommits :: AppConfig m => m (Maybe Int)
|
||||
numberOfCommits = asks oCommitCount
|
||||
|
||||
resultFormatter :: AppConfig m => m V.ResultsFormat
|
||||
resultFormatter = do
|
||||
c <- numberOfCommits
|
||||
return $ if isJust c
|
||||
then V.List
|
||||
else V.Column
|
||||
resultFormatter = bool V.Column V.List . isJust <$> numberOfCommits
|
||||
|
Loading…
Reference in New Issue
Block a user