-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A pager for grep -- -- vgrep is a pager for navigating through grep output. -- -- Usage: -- --
-- grep -rn foo | vgrep -- vgrep foo /some/path -- vgrep foo /some/path | vgrep bar ---- -- Use hjkl or arrow keys to navigate, Enter to view -- file, q to quit. -- @package vgrep @version 0.2.3.0 -- | A transactional priority queue, based on a Finger Tree. module Control.Concurrent.STM.TPQueue -- | TPQueue is an unbounded priority queue based on a Finger Tree. data TPQueue k v -- | Build a new TPQueue. newTPQueue :: Ord k => STM (TPQueue k v) -- | IO version of newTPQueue. This is useful for creating top-level -- TPQueues using unsafePerformIO, because using -- atomically inside unsafePerformIO isn't possible. newTPQueueIO :: Ord k => IO (TPQueue k v) -- | Write a value to a TPQueue. writeTPQueue :: Ord k => TPQueue k v -> k -> v -> STM () -- | Read the next minimal value from a TPQueue. readTPQueue :: Ord k => TPQueue k v -> STM v -- | A version of readTPQueue that does not retry, but returns -- Nothing instead if no value is available. tryReadTPQueue :: Ord k => TPQueue k v -> STM (Maybe v) -- | Get the next minimal value from a TPQueue without removing it. peekTPQueue :: Ord k => TPQueue k v -> STM v -- | A version of peekTPQueue that does not retry, but returns -- Nothing instead if no value is available. tryPeekTPQueue :: Ord k => TPQueue k v -> STM (Maybe v) -- | Returns True if the TPQueue is empty. isEmptyTPQueue :: Ord k => TPQueue k v -> STM Bool module Control.Lens.Compat pre :: Getting (First a) s a -> Getter s (Maybe a) assign :: MonadState s m => ASetter s s a b -> b -> m () modifying :: MonadState s m => ASetter s s a b -> (a -> b) -> m () traverseOf :: a -> a type Getter s a = SimpleGetter s a -- | & is a reverse application operator. This provides -- notational convenience. Its precedence is one higher than that of the -- forward application operator $, which allows & to be -- nested in $. -- --
-- >>> 5 & (+1) & show -- "6" --(&) :: a -> (a -> b) -> b infixl 1 & -- | Flipped version of <$>. -- --
-- (<&>) = flip fmap ---- --
-- >>> Just 2 <&> (+1) -- Just 3 ---- --
-- >>> [1,2,3] <&> (+1) -- [2,3,4] ---- --
-- >>> Right 3 <&> (+1) -- Right 4 --(<&>) :: Functor f => f a -> (a -> b) -> f b infixl 1 <&> -- | _Nothing targets a () if the Maybe is a -- Nothing, and doesn't target anything otherwise: -- --
-- >>> Just 1 ^.. _Nothing -- [] ---- --
-- >>> Nothing ^.. _Nothing -- [()] ---- -- It's not particularly useful (unless you want to use has -- _Nothing as a replacement for isNothing), and -- provided mainly for consistency. -- -- Implementation: -- --
-- _Nothing f Nothing = const Nothing <$> f () -- _Nothing _ j = pure j --_Nothing :: Traversal' (Maybe a) () -- | _Just targets the value contained in a Maybe, provided -- it's a Just. -- -- See documentation for _Left (as these 2 are pretty similar). In -- particular, it can be used to write these: -- --
-- fromJust = (^?! _Just) -- ---- --
-- isJust = has _Just -- ---- --
-- maybeToList = (^.. _Just) -- ---- --
-- catMaybes = (^.. each . _Just) -- --_Just :: Traversal (Maybe a) (Maybe a') a a' -- | _Right targets the value contained in an Either, -- provided it's a Right. -- -- See documentation for _Left. _Right :: Traversal (Either a b) (Either a b') b b' -- | _Left targets the value contained in an Either, provided -- it's a Left. -- -- Gathering all Lefts in a structure (like the lefts -- function, but not necessarily just for lists): -- --
-- >>> [Left 1, Right 'c', Left 3] ^.. each._Left -- [1,3] ---- -- Checking whether an Either is a Left (like -- isLeft): -- --
-- >>> has _Left (Left 1) -- True ---- --
-- >>> has _Left (Right 1) -- False ---- -- Extracting a value (if you're sure it's a Left): -- --
-- >>> Left 1 ^?! _Left -- 1 ---- -- Mapping over all Lefts: -- --
-- >>> (each._Left %~ map toUpper) [Left "foo", Right "bar"] -- [Left "FOO",Right "bar"] ---- -- Implementation: -- --
-- _Left f (Left a) = Left <$> f a -- _Left _ (Right b) = pure (Right b) --_Left :: Traversal (Either a b) (Either a' b) a a' -- | This generalizes mapAccumL to an arbitrary Traversal. -- (Note that it doesn't work on folds, only traversals.) -- --
-- mapAccumL ≡ mapAccumLOf traverse --mapAccumLOf :: LensLike (State acc) s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) -- | _last gives you access to the last element of the list: -- --
-- >>> "Hello." ^? _last -- '.' ---- -- See documentation for _head, as _last and _head -- are pretty similar. _last :: Snoc s s a a => Traversal' s a -- | _init gives you access to all-but-the-last elements of the -- list: -- --
-- >>> "Hello." ^. _init -- "Hello" ---- -- See documentation for _tail, as _init and _tail -- are pretty similar. _init :: Snoc s s a a => Traversal' s s -- | _tail gives you access to the tail of a list (or Seq, -- etc): -- --
-- >>> [1..5] ^? _tail -- Just [2,3,4,5] ---- -- You can modify the tail as well: -- --
-- >>> [4,1,2,3] & _tail %~ reverse -- [4,3,2,1] ---- -- Since lists are monoids, you can use _tail with plain -- (^.) (and then it'll return an empty list if you give it an -- empty list): -- --
-- >>> [1..5] ^. _tail -- [2,3,4,5] ---- --
-- >>> [] ^. _tail -- [] ---- -- If you want to traverse each element of the tail, use -- _tail with each: -- --
-- >>> "I HATE CAPS." & _tail.each %~ toLower -- "I hate caps." ---- -- This package only lets you use _tail on lists, but if you use -- microlens-ghc you get instances for ByteString and -- Seq, and if you use microlens-platform you -- additionally get instances for Text and Vector. _tail :: Cons s s a a => Traversal' s s -- | _head traverses the 1st element of something (usually a list, -- but can also be a Seq, etc): -- --
-- >>> [1..5] ^? _head -- Just 1 ---- -- It can be used to modify too, as in this example where the 1st letter -- of a sentence is capitalised: -- --
-- >>> "mary had a little lamb." & _head %~ toTitle -- "Mary had a little lamb." ---- -- The reason it's a traversal and not a lens is that there's nothing to -- traverse when the list is empty: -- --
-- >>> [] ^? _head -- Nothing ---- -- This package only lets you use _head on lists, but if you use -- microlens-ghc you get instances for ByteString and -- Seq, and if you use microlens-platform you -- additionally get instances for Text and Vector. _head :: Cons s s a a => Traversal' s a -- | both traverses both fields of a tuple. Unlike -- both from lens, it only works for pairs – not for -- triples or Either. -- --
-- >>> ("str","ing") ^. both
-- "string"
--
--
--
-- >>> ("str","ing") & both %~ reverse
-- ("rts","gni")
--
both :: Traversal (a, a) (b, b) a b
-- | filtered is a traversal that filters elements “passing” through
-- it:
--
-- -- >>> (1,2,3,4) ^.. each -- [1,2,3,4] ---- --
-- >>> (1,2,3,4) ^.. each . filtered even -- [2,4] ---- -- It also can be used to modify elements selectively: -- --
-- >>> (1,2,3,4) & each . filtered even %~ (*100) -- (1,200,3,400) ---- -- The implementation of filtered is very simple. Consider this -- traversal, which always “traverses” just the value it's given: -- --
-- id :: Traversal' a a -- id f s = f s ---- -- And this traversal, which traverses nothing (in other words, -- doesn't traverse the value it's given): -- --
-- ignored :: Traversal' a a -- ignored f s = pure s ---- -- And now combine them into a traversal that conditionally traverses the -- value it's given, and you get filtered: -- --
-- filtered :: (a -> Bool) -> Traversal' a a -- filtered p f s = if p s then f s else pure s ---- -- By the way, note that filtered can generate illegal traversals -- – sometimes this can bite you. In particular, an optimisation that -- should be safe becomes unsafe. (To the best of my knowledge, this -- optimisation never happens automatically. If you just use -- filtered to modify/view something, you're safe. If you don't -- define any traversals that use filtered, you're safe too.) -- -- Let's use evens as an example: -- --
-- evens = filtered even ---- -- If evens was a legal traversal, you'd be able to fuse several -- applications of evens like this: -- --
-- over evens f . over evens g = over evens (f . g) ---- -- Unfortunately, in case of evens this isn't a correct -- optimisation: -- --
-- pairedWithEvens :: Traversal [(Int, a)] [(Int, b)] a b -- pairedWithEvens = each . filtered (even . fst) . _2 ---- -- Since you can't do anything with the 1st components through this -- traversal, the following holds for any f and g: -- --
-- over pairedWithEvens f . over pairedWithEvens g = over pairedWithEvens (f . g) --filtered :: (a -> Bool) -> Traversal' a a -- | failing lets you chain traversals together; if the 1st -- traversal fails, the 2nd traversal will be used. -- --
-- >>> ([1,2],[3]) & failing (_1.each) (_2.each) .~ 0 -- ([0,0],[3]) ---- --
-- >>> ([],[3]) & failing (_1.each) (_2.each) .~ 0 -- ([],[0]) ---- -- Note that the resulting traversal won't be valid unless either both -- traversals don't touch each others' elements, or both traversals -- return exactly the same results. To see an example of how -- failing can generate invalid traversals, see this -- Stackoverflow question. failing :: Traversal s t a b -> Traversal s t a b -> Traversal s t a b infixl 5 `failing` -- | singular turns a traversal into a lens that behaves like a -- single-element traversal: -- --
-- >>> [1,2,3] ^. singular each -- 1 ---- --
-- >>> [1,2,3] & singular each %~ negate -- [-1,2,3] ---- -- If there is nothing to return, it'll throw an error: -- --
-- >>> [] ^. singular each -- *** Exception: Lens.Micro.singular: empty traversal ---- -- However, it won't fail if you are merely setting the value: -- --
-- >>> [] & singular each %~ negate --singular :: HasCallStack => Traversal s t a a -> Lens s t a a -- | traverseOf with flipped arguments. Useful if the “loop body” is -- a lambda or a do block. forOf :: LensLike f s t a b -> s -> (a -> f b) -> f t -- | non lets you “relabel” a Maybe by equating -- Nothing to an arbitrary value (which you can choose): -- --
-- >>> Just 1 ^. non 0 -- 1 ---- --
-- >>> Nothing ^. non 0 -- 0 ---- -- The most useful thing about non is that relabeling also works -- in other direction. If you try to set the “forbidden” value, -- it'll be turned to Nothing: -- --
-- >>> Just 1 & non 0 .~ 0 -- Nothing ---- -- Setting anything else works just fine: -- --
-- >>> Just 1 & non 0 .~ 5 -- Just 5 ---- -- Same happens if you try to modify a value: -- --
-- >>> Just 1 & non 0 %~ subtract 1 -- Nothing ---- --
-- >>> Just 1 & non 0 %~ (+ 1) -- Just 2 ---- -- non is often useful when combined with at. For instance, -- if you have a map of songs and their playcounts, it makes sense not to -- store songs with 0 plays in the map; non can act as a filter -- that wouldn't pass such entries. -- -- Decrease playcount of a song to 0, and it'll be gone: -- --
-- >>> fromList [("Soon",1),("Yesterday",3)] & at "Soon" . non 0 %~ subtract 1
-- fromList [("Yesterday",3)]
--
--
-- Try to add a song with 0 plays, and it won't be added:
--
--
-- >>> fromList [("Yesterday",3)] & at "Soon" . non 0 .~ 0
-- fromList [("Yesterday",3)]
--
--
-- But it will be added if you set any other number:
--
--
-- >>> fromList [("Yesterday",3)] & at "Soon" . non 0 .~ 1
-- fromList [("Soon",1),("Yesterday",3)]
--
--
-- non is also useful when working with nested maps. Here a nested
-- map is created when it's missing:
--
--
-- >>> Map.empty & at "Dez Mona" . non Map.empty . at "Soon" .~ Just 1
-- fromList [("Dez Mona",fromList [("Soon",1)])]
--
--
-- and here it is deleted when its last entry is deleted (notice that
-- non is used twice here):
--
--
-- >>> fromList [("Dez Mona",fromList [("Soon",1)])] & at "Dez Mona" . non Map.empty . at "Soon" . non 0 %~ subtract 1
-- fromList []
--
--
-- To understand the last example better, observe the flow of values in
-- it:
--
-- -- ix :: Int -> Lens' [a] a -- ix i = lens (!! i) -- getter -- (\s b -> take i s ++ b : drop (i+1) s) -- setter ---- -- Usage: -- --
-- >>> [1..9] ^. ix 3 -- 4 -- -- >>> [1..9] & ix 3 %~ negate -- [1,2,3,-4,5,6,7,8,9] ---- -- When getting, the setter is completely unused; when setting, the -- getter is unused. Both are used only when the value is being modified. -- For instance, here we define a lens for the 1st element of a list, but -- instead of a legitimate getter we use undefined. Then we use -- the resulting lens for setting and it works, which proves that -- the getter wasn't used: -- --
-- >>> [1,2,3] & lens undefined (\s b -> b : tail s) .~ 10 -- [10,2,3] --lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b -- | folding creates a fold out of any function that returns a -- Foldable container (for instance, a list): -- --
-- >>> [1..5] ^.. folding tail -- [2,3,4,5] --folding :: Foldable f => (s -> f a) -> SimpleFold s a -- | has checks whether a getter (any getter, including lenses, -- traversals, and folds) returns at least 1 value. -- -- Checking whether a list is non-empty: -- --
-- >>> has each [] -- False ---- -- You can also use it with e.g. _Left (and other 0-or-1 -- traversals) as a replacement for isNothing, isJust and -- other isConstructorName functions: -- --
-- >>> has _Left (Left 1) -- True --has :: Getting Any s a -> s -> Bool -- | traverseOf_ with flipped arguments. Useful if the “loop body” -- is a lambda or a do block, or in some other cases – for -- instance, you can avoid accidentally using for_ on a tuple or -- Either by switching to forOf_ each. Or -- you can write custom loops like these: -- --
-- forOf_ both (a, b) $ \x -> -- ... -- forOf_ each [1..10] $ \x -> -- ... -- forOf_ (each . _Right) $ \x -> -- ... --forOf_ :: Functor f => Getting (Traversed r f) s a -> s -> (a -> f r) -> f () -- | Apply an action to all targets and discard the result (like -- mapM_ or traverse_): -- --
-- >>> traverseOf_ both putStrLn ("hello", "world")
-- hello
-- world
--
--
-- Works with anything that allows getting, including lenses and getters
-- (so, anything except for ASetter). Should be faster than
-- traverseOf when you don't need the result.
traverseOf_ :: Functor f => Getting (Traversed r f) s a -> (a -> f r) -> s -> f ()
-- | (^?!) is an unsafe variant of (^?) – instead of using
-- Nothing to indicate that there were no elements returned, it
-- throws an exception.
(^?!) :: HasCallStack => s -> Getting (Endo a) s a -> a
infixl 8 ^?!
-- | s ^? t returns the 1st element t returns, or
-- Nothing if t doesn't return anything. It's trivially
-- implemented by passing the First monoid to the getter.
--
-- Safe head:
--
-- -- >>> [] ^? each -- Nothing ---- --
-- >>> [1..3] ^? each -- Just 1 ---- -- Converting Either to Maybe: -- --
-- >>> Left 1 ^? _Right -- Nothing ---- --
-- >>> Right 1 ^? _Right -- Just 1 ---- -- A non-operator version of (^?) is called preview, and -- – like view – it's a bit more general than (^?) (it -- works in MonadReader). If you need the general version, you -- can get it from microlens-mtl; otherwise there's preview -- available in Lens.Micro.Extras. (^?) :: s -> Getting (First a) s a -> Maybe a infixl 8 ^? -- | toListOf is a synonym for (^..). toListOf :: Getting (Endo [a]) s a -> s -> [a] -- | s ^.. t returns the list of all values that t gets -- from s. -- -- A Maybe contains either 0 or 1 values: -- --
-- >>> Just 3 ^.. _Just -- [3] ---- -- Gathering all values in a list of tuples: -- --
-- >>> [(1,2),(3,4)] ^.. each.each -- [1,2,3,4] --(^..) :: s -> Getting (Endo [a]) s a -> [a] infixl 8 ^.. -- | to creates a getter from any function: -- --
-- a ^. to f = f a ---- -- It's most useful in chains, because it lets you mix lenses and -- ordinary functions. Suppose you have a record which comes from some -- third-party library and doesn't have any lens accessors. You want to -- do something like this: -- --
-- value ^. _1 . field . at 2 ---- -- However, field isn't a getter, and you have to do this -- instead: -- --
-- field (value ^. _1) ^. at 2 ---- -- but now value is in the middle and it's hard to read the -- resulting code. A variant with to is prettier and more -- readable: -- --
-- value ^. _1 . to field . at 2 --to :: (s -> a) -> SimpleGetter s a -- | (^.) applies a getter to a value; in other words, it gets a -- value out of a structure using a getter (which can be a lens, -- traversal, fold, etc.). -- -- Getting 1st field of a tuple: -- --
-- (^. _1) :: (a, b) -> a -- (^. _1) = fst ---- -- When (^.) is used with a traversal, it combines all results -- using the Monoid instance for the resulting type. For instance, -- for lists it would be simple concatenation: -- --
-- >>> ("str","ing") ^. each
-- "string"
--
--
-- The reason for this is that traversals use Applicative, and the
-- Applicative instance for Const uses monoid concatenation
-- to combine “effects” of Const.
--
-- A non-operator version of (^.) is called view, and
-- it's a bit more general than (^.) (it works in
-- MonadReader). If you need the general version, you can get it
-- from microlens-mtl; otherwise there's view available in
-- Lens.Micro.Extras.
(^.) :: s -> Getting a s a -> a
infixl 8 ^.
-- | Transform every element by recursively applying a given ASetter
-- in a bottom-up manner.
transformOf :: ASetter a b a b -> (b -> b) -> a -> b
-- | → See an example on GitHub.
--
-- Rewrite by applying a rule everywhere you can. Ensures that the rule
-- cannot be applied anywhere in the result.
--
-- Usually transformOf is more appropriate, but rewriteOf
-- can give better compositionality. Given two single transformations
-- f and g, you can construct \a -> f a
-- <|> g a which performs both rewrites until a fixed
-- point.
rewriteOf :: ASetter a b a b -> (b -> Maybe a) -> a -> b
-- | This is a version of (.~) which modifies the structure and
-- returns it along with the old value:
--
-- -- >>> (1, 2) & _1 <<.~ 0 -- (1, (0, 2)) ---- -- Simpler type signatures: -- --
-- (<<.~) :: Lens s t a b -> b -> s -> (a, t) -- (<<.~) :: Monoid a => Traversal s t a b -> b -> s -> (a, t) --(<<.~) :: LensLike ((,) a) s t a b -> b -> s -> (a, t) infixr 4 <<.~ -- | This is a version of (%~) which modifies the structure and -- returns it along with the old value: -- --
-- >>> (1, 2) & _1 <<%~ negate -- (1, (-1, 2)) ---- -- Simpler type signatures: -- --
-- (<<%~) :: Lens s t a b -> (a -> b) -> s -> (a, t) -- (<<%~) :: Monoid a => Traversal s t a b -> (a -> b) -> s -> (a, t) --(<<%~) :: LensLike ((,) a) s t a b -> (a -> b) -> s -> (a, t) infixr 4 <<%~ -- | This is a version of (%~) which modifies the structure and -- returns it along with the new value: -- --
-- >>> (1, 2) & _1 <%~ negate -- (-1, (-1, 2)) ---- -- Simpler type signatures: -- --
-- (<%~) :: Lens s t a b -> (a -> b) -> s -> (b, t) -- (<%~) :: Monoid b => Traversal s t a b -> (a -> b) -> s -> (b, t) ---- -- Since it does getting in addition to setting, you can't use it with -- ASetter (but you can use it with lens and traversals). (<%~) :: LensLike ((,) b) s t a b -> (a -> b) -> s -> (b, t) infixr 4 <%~ -- | mapped is a setter for everything contained in a functor. You -- can use it to map over lists, Maybe, or even IO -- (which is something you can't do with traversed or -- each). -- -- Here mapped is used to turn a value to all non-Nothing -- values in a list: -- --
-- >>> [Just 3,Nothing,Just 5] & mapped.mapped .~ 0 -- [Just 0,Nothing,Just 0] ---- -- Keep in mind that while mapped is a more powerful setter than -- each, it can't be used as a getter! This won't work (and will -- fail with a type error): -- --
-- [(1,2),(3,4),(5,6)] ^.. mapped . both --mapped :: Functor f => ASetter (f a) (f b) a b -- | (?~) is a version of (.~) that wraps the value into -- Just before setting. -- --
-- l ?~ b = l .~ Just b ---- -- It can be useful in combination with at: -- --
-- >>> Map.empty & at 3 ?~ x -- fromList [(3,x)] --(?~) :: ASetter s t a (Maybe b) -> b -> s -> t infixr 4 ?~ -- | set is a synonym for (.~). -- -- Setting the 1st component of a pair: -- --
-- set _1 :: x -> (a, b) -> (x, b) -- set _1 = \x t -> (x, snd t) ---- -- Using it to rewrite (<$): -- --
-- set mapped :: Functor f => a -> f b -> f a -- set mapped = (<$) --set :: ASetter s t a b -> b -> s -> t -- | (.~) assigns a value to the target. It's the same thing as -- using (%~) with const: -- --
-- l .~ x = l %~ const x ---- -- See set if you want a non-operator synonym. -- -- Here it is used to change 2 fields of a 3-tuple: -- --
-- >>> (0,0,0) & _1 .~ 1 & _3 .~ 3 -- (1,0,3) --(.~) :: ASetter s t a b -> b -> s -> t infixr 4 .~ -- | (<>~) appends a value monoidally to the target. -- --
-- >>> ("hello", "goodbye") & both <>~ " world!"
-- ("hello world!", "goodbye world!")
--
(<>~) :: Monoid a => ASetter s t a a -> a -> s -> t
infixr 4 <>~
-- | Decrement the target(s) of a numerically valued Lens, or
-- Traversal.
--
-- -- >>> (a,b) & _1 -~ c -- (a - c,b) ---- --
-- >>> (a,b) & both -~ c -- (a - c,b - c) ---- --
-- >>> _1 -~ 2 $ (1,2) -- (-1,2) ---- --
-- >>> mapped.mapped -~ 1 $ [[4,5],[6,7]] -- [[3,4],[5,6]] ---- --
-- (-~) :: Num a => Lens' s a -> a -> s -> s -- (-~) :: Num a => Traversal' s a -> a -> s -> s --(-~) :: Num a => ASetter s t a a -> a -> s -> t infixr 4 -~ -- | Increment the target(s) of a numerically valued Lens or -- Traversal. -- --
-- >>> (a,b) & _1 +~ c -- (a + c,b) ---- --
-- >>> (a,b) & both +~ c -- (a + c,b + c) ---- --
-- >>> (1,2) & _2 +~ 1 -- (1,3) ---- --
-- >>> [(a,b),(c,d)] & traverse.both +~ e -- [(a + e,b + e),(c + e,d + e)] ---- --
-- (+~) :: Num a => Lens' s a -> a -> s -> s -- (+~) :: Num a => Traversal' s a -> a -> s -> s --(+~) :: Num a => ASetter s t a a -> a -> s -> t infixr 4 +~ -- | over is a synonym for (%~). -- -- Getting fmap in a roundabout way: -- --
-- over mapped :: Functor f => (a -> b) -> f a -> f b -- over mapped = fmap ---- -- Applying a function to both components of a pair: -- --
-- over both :: (a -> b) -> (a, a) -> (b, b) -- over both = \f t -> (f (fst t), f (snd t)) ---- -- Using over _2 as a replacement for -- second: -- --
-- >>> over _2 show (10,20) -- (10,"20") --over :: ASetter s t a b -> (a -> b) -> s -> t -- | (%~) applies a function to the target; an alternative -- explanation is that it is an inverse of sets, which turns a -- setter into an ordinary function. mapped %~ -- reverse is the same thing as fmap -- reverse. -- -- See over if you want a non-operator synonym. -- -- Negating the 1st element of a pair: -- --
-- >>> (1,2) & _1 %~ negate -- (-1,2) ---- -- Turning all Lefts in a list to upper case: -- --
-- >>> (mapped._Left.mapped %~ toUpper) [Left "foo", Right "bar"] -- [Left "FOO",Right "bar"] --(%~) :: ASetter s t a b -> (a -> b) -> s -> t infixr 4 %~ -- | sets creates an ASetter from an ordinary function. (The -- only thing it does is wrapping and unwrapping Identity.) sets :: ((a -> b) -> s -> t) -> ASetter s t a b -- | folded is a fold for anything Foldable. In a way, it's -- an opposite of mapped – the most powerful getter, but can't -- be used as a setter. folded :: forall (f :: Type -> Type) a. Foldable f => SimpleFold (f a) a -- | traversed traverses any Traversable container (list, -- vector, Map, Maybe, you name it): -- --
-- >>> Just 1 ^.. traversed -- [1] ---- -- traversed is the same as traverse, but can be faster -- thanks to magic rewrite rules. traversed :: forall (f :: Type -> Type) a b. Traversable f => Traversal (f a) (f b) a b -- | each tries to be a universal Traversal – it behaves like -- traversed in most situations, but also adds support for e.g. -- tuples with same-typed values: -- --
-- >>> (1,2) & each %~ succ -- (2,3) ---- --
-- >>> ["x", "y", "z"] ^. each -- "xyz" ---- -- However, note that each doesn't work on every instance -- of Traversable. If you have a Traversable which isn't -- supported by each, you can use traversed instead. -- Personally, I like using each instead of traversed -- whenever possible – it's shorter and more descriptive. -- -- You can use each with these things: -- --
-- each :: Traversal [a] [b] a b -- -- each :: Traversal (Maybe a) (Maybe b) a b -- each :: Traversal (Either a a) (Either b b) a b -- since 0.4.11 -- -- each :: Traversal (a,a) (b,b) a b -- each :: Traversal (a,a,a) (b,b,b) a b -- each :: Traversal (a,a,a,a) (b,b,b,b) a b -- each :: Traversal (a,a,a,a,a) (b,b,b,b,b) a b -- -- each :: (RealFloat a, RealFloat b) => Traversal (Complex a) (Complex b) a b ---- -- You can also use each with types from array, -- bytestring, and containers by using -- microlens-ghc, or additionally with types from vector, -- text, and unordered-containers by using -- microlens-platform. each :: Each s t a b => Traversal s t a b -- | This traversal lets you access (and update) an arbitrary element in a -- list, array, Map, etc. (If you want to insert or delete -- elements as well, look at at.) -- -- An example for lists: -- --
-- >>> [0..5] & ix 3 .~ 10 -- [0,1,2,10,4,5] ---- -- You can use it for getting, too: -- --
-- >>> [0..5] ^? ix 3 -- Just 3 ---- -- Of course, the element may not be present (which means that you can -- use ix as a safe variant of (!!)): -- --
-- >>> [0..5] ^? ix 10 -- Nothing ---- -- Another useful instance is the one for functions – it lets you modify -- their outputs for specific inputs. For instance, here's maximum -- that returns 0 when the list is empty (instead of throwing an -- exception): -- --
-- maximum0 = maximum & ix [] .~ 0 ---- -- The following instances are provided in this package: -- --
-- ix :: Int -> Traversal' [a] a -- -- ix :: (Eq e) => e -> Traversal' (e -> a) a ---- -- You can also use ix with types from array, -- bytestring, and containers by using -- microlens-ghc, or additionally with types from vector, -- text, and unordered-containers by using -- microlens-platform. ix :: Ixed m => Index m -> Traversal' m (IxValue m) -- | This lens lets you read, write, or delete elements in -- Map-like structures. It returns Nothing when the value -- isn't found, just like lookup: -- --
-- Data.Map.lookup k m = m ^. at k ---- -- However, it also lets you insert and delete values by setting the -- value to Just value or Nothing: -- --
-- Data.Map.insert k a m = m & at k .~ Just a -- -- Data.Map.delete k m = m & at k .~ Nothing ---- -- Or you could use (?~) instead of (.~): -- --
-- Data.Map.insert k a m = m & at k ?~ a ---- -- Note that at doesn't work for arrays or lists. You can't delete -- an arbitrary element from an array (what would be left in its place?), -- and you can't set an arbitrary element in a list because if the index -- is out of list's bounds, you'd have to somehow fill the stretch -- between the last element and the element you just inserted (i.e. -- [1,2,3] & at 10 .~ 5 is undefined). If you want to modify -- an already existing value in an array or list, you should use -- ix instead. -- -- at is often used with non. See the documentation of -- non for examples. -- -- Note that at isn't strict for Map, even if you're -- using Data.Map.Strict: -- --
-- >>> Data.Map.Strict.size (Data.Map.Strict.empty & at 1 .~ Just undefined) -- 1 ---- -- The reason for such behavior is that there's actually no “strict -- Map” type; Data.Map.Strict just provides some strict -- functions for ordinary Maps. -- -- This package doesn't actually provide any instances for at, but -- there are instances for Map and IntMap in -- microlens-ghc and an instance for HashMap in -- microlens-platform. at :: At m => Index m -> Lens' m (Maybe (IxValue m)) -- | Gives access to the 1st field of a tuple (up to 5-tuples). -- -- Getting the 1st component: -- --
-- >>> (1,2,3,4,5) ^. _1 -- 1 ---- -- Setting the 1st component: -- --
-- >>> (1,2,3) & _1 .~ 10 -- (10,2,3) ---- -- Note that this lens is lazy, and can set fields even of -- undefined: -- --
-- >>> set _1 10 undefined :: (Int, Int) -- (10,*** Exception: Prelude.undefined ---- -- This is done to avoid violating a lens law stating that you can get -- back what you put: -- --
-- >>> view _1 . set _1 10 $ (undefined :: (Int, Int)) -- 10 ---- -- The implementation (for 2-tuples) is: -- --
-- _1 f t = (,) <$> f (fst t) -- <*> pure (snd t) ---- -- or, alternatively, -- --
-- _1 f ~(a,b) = (\a' -> (a',b)) <$> f a ---- -- (where ~ means a lazy pattern). -- -- _2, _3, _4, and _5 are also available (see -- below). _1 :: Field1 s t a b => Lens s t a b _2 :: Field2 s t a b => Lens s t a b _3 :: Field3 s t a b => Lens s t a b _4 :: Field4 s t a b => Lens s t a b _5 :: Field5 s t a b => Lens s t a b -- | strict lets you convert between strict and lazy versions of a -- datatype: -- --
-- >>> let someText = "hello" :: Lazy.Text -- -- >>> someText ^. strict -- "hello" :: Strict.Text ---- -- It can also be useful if you have a function that works on a strict -- type but your type is lazy: -- --
-- stripDiacritics :: Strict.Text -> Strict.Text -- stripDiacritics = ... ---- --
-- >>> let someText = "Paul Erdős" :: Lazy.Text -- -- >>> someText & strict %~ stripDiacritics -- "Paul Erdos" :: Lazy.Text ---- -- strict works on ByteString and -- StateT/WriterT/RWST if you use -- microlens-ghc, and additionally on Text if you use -- microlens-platform. strict :: Strict lazy strict => Lens' lazy strict -- | lazy is like strict but works in opposite direction: -- --
-- >>> let someText = "hello" :: Strict.Text -- -- >>> someText ^. lazy -- "hello" :: Lazy.Text --lazy :: Strict lazy strict => Lens' strict lazy -- | ASetter s t a b is something that turns a function modifying -- a value into a function modifying a structure. If you ignore -- Identity (as Identity a is the same thing as -- a), the type is: -- --
-- type ASetter s t a b = (a -> b) -> s -> t ---- -- The reason Identity is used here is for ASetter to be -- composable with other types, such as Lens. -- -- Technically, if you're writing a library, you shouldn't use this type -- for setters you are exporting from your library; the right type to use -- is Setter, but it is not provided by this package -- (because then it'd have to depend on distributive). It's -- completely alright, however, to export functions which take an -- ASetter as an argument. type ASetter s t a b = a -> Identity b -> s -> Identity t -- | This is a type alias for monomorphic setters which don't change the -- type of the container (or of the value inside). It's useful more often -- than the same type in lens, because we can't provide real setters and -- so it does the job of both ASetter' and -- Setter'. type ASetter' s a = ASetter s s a a -- | A SimpleGetter s a extracts a from s; so, -- it's the same thing as (s -> a), but you can use it in -- lens chains because its type looks like this: -- --
-- type SimpleGetter s a = -- forall r. (a -> Const r a) -> s -> Const r s ---- -- Since Const r is a functor, SimpleGetter has the same -- shape as other lens types and can be composed with them. To get (s -- -> a) out of a SimpleGetter, choose r ~ a and -- feed Const :: a -> Const a a to the getter: -- --
-- -- the actual signature is more permissive: -- -- view :: Getting a s a -> s -> a -- view :: SimpleGetter s a -> s -> a -- view getter = getConst . getter Const ---- -- The actual Getter from lens is more general: -- --
-- type Getter s a = -- forall f. (Contravariant f, Functor f) => (a -> f a) -> s -> f s ---- -- I'm not currently aware of any functions that take lens's -- Getter but won't accept SimpleGetter, but you should -- try to avoid exporting SimpleGetters anyway to minimise -- confusion. Alternatively, look at microlens-contra, which -- provides a fully lens-compatible Getter. -- -- Lens users: you can convert a SimpleGetter to Getter -- by applying to . view to it. type SimpleGetter s a = forall r. () => Getting r s a -- | Functions that operate on getters and folds – such as (^.), -- (^..), (^?) – use Getter r s a (with different -- values of r) to describe what kind of result they need. For -- instance, (^.) needs the getter to be able to return a single -- value, and so it accepts a getter of type Getting a s a. -- (^..) wants the getter to gather values together, so it uses -- Getting (Endo [a]) s a (it could've used Getting [a] s -- a instead, but it's faster with Endo). The choice of -- r depends on what you want to do with elements you're -- extracting from s. type Getting r s a = a -> Const r a -> s -> Const r s -- | A SimpleFold s a extracts several as from -- s; so, it's pretty much the same thing as (s -> -- [a]), but you can use it with lens operators. -- -- The actual Fold from lens is more general: -- --
-- type Fold s a = -- forall f. (Contravariant f, Applicative f) => (a -> f a) -> s -> f s ---- -- There are several functions in lens that accept lens's Fold -- but won't accept SimpleFold; I'm aware of -- takingWhile, droppingWhile, -- backwards, foldByOf, -- foldMapByOf. For this reason, try not to export -- SimpleFolds if at all possible. microlens-contra -- provides a fully lens-compatible Fold. -- -- Lens users: you can convert a SimpleFold to Fold by -- applying folded . toListOf to it. type SimpleFold s a = forall r. Monoid r => Getting r s a -- | Lens s t a b is the lowest common denominator of a setter and -- a getter, something that has the power of both; it has a -- Functor constraint, and since both Const and -- Identity are functors, it can be used whenever a getter or a -- setter is needed. -- --
-- t pure ≡ pure -- fmap (t f) . t g ≡ getCompose . t (Compose . fmap f . g) ---- -- The 1st law states that you can't change the shape of the structure or -- do anything funny with elements (traverse elements which aren't in the -- structure, create new elements out of thin air, etc.). The 2nd law -- states that you should be able to fuse 2 identical traversals into -- one. For a more detailed explanation of the laws, see this blog -- post (if you prefer rambling blog posts), or The Essence Of The -- Iterator Pattern (if you prefer papers). -- -- Traversing any value twice is a violation of traversal laws. You can, -- however, traverse values in any order. type Traversal s t a b = forall (f :: Type -> Type). Applicative f => a -> f b -> s -> f t -- | This is a type alias for monomorphic traversals which don't change the -- type of the container (or of the values inside). type Traversal' s a = Traversal s s a a -- | LensLike is a type that is often used to make combinators as -- general as possible. For instance, take (<<%~), which -- only requires the passed lens to be able to work with the (,) -- a functor (lenses and traversals can do that). The fully expanded -- type is as follows: -- --
-- (<<%~) :: ((a -> (a, b)) -> s -> (a, t)) -> (a -> b) -> s -> (a, t) ---- -- With LensLike, the intent to use the (,) a functor can -- be made a bit clearer: -- --
-- (<<%~) :: LensLike ((,) a) s t a b -> (a -> b) -> s -> (a, t) --type LensLike (f :: Type -> Type) s t a b = a -> f b -> s -> f t -- | A type alias for monomorphic LensLikes. type LensLike' (f :: Type -> Type) s a = LensLike f s s a a -- | Traverse characters in a strict or lazy ByteString (to -- traverse bytes instead of characters, use each). chars :: IsByteString t => Traversal' t Char -- | Treat a strict or lazy ByteString as a String. (Just -- as packedChars, it will garble characters above 0xFF.) unpackedChars :: IsByteString t => Lens' t String -- | Treat a String as a strict or lazy ByteString. (Note -- that it will garble characters above 0xFF, same as pack does.) packedChars :: IsByteString t => Lens' String t -- | Treat a list of bytes as a strict or lazy ByteString. packedBytes :: IsByteString t => Lens' [Word8] t -- | Treat a strict or lazy ByteString as a list of bytes. unpackedBytes :: IsByteString t => Lens' t [Word8] -- | (<?=) is a version of (<.=) that wraps the value -- into Just before setting. -- --
-- l <?= b = do -- l .= Just b -- return b ---- -- It can be useful in combination with at. (=) :: MonadState s m => LensLike ((,) b) s s a (Maybe b) -> b -> m b infix 4 = -- | Set state and return new value. -- --
-- l <.= b = do -- l .= b -- return b --(<.=) :: MonadState s m => LensLike ((,) b) s s a b -> b -> m b infix 4 <.= -- | Set state and return the old value. -- --
-- l <<.= b = do -- old <- use l -- l .= b -- return old --(<<.=) :: MonadState s m => LensLike ((,) a) s s a b -> b -> m a infix 4 <<.= -- | Modify state and return the old value (i.e. as it was before the -- modificaton). -- --
-- l <<%= f = do -- old <- use l -- l %= f -- return old --(<<%=) :: MonadState s m => LensLike ((,) a) s s a b -> (a -> b) -> m a infix 4 <<%= -- | Modify state and return the modified (new) value. -- --
-- l <%= f = do -- l %= f -- use l --(<%=) :: MonadState s m => LensLike ((,) b) s s a b -> (a -> b) -> m b infix 4 <%= (//=) :: (MonadState s m, Fractional a) => ASetter s s a a -> a -> m () infix 4 //= (*=) :: (MonadState s m, Num a) => ASetter s s a a -> a -> m () infix 4 *= (-=) :: (MonadState s m, Num a) => ASetter s s a a -> a -> m () infix 4 -= (+=) :: (MonadState s m, Num a) => ASetter s s a a -> a -> m () infix 4 += -- | Modify state by applying a function to a part of the state. An -- example: -- --
-- >>> execState (do _1 %= (+1); _2 %= reverse) (1,"hello") -- (2,"olleh") ---- -- Implementation: -- --
-- l %= f = modify (l %~ f) ---- -- If you also want to get the value before/after the modification, use -- (<<%=)/(<%=). -- -- There are a few specialised versions of (%=) which mimic C -- operators: -- -- (%=) :: MonadState s m => ASetter s s a b -> (a -> b) -> m () infix 4 %= -- | (<~) is a version of (.=) that takes a monadic value -- (and then executes it and assigns the result to the lens). -- --
-- l <~ mb = do -- b <- mb -- l .= b --(<~) :: MonadState s m => ASetter s s a b -> m b -> m () infixr 2 <~ -- | (?=) is a version of (.=) that wraps the value into -- Just before setting. -- --
-- l ?= b = l .= Just b ---- -- It can be useful in combination with at. (?=) :: MonadState s m => ASetter s s a (Maybe b) -> b -> m () infix 4 ?= -- | Modify state by “assigning” a value to a part of the state. -- -- This is merely (.~) which works in MonadState: -- --
-- l .= x = modify (l .~ x) ---- -- If you also want to know the value that was replaced by (.=), -- use (<<.=). (.=) :: MonadState s m => ASetter s s a b -> b -> m () infix 4 .= -- | This can be used to chain lens operations using op= syntax -- rather than op~ syntax for simple non-type-changing cases. -- >>> (10,20) & _1 .~ 30 & _2 .~ 40 (30,40) -- --
-- >>> (10,20) &~ do _1 .= 30; _2 .= 40 -- (30,40) ---- -- This does not support type-changing assignment, e.g. -- --
-- >>> (10,20) & _1 .~ "hello"
-- ("hello",20)
--
(&~) :: s -> State s a -> s
infixl 1 &~
-- | preuse is (^?) (or preview) which implicitly
-- operates on the state – it takes the state and applies a traversal (or
-- fold) to it to extract the 1st element the traversal points at.
--
-- -- preuse l = gets (preview l) --preuse :: MonadState s m => Getting (First a) s a -> m (Maybe a) -- | use is (^.) (or view) which implicitly operates -- on the state; for instance, if your state is a record containing a -- field foo, you can write -- --
-- x <- use foo ---- -- to extract foo from the state. In other words, use is -- the same as gets, but for getters instead of functions. -- -- The implementation of use is straightforward: -- --
-- use l = gets (view l) ---- -- If you need to extract something with a fold or traversal, you need -- preuse. use :: MonadState s m => Getting a s a -> m a -- | preview is a synonym for (^?), generalised for -- MonadReader (just like view, which is a synonym for -- (^.)). -- --
-- >>> preview each [1..5] -- Just 1 --preview :: MonadReader s m => Getting (First a) s a -> m (Maybe a) -- | view is a synonym for (^.), generalised for -- MonadReader (we are able to use it instead of (^.) since -- functions are instances of the MonadReader class): -- --
-- >>> view _1 (1, 2) -- 1 ---- -- When you're using Reader for config and your config type has -- lenses generated for it, most of the time you'll be using view -- instead of asks: -- --
-- doSomething :: (MonadReader Config m) => m Int -- doSomething = do -- thingy <- view setting1 -- same as “asks (^. setting1)” -- anotherThingy <- view setting2 -- ... --view :: MonadReader s m => Getting a s a -> m a -- | When you're in a state monad, this function lets you operate on a part -- of your state. For instance, if your state was a record containing a -- position field, after zooming position would become -- your whole state (and when you modify it, the bigger structure would -- be modified as well). -- -- (Your State / StateT or RWS / RWST can be -- anywhere in the stack, but you can't use zoom with arbitrary -- MonadState because it doesn't provide any methods to change the -- type of the state. See this issue for details.) -- -- For the sake of the example, let's define some types first: -- --
-- data Position = Position {
-- _x, _y :: Int }
--
-- data Player = Player {
-- _position :: Position,
-- ... }
--
-- data Game = Game {
-- _player :: Player,
-- _obstacles :: [Position],
-- ... }
--
-- concat <$> mapM makeLenses [''Position, ''Player, ''Game]
--
--
-- Now, here's an action that moves the player north-east:
--
-- -- moveNE :: State Game () -- moveNE = do -- player.position.x += 1 -- player.position.y += 1 ---- -- With zoom, you can use player.position to focus just -- on a part of the state: -- --
-- moveNE :: State Game () -- moveNE = do -- zoom (player.position) $ do -- x += 1 -- y += 1 ---- -- You can just as well use it for retrieving things out of the state: -- --
-- getCoords :: State Game (Int, Int) -- getCoords = zoom (player.position) ((,) <$> use x <*> use y) ---- -- Or more explicitly: -- --
-- getCoords = zoom (player.position) $ do -- x' <- use x -- y' <- use y -- return (x', y') ---- -- When you pass a traversal to zoom, it'll work as a loop. For -- instance, here we move all obstacles: -- --
-- moveObstaclesNE :: State Game () -- moveObstaclesNE = do -- zoom (obstacles.each) $ do -- x += 1 -- y += 1 ---- -- If the action returns a result, all results would be combined with -- <> – the same way they're combined when ^. is -- passed a traversal. In this example, moveObstaclesNE returns -- a list of old coordinates of obstacles in addition to moving them: -- --
-- moveObstaclesNE = do -- xys <- zoom (obstacles.each) $ do -- -- Get old coordinates. -- x' <- use x -- y' <- use y -- -- Update them. -- x .= x' + 1 -- y .= y' + 1 -- -- Return a single-element list with old coordinates. -- return [(x', y')] -- ... ---- -- Finally, you might need to write your own instances of Zoom if -- you use newtyped transformers in your monad stack. This can -- be done as follows: -- --
-- import Lens.Micro.Mtl.Internal -- -- type instance Zoomed (MyStateT s m) = Zoomed (StateT s m) -- -- instance Monad m => Zoom (MyStateT s m) (MyStateT t m) s t where -- zoom l (MyStateT m) = MyStateT (zoom l m) --zoom :: Zoom m n s t => LensLike' (Zoomed m c) t s -> m c -> n c infixr 2 `zoom` -- | This is an equivalent of local which lets you apply a getter to -- your environment instead of merely applying a function (and it also -- lets you change the type of the environment). -- --
-- local :: (r -> r) -> Reader r a -> Reader r a -- magnify :: Getter r x -> Reader x a -> Reader r a ---- -- magnify works with Reader / ReaderT, RWS / -- RWST, and (->). -- -- Here's an example of magnify being used to work with a part of -- a bigger config. First, the types: -- --
-- data URL = URL {
-- _protocol :: Maybe String,
-- _path :: String }
--
-- data Config = Config {
-- _base :: URL,
-- ... }
--
-- makeLenses ''URL
-- makeLenses ''Config
--
--
-- Now, let's define a function which returns the base url:
--
-- -- getBase :: Reader Config String -- getBase = do -- protocol <- fromMaybe "https" <$> view (base.protocol) -- path <- view (base.path) -- return (protocol ++ path) ---- -- With magnify, we can factor out base: -- --
-- getBase = magnify base $ do -- protocol <- fromMaybe "https" <$> view protocol -- path <- view path -- return (protocol ++ path) ---- -- This concludes the example. -- -- Finally, you should know writing instances of Magnify for your -- own types can be done as follows: -- --
-- import Lens.Micro.Mtl.Internal -- -- type instance Magnified (MyReaderT r m) = Magnified (ReaderT r m) -- -- instance Monad m => Magnify (MyReaderT r m) (MyReaderT t m) r t where -- magnify l (MyReaderT m) = MyReaderT (magnify l m) --magnify :: Magnify m n b a => LensLike' (Magnified m c) a b -> m c -> n c infixr 2 `magnify` -- | Name to give to a generated lens (used in lensField). data DefName -- | Simple top-level definiton name TopName :: Name -> DefName -- | makeFields-style class name and method name MethodName :: Name -> Name -> DefName -- | Rules used to generate lenses. The fields are intentionally not -- exported; to create your own rules, see lenses in the “Configuring -- lens rules” section. You'd have to customise one of the existing -- rulesets; for an example of doing that, see makeLensesWith. data LensRules -- | Generate lenses for a data type or a newtype. -- -- To use it, you have to enable Template Haskell first: -- --
-- {-# LANGUAGE TemplateHaskell #-}
--
--
-- Then, after declaring the datatype (let's say Foo), add
-- makeLenses ''Foo on a separate line (if you do it before the
-- type is declared, you'll get a “not in scope” error – see the section
-- at the top of this page):
--
--
-- data Foo = Foo {
-- _x :: Int,
-- _y :: Bool }
--
-- makeLenses ''Foo
--
--
-- This would generate the following lenses, which can be used to access
-- the fields of Foo:
--
--
-- x :: Lens' Foo Int
-- x f foo = (\x' -> foo {_x = x'}) <$> f (_x foo)
--
-- y :: Lens' Foo Bool
-- y f foo = (\y' -> foo {_y = y'}) <$> f (_y foo)
--
--
-- (If you don't want a lens to be generated for some field, don't prefix
-- it with “_”.)
--
-- If you want to create lenses for many types, you can do it all in one
-- place like this (of course, instead you just can use makeLenses
-- several times if you feel it would be more readable):
--
-- -- data Foo = ... -- data Bar = ... -- data Quux = ... -- -- concat <$> mapM makeLenses [''Foo, ''Bar, ''Quux] ---- -- When the data type has type parameters, it's possible for a lens to do -- a polymorphic update – i.e. change the type of the thing along with -- changing the type of the field. For instance, with this type -- --
-- data Foo a = Foo {
-- _x :: a,
-- _y :: Bool }
--
--
-- the following lenses would be generated:
--
-- -- x :: Lens (Foo a) (Foo b) a b -- y :: Lens' (Foo a) Bool ---- -- However, when there are several fields using the same type parameter, -- type-changing updates are no longer possible: -- --
-- data Foo a = Foo {
-- _x :: a,
-- _y :: a }
--
--
-- generates
--
-- -- x :: Lens' (Foo a) a -- y :: Lens' (Foo a) a ---- -- Finally, when the type has several constructors, some of fields may -- not be always present – for those, a Traversal is -- generated instead. For instance, in this example y can be -- present or absent: -- --
-- data FooBar
-- = Foo { _x :: Int, _y :: Bool }
-- | Bar { _x :: Int }
--
--
-- and the following accessors would be generated:
--
-- -- x :: Lens' FooBar Int -- y :: Traversal' FooBar Bool ---- -- So, to get _y, you'd have to either use (^?) if you're -- not sure it's there, or (^?!) if you're absolutely sure (and if -- you're wrong, you'll get an exception). Setting and updating -- _y can be done as usual. makeLenses :: Name -> DecsQ -- | Like makeLenses, but lets you choose your own names for lenses: -- --
-- data Foo = Foo {foo :: Int, bar :: Bool}
--
-- makeLensesFor [("foo", "fooLens"), ("bar", "_bar")] ''Foo
--
--
-- would create lenses called fooLens and _bar. This is
-- useful, for instance, when you don't want to prefix your fields with
-- underscores and want to prefix lenses with underscores instead.
--
-- If you give the same name to different fields, it will generate a
-- Traversal instead:
--
--
-- data Foo = Foo {slot1, slot2, slot3 :: Int}
--
-- makeLensesFor [("slot1", "slots"),
-- ("slot2", "slots"),
-- ("slot3", "slots")] ''Foo
--
--
-- would generate
--
-- -- slots :: Traversal' Foo Int -- slots f foo = Foo <$> f (slot1 foo) -- <*> f (slot2 foo) -- <*> f (slot3 foo) --makeLensesFor :: [(String, String)] -> Name -> DecsQ -- | Generate lenses with custom parameters. -- -- To see what exactly you can customise, look at the “Configuring lens -- rules” section. Usually you would build upon the lensRules -- configuration, which is used by makeLenses: -- --
-- makeLenses = makeLensesWith lensRules ---- -- Here's an example of generating lenses that would use lazy patterns: -- --
-- data Foo = Foo {_x, _y :: Int}
--
-- makeLensesWith (lensRules & generateLazyPatterns .~ True) ''Foo
--
--
-- When there are several modifications to the rules, the code looks
-- nicer when you use flip:
--
-- -- flip makeLensesWith ''Foo $ -- lensRules -- & generateLazyPatterns .~ True -- & generateSignatures .~ False --makeLensesWith :: LensRules -> Name -> DecsQ -- | Generate overloaded lenses. -- -- This lets you deal with several data types having same fields. For -- instance, let's say you have Foo and Bar, and both -- have a field named x. To avoid those fields clashing, you -- would have to use prefixes: -- --
-- data Foo a = Foo {
-- fooX :: Int,
-- fooY :: a }
--
-- data Bar = Bar {
-- barX :: Char }
--
--
-- However, if you use makeFields on both Foo and
-- Bar now, it would generate lenses called x and
-- y – and x would be able to access both fooX
-- and barX! This is done by generating a separate class for
-- each field, and making relevant types instances of that class:
--
-- -- class HasX s a | s -> a where -- x :: Lens' s a -- -- instance HasX (Foo a) Int where -- x :: Lens' (Foo a) Int -- x = ... -- -- instance HasX Bar Char where -- x :: Lens' Bar Char -- x = ... -- -- -- class HasY s a | s -> a where -- y :: Lens' s a -- -- instance HasY (Foo a) a where -- y :: Lens' (Foo a) a -- y = ... ---- -- (There's a minor drawback, though: you can't perform type-changing -- updates with these lenses.) -- -- If you only want to make lenses for some fields, you can prefix them -- with underscores – the rest would be untouched. If no fields are -- prefixed with underscores, lenses would be created for all fields. -- -- The prefix must be the same as the name of the name of the data type -- (not the constructor). If you don't like this behavior, use -- makeLensesWith abbreviatedFields – it allows -- any prefix (and even different prefixes). -- -- If you want to use makeFields on types declared in different -- modules, you can do it, but then you would have to export the -- Has* classes from one of the modules – makeFields -- creates a class if it's not in scope yet, so the class must be in -- scope or else there would be duplicate classes and you would get an -- “Ambiguous occurrence” error. -- -- Finally, makeFields is implemented as makeLensesWith -- camelCaseFields, so you can build on -- camelCaseFields if you want to customise behavior of -- makeFields. makeFields :: Name -> DecsQ -- | Generate overloaded lenses without ad-hoc classes; useful when there's -- a collection of fields that you want to make common for several types. -- -- Like makeFields, each lens is a member of a class. However, the -- classes are per-type and not per-field. Let's take the following type: -- --
-- data Person = Person {
-- _name :: String,
-- _age :: Double }
--
--
-- makeClassy would generate a single class with 3 methods:
--
-- -- class HasPerson c where -- person :: Lens' c Person -- -- age :: Lens' c Double -- age = person.age -- -- name :: Lens' c String -- name = person.name ---- -- And an instance: -- --
-- instance HasPerson Person where -- person = id -- -- name = ... -- age = ... ---- -- So, you can use name and age to refer to the -- _name and _age fields, as usual. However, the extra -- lens – person – allows you to do a kind of subtyping. Let's -- say that there's a type called Worker and every worker has -- the same fields that a person has, but also a job. If you -- were using makeFields, you'd do the following: -- --
-- data Worker = Worker {
-- _workerName :: String,
-- _workerAge :: Double,
-- _workerJob :: String }
--
--
-- However, with makeClassy you can say “every worker is a person”
-- in a more principled way:
--
--
-- data Worker = Worker {
-- _workerPerson :: Person,
-- _job :: String }
--
-- makeClassy ''Worker
--
-- instance HasPerson Worker where person = workerPerson
--
--
-- Now you can use age and name to access name/age of a
-- Worker, but you also can use person to “downgrade” a
-- Worker to a Person (and e.g. apply some
-- Person-specific function to it).
--
-- Unlike makeFields, makeClassy doesn't make use of
-- prefixes. _workerPerson could've just as well been named
-- _foobar.
--
-- makeClassy is implemented as makeLensesWith
-- classyRules, so you can build on classyRules if you
-- want to customise behavior of makeClassy.
makeClassy :: Name -> DecsQ
-- | Generate simple (monomorphic) lenses even when type-changing lenses
-- are possible – i.e. Lens' instead of Lens and
-- Traversal' instead of Traversal. Just in case, here's an
-- example of a situation when type-changing lenses would be normally
-- generated:
--
--
-- data Foo a = Foo { _foo :: a }
--
--
-- Generated lens:
--
-- -- foo :: Lens (Foo a) (Foo b) a b ---- -- Generated lens with simpleLenses turned on: -- --
-- foo :: Lens' (Foo a) a ---- -- This option is disabled by default. simpleLenses :: Lens' LensRules Bool -- | Supply type signatures for the generated lenses. -- -- This option is enabled by default. Disable it if you want to write the -- signature by yourself – for instance, if the signature should be more -- restricted, or if you want to write haddocks for the lens (as haddocks -- are attached to the signature and not to the definition). generateSignatures :: Lens' LensRules Bool -- | Generate “updateable” optics. When turned off, SimpleFolds will -- be generated instead of Traversals and SimpleGetters -- will be generated instead of Lenses. -- -- This option is enabled by default. Disabling it can be useful for -- types with invariants (also known as “types with smart constructors”) -- – if you generate updateable optics, anyone would be able to use them -- to break your invariants. generateUpdateableOptics :: Lens' LensRules Bool -- | Generate lenses using lazy pattern matches. This can allow fields of -- an undefined value to be initialized with lenses: -- --
-- data Foo = Foo {_x :: Int, _y :: Bool}
-- deriving Show
--
-- makeLensesWith (lensRules & generateLazyPatterns .~ True) ''Foo
--
--
--
-- >>> undefined & x .~ 8 & y .~ True
-- Foo {_x = 8, _y = True}
--
--
-- (Without generateLazyPatterns, the result would be just
-- undefined.)
--
-- This option is disabled by default. The downside of enabling it is
-- that it can lead to space-leaks and code-size/compile-time increases
-- when lenses are generated for large records.
--
-- When you have a lazy lens, you can get a strict lens from it by
-- composing with ($!):
--
-- -- strictLens = ($!) . lazyLens --generateLazyPatterns :: Lens' LensRules Bool -- | This lets you choose which fields would have lenses generated for them -- and how would those lenses be called. To do that, you provide a -- function that would take a field name and output a list (possibly -- empty) of lenses that should be generated for that field. -- -- Here's the full type of the function you have to provide: -- --
-- Name -> -- The datatype lenses are being generated for -- [Name] -> -- A list of all fields of the datatype -- Name -> -- The current field -- [DefName] -- A list of lens names ---- -- Most of the time you won't need the first 2 parameters, but sometimes -- they are useful – for instance, the list of all fields would be useful -- if you wanted to implement a slightly more complicated rule like “if -- some fields are prefixed with underscores, generate lenses for them, -- but if no fields are prefixed with underscores, generate lenses for -- all fields”. -- -- As an example, here's a function used by default. It strips “_” off -- the field name, lowercases the next character after “_”, and skips the -- field entirely if it doesn't start with “_”: -- --
-- \_ _ n -> -- case nameBase n of -- '_':x:xs -> [TopName (mkName (toLower x : xs))] -- _ -> [] ---- -- You can also generate classes (i.e. what makeFields does) by -- using MethodName className lensName instead of -- TopName lensName. lensField :: Lens' LensRules (Name -> [Name] -> Name -> [DefName]) -- | This lets you choose whether a class would be generated for the type -- itself (like makeClassy does). If so, you can choose the name -- of the class and the name of the type-specific lens. -- -- For makeLenses and makeFields this is just const -- Nothing. For makeClassy this function is defined like -- this: -- --
-- \n -> -- case nameBase n of -- x:xs -> Just (mkName (Has ++ x:xs), mkName (toLower x : xs)) -- [] -> Nothing --lensClass :: Lens' LensRules (Name -> Maybe (Name, Name)) -- | Decide whether generation of classes is allowed at all. -- -- If this is disabled, neither makeFields nor makeClassy -- would work, regardless of values of lensField or -- lensClass. On the other hand, if lensField and -- lensClass don't generate any classes, enabling this won't have -- any effect. createClass :: Lens' LensRules Bool -- | Lens rules used by default (i.e. in makeLenses): -- --
_fooBarBaz
_someX
someY
-- withSpawn buffer $ \(output, input) -> ... ---- -- withSpawn is exception-safe, since it uses bracket -- internally. withSpawn :: Ord p => ((Output (p, a), Input a) -> IO r) -> IO r -- | An exhaustible source of values -- -- recv returns Nothing if the source is exhausted newtype Input a Input :: STM (Maybe a) -> Input a [recv] :: Input a -> STM (Maybe a) -- | An exhaustible sink of values -- -- send returns False if the sink is exhausted newtype Output a Output :: (a -> STM Bool) -> Output a [send] :: Output a -> a -> STM Bool -- | Convert an Input to a Producer -- -- fromInput terminates when the Input is exhausted. fromInput :: forall (m :: Type -> Type) a. MonadIO m => Input a -> Producer' a m () -- | Convert an Output to a Consumer -- -- toOutput terminates when the Output is exhausted. toOutput :: forall (m :: Type -> Type) a. MonadIO m => Output a -> Consumer' a m () module Vgrep.Ansi.Type -- | A representattion of formatted Text. The attribute is usually a -- Monoid so that different formattings can be combined by nesting -- them. data Formatted attr -- | An empty block Empty :: Formatted attr -- | A bare (unformatted) text node Text :: !Int -> Text -> Formatted attr -- | Adds formatting to a block Format :: !Int -> attr -> Formatted attr -> Formatted attr -- | Concatenates several blocks of formatted text Cat :: !Int -> [Formatted attr] -> Formatted attr -- | Type alias for Text formatted with Attr from -- Graphics.Vty. type AnsiFormatted = Formatted Attr -- | Smart constructor for an empty Formatted text. emptyFormatted :: Formatted attr -- | Smart constructor for bare (unformatted) text. -- --
-- >>> bare "" -- Empty ---- --
-- >>> bare "Text" -- Text 4 "Text" --bare :: Text -> Formatted attr -- | Adds formatting to a Formatted text. The Eq and -- Monoid instances for attr are used to flatten -- redundant formattings. -- --
-- >>> format (Just ()) (format Nothing (bare "Text")) -- Format 4 (Just ()) (Text 4 "Text") ---- --
-- >>> format (Just ()) (format (Just ()) (bare "Text")) -- Format 4 (Just ()) (Text 4 "Text") ---- --
-- >>> format Nothing (bare "Text") -- Text 4 "Text" --format :: (Eq attr, Monoid attr) => attr -> Formatted attr -> Formatted attr -- | Concatenates pieces of Formatted text. Redundant formattings -- and blocks of equal formatting are fused together. cat :: (Eq attr, Monoid attr) => [Formatted attr] -> Formatted attr -- | Apply a function to each piece of text in the Formatted tree. -- --
-- >>> mapText T.toUpper (Cat 11 [Text 6 "Hello ", Format 5 () (Text 5 "World")]) -- Cat 11 [Text 6 "HELLO ",Format 5 () (Text 5 "WORLD")] --mapText :: (Text -> Text) -> Formatted a -> Formatted a -- | Like mapText, but passes the position of the text chunk to the -- function as well. Can be used for formatting text -- position-dependently, e.g. for expanding tabs to spaces. mapTextWithPos :: (Int -> Text -> Text) -> Formatted a -> Formatted a -- | Crops the text to a given length. If the text is already shorter than -- the desired width, it is returned as-is. takeFormatted :: Int -> Formatted a -> Formatted a -- | Drops a prefix of the given length. If the text is already shorter -- than the number of characters to be dropped, emptyFormatted is -- returned. dropFormatted :: Int -> Formatted a -> Formatted a -- | Pads the text to a given width. If the text is already longer than the -- desired width, it is returned as-is. padFormatted :: Int -> Char -> Formatted a -> Formatted a -- | Simplifies Formatted text by leaving out redundant empty bits, -- joining pieces of text with the same formatting, and flattening -- redundant applications of the same style. -- --
-- >>> emptyFormatted `fuse` bare "Text" -- Text 4 "Text" ---- --
-- >>> format (Just ()) (bare "Left") `fuse` format (Just ()) (bare "Right") -- Format 9 (Just ()) (Text 9 "LeftRight") --fuse :: (Eq attr, Semigroup attr) => Formatted attr -> Formatted attr -> Formatted attr instance GHC.Show.Show attr => GHC.Show.Show (Vgrep.Ansi.Type.Formatted attr) instance GHC.Classes.Eq attr => GHC.Classes.Eq (Vgrep.Ansi.Type.Formatted attr) instance GHC.Base.Functor Vgrep.Ansi.Type.Formatted instance (GHC.Classes.Eq attr, GHC.Base.Semigroup attr) => GHC.Base.Semigroup (Vgrep.Ansi.Type.Formatted attr) instance (GHC.Classes.Eq attr, GHC.Base.Semigroup attr) => GHC.Base.Monoid (Vgrep.Ansi.Type.Formatted attr) module Vgrep.Ansi.Parser -- | Directly parses ANSI formatted text using ansiFormatted. -- -- Parsing ANSI color codes: -- --
-- >>> parseAnsi "Hello \ESC[31mWorld\ESC[m!"
-- Cat 12 [Text 6 "Hello ",Format 5 (Attr {attrStyle = KeepCurrent, attrForeColor = SetTo (ISOColor 1), attrBackColor = KeepCurrent, attrURL = KeepCurrent}) (Text 5 "World"),Text 1 "!"]
--
--
-- More elaborate example with nested foreground and background colors:
--
--
-- >>> parseAnsi "\ESC[m\ESC[40mHello \ESC[31mWorld\ESC[39m!"
-- Cat 12 [Format 6 (Attr {attrStyle = KeepCurrent, attrForeColor = KeepCurrent, attrBackColor = SetTo (ISOColor 0), attrURL = KeepCurrent}) (Text 6 "Hello "),Format 5 (Attr {attrStyle = KeepCurrent, attrForeColor = SetTo (ISOColor 1), attrBackColor = SetTo (ISOColor 0), attrURL = KeepCurrent}) (Text 5 "World"),Format 1 (Attr {attrStyle = KeepCurrent, attrForeColor = KeepCurrent, attrBackColor = SetTo (ISOColor 0), attrURL = KeepCurrent}) (Text 1 "!")]
--
--
-- Some CSI sequences are ignored, since they are not supported by
-- Vty:
--
--
-- >>> parseAnsi "\ESC[A\ESC[B\ESC[31mfoo\ESC[1K\ESC[mbar"
-- Cat 6 [Format 3 (Attr {attrStyle = KeepCurrent, attrForeColor = SetTo (ISOColor 1), attrBackColor = KeepCurrent, attrURL = KeepCurrent}) (Text 3 "foo"),Text 3 "bar"]
--
--
-- Non-CSI sequences are not parsed, but included in the output:
--
--
-- >>> parseAnsi "\ESC]710;font\007foo\ESC[31mbar"
-- Cat 17 [Text 14 "\ESC]710;font\afoo",Format 3 (Attr {attrStyle = KeepCurrent, attrForeColor = SetTo (ISOColor 1), attrBackColor = KeepCurrent, attrURL = KeepCurrent}) (Text 3 "bar")]
--
parseAnsi :: Text -> AnsiFormatted
-- | Parser for ANSI formatted text. Recognized escape sequences are the
-- SGR (Select Graphic Rendition) sequences (ESC[…m) supported
-- by Attr. Unsupported SGR sequences and other CSI escape
-- sequences (ESC[…) are ignored. Other (non-CSI) escape
-- sequences are not parsed, and included in the output.
--
-- This parser does not fail, it will rather consume and return the
-- remaining input as unformatted text.
ansiFormatted :: Parser AnsiFormatted
-- | Parser for ANSI CSI escape sequences. Recognized escape sequences are
-- the SGR (Select Graphic Rendition) sequences (ESC[…m)
-- supported by Attr. Unsupported SGR sequences and other CSI
-- escape sequences (ESC[…) are ignored by returning id.
--
-- This parser fails when encountering any other (non-CSI) escape
-- sequence.
attrChange :: Parser (Attr -> Attr)
module Vgrep.Ansi.Vty.Attributes
-- | A display attribute defines the Color and Style of all the characters
-- rendered after the attribute is applied.
--
-- At most 256 colors, picked from a 240 and 16 color palette, are
-- possible for the background and foreground. The 240 colors and 16
-- colors are points in different palettes. See Color for more
-- information.
data Attr
-- | Combines two Attrs. This differs from mappend from the
-- Monoid instance of Attr in that Styles are
-- combined rather than overwritten.
combineStyles :: Attr -> Attr -> Attr
-- | Utilities for printing ANSI formatted text.
module Vgrep.Ansi
-- | Type alias for Text formatted with Attr from
-- Graphics.Vty.
type AnsiFormatted = Formatted Attr
-- | A representattion of formatted Text. The attribute is usually a
-- Monoid so that different formattings can be combined by nesting
-- them.
data Formatted attr
-- | Smart constructor for an empty Formatted text.
emptyFormatted :: Formatted attr
-- | Smart constructor for bare (unformatted) text.
--
-- -- >>> bare "" -- Empty ---- --
-- >>> bare "Text" -- Text 4 "Text" --bare :: Text -> Formatted attr -- | Adds formatting to a Formatted text. The Eq and -- Monoid instances for attr are used to flatten -- redundant formattings. -- --
-- >>> format (Just ()) (format Nothing (bare "Text")) -- Format 4 (Just ()) (Text 4 "Text") ---- --
-- >>> format (Just ()) (format (Just ()) (bare "Text")) -- Format 4 (Just ()) (Text 4 "Text") ---- --
-- >>> format Nothing (bare "Text") -- Text 4 "Text" --format :: (Eq attr, Monoid attr) => attr -> Formatted attr -> Formatted attr -- | Concatenates pieces of Formatted text. Redundant formattings -- and blocks of equal formatting are fused together. cat :: (Eq attr, Monoid attr) => [Formatted attr] -> Formatted attr -- | Apply a function to each piece of text in the Formatted tree. -- --
-- >>> mapText T.toUpper (Cat 11 [Text 6 "Hello ", Format 5 () (Text 5 "World")]) -- Cat 11 [Text 6 "HELLO ",Format 5 () (Text 5 "WORLD")] --mapText :: (Text -> Text) -> Formatted a -> Formatted a -- | Like mapText, but passes the position of the text chunk to the -- function as well. Can be used for formatting text -- position-dependently, e.g. for expanding tabs to spaces. mapTextWithPos :: (Int -> Text -> Text) -> Formatted a -> Formatted a -- | Crops the text to a given length. If the text is already shorter than -- the desired width, it is returned as-is. takeFormatted :: Int -> Formatted a -> Formatted a -- | Drops a prefix of the given length. If the text is already shorter -- than the number of characters to be dropped, emptyFormatted is -- returned. dropFormatted :: Int -> Formatted a -> Formatted a -- | Pads the text to a given width. If the text is already longer than the -- desired width, it is returned as-is. padFormatted :: Int -> Char -> Formatted a -> Formatted a -- | Converts ANSI formatted text to an Image. Nested formattings -- are combined with combineStyles. The given Attr is used -- as style for the root of the Formatted tree. -- --
-- >>> import Graphics.Vty.Image.Internal (Image (HorizText, attr))
--
-- >>> let HorizText { attr = attr } = renderAnsi Vty.defAttr (bare "Text")
--
-- >>> attr
-- Attr {attrStyle = Default, attrForeColor = Default, attrBackColor = Default, attrURL = Default}
--
renderAnsi :: Attr -> AnsiFormatted -> Image
-- | Strips away all formattings to plain Text.
stripAnsi :: Formatted a -> Text
module Vgrep.Command
data Command
-- | Display the pager full-screen
DisplayPagerOnly :: Command
-- | Display the results list full-screen
DisplayResultsOnly :: Command
-- | Split screen, focus on pager
SplitFocusPager :: Command
-- | Split screen, focus on results list
SplitFocusResults :: Command
-- | Scroll one line up in pager
PagerUp :: Command
-- | Scroll one line down in pager
PagerDown :: Command
-- | Scroll one page up in pager
PagerPageUp :: Command
-- | Scroll one page down in pager
PagerPageDown :: Command
-- | Scroll half a page up in pager
PagerHalfPageUp :: Command
-- | Scroll half a page down in pager
PagerHalfPageDown :: Command
-- | Scroll eight characters left in pager
PagerScrollLeft :: Command
-- | Scroll eight characters right in pager
PagerScrollRight :: Command
-- | Move to previous result
ResultsUp :: Command
-- | Move to next result
ResultsDown :: Command
-- | Move one page up in results list
ResultsPageUp :: Command
-- | Move one page down in results list
ResultsPageDown :: Command
-- | Move to previous result and update pager
PrevResult :: Command
-- | Move to next result and update pager
NextResult :: Command
-- | Update pager with currently selected result
PagerGotoResult :: Command
-- | Open file in external editor and jump to currently selected result
OpenFileInEditor :: Command
-- | Exit the application
Exit :: Command
-- | Treat keybinding as if not present, fall back to alternative binding
-- (used to override keybindings)
Unset :: Command
instance GHC.Generics.Generic Vgrep.Command.Command
instance GHC.Show.Show Vgrep.Command.Command
instance GHC.Classes.Eq Vgrep.Command.Command
-- | Basic definitions for Keys, Modifiers, and Chords
-- of Keys and Modifiers. We can read key Chords
-- from Graphics.Vty EvKey events using fromVty.
--
-- This module is intended for qualified import:
--
-- -- import qualified Vgrep.Key as Key ---- -- We define our own Key and Mod types rather than using -- Graphics.Vty's Key and Modifier, because it -- simplifies parsing (of keys like Space and Tab, which -- are represented as ' ' and 't' in -- Graphics.Vty), and because a Set of Mods is -- simpler to check for equality than a list of Modifiers. module Vgrep.Key -- | A chord of keys and modifiers pressed simultaneously. data Chord Chord :: Set Mod -> Key -> Chord data Key Char :: Char -> Key Space :: Key Esc :: Key Backspace :: Key Enter :: Key Del :: Key Tab :: Key Left :: Key Right :: Key Up :: Key Down :: Key Home :: Key End :: Key PageUp :: Key PageDown :: Key data Mod Ctrl :: Mod Meta :: Mod Shift :: Mod -- | Reads the key and modifiers from an Event. Non-key events and -- events with unknown keys are mapped to Nothing. fromVty :: Event -> Maybe Chord -- | Build a Chord from a single Key key :: Key -> Chord -- | Add a Modifier to a Chord withModifier :: Chord -> Mod -> Chord instance GHC.Generics.Generic Vgrep.Key.Key instance GHC.Read.Read Vgrep.Key.Key instance GHC.Show.Show Vgrep.Key.Key instance GHC.Classes.Ord Vgrep.Key.Key instance GHC.Classes.Eq Vgrep.Key.Key instance GHC.Generics.Generic Vgrep.Key.Mod instance GHC.Show.Show Vgrep.Key.Mod instance GHC.Classes.Ord Vgrep.Key.Mod instance GHC.Classes.Eq Vgrep.Key.Mod instance GHC.Generics.Generic Vgrep.Key.Chord instance GHC.Show.Show Vgrep.Key.Chord instance GHC.Classes.Ord Vgrep.Key.Chord instance GHC.Classes.Eq Vgrep.Key.Chord module Vgrep.KeybindingMap newtype KeybindingMap KeybindingMap :: Map Chord Command -> KeybindingMap [unKeybindingMap] :: KeybindingMap -> Map Chord Command lookup :: Chord -> KeybindingMap -> Maybe Command fromList :: [(Chord, Command)] -> KeybindingMap instance GHC.Base.Monoid Vgrep.KeybindingMap.KeybindingMap instance GHC.Base.Semigroup Vgrep.KeybindingMap.KeybindingMap instance GHC.Classes.Eq Vgrep.KeybindingMap.KeybindingMap instance GHC.Show.Show Vgrep.KeybindingMap.KeybindingMap module Vgrep.Environment.Config.Monoid -- | A Monoid for reading partial configs. The ConfigMonoid -- can be converted to an actual Config using -- fromConfigMonoid. -- -- The Monoid consists mostly of 'First a' values, so the most important -- config (the one that overrides all the others) should be read first. data ConfigMonoid ConfigMonoid :: ColorsMonoid -> First Int -> First String -> KeybindingsMonoid -> ConfigMonoid [_mcolors] :: ConfigMonoid -> ColorsMonoid [_mtabstop] :: ConfigMonoid -> First Int [_meditor] :: ConfigMonoid -> First String [_mkeybindings] :: ConfigMonoid -> KeybindingsMonoid -- | A Monoid for reading partial Colors configurations. -- -- Note that the attributes are not merged, but overridden: -- --
-- >>> import Graphics.Vty.Attributes
--
-- >>> let leftStyle = defAttr `withStyle` standout
--
-- >>> let rightStyle = defAttr `withForeColor` black
--
-- >>> let l = mempty { _mnormal = First (Just leftStyle)}
--
-- >>> let r = mempty { _mnormal = First (Just rightStyle)}
--
-- >>> _mnormal (l <> r) == First (Just (leftStyle <> rightStyle))
-- False
--
-- >>> _mnormal (l <> r) == First (Just leftStyle)
-- True
--
data ColorsMonoid
ColorsMonoid :: First Attr -> First Attr -> First Attr -> First Attr -> First Attr -> First Attr -> ColorsMonoid
[_mlineNumbers] :: ColorsMonoid -> First Attr
[_mlineNumbersHl] :: ColorsMonoid -> First Attr
[_mnormal] :: ColorsMonoid -> First Attr
[_mnormalHl] :: ColorsMonoid -> First Attr
[_mfileHeaders] :: ColorsMonoid -> First Attr
[_mselected] :: ColorsMonoid -> First Attr
-- | A Monoid for reading a partial Keybindings
-- configuration.
--
-- Mappings are combined using left-biased union:
--
--
-- >>> let l = Just (KeybindingMap (fromList [(Key.Chord mempty Key.Down, ResultsDown), (Key.Chord mempty Key.Up, ResultsUp)]))
--
-- >>> let r = Just (KeybindingMap (fromList [(Key.Chord mempty Key.Down, PagerDown)]))
--
-- >>> l <> r
-- Just (KeybindingMap {unKeybindingMap = fromList [(Chord (fromList []) Up,ResultsUp),(Chord (fromList []) Down,ResultsDown)]})
--
-- >>> r <> l
-- Just (KeybindingMap {unKeybindingMap = fromList [(Chord (fromList []) Up,ResultsUp),(Chord (fromList []) Down,PagerDown)]})
--
--
-- In particular, Just (fromList []) (declaring an
-- empty list of mappings) and Nothing (not declaring
-- anything) are equivalent, given that there are already default
-- mappings:
--
-- -- >>> l <> Just (KeybindingMap (fromList [])) == l <> Nothing -- True ---- -- This means that new keybindings override the previous ones if they -- collide, otherwise they are simply added. To remove a keybinding, it -- has to be mapped to Unset explicitly. data KeybindingsMonoid KeybindingsMonoid :: Maybe KeybindingMap -> Maybe KeybindingMap -> Maybe KeybindingMap -> KeybindingsMonoid [_mresultsKeybindings] :: KeybindingsMonoid -> Maybe KeybindingMap [_mpagerKeybindings] :: KeybindingsMonoid -> Maybe KeybindingMap [_mglobalKeybindings] :: KeybindingsMonoid -> Maybe KeybindingMap instance GHC.Generics.Generic Vgrep.Environment.Config.Monoid.ColorsMonoid instance GHC.Show.Show Vgrep.Environment.Config.Monoid.ColorsMonoid instance GHC.Classes.Eq Vgrep.Environment.Config.Monoid.ColorsMonoid instance GHC.Generics.Generic Vgrep.Environment.Config.Monoid.KeybindingsMonoid instance GHC.Show.Show Vgrep.Environment.Config.Monoid.KeybindingsMonoid instance GHC.Classes.Eq Vgrep.Environment.Config.Monoid.KeybindingsMonoid instance GHC.Generics.Generic Vgrep.Environment.Config.Monoid.ConfigMonoid instance GHC.Show.Show Vgrep.Environment.Config.Monoid.ConfigMonoid instance GHC.Classes.Eq Vgrep.Environment.Config.Monoid.ConfigMonoid instance GHC.Base.Semigroup Vgrep.Environment.Config.Monoid.ConfigMonoid instance GHC.Base.Monoid Vgrep.Environment.Config.Monoid.ConfigMonoid instance GHC.Base.Semigroup Vgrep.Environment.Config.Monoid.KeybindingsMonoid instance GHC.Base.Monoid Vgrep.Environment.Config.Monoid.KeybindingsMonoid instance GHC.Base.Semigroup Vgrep.Environment.Config.Monoid.ColorsMonoid instance GHC.Base.Monoid Vgrep.Environment.Config.Monoid.ColorsMonoid module Vgrep.Environment.Config.Sources.File -- | Reads the configuration from a JSON or YAML file. The file should be -- located in one of the following places: -- --
-- >>> import qualified Vgrep.Environment.Config as C -- -- >>> Right config <- decodeFileEither "config.yaml.example" :: IO (Either ParseException ConfigMonoid) -- -- >>> C.fromConfigMonoid config == C.defaultConfig -- True ---- -- Example YAML config file for defaultConfig: -- --
-- colors:
-- line-numbers:
-- fore-color: blue
-- line-numbers-hl:
-- fore-color: blue
-- style: bold
-- normal: {}
-- normal-hl:
-- style: bold
-- file-headers:
-- back-color: green
-- selected:
-- style: standout
-- tabstop: 8
-- editor: "vi"
--
--
-- Example JSON file for the same config:
--
--
-- {
-- "colors": {
-- "line-numbers" : {
-- "fore-color": "blue"
-- },
-- "line-numbers-hl": {
-- "fore-color": "blue",
-- "style": "bold"
-- },
-- "normal": {},
-- "normal-hl": {
-- "style": "bold"
-- },
-- "file-headers": {
-- "back-color": "green"
-- },
-- "selected": {
-- "style": "standout"
-- }
-- },
-- "tabstop": 8,
-- "editor": "vi"
-- }
--
--
-- The JSON/YAML keys correspond to the lenses in
-- Vgrep.Environment.Config, the values for Color and
-- Style can be obtained from the corresponding predefined
-- constants in Graphics.Vty.Attributes.
configFromFile :: MonadIO io => io ConfigMonoid
-- | A JSON-parsable data type for Attr.
--
-- JSON example:
--
--
-- >>> decodeEither' "{\"fore-color\": \"black\", \"style\": \"standout\"}" :: Either ParseException Attr
-- Right (Attr {foreColor = Just Black, backColor = Nothing, style = Just Standout})
--
--
-- JSON example without quotes: >>> decodeEither' "{fore-color:
-- black, style: standout}" :: Either ParseException Attr Right (Attr
-- {foreColor = Just Black, backColor = Nothing, style = Just Standout})
--
-- YAML example:
--
--
-- >>> :{
--
-- >>> decodeEither'
--
-- >>> $ "fore-color: \"blue\"\n"
--
-- >>> <> "back-color: \"bright-blue\"\n"
--
-- >>> <> "style: \"reverse-video\"\n"
--
-- >>> :: Either ParseException Attr
--
-- >>> :}
-- Right (Attr {foreColor = Just Blue, backColor = Just BrightBlue, style = Just ReverseVideo})
--
--
-- YAML example without quotes:
--
--
-- >>> :{
--
-- >>> decodeEither'
--
-- >>> $ "fore-color: blue\n"
--
-- >>> <> "back-color: bright-blue\n"
--
-- >>> <> "style: reverse-video\n"
--
-- >>> :: Either ParseException Attr
--
-- >>> :}
-- Right (Attr {foreColor = Just Blue, backColor = Just BrightBlue, style = Just ReverseVideo})
--
--
-- An empty JSON/YAML object yields the default colors:
--
--
-- >>> decodeEither' "{}" :: Either ParseException Attr
-- Right (Attr {foreColor = Nothing, backColor = Nothing, style = Nothing})
--
data Attr
-- | A JSON-parsable data type for Color.
--
-- -- >>> decodeEither' "[\"black\",\"red\",\"bright-black\"]" :: Either ParseException [Color] -- Right [Black,Red,BrightBlack] ---- -- Also works without quotes: -- --
-- >>> decodeEither' "[black,red,bright-black]" :: Either ParseException [Color] -- Right [Black,Red,BrightBlack] ---- -- Fails with error message if the Color cannot be parsed: -- --
-- >>> isLeft (decodeEither' "foo" :: Either ParseException Color) -- True --data Color -- | A JSON-parsable data type for Style. -- --
-- >>> decodeEither' "[\"standout\", \"underline\", \"bold\"]" :: Either ParseException [Style] -- Right [Standout,Underline,Bold] ---- -- Also works without quotes: -- --
-- >>> decodeEither' "[standout, underline, bold]" :: Either ParseException [Style] -- Right [Standout,Underline,Bold] ---- -- Fails with error message if the Style cannot be parsed: -- --
-- >>> isLeft (decodeEither' "foo" :: Either ParseException Color) -- True --data Style instance GHC.Generics.Generic Vgrep.Environment.Config.Sources.File.Color instance GHC.Show.Show Vgrep.Environment.Config.Sources.File.Color instance GHC.Classes.Eq Vgrep.Environment.Config.Sources.File.Color instance GHC.Generics.Generic Vgrep.Environment.Config.Sources.File.Style instance GHC.Show.Show Vgrep.Environment.Config.Sources.File.Style instance GHC.Classes.Eq Vgrep.Environment.Config.Sources.File.Style instance GHC.Generics.Generic Vgrep.Environment.Config.Sources.File.Attr instance GHC.Show.Show Vgrep.Environment.Config.Sources.File.Attr instance GHC.Classes.Eq Vgrep.Environment.Config.Sources.File.Attr instance Data.Aeson.Types.FromJSON.FromJSON Vgrep.Environment.Config.Sources.File.Attr instance Data.Aeson.Types.FromJSON.FromJSON Vgrep.Environment.Config.Sources.File.Style instance Data.Aeson.Types.FromJSON.FromJSON Vgrep.Environment.Config.Sources.File.Color instance Data.Aeson.Types.FromJSON.FromJSON Vgrep.Environment.Config.Monoid.ConfigMonoid instance Data.Aeson.Types.FromJSON.FromJSON Vgrep.Environment.Config.Monoid.ColorsMonoid instance Data.Aeson.Types.FromJSON.FromJSON Graphics.Vty.Attributes.Attr instance Data.Aeson.Types.FromJSON.FromJSON Vgrep.Environment.Config.Monoid.KeybindingsMonoid instance Data.Aeson.Types.FromJSON.FromJSON Vgrep.Command.Command instance Data.Aeson.Types.FromJSON.FromJSON Vgrep.KeybindingMap.KeybindingMap module Vgrep.Environment.Config.Sources.Env -- | Determines the ConfigMonoid value for _editor -- (_meditor) from the environment variable $EDITOR. editorConfigFromEnv :: MonadIO io => io ConfigMonoid module Vgrep.Environment.Config.Sources -- | Determines the ConfigMonoid value for _editor -- (_meditor) from the environment variable $EDITOR. editorConfigFromEnv :: MonadIO io => io ConfigMonoid -- | Reads the configuration from a JSON or YAML file. The file should be -- located in one of the following places: -- --
-- >>> import qualified Vgrep.Environment.Config as C -- -- >>> Right config <- decodeFileEither "config.yaml.example" :: IO (Either ParseException ConfigMonoid) -- -- >>> C.fromConfigMonoid config == C.defaultConfig -- True ---- -- Example YAML config file for defaultConfig: -- --
-- colors:
-- line-numbers:
-- fore-color: blue
-- line-numbers-hl:
-- fore-color: blue
-- style: bold
-- normal: {}
-- normal-hl:
-- style: bold
-- file-headers:
-- back-color: green
-- selected:
-- style: standout
-- tabstop: 8
-- editor: "vi"
--
--
-- Example JSON file for the same config:
--
--
-- {
-- "colors": {
-- "line-numbers" : {
-- "fore-color": "blue"
-- },
-- "line-numbers-hl": {
-- "fore-color": "blue",
-- "style": "bold"
-- },
-- "normal": {},
-- "normal-hl": {
-- "style": "bold"
-- },
-- "file-headers": {
-- "back-color": "green"
-- },
-- "selected": {
-- "style": "standout"
-- }
-- },
-- "tabstop": 8,
-- "editor": "vi"
-- }
--
--
-- The JSON/YAML keys correspond to the lenses in
-- Vgrep.Environment.Config, the values for Color and
-- Style can be obtained from the corresponding predefined
-- constants in Graphics.Vty.Attributes.
configFromFile :: MonadIO io => io ConfigMonoid
module Vgrep.Environment.Config
data Config
Config :: Colors -> Int -> String -> Keybindings -> Config
-- | Color configuration
[_colors] :: Config -> Colors
-- | Tabstop width (default: 8)
[_tabstop] :: Config -> Int
-- | Executable for e key (default: environment variable
-- $EDITOR, or vi if $EDITOR is not set)
[_editor] :: Config -> String
[_keybindings] :: Config -> Keybindings
data Colors
Colors :: Attr -> Attr -> Attr -> Attr -> Attr -> Attr -> Colors
-- | Line numbers (default: blue)
[_lineNumbers] :: Colors -> Attr
-- | Highlighted line numbers (default: bold blue)
[_lineNumbersHl] :: Colors -> Attr
-- | Normal text (default: terminal default)
[_normal] :: Colors -> Attr
-- | Highlighted text (default: bold)
[_normalHl] :: Colors -> Attr
-- | File names in results view (default: terminal default color on green
-- background)
[_fileHeaders] :: Colors -> Attr
-- | Selected entry (default: terminal default, inverted)
[_selected] :: Colors -> Attr
data Keybindings
Keybindings :: KeybindingMap -> KeybindingMap -> KeybindingMap -> Keybindings
-- | Keybindings in effect when results list is focused.
[_resultsKeybindings] :: Keybindings -> KeybindingMap
-- | Keybindings in effect when pager is focused.
[_pagerKeybindings] :: Keybindings -> KeybindingMap
-- | Global keybindings are in effect both for pager and results list, but
-- can be overridden by either one.
[_globalKeybindings] :: Keybindings -> KeybindingMap
tabstop :: Lens' Config Int
keybindings :: Lens' Config Keybindings
editor :: Lens' Config String
colors :: Lens' Config Colors
selected :: Lens' Colors Attr
normalHl :: Lens' Colors Attr
normal :: Lens' Colors Attr
lineNumbersHl :: Lens' Colors Attr
lineNumbers :: Lens' Colors Attr
fileHeaders :: Lens' Colors Attr
resultsKeybindings :: Lens' Keybindings KeybindingMap
pagerKeybindings :: Lens' Keybindings KeybindingMap
globalKeybindings :: Lens' Keybindings KeybindingMap
-- | Convert a ConfigMonoid to a Config. Missing
-- (mempty) values in the ConfigMonoid are
-- supplied from the defaultConfig.
fromConfigMonoid :: ConfigMonoid -> Config
-- | Convert a ColorsMonoid to a Colors config.
fromColorsMonoid :: ColorsMonoid -> Colors
fromFirst :: a -> First a -> a
fromKeybindingsMonoid :: KeybindingsMonoid -> Keybindings
defaultConfig :: Config
defaultColors :: Colors
defaultKeybindings :: Keybindings
-- | Gathers ConfigMonoids from various sources and builds a
-- Config based on the defaultConfig:
--
-- -- fromList [] == empty -- fromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")] -- fromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")] --fromList :: Ord k => [(k, a)] -> Map k a instance GHC.Base.Semigroup (Vgrep.Event.Next a) instance GHC.Base.Monoid (Vgrep.Event.Next a) instance GHC.Base.Functor Vgrep.Event.Next instance GHC.Base.Semigroup Vgrep.Event.Redraw instance GHC.Base.Monoid Vgrep.Event.Redraw module Vgrep.Results newtype File File :: Text -> File [_fileName] :: File -> Text fileName :: Lens' File Text data LineReference LineReference :: Maybe Int -> AnsiFormatted -> LineReference [_lineNumber] :: LineReference -> Maybe Int [_lineText] :: LineReference -> AnsiFormatted lineNumber :: Lens' LineReference (Maybe Int) lineText :: Lens' LineReference AnsiFormatted data FileLineReference FileLineReference :: File -> LineReference -> FileLineReference [_file] :: FileLineReference -> File [_lineReference] :: FileLineReference -> LineReference file :: Lens' FileLineReference File lineReference :: Lens' FileLineReference LineReference instance GHC.Show.Show Vgrep.Results.FileLineReference instance GHC.Classes.Eq Vgrep.Results.FileLineReference instance GHC.Show.Show Vgrep.Results.LineReference instance GHC.Classes.Eq Vgrep.Results.LineReference instance GHC.Show.Show Vgrep.Results.File instance GHC.Classes.Eq Vgrep.Results.File module Vgrep.Parser -- | Parses lines of Text, skipping lines that are not valid -- grep output. parseGrepOutput :: [Text] -> [FileLineReference] -- | Parses a line of grep output. Returns Nothing if the -- line cannot be parsed. -- -- The output should consist of a file name, line number and the content, -- separated by colons: -- --
-- >>> parseLine "path/to/file:123:foobar"
-- Just (FileLineReference {_file = File {_fileName = "path/to/file"}, _lineReference = LineReference {_lineNumber = Just 123, _lineText = Text 6 "foobar"}})
--
--
-- Omitting the line number still produces valid output:
--
--
-- >>> parseLine "path/to/file:foobar"
-- Just (FileLineReference {_file = File {_fileName = "path/to/file"}, _lineReference = LineReference {_lineNumber = Nothing, _lineText = Text 6 "foobar"}})
--
--
-- However, an file name must be present:
--
-- -- >>> parseLine "foobar" -- Nothing ---- -- ANSI escape codes in the line text are parsed correctly: -- --
-- >>> parseLine "path/to/file:foo\ESC[31mbar\ESC[mbaz"
-- Just (FileLineReference {_file = File {_fileName = "path/to/file"}, _lineReference = LineReference {_lineNumber = Nothing, _lineText = Cat 9 [Text 3 "foo",Format 3 (Attr {attrStyle = KeepCurrent, attrForeColor = SetTo (ISOColor 1), attrBackColor = KeepCurrent, attrURL = KeepCurrent}) (Text 3 "bar"),Text 3 "baz"]}})
--
parseLine :: Text -> Maybe FileLineReference
data FileLineReference
-- | Utilities for invoking grep
module Vgrep.System.Grep
-- | Takes a Text stream and runs it through a grep
-- process, returning a stream of results. The original command line
-- arguments are passed to the process.
grep :: Producer Text IO () -> Producer Text IO ()
-- | Like grep, but if the input is not prefixed with a file and
-- line number, i. e. is not valid grep -nH output, then adds
-- -nH (-n: with line number, -H: with file
-- name) to the grep command line arguments.
grepForApp :: Producer Text IO () -> Producer Text IO ()
-- | Invokes grep -nH -rI (-n: with line number,
-- -H: with file name, -r: recursive, -I:
-- ignore binary files) and returns the results as a stream. More
-- arguments (e. g. pattern and directory) are taken from the command
-- line.
recursiveGrep :: Producer Text IO ()
grepVersion :: Producer Text IO ()
module Vgrep.Text
-- | Expand a list of lines
expandForDisplay :: (Functor f, MonadReader Environment m) => f Text -> m (f Text)
-- | Expand a single line
expandLineForDisplay :: MonadReader Environment m => Text -> m Text
-- | Expand an ANSI formatted line
expandFormattedLine :: MonadReader Environment m => Formatted a -> m (Formatted a)
-- | The VgrepT monad transformer allows reading from the
-- Environment and changing the state of the App or a
-- Widget.
module Vgrep.Type
-- | The VgrepT monad transformer is parameterized over the state
-- s of a Widget or an App.
data VgrepT s m a
type Vgrep s = VgrepT s Identity
-- | Lift a monadic action to VgrepT.
mkVgrepT :: Monad m => (s -> Environment -> m (a, s)) -> VgrepT s m a
-- | Pass an initial state and an Environment and reduce a
-- VgrepT action to an action in the base monad.
runVgrepT :: Monad m => VgrepT s m a -> s -> Environment -> m (a, s)
-- | The Environment of VgrepT is not stateful, however it
-- can be modified globally. An example is resizing the application by
-- changing the display bounds.
modifyEnvironment :: Monad m => (Environment -> Environment) -> VgrepT s m ()
-- | A version of bracket where the action is lifted to
-- VgrepT.
vgrepBracket :: IO a -> (a -> IO c) -> (a -> VgrepT s IO b) -> VgrepT s IO b
-- | Lift a computation from the argument monad to the constructed monad.
lift :: (MonadTrans t, Monad m) => m a -> t m a
-- | Lift a monad morphism from m to n into a monad
-- morphism from (t m) to (t n)
--
-- The first argument to hoist must be a monad morphism, even
-- though the type system does not enforce this
hoist :: forall m n (b :: k). (MFunctor t, Monad m) => (forall a. () => m a -> n a) -> t m b -> t n b
-- | Monadic state transformer.
--
-- Maps an old state to a new state inside a state monad. The old state
-- is thrown away.
--
-- -- Main> :t modify ((+1) :: Int -> Int) -- modify (...) :: (MonadState Int a) => a () ---- -- This says that modify (+1) acts over any Monad that is a -- member of the MonadState class, with an Int state. modify :: MonadState s m => (s -> s) -> m () -- | See examples in Control.Monad.Reader. Note, the partially -- applied function type (->) r is a simple reader monad. See -- the instance declaration below. class Monad m => MonadReader r (m :: Type -> Type) | m -> r -- | Retrieves the monad environment. ask :: MonadReader r m => m r -- | Executes a computation in a modified environment. local :: MonadReader r m => (r -> r) -> m a -> m a -- | Minimal definition is either both of get and put or -- just state class Monad m => MonadState s (m :: Type -> Type) | m -> s -- | Return the state from the internals of the monad. get :: MonadState s m => m s -- | Replace the state inside the monad. put :: MonadState s m => s -> m () instance Control.Monad.IO.Class.MonadIO m => Control.Monad.IO.Class.MonadIO (Vgrep.Type.VgrepT s m) instance GHC.Base.Monad m => GHC.Base.Monad (Vgrep.Type.VgrepT s m) instance GHC.Base.Monad m => GHC.Base.Applicative (Vgrep.Type.VgrepT s m) instance GHC.Base.Functor m => GHC.Base.Functor (Vgrep.Type.VgrepT s m) instance GHC.Base.Monad m => Control.Monad.Reader.Class.MonadReader Vgrep.Environment.Environment (Vgrep.Type.VgrepT s m) instance GHC.Base.Monad m => Control.Monad.State.Class.MonadState s (Vgrep.Type.VgrepT s m) instance Control.Monad.Trans.Class.MonadTrans (Vgrep.Type.VgrepT s) instance Control.Monad.Morph.MFunctor (Vgrep.Type.VgrepT s) instance GHC.Base.Monad m => Lens.Micro.Mtl.Internal.Zoom (Vgrep.Type.VgrepT s m) (Vgrep.Type.VgrepT t m) s t module Vgrep.App.Internal -- | User events do have higher priority than System events, -- so that the application stays responsive even in case of event queue -- congestion. data EventPriority User :: EventPriority System :: EventPriority -- | We need the viewport in order to initialize the app, which in turn -- will start Vty. To resolve this circular dependency, we start -- once Vty in order to determine the display viewport, and shut -- it down again immediately. viewportHack :: IO Viewport -- | Spawns a thread parallel to the action that listens to Vty -- events and redirects them to the Consumer. withEvThread :: Consumer Event IO () -> Vty -> VgrepT s IO a -> VgrepT s IO a -- | Passes a Vty instance to the action and shuts it down properly -- after the action finishes. The inputFd and outputFd -- handles are connected to /dev/tty (see tty). withVty :: (Vty -> IO a) -> IO a -- | Like withVty, but lifted to VgrepT s IO. withVgrepVty :: (Vty -> VgrepT s IO a) -> VgrepT s IO a -- | Passes two file descriptors for read and write access to -- /dev/tty to the action, and closes them after the action has -- finished. withTty :: (Fd -> IO a) -> IO a -- | Like withTty, but lifted to VgrepT s IO. withVgrepTty :: (Fd -> VgrepT s IO a) -> VgrepT s IO a -- | Opens /dev/tty in Read/Write mode. Should be connected to the -- stdin and stdout of a GUI process (e. g. -- Vty). tty :: IO Fd instance GHC.Enum.Enum Vgrep.App.Internal.EventPriority instance GHC.Classes.Ord Vgrep.App.Internal.EventPriority instance GHC.Classes.Eq Vgrep.App.Internal.EventPriority module Vgrep.App -- | The App type is parameterized over the type e of -- events it handles and the type s of its state. data App e s App :: (forall m. MonadIO m => m s) -> (Event -> e) -> (forall m. MonadIO m => e -> Environment -> s -> Next (VgrepT s m Redraw)) -> (forall m. Monad m => VgrepT s m Picture) -> App e s -- | Creates the initial state for the app. [initialize] :: App e s -> forall m. MonadIO m => m s -- | How to convert an external Event to the App's event [liftEvent] :: App e s -> Event -> e -- | Handles an event, possibly modifying the App's state. -- --
-- handleEvent e s = case e of -- EvKey KEnter [] -> Continue (pure Unchanged) -- -- Handles the Enter key, but does nothing. -- -- EvKey KUp [] -> Continue (pure Redraw) -- -- Handles the Up key and triggers a redraw. -- -- _otherwise -> Skip -- -- Does not handle the event, so other handlers may be invoked. --[handleEvent] :: App e s -> forall m. MonadIO m => e -> Environment -> s -> Next (VgrepT s m Redraw) -- | Creates a Picture to be displayed. May modify the App's state -- (e. g. for resizing). [render] :: App e s -> forall m. Monad m => VgrepT s m Picture -- | Runs runs the event loop until an Interrupt -- Halt is encountered. Events handled with -- Interrupt (Suspend action) will shut down -- Vty, run the action (e. g. invoking an external editor), and -- start Vty again. runApp :: App e s -> Config -> Producer e IO () -> IO s -- | Like runApp, but does not return the final state. runApp_ :: App e s -> Config -> Producer e IO () -> IO () module Vgrep.Widget.HorizontalSplit.Internal -- | The internal state of the split-view widget. Tracks the state of both -- child widgets and the current layout. data HSplit s t HSplit :: s -> t -> Layout -> HSplit s t -- | State of the left widget [_leftWidget] :: HSplit s t -> s -- | State of the right widget [_rightWidget] :: HSplit s t -> t -- | Current layout [_layout] :: HSplit s t -> Layout data Layout LeftOnly :: Layout RightOnly :: Layout Split :: Focus -> Rational -> Layout data Focus FocusLeft :: Focus FocusRight :: Focus leftWidget :: forall s_aF0c t_aF0d s_aF2u. Lens (HSplit s_aF0c t_aF0d) (HSplit s_aF2u t_aF0d) s_aF0c s_aF2u rightWidget :: forall s_aF0c t_aF0d t_aF2v. Lens (HSplit s_aF0c t_aF0d) (HSplit s_aF0c t_aF2v) t_aF0d t_aF2v layout :: forall s_aF0c t_aF0d. Lens' (HSplit s_aF0c t_aF0d) Layout -- | The currently focused child widget -- --
-- >>> view currentWidget $ HSplit { _leftWidget = "foo", _layout = LeftOnly }
-- Left "foo"
--
currentWidget :: Lens' (HSplit s t) (Either s t)
-- | Traverses the left widget if focused
--
--
-- >>> has leftWidgetFocused $ HSplit { _layout = LeftOnly }
-- True
--
--
--
-- >>> has leftWidgetFocused $ HSplit { _layout = RightOnly }
-- False
--
--
--
-- >>> has leftWidgetFocused $ HSplit { _layout = Split FocusLeft (1 % 2) }
-- True
--
leftWidgetFocused :: Traversal' (HSplit s t) s
-- | Traverses the right widget if focused
--
--
-- >>> has rightWidgetFocused $ HSplit { _layout = RightOnly }
-- True
--
--
--
-- >>> has rightWidgetFocused $ HSplit { _layout = LeftOnly }
-- False
--
--
--
-- >>> has rightWidgetFocused $ HSplit { _layout = Split FocusRight (1 % 2) }
-- True
--
rightWidgetFocused :: Traversal' (HSplit s t) t
-- | Forms the ratio of two integral numbers.
(%) :: Integral a => a -> a -> Ratio a
infixl 7 %
instance GHC.Classes.Eq Vgrep.Widget.HorizontalSplit.Internal.Focus
instance GHC.Classes.Eq Vgrep.Widget.HorizontalSplit.Internal.Layout
module Vgrep.Widget.Pager.Internal
-- | Keeps track of the lines of text to display, the current scroll
-- positions, and the set of highlighted line numbers.
data Pager
Pager :: Int -> IntMap AnsiFormatted -> Seq Text -> Seq Text -> Pager
-- | The current column offset for horizontal scrolling
[_column] :: Pager -> Int
-- | Set of line numbers that are highlighted (i.e. they contain matches)
[_highlighted] :: Pager -> IntMap AnsiFormatted
-- | Zipper: Lines above the screen
[_above] :: Pager -> Seq Text
-- | Zipper: Lines on screen and below
[_visible] :: Pager -> Seq Text
-- | The number of invisible lines above the screen
position :: Getter Pager Int
column :: Lens' Pager Int
above :: Lens' Pager (Seq Text)
visible :: Lens' Pager (Seq Text)
highlighted :: Lens' Pager (IntMap AnsiFormatted)
instance GHC.Show.Show Vgrep.Widget.Pager.Internal.Pager
instance GHC.Classes.Eq Vgrep.Widget.Pager.Internal.Pager
module Vgrep.Widget.Results.Internal
-- | Results widget state
data Results
-- | The results list is empty
EmptyResults :: Results
-- | The structure of the Results buffer is a double Zipper:
--
-- -- widgetAction :: VgrepT s m Redraw --data Widget s Widget :: s -> (forall m. Monad m => VgrepT s m Image) -> Widget s -- | The initial state of the widget [initialize] :: Widget s -> s -- | Generate a renderable Image from the widget state. The state -- can be modified (e. g. for resizing). [draw] :: Widget s -> forall m. Monad m => VgrepT s m Image data Redraw -- | Indicates that the state has been changed visibly, so the screen -- should be refreshed. Redraw :: Redraw -- | The state has not changed or the change would not be visible, so -- refreshing the screen is not required. Unchanged :: Redraw -- | The type of action to be performed on an event. data Next a -- | Do not handle the event (fall-through to other event handlers) Skip :: Next a -- | Handle the event by performing an action Continue :: a -> Next a -- | Interrupt the application Interrupt :: Interrupt -> Next a module Vgrep.Widget.Results -- | The results widget displays a list of lines with line numbers, grouped -- by files. -- --
-- scroll (-1) -- scroll one line up -- scroll 1 -- scroll one line down --scroll :: Monad m => Int -> VgrepT Pager m Redraw -- | Scroll up or down one page. The first line on the current screen will -- be the last line on the scrolled screen and vice versa. -- --
-- scrollPage (-1) -- scroll one page up -- scrollPage 1 -- scroll one page down --scrollPage :: Monad m => Int -> VgrepT Pager m Redraw -- | Scroll up or down a fraction of a page. For integers, -- 'scrollPageFraction n == scrollPage n'. -- --
-- scrollPageFraction (-1%2) -- scroll one half page up -- scrollPageFraction (1%2) -- scroll one half page down -- scrollPageFraction (fromRational 1) -- scroll one page down --scrollPageFraction :: Monad m => Rational -> VgrepT Pager m Redraw -- | Horizontal scrolling. Increment is one tabstop. -- --
-- hScroll (-1) -- scroll one tabstop left -- hScroll 1 -- scroll one tabstop right --hScroll :: Monad m => Int -> VgrepT Pager m Redraw -- | Replace the currently displayed text. replaceBufferContents :: Monad m => Seq Text -> IntMap AnsiFormatted -> VgrepT Pager m () -- | A split-view widget that displays two widgets side-by-side. module Vgrep.Widget.HorizontalSplit -- | Compose two Widgets side-by-side -- --
-- >>> view currentWidget $ HSplit { _leftWidget = "foo", _layout = LeftOnly }
-- Left "foo"
--
currentWidget :: Lens' (HSplit s t) (Either s t)
-- | Traverses the left widget if focused
--
--
-- >>> has leftWidgetFocused $ HSplit { _layout = LeftOnly }
-- True
--
--
--
-- >>> has leftWidgetFocused $ HSplit { _layout = RightOnly }
-- False
--
--
--
-- >>> has leftWidgetFocused $ HSplit { _layout = Split FocusLeft (1 % 2) }
-- True
--
leftWidgetFocused :: Traversal' (HSplit s t) s
-- | Traverses the right widget if focused
--
--
-- >>> has rightWidgetFocused $ HSplit { _layout = RightOnly }
-- True
--
--
--
-- >>> has rightWidgetFocused $ HSplit { _layout = LeftOnly }
-- False
--
--
--
-- >>> has rightWidgetFocused $ HSplit { _layout = Split FocusRight (1 % 2) }
-- True
--
rightWidgetFocused :: Traversal' (HSplit s t) t
module Vgrep.Widget