on master help sign in
Disable spinner on non-terminal and bash-completion
commited7215f9274865cd9709cadf0bec68b4962cfcb2 authorSridhar Ratnakumar <srid@srid.ca> dateThu 10 May 2018, 11:31 (8 years ago) parent2835ba0
1 file changed+18−9
lib/command/src/Obelisk/Command/CLI.hs+18−9
lib/command/src/Obelisk/Command/CLI.hs+18−9
@@ -1,11 +1,15 @@
11{-# LANGUAGE LambdaCase #-}
2+{-# LANGUAGE MultiWayIf #-}
23module Obelisk.Command.CLI where
34
4−import Control.Exception (SomeException, displayException, finally, handle)
5+import Control.Exception (finally)
6+import Data.List (isInfixOf)
57import Data.Text (Text)
68import qualified Data.Text as T
79import qualified Data.Text.IO as T
8−import System.Exit (ExitCode (ExitFailure), exitWith)
10+import GHC.IO.Handle.FD (stdout)
11+import System.Environment (getArgs)
12+import System.IO (hIsTerminalDevice)
913
1014import System.Console.ANSI
1115import System.Console.Questioner (dots1Spinner, stopIndicator)
@@ -20,13 +24,18 @@ withSpinner
2024 -> IO a -- ^ Action to run and wait for
2125 -> IO a
2226withSpinner s e f = do
23− spinner <- dots1Spinner (1000 * 200) s
24− result <- finally f $ do
25− stopIndicator spinner
26− case e of
27− Just exitMsg -> putInfo $ T.pack exitMsg
28− _ -> return ()
29− return result
27+ isTerm <- hIsTerminalDevice stdout
28+ -- When running in bash completion, disable the spinner. TODO: Do this using ReaderT and config.
29+ inBashCompletion <- isInfixOf "bash-completion" . unwords <$> getArgs
30+ if | not isTerm || inBashCompletion -> f
31+ | otherwise -> do
32+ spinner <- dots1Spinner (1000 * 200) s
33+ result <- finally f $ do
34+ stopIndicator spinner
35+ case e of
36+ Just exitMsg -> putInfo $ T.pack exitMsg
37+ _ -> return ()
38+ return result
3039
3140data Level = Level_Normal | Level_Warning | Level_Error
3241