Extract views to a Views module

Why?
====

View logic was scattered all over the place; this introduces a views
module to encapsulate any corresponding view work into one spot.
This commit is contained in:
Joshua Clayton 2016-05-27 05:52:56 -04:00
parent 1b945eba18
commit 4dfd788318
10 changed files with 68 additions and 47 deletions

View File

@ -9,7 +9,8 @@ import Unused.TermSearch (SearchResults(..), fromResults)
import Unused.ResultsClassifier
import Unused.ResponseFilter (withOneOccurrence, withLikelihoods, ignoringPaths)
import Unused.Grouping (CurrentGrouping(..), groupedResponses)
import Unused.CLI (SearchRunner(..), withoutCursor, renderHeader, executeSearch, printMissingTagsFileError, printSearchResults, resetScreen, withInterruptHandler)
import Unused.CLI (SearchRunner(..), withoutCursor, renderHeader, executeSearch, resetScreen, withInterruptHandler)
import qualified Unused.CLI.Views as V
import Unused.Cache
import Unused.TagsSource
@ -42,7 +43,7 @@ run options = withoutCursor $ do
terms' <- calculateTagInput options
case terms' of
(Left e) -> printMissingTagsFileError e
(Left e) -> V.missingTagsFileError e
(Right terms) -> do
renderHeader terms
@ -59,7 +60,7 @@ run options = withoutCursor $ do
return ()
printResults :: Options -> TermMatchSet -> IO ()
printResults options = printSearchResults . groupedResponses (oGrouping options) . optionFilters options
printResults options = V.searchResults . groupedResponses (oGrouping options) . optionFilters options
loadLanguageConfig :: IO [LanguageConfiguration]
loadLanguageConfig = either (const []) id <$> loadConfig

View File

@ -3,6 +3,4 @@ module Unused.CLI
) where
import Unused.CLI.Search as X
import Unused.CLI.MissingTagsFileError as X
import Unused.CLI.SearchResult as X
import Unused.CLI.Util as X

View File

@ -6,6 +6,7 @@ module Unused.CLI.Search
import Unused.TermSearch (SearchResults, search)
import Unused.CLI.Util
import qualified Unused.CLI.Views as V
import Unused.CLI.ProgressIndicator
data SearchRunner = SearchWithProgress | SearchWithoutProgress
@ -13,26 +14,13 @@ data SearchRunner = SearchWithProgress | SearchWithoutProgress
renderHeader :: [String] -> IO ()
renderHeader terms = do
resetScreen
printAnalysisHeader terms
V.analysisHeader terms
executeSearch :: SearchRunner -> [String] -> IO SearchResults
executeSearch runner terms = do
renderHeader terms
runSearch runner terms <* resetScreen
printAnalysisHeader :: [String] -> IO ()
printAnalysisHeader terms = do
setSGR [SetConsoleIntensity BoldIntensity]
putStr "Unused: "
setSGR [Reset]
putStr "analyzing "
setSGR [SetColor Foreground Dull Green]
putStr $ show $ length terms
setSGR [Reset]
putStr " terms"
runSearch :: SearchRunner -> [String] -> IO SearchResults
runSearch SearchWithProgress = progressWithIndicator search createProgressBar
runSearch SearchWithoutProgress = progressWithIndicator search createSpinner

8
src/Unused/CLI/Views.hs Normal file
View File

@ -0,0 +1,8 @@
module Unused.CLI.Views
( module X
) where
import Unused.CLI.Views.NoResultsFound as X
import Unused.CLI.Views.AnalysisHeader as X
import Unused.CLI.Views.MissingTagsFileError as X
import Unused.CLI.Views.SearchResult as X

View File

@ -0,0 +1,18 @@
module Unused.CLI.Views.AnalysisHeader
( analysisHeader
) where
import Unused.CLI.Util
analysisHeader :: [String] -> IO ()
analysisHeader terms = do
setSGR [SetConsoleIntensity BoldIntensity]
putStr "Unused: "
setSGR [Reset]
putStr "analyzing "
setSGR [SetColor Foreground Dull Green]
putStr $ show $ length terms
setSGR [Reset]
putStr " terms"

View File

@ -1,12 +1,12 @@
module Unused.CLI.MissingTagsFileError
( printMissingTagsFileError
module Unused.CLI.Views.MissingTagsFileError
( missingTagsFileError
) where
import Unused.TagsSource
import Unused.CLI.Util
printMissingTagsFileError :: TagSearchOutcome -> IO ()
printMissingTagsFileError e = do
missingTagsFileError :: TagSearchOutcome -> IO ()
missingTagsFileError e = do
setSGR [SetColor Background Vivid Red]
setSGR [SetColor Foreground Vivid White]
setSGR [SetConsoleIntensity BoldIntensity]
@ -45,4 +45,3 @@ printOutcomeMessage :: TagSearchOutcome -> IO ()
printOutcomeMessage (TagsFileNotFound directoriesSearched) = do
putStrLn "Looked for a 'tags' file in the following directories:\n"
mapM_ (\d -> putStrLn $ "* " ++ d) directoriesSearched

View File

@ -0,0 +1,12 @@
module Unused.CLI.Views.NoResultsFound
( noResultsFound
) where
import Unused.CLI.Util
noResultsFound :: IO ()
noResultsFound = do
setSGR [SetColor Foreground Dull Green]
setSGR [SetConsoleIntensity BoldIntensity]
putStrLn "Unused found no results"
setSGR [Reset]

View File

@ -1,34 +1,35 @@
module Unused.CLI.SearchResult
( printSearchResults
module Unused.CLI.Views.SearchResult
( searchResults
) where
import Control.Monad (forM_)
import qualified Data.Map.Strict as Map
import Unused.Types
import Unused.Grouping (Grouping(..), GroupedTerms)
import Unused.CLI.SearchResult.ColumnFormatter
import Unused.CLI.Views.SearchResult.ColumnFormatter
import Unused.CLI.Util
import qualified Unused.CLI.Views.NoResultsFound as V
printSearchResults :: [GroupedTerms] -> IO ()
printSearchResults responses =
printFormattedResponses columnFormat responses
searchResults :: [GroupedTerms] -> IO ()
searchResults terms =
printFormattedTerms columnFormat terms
where
allSets = listFromMatchSet =<< map snd responses
allSets = listFromMatchSet =<< map snd terms
allResults = map snd allSets
columnFormat = buildColumnFormatter allResults
printFormattedResponses :: ColumnFormat -> [GroupedTerms] -> IO ()
printFormattedResponses _ [] = printNoResultsFound
printFormattedResponses cf r = mapM_ (printGroupingSection cf) r
printFormattedTerms :: ColumnFormat -> [GroupedTerms] -> IO ()
printFormattedTerms _ [] = V.noResultsFound
printFormattedTerms cf ts = mapM_ (printGroupingSection cf) ts
listFromMatchSet :: TermMatchSet -> [(String, TermResults)]
listFromMatchSet =
Map.toList
printGroupingSection :: ColumnFormat -> GroupedTerms -> IO ()
printGroupingSection cf (g, ss) = do
printGroupingSection cf (g, tms) = do
printGrouping g
mapM_ (printTermResults cf) $ listFromMatchSet ss
mapM_ (printTermResults cf) $ listFromMatchSet tms
printGrouping :: Grouping -> IO ()
printGrouping NoGrouping = return ()
@ -76,10 +77,3 @@ printMatches cf r ms =
printNumber = cfPrintNumber cf
termColor = likelihoodColor . rLikelihood . trRemoval
removalReason = rReason . trRemoval
printNoResultsFound :: IO ()
printNoResultsFound = do
setSGR [SetColor Foreground Dull Green]
setSGR [SetConsoleIntensity BoldIntensity]
putStrLn "Unused found no results"
setSGR [Reset]

View File

@ -1,4 +1,4 @@
module Unused.CLI.SearchResult.ColumnFormatter
module Unused.CLI.Views.SearchResult.ColumnFormatter
( ColumnFormat(..)
, buildColumnFormatter
) where

View File

@ -37,10 +37,13 @@ library
, Unused.TagsSource
, Unused.CLI
, Unused.CLI.Search
, Unused.CLI.MissingTagsFileError
, Unused.CLI.SearchResult
, Unused.CLI.SearchResult.ColumnFormatter
, Unused.CLI.Util
, Unused.CLI.Views
, Unused.CLI.Views.NoResultsFound
, Unused.CLI.Views.AnalysisHeader
, Unused.CLI.Views.MissingTagsFileError
, Unused.CLI.Views.SearchResult
, Unused.CLI.Views.SearchResult.ColumnFormatter
, Unused.CLI.ProgressIndicator
, Unused.CLI.ProgressIndicator.Internal
, Unused.CLI.ProgressIndicator.Types