{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
module Graphics.Vty.Config
( InputMap
, VtyUserConfig(..)
, userConfig
, overrideEnvConfig
, currentTerminalName
, runParseConfig
, parseConfigFile
, defaultConfig
, vtyConfigPath
, widthTableFilename
, vtyDataDirectory
, terminalWidthTablePath
, vtyConfigFileEnvName
, ConfigUpdateResult(..)
, addConfigWidthMap
)
where
import Prelude
import Control.Applicative hiding (many)
import Control.Exception (catch, IOException)
import Control.Monad (liftM, guard, void)
import qualified Data.ByteString as BS
#if !(MIN_VERSION_base(4,8,0))
import Data.Monoid (Monoid(..))
#endif
#if !(MIN_VERSION_base(4,11,0))
import Data.Semigroup (Semigroup(..))
#endif
import Text.Read (readMaybe)
import Graphics.Vty.Attributes.Color (ColorMode(..))
import Graphics.Vty.Input.Events
import GHC.Generics
import System.Directory ( getAppUserDataDirectory, doesFileExist
, createDirectoryIfMissing
)
import System.Environment (lookupEnv)
import System.FilePath ((</>), takeDirectory)
import Text.Parsec hiding ((<|>))
import Text.Parsec.Token ( GenLanguageDef(..) )
import qualified Text.Parsec.Token as P
type InputMap = [(Maybe String, String, Event)]
data VtyUserConfig =
VtyUserConfig { VtyUserConfig -> Maybe [Char]
configDebugLog :: Maybe FilePath
, VtyUserConfig -> InputMap
configInputMap :: InputMap
, VtyUserConfig -> [([Char], [Char])]
configTermWidthMaps :: [(String, FilePath)]
, VtyUserConfig -> Maybe Bool
configAllowCustomUnicodeWidthTables :: Maybe Bool
, VtyUserConfig -> Maybe ColorMode
configPreferredColorMode :: Maybe ColorMode
}
deriving (Int -> VtyUserConfig -> ShowS
[VtyUserConfig] -> ShowS
VtyUserConfig -> [Char]
(Int -> VtyUserConfig -> ShowS)
-> (VtyUserConfig -> [Char])
-> ([VtyUserConfig] -> ShowS)
-> Show VtyUserConfig
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VtyUserConfig -> ShowS
showsPrec :: Int -> VtyUserConfig -> ShowS
$cshow :: VtyUserConfig -> [Char]
show :: VtyUserConfig -> [Char]
$cshowList :: [VtyUserConfig] -> ShowS
showList :: [VtyUserConfig] -> ShowS
Show, VtyUserConfig -> VtyUserConfig -> Bool
(VtyUserConfig -> VtyUserConfig -> Bool)
-> (VtyUserConfig -> VtyUserConfig -> Bool) -> Eq VtyUserConfig
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VtyUserConfig -> VtyUserConfig -> Bool
== :: VtyUserConfig -> VtyUserConfig -> Bool
$c/= :: VtyUserConfig -> VtyUserConfig -> Bool
/= :: VtyUserConfig -> VtyUserConfig -> Bool
Eq)
defaultConfig :: VtyUserConfig
defaultConfig :: VtyUserConfig
defaultConfig = VtyUserConfig
forall a. Monoid a => a
mempty
instance Semigroup VtyUserConfig where
VtyUserConfig
c0 <> :: VtyUserConfig -> VtyUserConfig -> VtyUserConfig
<> VtyUserConfig
c1 =
VtyUserConfig { configDebugLog :: Maybe [Char]
configDebugLog =
VtyUserConfig -> Maybe [Char]
configDebugLog VtyUserConfig
c1 Maybe [Char] -> Maybe [Char] -> Maybe [Char]
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> VtyUserConfig -> Maybe [Char]
configDebugLog VtyUserConfig
c0
, configInputMap :: InputMap
configInputMap =
VtyUserConfig -> InputMap
configInputMap VtyUserConfig
c0 InputMap -> InputMap -> InputMap
forall a. Semigroup a => a -> a -> a
<> VtyUserConfig -> InputMap
configInputMap VtyUserConfig
c1
, configTermWidthMaps :: [([Char], [Char])]
configTermWidthMaps =
VtyUserConfig -> [([Char], [Char])]
configTermWidthMaps VtyUserConfig
c1 [([Char], [Char])] -> [([Char], [Char])] -> [([Char], [Char])]
forall a. [a] -> [a] -> [a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> VtyUserConfig -> [([Char], [Char])]
configTermWidthMaps VtyUserConfig
c0
, configAllowCustomUnicodeWidthTables :: Maybe Bool
configAllowCustomUnicodeWidthTables =
VtyUserConfig -> Maybe Bool
configAllowCustomUnicodeWidthTables VtyUserConfig
c1 Maybe Bool -> Maybe Bool -> Maybe Bool
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> VtyUserConfig -> Maybe Bool
configAllowCustomUnicodeWidthTables VtyUserConfig
c0
, configPreferredColorMode :: Maybe ColorMode
configPreferredColorMode =
VtyUserConfig -> Maybe ColorMode
configPreferredColorMode VtyUserConfig
c1 Maybe ColorMode -> Maybe ColorMode -> Maybe ColorMode
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> VtyUserConfig -> Maybe ColorMode
configPreferredColorMode VtyUserConfig
c0
}
instance Monoid VtyUserConfig where
mempty :: VtyUserConfig
mempty =
VtyUserConfig { configDebugLog :: Maybe [Char]
configDebugLog = Maybe [Char]
forall a. Monoid a => a
mempty
, configInputMap :: InputMap
configInputMap = InputMap
forall a. Monoid a => a
mempty
, configTermWidthMaps :: [([Char], [Char])]
configTermWidthMaps = []
, configAllowCustomUnicodeWidthTables :: Maybe Bool
configAllowCustomUnicodeWidthTables = Maybe Bool
forall a. Maybe a
Nothing
, configPreferredColorMode :: Maybe ColorMode
configPreferredColorMode = Maybe ColorMode
forall a. Maybe a
Nothing
}
#if !(MIN_VERSION_base(4,11,0))
mappend = (<>)
#endif
vtyDataDirectory :: IO FilePath
vtyDataDirectory :: IO [Char]
vtyDataDirectory = [Char] -> IO [Char]
getAppUserDataDirectory [Char]
"vty"
vtyConfigPath :: IO FilePath
vtyConfigPath :: IO [Char]
vtyConfigPath = do
dir <- IO [Char]
vtyDataDirectory
return $ dir </> "config"
vtyConfigFileEnvName :: String
vtyConfigFileEnvName :: [Char]
vtyConfigFileEnvName = [Char]
"VTY_CONFIG_FILE"
userConfig :: IO VtyUserConfig
userConfig :: IO VtyUserConfig
userConfig = do
configFile <- IO [Char]
vtyConfigPath IO [Char] -> ([Char] -> IO VtyUserConfig) -> IO VtyUserConfig
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [Char] -> IO VtyUserConfig
parseConfigFile
overrideConfig <- maybe (return defaultConfig) parseConfigFile =<<
lookupEnv vtyConfigFileEnvName
let base = VtyUserConfig
configFile VtyUserConfig -> VtyUserConfig -> VtyUserConfig
forall a. Semigroup a => a -> a -> a
<> VtyUserConfig
overrideConfig
mappend base <$> overrideEnvConfig
widthTableFilename :: String -> String
widthTableFilename :: ShowS
widthTableFilename [Char]
term = [Char]
"width_table_" [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char]
term [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char]
".dat"
termVariable :: String
termVariable :: [Char]
termVariable = [Char]
"TERM"
currentTerminalName :: IO (Maybe String)
currentTerminalName :: IO (Maybe [Char])
currentTerminalName = [Char] -> IO (Maybe [Char])
lookupEnv [Char]
termVariable
terminalWidthTablePath :: IO (Maybe FilePath)
terminalWidthTablePath :: IO (Maybe [Char])
terminalWidthTablePath = do
dataDir <- IO [Char]
vtyDataDirectory
result <- lookupEnv termVariable
case result of
Maybe [Char]
Nothing -> Maybe [Char] -> IO (Maybe [Char])
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe [Char]
forall a. Maybe a
Nothing
Just [Char]
term -> do
Maybe [Char] -> IO (Maybe [Char])
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe [Char] -> IO (Maybe [Char]))
-> Maybe [Char] -> IO (Maybe [Char])
forall a b. (a -> b) -> a -> b
$ [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just ([Char] -> Maybe [Char]) -> [Char] -> Maybe [Char]
forall a b. (a -> b) -> a -> b
$ [Char]
dataDir [Char] -> ShowS
</> ShowS
widthTableFilename [Char]
term
overrideEnvConfig :: IO VtyUserConfig
overrideEnvConfig :: IO VtyUserConfig
overrideEnvConfig = do
d <- [Char] -> IO (Maybe [Char])
lookupEnv [Char]
"VTY_DEBUG_LOG"
return $ defaultConfig { configDebugLog = d }
parseConfigFile :: FilePath -> IO VtyUserConfig
parseConfigFile :: [Char] -> IO VtyUserConfig
parseConfigFile [Char]
path = do
IO VtyUserConfig
-> (IOException -> IO VtyUserConfig) -> IO VtyUserConfig
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catch ([Char] -> ByteString -> VtyUserConfig
runParseConfig [Char]
path (ByteString -> VtyUserConfig) -> IO ByteString -> IO VtyUserConfig
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> IO ByteString
BS.readFile [Char]
path)
(\(IOException
_ :: IOException) -> VtyUserConfig -> IO VtyUserConfig
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return VtyUserConfig
defaultConfig)
runParseConfig :: String -> BS.ByteString -> VtyUserConfig
runParseConfig :: [Char] -> ByteString -> VtyUserConfig
runParseConfig [Char]
name ByteString
cfgTxt =
case Parsec ByteString () VtyUserConfig
-> () -> [Char] -> ByteString -> Either ParseError VtyUserConfig
forall s t u a.
Stream s Identity t =>
Parsec s u a -> u -> [Char] -> s -> Either ParseError a
runParser Parsec ByteString () VtyUserConfig
parseConfig () [Char]
name ByteString
cfgTxt of
Right VtyUserConfig
cfg -> VtyUserConfig
cfg
Left{} -> VtyUserConfig
defaultConfig
type Parser = Parsec BS.ByteString ()
configLanguage :: Monad m => P.GenLanguageDef BS.ByteString () m
configLanguage :: forall (m :: * -> *). Monad m => GenLanguageDef ByteString () m
configLanguage = LanguageDef
{ commentStart :: [Char]
commentStart = [Char]
"{-"
, commentEnd :: [Char]
commentEnd = [Char]
"-}"
, commentLine :: [Char]
commentLine = [Char]
"--"
, nestedComments :: Bool
nestedComments = Bool
True
, identStart :: ParsecT ByteString () m Char
identStart = ParsecT ByteString () m Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
letter ParsecT ByteString () m Char
-> ParsecT ByteString () m Char -> ParsecT ByteString () m Char
forall a.
ParsecT ByteString () m a
-> ParsecT ByteString () m a -> ParsecT ByteString () m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Char -> ParsecT ByteString () m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'_'
, identLetter :: ParsecT ByteString () m Char
identLetter = ParsecT ByteString () m Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
alphaNum ParsecT ByteString () m Char
-> ParsecT ByteString () m Char -> ParsecT ByteString () m Char
forall a.
ParsecT ByteString () m a
-> ParsecT ByteString () m a -> ParsecT ByteString () m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Char] -> ParsecT ByteString () m Char
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m Char
oneOf [Char]
"_'"
, opStart :: ParsecT ByteString () m Char
opStart = GenLanguageDef ByteString () m -> ParsecT ByteString () m Char
forall s u (m :: * -> *).
GenLanguageDef s u m -> ParsecT s u m Char
opLetter GenLanguageDef ByteString () m
forall (m :: * -> *). Monad m => GenLanguageDef ByteString () m
configLanguage
, opLetter :: ParsecT ByteString () m Char
opLetter = [Char] -> ParsecT ByteString () m Char
forall s (m :: * -> *) u.
Stream s m Char =>
[Char] -> ParsecT s u m Char
oneOf [Char]
":!#$%&*+./<=>?@\\^|-~"
, reservedOpNames :: [[Char]]
reservedOpNames = []
, reservedNames :: [[Char]]
reservedNames = []
, caseSensitive :: Bool
caseSensitive = Bool
True
}
configLexer :: Monad m => P.GenTokenParser BS.ByteString () m
configLexer :: forall (m :: * -> *). Monad m => GenTokenParser ByteString () m
configLexer = GenLanguageDef ByteString () m -> GenTokenParser ByteString () m
forall s (m :: * -> *) u.
Stream s m Char =>
GenLanguageDef s u m -> GenTokenParser s u m
P.makeTokenParser GenLanguageDef ByteString () m
forall (m :: * -> *). Monad m => GenLanguageDef ByteString () m
configLanguage
mapDecl :: Parser VtyUserConfig
mapDecl :: Parsec ByteString () VtyUserConfig
mapDecl = do
[Char] -> Parsec ByteString () VtyUserConfig
"map" <- GenTokenParser ByteString () Identity
-> ParsecT ByteString () Identity [Char]
forall s u (m :: * -> *).
GenTokenParser s u m -> ParsecT s u m [Char]
P.identifier GenTokenParser ByteString () Identity
forall (m :: * -> *). Monad m => GenTokenParser ByteString () m
configLexer
termIdent <- (char '_' >> P.whiteSpace configLexer >> return Nothing)
<|> (Just <$> P.stringLiteral configLexer)
bytes <- P.stringLiteral configLexer
key <- parseValue
modifiers <- parseValue
return defaultConfig { configInputMap = [(termIdent, bytes, EvKey key modifiers)] }
debugLogDecl :: Parser VtyUserConfig
debugLogDecl :: Parsec ByteString () VtyUserConfig
debugLogDecl = do
[Char] -> Parsec ByteString () VtyUserConfig
"debugLog" <- GenTokenParser ByteString () Identity
-> ParsecT ByteString () Identity [Char]
forall s u (m :: * -> *).
GenTokenParser s u m -> ParsecT s u m [Char]
P.identifier GenTokenParser ByteString () Identity
forall (m :: * -> *). Monad m => GenTokenParser ByteString () m
configLexer
path <- P.stringLiteral configLexer
return defaultConfig { configDebugLog = Just path }
colorModeDecl :: Parser VtyUserConfig
colorModeDecl :: Parsec ByteString () VtyUserConfig
colorModeDecl = do
[Char] -> Parsec ByteString () VtyUserConfig
"colorMode" <- GenTokenParser ByteString () Identity
-> ParsecT ByteString () Identity [Char]
forall s u (m :: * -> *).
GenTokenParser s u m -> ParsecT s u m [Char]
P.identifier GenTokenParser ByteString () Identity
forall (m :: * -> *). Monad m => GenTokenParser ByteString () m
configLexer
mode <- P.stringLiteral configLexer
return defaultConfig { configPreferredColorMode = readMaybe mode }
widthMapDecl :: Parser VtyUserConfig
widthMapDecl :: Parsec ByteString () VtyUserConfig
widthMapDecl = do
[Char] -> Parsec ByteString () VtyUserConfig
"widthMap" <- GenTokenParser ByteString () Identity
-> ParsecT ByteString () Identity [Char]
forall s u (m :: * -> *).
GenTokenParser s u m -> ParsecT s u m [Char]
P.identifier GenTokenParser ByteString () Identity
forall (m :: * -> *). Monad m => GenTokenParser ByteString () m
configLexer
tName <- P.stringLiteral configLexer
path <- P.stringLiteral configLexer
return defaultConfig { configTermWidthMaps = [(tName, path)] }
ignoreLine :: Parser ()
ignoreLine :: ParsecT ByteString () Identity ()
ignoreLine = ParsecT ByteString () Identity [Char]
-> ParsecT ByteString () Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT ByteString () Identity [Char]
-> ParsecT ByteString () Identity ())
-> ParsecT ByteString () Identity [Char]
-> ParsecT ByteString () Identity ()
forall a b. (a -> b) -> a -> b
$ ParsecT ByteString () Identity Char
-> ParsecT ByteString () Identity Char
-> ParsecT ByteString () Identity [Char]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill ParsecT ByteString () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar ParsecT ByteString () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
newline
parseConfig :: Parser VtyUserConfig
parseConfig :: Parsec ByteString () VtyUserConfig
parseConfig = ([VtyUserConfig] -> VtyUserConfig)
-> ParsecT ByteString () Identity [VtyUserConfig]
-> Parsec ByteString () VtyUserConfig
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [VtyUserConfig] -> VtyUserConfig
forall a. Monoid a => [a] -> a
mconcat (ParsecT ByteString () Identity [VtyUserConfig]
-> Parsec ByteString () VtyUserConfig)
-> ParsecT ByteString () Identity [VtyUserConfig]
-> Parsec ByteString () VtyUserConfig
forall a b. (a -> b) -> a -> b
$ Parsec ByteString () VtyUserConfig
-> ParsecT ByteString () Identity [VtyUserConfig]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (Parsec ByteString () VtyUserConfig
-> ParsecT ByteString () Identity [VtyUserConfig])
-> Parsec ByteString () VtyUserConfig
-> ParsecT ByteString () Identity [VtyUserConfig]
forall a b. (a -> b) -> a -> b
$ do
GenTokenParser ByteString () Identity
-> ParsecT ByteString () Identity ()
forall s u (m :: * -> *). GenTokenParser s u m -> ParsecT s u m ()
P.whiteSpace GenTokenParser ByteString () Identity
forall (m :: * -> *). Monad m => GenTokenParser ByteString () m
configLexer
let directives :: [Parsec ByteString () VtyUserConfig]
directives = [Parsec ByteString () VtyUserConfig
-> Parsec ByteString () VtyUserConfig
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try Parsec ByteString () VtyUserConfig
mapDecl, Parsec ByteString () VtyUserConfig
-> Parsec ByteString () VtyUserConfig
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try Parsec ByteString () VtyUserConfig
debugLogDecl, Parsec ByteString () VtyUserConfig
-> Parsec ByteString () VtyUserConfig
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try Parsec ByteString () VtyUserConfig
widthMapDecl, Parsec ByteString () VtyUserConfig
-> Parsec ByteString () VtyUserConfig
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try Parsec ByteString () VtyUserConfig
colorModeDecl]
[Parsec ByteString () VtyUserConfig]
-> Parsec ByteString () VtyUserConfig
forall s (m :: * -> *) t u a.
Stream s m t =>
[ParsecT s u m a] -> ParsecT s u m a
choice [Parsec ByteString () VtyUserConfig]
directives Parsec ByteString () VtyUserConfig
-> Parsec ByteString () VtyUserConfig
-> Parsec ByteString () VtyUserConfig
forall a.
ParsecT ByteString () Identity a
-> ParsecT ByteString () Identity a
-> ParsecT ByteString () Identity a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ParsecT ByteString () Identity ()
ignoreLine ParsecT ByteString () Identity ()
-> Parsec ByteString () VtyUserConfig
-> Parsec ByteString () VtyUserConfig
forall a b.
ParsecT ByteString () Identity a
-> ParsecT ByteString () Identity b
-> ParsecT ByteString () Identity b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> VtyUserConfig -> Parsec ByteString () VtyUserConfig
forall a. a -> ParsecT ByteString () Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return VtyUserConfig
defaultConfig)
class Parse a where parseValue :: Parser a
instance Parse Char where parseValue :: ParsecT ByteString () Identity Char
parseValue = GenTokenParser ByteString () Identity
-> ParsecT ByteString () Identity Char
forall s u (m :: * -> *).
GenTokenParser s u m -> ParsecT s u m Char
P.charLiteral GenTokenParser ByteString () Identity
forall (m :: * -> *). Monad m => GenTokenParser ByteString () m
configLexer
instance Parse Int where parseValue :: Parser Int
parseValue = Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Int)
-> ParsecT ByteString () Identity Integer -> Parser Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenTokenParser ByteString () Identity
-> ParsecT ByteString () Identity Integer
forall s u (m :: * -> *).
GenTokenParser s u m -> ParsecT s u m Integer
P.natural GenTokenParser ByteString () Identity
forall (m :: * -> *). Monad m => GenTokenParser ByteString () m
configLexer
instance Parse Key where parseValue :: Parser Key
parseValue = Parser Key
forall a. (Generic a, GParse (Rep a)) => Parser a
genericParse
instance Parse Modifier where parseValue :: Parser Modifier
parseValue = Parser Modifier
forall a. (Generic a, GParse (Rep a)) => Parser a
genericParse
instance Parse a => Parse [a] where
parseValue :: Parser [a]
parseValue = GenTokenParser ByteString () Identity
-> forall a.
ParsecT ByteString () Identity a
-> ParsecT ByteString () Identity a
forall s u (m :: * -> *).
GenTokenParser s u m
-> forall a. ParsecT s u m a -> ParsecT s u m a
P.brackets GenTokenParser ByteString () Identity
forall (m :: * -> *). Monad m => GenTokenParser ByteString () m
configLexer
(Parser a
forall a. Parse a => Parser a
parseValue Parser a -> ParsecT ByteString () Identity [Char] -> Parser [a]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
`sepBy` GenTokenParser ByteString () Identity
-> [Char] -> ParsecT ByteString () Identity [Char]
forall s u (m :: * -> *).
GenTokenParser s u m -> [Char] -> ParsecT s u m [Char]
P.symbol GenTokenParser ByteString () Identity
forall (m :: * -> *). Monad m => GenTokenParser ByteString () m
configLexer [Char]
",")
genericParse :: (Generic a, GParse (Rep a)) => Parser a
genericParse :: forall a. (Generic a, GParse (Rep a)) => Parser a
genericParse = Rep a (ZonkAny 0) -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
to (Rep a (ZonkAny 0) -> a)
-> ParsecT ByteString () Identity (Rep a (ZonkAny 0))
-> ParsecT ByteString () Identity a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT ByteString () Identity (Rep a (ZonkAny 0))
forall a. Parser (Rep a a)
forall (f :: * -> *) a. GParse f => Parser (f a)
gparse
class GParse f where gparse :: Parser (f a)
instance GParse f => GParse (M1 S i f) where gparse :: forall a. Parser (M1 S i f a)
gparse = f a -> M1 S i f a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (f a -> M1 S i f a)
-> ParsecT ByteString () Identity (f a)
-> ParsecT ByteString () Identity (M1 S i f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT ByteString () Identity (f a)
forall a. Parser (f a)
forall (f :: * -> *) a. GParse f => Parser (f a)
gparse
instance GParse U1 where gparse :: forall a. Parser (U1 a)
gparse = U1 a -> ParsecT ByteString () Identity (U1 a)
forall a. a -> ParsecT ByteString () Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return U1 a
forall k (p :: k). U1 p
U1
instance Parse a => GParse (K1 i a) where gparse :: forall a. Parser (K1 i a a)
gparse = a -> K1 i a a
forall k i c (p :: k). c -> K1 i c p
K1 (a -> K1 i a a)
-> ParsecT ByteString () Identity a
-> ParsecT ByteString () Identity (K1 i a a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT ByteString () Identity a
forall a. Parse a => Parser a
parseValue
instance (GParse f, GParse g) => GParse (f :*: g) where
gparse :: forall a. Parser ((:*:) f g a)
gparse = f a -> g a -> (:*:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) (f a -> g a -> (:*:) f g a)
-> ParsecT ByteString () Identity (f a)
-> ParsecT ByteString () Identity (g a -> (:*:) f g a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT ByteString () Identity (f a)
forall a. Parser (f a)
forall (f :: * -> *) a. GParse f => Parser (f a)
gparse ParsecT ByteString () Identity (g a -> (:*:) f g a)
-> ParsecT ByteString () Identity (g a)
-> ParsecT ByteString () Identity ((:*:) f g a)
forall a b.
ParsecT ByteString () Identity (a -> b)
-> ParsecT ByteString () Identity a
-> ParsecT ByteString () Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT ByteString () Identity (g a)
forall a. Parser (g a)
forall (f :: * -> *) a. GParse f => Parser (f a)
gparse
instance GParseAlts f => GParse (M1 D i f) where
gparse :: forall a. Parser (M1 D i f a)
gparse =
do con <- GenTokenParser ByteString () Identity
-> ParsecT ByteString () Identity [Char]
forall s u (m :: * -> *).
GenTokenParser s u m -> ParsecT s u m [Char]
P.identifier GenTokenParser ByteString () Identity
forall (m :: * -> *). Monad m => GenTokenParser ByteString () m
configLexer
M1 <$> gparseAlts con
class GParseAlts f where
gparseAlts :: String -> Parser (f a)
instance (Constructor i, GParse f) => GParseAlts (M1 C i f) where
gparseAlts :: forall a. [Char] -> Parser (M1 C i f a)
gparseAlts [Char]
con =
do Bool -> ParsecT ByteString () Identity ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard ([Char]
con [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== M1 C i Maybe (ZonkAny 1) -> [Char]
forall {k} (c :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
(f :: k1 -> *) (a :: k1).
Constructor c =>
t c f a -> [Char]
forall k1 (t :: Meta -> (k1 -> *) -> k1 -> *) (f :: k1 -> *)
(a :: k1).
t i f a -> [Char]
conName (Maybe a -> M1 C i Maybe a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 Maybe a
forall a. Maybe a
Nothing :: C1 i Maybe a))
f a -> M1 C i f a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (f a -> M1 C i f a)
-> ParsecT ByteString () Identity (f a)
-> ParsecT ByteString () Identity (M1 C i f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT ByteString () Identity (f a)
forall a. Parser (f a)
forall (f :: * -> *) a. GParse f => Parser (f a)
gparse
instance (GParseAlts f, GParseAlts g) => GParseAlts (f :+: g) where
gparseAlts :: forall a. [Char] -> Parser ((:+:) f g a)
gparseAlts [Char]
con = f a -> (:+:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (f a -> (:+:) f g a)
-> ParsecT ByteString () Identity (f a)
-> ParsecT ByteString () Identity ((:+:) f g a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> ParsecT ByteString () Identity (f a)
forall a. [Char] -> Parser (f a)
forall (f :: * -> *) a. GParseAlts f => [Char] -> Parser (f a)
gparseAlts [Char]
con ParsecT ByteString () Identity ((:+:) f g a)
-> ParsecT ByteString () Identity ((:+:) f g a)
-> ParsecT ByteString () Identity ((:+:) f g a)
forall a.
ParsecT ByteString () Identity a
-> ParsecT ByteString () Identity a
-> ParsecT ByteString () Identity a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> g a -> (:+:) f g a
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (g a -> (:+:) f g a)
-> ParsecT ByteString () Identity (g a)
-> ParsecT ByteString () Identity ((:+:) f g a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> ParsecT ByteString () Identity (g a)
forall a. [Char] -> Parser (g a)
forall (f :: * -> *) a. GParseAlts f => [Char] -> Parser (f a)
gparseAlts [Char]
con
instance GParseAlts V1 where gparseAlts :: forall a. [Char] -> Parser (V1 a)
gparseAlts [Char]
_ = [Char] -> ParsecT ByteString () Identity (V1 a)
forall a.
HasCallStack =>
[Char] -> ParsecT ByteString () Identity a
forall (m :: * -> *) a.
(MonadFail m, HasCallStack) =>
[Char] -> m a
fail [Char]
"GParse: V1"
data ConfigUpdateResult =
ConfigurationCreated
| ConfigurationModified
| ConfigurationConflict String
| ConfigurationRedundant
deriving (ConfigUpdateResult -> ConfigUpdateResult -> Bool
(ConfigUpdateResult -> ConfigUpdateResult -> Bool)
-> (ConfigUpdateResult -> ConfigUpdateResult -> Bool)
-> Eq ConfigUpdateResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ConfigUpdateResult -> ConfigUpdateResult -> Bool
== :: ConfigUpdateResult -> ConfigUpdateResult -> Bool
$c/= :: ConfigUpdateResult -> ConfigUpdateResult -> Bool
/= :: ConfigUpdateResult -> ConfigUpdateResult -> Bool
Eq, Int -> ConfigUpdateResult -> ShowS
[ConfigUpdateResult] -> ShowS
ConfigUpdateResult -> [Char]
(Int -> ConfigUpdateResult -> ShowS)
-> (ConfigUpdateResult -> [Char])
-> ([ConfigUpdateResult] -> ShowS)
-> Show ConfigUpdateResult
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConfigUpdateResult -> ShowS
showsPrec :: Int -> ConfigUpdateResult -> ShowS
$cshow :: ConfigUpdateResult -> [Char]
show :: ConfigUpdateResult -> [Char]
$cshowList :: [ConfigUpdateResult] -> ShowS
showList :: [ConfigUpdateResult] -> ShowS
Show)
addConfigWidthMap :: FilePath
-> String
-> FilePath
-> IO ConfigUpdateResult
addConfigWidthMap :: [Char] -> [Char] -> [Char] -> IO ConfigUpdateResult
addConfigWidthMap [Char]
configPath [Char]
term [Char]
tablePath = do
configEx <- [Char] -> IO Bool
doesFileExist [Char]
configPath
if configEx
then updateConfig
else createConfig >> return ConfigurationCreated
where
directive :: [Char]
directive = [Char]
"widthMap " [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> ShowS
forall a. Show a => a -> [Char]
show [Char]
term [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char]
" " [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> ShowS
forall a. Show a => a -> [Char]
show [Char]
tablePath [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> [Char]
"\n"
createConfig :: IO ()
createConfig = do
let dir :: [Char]
dir = ShowS
takeDirectory [Char]
configPath
Bool -> [Char] -> IO ()
createDirectoryIfMissing Bool
True [Char]
dir
[Char] -> [Char] -> IO ()
writeFile [Char]
configPath [Char]
directive
updateConfig :: IO ConfigUpdateResult
updateConfig = do
config <- [Char] -> IO VtyUserConfig
parseConfigFile [Char]
configPath
if (term, tablePath) `elem` configTermWidthMaps config
then return ConfigurationRedundant
else case lookup term (configTermWidthMaps config) of
Just [Char]
other -> ConfigUpdateResult -> IO ConfigUpdateResult
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConfigUpdateResult -> IO ConfigUpdateResult)
-> ConfigUpdateResult -> IO ConfigUpdateResult
forall a b. (a -> b) -> a -> b
$ [Char] -> ConfigUpdateResult
ConfigurationConflict [Char]
other
Maybe [Char]
Nothing -> do
[Char] -> [Char] -> IO ()
appendFile [Char]
configPath [Char]
directive
ConfigUpdateResult -> IO ConfigUpdateResult
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ConfigUpdateResult
ConfigurationModified