Better manage column formatting

Why?
====

Formatting each column requires context on the column, as well as
information on alignment. This extracts the column formatting logic to a
specific formatter.

ColumnFormatter is coupled to the order of columns/data displayed to the
user.
This commit is contained in:
Joshua Clayton 2016-05-09 09:31:06 -04:00
parent 11d35a6263
commit 4947e54f27
3 changed files with 63 additions and 17 deletions

View File

@ -3,30 +3,29 @@ module Unused.CLI.SearchResult
) where ) where
import Control.Monad (forM_) import Control.Monad (forM_)
import Text.Printf
import qualified Data.Map.Strict as Map import qualified Data.Map.Strict as Map
import Unused.Types import Unused.Types
import Unused.DirectoryGrouping (DirectoryPrefix(..), responsesGroupedByPath) import Unused.DirectoryGrouping (DirectoryPrefix(..), responsesGroupedByPath)
import Unused.CLI.SearchResult.ColumnFormatter
import Unused.CLI.Util import Unused.CLI.Util
printSearchResults :: TermMatchSet -> IO () printSearchResults :: TermMatchSet -> IO ()
printSearchResults termMatchSet = printSearchResults termMatchSet =
mapM_ (printDirectorySection maxWidth) responses mapM_ (printDirectorySection columnFormat) responses
where where
responses = responsesGroupedByPath termMatchSet responses = responsesGroupedByPath termMatchSet
allSets = listFromMatchSet =<< map snd responses allSets = listFromMatchSet =<< map snd responses
allResults = map snd allSets allResults = map snd allSets
termLength = return . length . tmTerm columnFormat = buildColumnFormatter allResults
maxWidth = maximum $ termLength =<< trMatches =<< allResults
listFromMatchSet :: TermMatchSet -> [(String, TermResults)] listFromMatchSet :: TermMatchSet -> [(String, TermResults)]
listFromMatchSet = listFromMatchSet =
Map.toList Map.toList
printDirectorySection :: Int -> (DirectoryPrefix, TermMatchSet) -> IO () printDirectorySection :: ColumnFormat -> (DirectoryPrefix, TermMatchSet) -> IO ()
printDirectorySection w (dir, ss) = do printDirectorySection cf (dir, ss) = do
printDirectory dir printDirectory dir
mapM_ (printTermResults w) $ listFromMatchSet ss mapM_ (printTermResults cf) $ listFromMatchSet ss
putStr "\n" putStr "\n"
printDirectory :: DirectoryPrefix -> IO () printDirectory :: DirectoryPrefix -> IO ()
@ -36,9 +35,9 @@ printDirectory (DirectoryPrefix dir) = do
putStrLn dir putStrLn dir
setSGR [Reset] setSGR [Reset]
printTermResults :: Int -> (String, TermResults) -> IO () printTermResults :: ColumnFormat -> (String, TermResults) -> IO ()
printTermResults w (_, results) = printTermResults cf (_, results) =
printMatches w results $ trMatches results printMatches cf results $ trMatches results
likelihoodColor :: RemovalLikelihood -> Color likelihoodColor :: RemovalLikelihood -> Color
likelihoodColor High = Red likelihoodColor High = Red
@ -46,24 +45,25 @@ likelihoodColor Medium = Yellow
likelihoodColor Low = Green likelihoodColor Low = Green
likelihoodColor Unknown = Black likelihoodColor Unknown = Black
printMatches :: Int -> TermResults -> [TermMatch] -> IO () printMatches :: ColumnFormat -> TermResults -> [TermMatch] -> IO ()
printMatches w r ms = printMatches cf r ms =
forM_ ms $ \m -> do forM_ ms $ \m -> do
setSGR [SetColor Foreground Dull (likelihoodColor $ trRemovalLikelihood r)] setSGR [SetColor Foreground Dull (likelihoodColor $ trRemovalLikelihood r)]
setSGR [SetConsoleIntensity NormalIntensity] setSGR [SetConsoleIntensity NormalIntensity]
putStr $ " " ++ printf termFormat (tmTerm m) putStr $ " " ++ printTerm (tmTerm m)
setSGR [Reset] setSGR [Reset]
setSGR [SetColor Foreground Vivid Cyan] setSGR [SetColor Foreground Vivid Cyan]
setSGR [SetConsoleIntensity NormalIntensity] setSGR [SetConsoleIntensity NormalIntensity]
putStr $ printNumber (trTotalFiles r) ++ "," ++ printNumber (trTotalOccurrences r) ++ " " putStr $ " " ++ printNumber (trTotalFiles r) ++ ", " ++ printNumber (trTotalOccurrences r)
setSGR [Reset] setSGR [Reset]
setSGR [SetColor Foreground Dull Cyan] setSGR [SetColor Foreground Dull Cyan]
setSGR [SetConsoleIntensity FaintIntensity] setSGR [SetConsoleIntensity FaintIntensity]
putStr $ " " ++ tmPath m putStr $ " " ++ printPath (tmPath m)
setSGR [Reset] setSGR [Reset]
putStr "\n" putStr "\n"
where where
termFormat = "%-" ++ show w ++ "s" printTerm = cfPrintTerm cf
printNumber = printf "%2d" printPath = cfPrintPath cf
printNumber = cfPrintNumber cf

View File

@ -0,0 +1,45 @@
module Unused.CLI.SearchResult.ColumnFormatter
( ColumnFormat(..)
, buildColumnFormatter
) where
import Text.Printf
import Unused.Types (TermResults(..), TermMatch(..))
data ColumnFormat = ColumnFormat
{ cfPrintTerm :: String -> String
, cfPrintPath :: String -> String
, cfPrintNumber :: Int -> String
}
buildColumnFormatter :: [TermResults] -> ColumnFormat
buildColumnFormatter r =
ColumnFormat (printf $ termFormat r) (printf $ pathFormat r) (printf $ numberFormat r)
termFormat :: [TermResults] -> String
termFormat rs =
"%-" ++ show termWidth ++ "s"
where
termWidth = maximum $ termLength =<< trMatches =<< rs
termLength = return . length . tmTerm
pathFormat :: [TermResults] -> String
pathFormat rs =
"%-" ++ show pathWidth ++ "s"
where
pathWidth = maximum $ pathLength =<< trMatches =<< rs
pathLength = return . length . tmPath
numberFormat :: [TermResults] -> String
numberFormat rs =
"%" ++ show numberWidth ++ "d"
where
numberWidth = maximum [fileWidth, occurrenceWidth]
fileWidth = maximum $ fileLength =<< rs
occurrenceWidth = maximum $ occurrenceLength =<< rs
fileLength = return . numberLength . trTotalFiles
occurrenceLength = return . numberLength . trTotalOccurrences
numberLength :: Int -> Int
numberLength i =
1 + floor (logBase 10 $ fromIntegral i)

View File

@ -27,6 +27,7 @@ library
, Unused.CLI.Search , Unused.CLI.Search
, Unused.CLI.SearchError , Unused.CLI.SearchError
, Unused.CLI.SearchResult , Unused.CLI.SearchResult
, Unused.CLI.SearchResult.ColumnFormatter
, Unused.CLI.SearchWithProgress , Unused.CLI.SearchWithProgress
, Unused.CLI.SearchWithoutProgress , Unused.CLI.SearchWithoutProgress
, Unused.CLI.Util , Unused.CLI.Util