on master help sign in
Merge pull request #112 from glguy/term-env-exception

Raise a VTY specific exception on missing TERM

commitf8f7577d8c7b23867b9d1bf1b4afde61c4a1507f authorJonathan Daugherty <cygnus@foobox.com> dateSat 3 Sep 2016, 19:06 (10 years ago) parent5208cc07f899b5
1 file changed+29−10
src/Graphics/Vty/Config.hs+29−10
src/Graphics/Vty/Config.hs+29−10
@@ -1,4 +1,5 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DeriveDataTypeable #-}
23{-# LANGUAGE FlexibleContexts, FlexibleInstances #-}
34{-# LANGUAGE TypeOperators #-}
45{-# LANGUAGE ScopedTypeVariables #-}
@@ -67,6 +68,7 @@
6768module Graphics.Vty.Config
6869 ( InputMap
6970 , Config(..)
71+ , VtyConfigurationError(..)
7072 , userConfig
7173 , overrideEnvConfig
7274 , standardIOConfig
@@ -82,12 +84,13 @@ import Prelude hiding (catch)
8284
8385import Control.Applicative hiding (many)
8486
85−import Control.Exception (catch, IOException)
87+import Control.Exception (catch, IOException, Exception(..), throwIO)
8688import Control.Monad (liftM, guard, void)
8789
8890import qualified Data.ByteString as BS
8991import Data.Default
9092import Data.Monoid
93+import Data.Typeable (Typeable)
9194
9295import Graphics.Vty.Input.Events
9396
@@ -102,6 +105,16 @@ import Text.Parsec hiding ((<|>))
102105import Text.Parsec.Token ( GenLanguageDef(..) )
103106import qualified Text.Parsec.Token as P
104107
108+-- | Type of errors that can be thrown when configuring VTY
109+data VtyConfigurationError
110+ = VtyMissingTermEnvVar -- ^ TERM environment variable not set
111+ deriving (Show, Eq, Typeable)
112+
113+instance Exception VtyConfigurationError where
114+#if MIN_VERSION_base(4,8,0)
115+ displayException VtyMissingTermEnvVar = "TERM environment variable not set"
116+#endif
117+
105118-- | Mappings from input bytes to event in the order specified. Later entries take precedence over
106119-- earlier in the case multiple entries have the same byte string.
107120type InputMap = [(Maybe String, String, Event)]
@@ -173,17 +186,23 @@ overrideEnvConfig = do
173186 d <- getEnv "VTY_DEBUG_LOG"
174187 return $ def { debugLog = d }
175188
189+-- | Configures VTY using defaults suitable for terminals. This action
190+-- can raise 'VtyConfigurationError'.
176191standardIOConfig :: IO Config
177192standardIOConfig = do
178− Just t <- getEnv "TERM"
179− return $ def { vmin = Just 1
180− , mouseMode = Just False
181− , bracketedPasteMode = Just False
182− , vtime = Just 100
183− , inputFd = Just stdInput
184− , outputFd = Just stdOutput
185− , termName = Just t
186− }
193+ mb <- getEnv "TERM"
194+ case mb of
195+ Nothing -> throwIO VtyMissingTermEnvVar
196+ Just t ->
197+ return def
198+ { vmin = Just 1
199+ , mouseMode = Just False
200+ , bracketedPasteMode = Just False
201+ , vtime = Just 100
202+ , inputFd = Just stdInput
203+ , outputFd = Just stdOutput
204+ , termName = Just t
205+ }
187206
188207parseConfigFile :: FilePath -> IO Config
189208parseConfigFile path = do