From 2d65555b607e4b692267decc6194aa2cad51521e Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Sun, 15 May 2016 14:35:50 -0400 Subject: [PATCH] Wrap cursor management into one function --- app/Main.hs | 4 ++-- src/Unused/CLI/Search.hs | 3 +-- src/Unused/CLI/Util.hs | 6 ++++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 8b48a48..209ce40 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -7,7 +7,7 @@ import Unused.Parser (parseLines) import Unused.Types (ParseResponse, RemovalLikelihood(..)) import Unused.ResponseFilter (withOneOccurrence, withLikelihoods, ignoringPaths) import Unused.Grouping (CurrentGrouping(..), groupedResponses) -import Unused.CLI (SearchRunner(..), renderHeader, executeSearch, printParseError, printSearchResults, resetScreen, withInterruptHandler) +import Unused.CLI (SearchRunner(..), withoutCursor, renderHeader, executeSearch, printParseError, printSearchResults, resetScreen, withInterruptHandler) data Options = Options { oSearchRunner :: SearchRunner @@ -31,7 +31,7 @@ main = withInterruptHandler $ pFooter = "CLI USAGE: $ cat path/to/ctags | cut -f1 | sort -u | unused" run :: Options -> IO () -run options = do +run options = withoutCursor $ do hSetBuffering stdout NoBuffering terms <- pure . lines =<< getContents diff --git a/src/Unused/CLI/Search.hs b/src/Unused/CLI/Search.hs index 2085ced..68ae8c5 100644 --- a/src/Unused/CLI/Search.hs +++ b/src/Unused/CLI/Search.hs @@ -13,13 +13,12 @@ data SearchRunner = SearchWithProgress | SearchWithoutProgress renderHeader :: [String] -> IO () renderHeader terms = do resetScreen - hideCursor printAnalysisHeader terms executeSearch :: SearchRunner -> [String] -> IO [String] executeSearch runner terms = do renderHeader terms - runSearch runner terms <* resetScreen <* showCursor + runSearch runner terms <* resetScreen printAnalysisHeader :: [String] -> IO () printAnalysisHeader terms = do diff --git a/src/Unused/CLI/Util.hs b/src/Unused/CLI/Util.hs index 37dd550..f209ef3 100644 --- a/src/Unused/CLI/Util.hs +++ b/src/Unused/CLI/Util.hs @@ -1,5 +1,6 @@ module Unused.CLI.Util ( resetScreen + , withoutCursor , withInterruptHandler , module System.Console.ANSI ) where @@ -11,6 +12,11 @@ import System.Posix.Signals (Handler(Catch), installHandler, keyboardSignal) import Control.Concurrent (ThreadId, myThreadId) import System.Exit (ExitCode(ExitFailure)) +withoutCursor :: IO a -> IO a +withoutCursor body = do + hideCursor + body <* showCursor + resetScreen :: IO () resetScreen = do clearScreen