module Vgrep.Environment.Config where
import Control.Lens.Compat
import Control.Monad.IO.Class
import Data.Maybe
import Data.Monoid
import Graphics.Vty.Attributes
( Attr
, blue
, bold
, defAttr
, green
, standout
, withBackColor
, withForeColor
, withStyle
)
import Vgrep.Command
import Vgrep.Environment.Config.Monoid
import Vgrep.Environment.Config.Sources
import qualified Vgrep.Key as Key
import Vgrep.KeybindingMap (KeybindingMap (..))
import qualified Vgrep.KeybindingMap as KeybindingMap
data Config = Config
{ _colors :: Colors
, _tabstop :: Int
, _editor :: String
, _keybindings :: Keybindings
} deriving (Eq, Show)
data Colors = Colors
{ _lineNumbers :: Attr
, _lineNumbersHl :: Attr
, _normal :: Attr
, _normalHl :: Attr
, _fileHeaders :: Attr
, _selected :: Attr
} deriving (Eq, Show)
data Keybindings = Keybindings
{ _resultsKeybindings :: KeybindingMap
, _pagerKeybindings :: KeybindingMap
, _globalKeybindings :: KeybindingMap
} deriving (Eq, Show)
makeLenses ''Config
makeLenses ''Colors
makeLenses ''Keybindings
fromConfigMonoid :: ConfigMonoid -> Config
fromConfigMonoid ConfigMonoid{..} = Config
{ _colors = fromColorsMonoid _mcolors
, _tabstop = fromFirst (_tabstop defaultConfig) _mtabstop
, _editor = fromFirst (_editor defaultConfig) _meditor
, _keybindings = fromKeybindingsMonoid _mkeybindings }
fromColorsMonoid :: ColorsMonoid -> Colors
fromColorsMonoid ColorsMonoid{..} = Colors
{ _lineNumbers = fromFirst (_lineNumbers defaultColors) _mlineNumbers
, _lineNumbersHl = fromFirst (_lineNumbersHl defaultColors) _mlineNumbersHl
, _normal = fromFirst (_normal defaultColors) _mnormal
, _normalHl = fromFirst (_normalHl defaultColors) _mnormalHl
, _fileHeaders = fromFirst (_fileHeaders defaultColors) _mfileHeaders
, _selected = fromFirst (_selected defaultColors) _mselected }
fromFirst :: a -> First a -> a
fromFirst a = fromMaybe a . getFirst
fromKeybindingsMonoid :: KeybindingsMonoid -> Keybindings
fromKeybindingsMonoid KeybindingsMonoid{..} = Keybindings
{ _resultsKeybindings = fromMaybe mempty _mresultsKeybindings <> _resultsKeybindings defaultKeybindings
, _pagerKeybindings = fromMaybe mempty _mpagerKeybindings <> _pagerKeybindings defaultKeybindings
, _globalKeybindings = fromMaybe mempty _mglobalKeybindings <> _globalKeybindings defaultKeybindings }
defaultConfig :: Config
defaultConfig = Config
{ _colors = defaultColors
, _tabstop = 8
, _editor = "vi"
, _keybindings = defaultKeybindings }
defaultColors :: Colors
defaultColors = Colors
{ _lineNumbers = defAttr `withForeColor` blue
, _lineNumbersHl = defAttr `withForeColor` blue
`withStyle` bold
, _normal = defAttr
, _normalHl = defAttr `withStyle` bold
, _fileHeaders = defAttr `withBackColor` green
, _selected = defAttr `withStyle` standout }
defaultKeybindings :: Keybindings
defaultKeybindings = Keybindings
{ _resultsKeybindings = KeybindingMap.fromList
[ (Key.key Key.Up, ResultsUp)
, (Key.key Key.Down, ResultsDown)
, (Key.key Key.PageUp, ResultsPageUp)
, (Key.key Key.PageDown, ResultsPageDown)
, (Key.key Key.Enter, PagerGotoResult)
, (Key.key (Key.Char 'k'), ResultsUp)
, (Key.key (Key.Char 'j'), ResultsDown)
, (Key.key (Key.Char 'b') `Key.withModifier` Key.Ctrl, ResultsPageUp)
, (Key.key (Key.Char 'f') `Key.withModifier` Key.Ctrl, ResultsPageDown)
, (Key.key (Key.Char 'f'), DisplayResultsOnly)
, (Key.key Key.Tab, SplitFocusPager) ]
, _pagerKeybindings = KeybindingMap.fromList
[ (Key.key Key.Up, PagerUp)
, (Key.key Key.Down, PagerDown)
, (Key.key Key.PageUp, PagerPageUp)
, (Key.key Key.PageDown, PagerPageDown)
, (Key.key Key.Left, PagerScrollLeft)
, (Key.key Key.Right, PagerScrollRight)
, (Key.key (Key.Char 'k'), PagerUp)
, (Key.key (Key.Char 'j'), PagerDown)
, (Key.key (Key.Char 'h'), PagerScrollLeft)
, (Key.key (Key.Char 'l'), PagerScrollRight)
, (Key.key (Key.Char 'u') `Key.withModifier` Key.Ctrl, PagerHalfPageUp)
, (Key.key (Key.Char 'd') `Key.withModifier` Key.Ctrl, PagerHalfPageDown)
, (Key.key (Key.Char 'b') `Key.withModifier` Key.Ctrl, PagerPageUp)
, (Key.key (Key.Char 'f') `Key.withModifier` Key.Ctrl, PagerPageDown)
, (Key.key (Key.Char 'f'), DisplayPagerOnly)
, (Key.key Key.Tab, SplitFocusResults)
, (Key.key (Key.Char 'q'), DisplayResultsOnly) ]
, _globalKeybindings = KeybindingMap.fromList
[ (Key.key (Key.Char 'e'), OpenFileInEditor)
, (Key.key (Key.Char 'q'), Exit) ]
}
loadConfig
:: MonadIO io
=> ConfigMonoid
-> io Config
loadConfig configFromArgs = do
configs <- sequence
[ pure configFromArgs
, configFromFile
, editorConfigFromEnv ]
pure (fromConfigMonoid (mconcat configs))